Class: ROS::TCPROS::ServiceServer
- Inherits:
-
GServer
- Object
- GServer
- ROS::TCPROS::ServiceServer
- Includes:
- Message
- Defined in:
- lib/ros/tcpros/service_server.rb
Overview
TCPROS protocol Service Server
Constant Summary
- MAX_CONNECTION =
max number of connection with clients
100
Instance Attribute Summary (collapse)
-
- (Integer) byte_received
readonly
received data amout for slave api.
-
- (Integer) byte_sent
readonly
sent data amout for slave api.
Instance Method Summary (collapse)
-
- (Header) build_header
build header message for service server.
-
- (Boolean) check_header(header)
check header.
-
- (ServiceServer) initialize(caller_id, service_name, service_type, callback, options = {})
constructor
A new instance of ServiceServer.
-
- (Boolean) read_and_callback(socket)
main loop of this connection.
-
- (Object) send_ng_byte(socket)
message must send 1 byte for service call result (fail).
-
- (Object) send_ok_byte(socket)
message must send 1 byte for service call result (success).
-
- (Object) serve(socket)
this is called by socket accept.
Methods included from Message
#protocol, #read_all, #read_header, #write_header, #write_msg
Constructor Details
- (ServiceServer) initialize(caller_id, service_name, service_type, callback, options = {})
Returns a new instance of ServiceServer
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ros/tcpros/service_server.rb', line 32 def initialize(caller_id, service_name, service_type, callback, ={}) if [:host] host = [:host] else host = Socket.gethostname end if [:port] port = [:port] else port = 0 end super(port, host, MAX_CONNECTION) @caller_id = caller_id @service_name = service_name @service_type = service_type @callback = callback @byte_received = 0 @byte_sent = 0 end |
Instance Attribute Details
- (Integer) byte_received (readonly)
received data amout for slave api
137 138 139 |
# File 'lib/ros/tcpros/service_server.rb', line 137 def byte_received @byte_received end |
- (Integer) byte_sent (readonly)
sent data amout for slave api
141 142 143 |
# File 'lib/ros/tcpros/service_server.rb', line 141 def byte_sent @byte_sent end |
Instance Method Details
- (Header) build_header
build header message for service server. It contains callerid, type, md5sum.
127 128 129 130 131 132 133 |
# File 'lib/ros/tcpros/service_server.rb', line 127 def build_header header = Header.new header["callerid"] = @caller_id header['type'] = @service_type.type header['md5sum'] = @service_type.md5sum header end |
- (Boolean) check_header(header)
check header. check md5sum only.
119 120 121 |
# File 'lib/ros/tcpros/service_server.rb', line 119 def check_header(header) header.valid?('md5sum', @service_type.md5sum) end |
- (Boolean) read_and_callback(socket)
main loop of this connection. read data and do callback.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/ros/tcpros/service_server.rb', line 73 def read_and_callback(socket) request = @service_type.request_class.new response = @service_type.response_class.new data = read_all(socket) @byte_received += data.length request.deserialize(data) result = @callback.call(request, response) if result send_ok_byte(socket) data = write_msg(socket, response) @byte_sent += data.length else send_ng_byte(socket) write_header(socket, build_header) # write some message end result end |
- (Object) send_ng_byte(socket)
message must send 1 byte for service call result (fail)
64 65 66 |
# File 'lib/ros/tcpros/service_server.rb', line 64 def send_ng_byte(socket) socket.write([0].pack('c')) end |
- (Object) send_ok_byte(socket)
message must send 1 byte for service call result (success)
57 58 59 |
# File 'lib/ros/tcpros/service_server.rb', line 57 def send_ok_byte(socket) socket.write([1].pack('c')) end |
- (Object) serve(socket)
this is called by socket accept
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ros/tcpros/service_server.rb', line 95 def serve(socket) header = read_header(socket) # not documented protocol? if header['probe'] == '1' write_header(socket, build_header) elsif check_header(header) write_header(socket, build_header) read_and_callback(socket) if header['persistent'] == '1' loop do read_and_callback(socket) end end else socket.close raise 'header check error' end end |