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

- (ServiceServer) initialize(caller_id, service_name, service_type, callback, host)

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.

  • host (String)

    host name



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

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

Instance Attribute Details

- (Integer) num_request (readonly)

Returns how many times this service called

Returns:

  • (Integer)

    how many times this service called



92
93
94
# File 'lib/ros/service_server.rb', line 92

def num_request
  @num_request
end

Instance Method Details

- (Boolean) call(request, response)

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



55
56
57
58
# File 'lib/ros/service_server.rb', line 55

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

- (Object) close

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



69
70
71
# File 'lib/ros/service_server.rb', line 69

def close #:nodoc:
  @server.shutdown
end

- (Array) get_connection_data

Returns connection data

Returns:

  • (Array)

    connection data



87
88
89
# File 'lib/ros/service_server.rb', line 87

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

- (String) service_uri

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

Returns:

  • (String)

    rosrpc service uri



62
63
64
# File 'lib/ros/service_server.rb', line 62

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

- (Object) set_manager(manager)

set GraphManager for shutdown

Parameters:



82
83
84
# File 'lib/ros/service_server.rb', line 82

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

- (Object) shutdown

shutdown the service connection



76
77
78
# File 'lib/ros/service_server.rb', line 76

def shutdown
  @manager.shutdow_service_server(self)
end