Class: ROS::ServiceServer

Inherits:
Service
  • Object
show all
Defined in:
lib/ros/service_server.rb

Overview

server of ROS Service. Node#advertise_service return a instance of this class. Service can be shutdown by #shutdown. This class uses TCPROS::ServiceServer for data transfer.

Examples:

node = ROS::Node.new('/rosruby/sample_service_server')
server = node.advertise_service('/add_two_ints', Roscpp_tutorials::TwoInts) do |req, res|
  res.sum = req.a + req.b
  node.loginfo("a=#{req.a}, b=#{req.b}")
  node.loginfo("  sum = #{res.sum}")
  true
end
while node.ok?
  sleep (1.0)
end

Instance Attribute Summary collapse

Attributes inherited from Service

#caller_id, #service_name, #service_type

Instance Method Summary collapse

Constructor Details

#initialize(caller_id, service_name, service_type, callback) ⇒ ServiceServer

Returns a new instance of ServiceServer.

Parameters:

  • caller_id (String)

    caller id of this node

  • service_name (String)

    name of this service (String)

  • service_type (Class)

    class of srv

  • callback (Proc)

    callback object of this service.



36
37
38
39
40
41
42
43
44
45
# File 'lib/ros/service_server.rb', line 36

def initialize(caller_id, service_name, service_type, callback)
  super(caller_id, service_name, service_type)
  @callback = callback
  @server = TCPROS::ServiceServer.new(@caller_id,
                                      @service_name,
                                      @service_type,
                                      self)
  @server.start
  @num_request = 0
end

Instance Attribute Details

#num_requestInteger (readonly)

Returns how many times this service called.

Returns:

  • (Integer)

    how many times this service called



90
91
92
# File 'lib/ros/service_server.rb', line 90

def num_request
  @num_request
end

Instance Method Details

#call(request, response) ⇒ Boolean

execute the service callback. User should not call this directly.

Parameters:

  • request (Message)

    srv Request instance

  • response (Message)

    srv Response instance

Returns:

  • (Boolean)

    callback result



53
54
55
56
# File 'lib/ros/service_server.rb', line 53

def call(request, response)
  @num_request += 1
  @callback.call(request, response)
end

#closeObject

user should not call this method. Please use shutdown method.



67
68
69
# File 'lib/ros/service_server.rb', line 67

def close #:nodoc:
  @server.shutdown
end

#get_connection_dataArray

Returns connection data.

Returns:

  • (Array)

    connection data



85
86
87
# File 'lib/ros/service_server.rb', line 85

def get_connection_data
  [@num_request, @server.byte_received, @server.byte_sent]
end

#service_uriString

URI of this service (rosrpc://**)

Returns:

  • (String)

    rosrpc service uri



60
61
62
# File 'lib/ros/service_server.rb', line 60

def service_uri
  'rosrpc://' + @server.host + ':' + @server.port.to_s
end

#set_manager(manager) ⇒ Object

set GraphManager for shutdown

Parameters:



80
81
82
# File 'lib/ros/service_server.rb', line 80

def set_manager(manager) #:nodoc:
  @manager = manager
end

#shutdownObject

shutdown the service connection



74
75
76
# File 'lib/ros/service_server.rb', line 74

def shutdown
  @manager.shutdow_service_server(self)
end