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 collapse
- MAX_CONNECTION =
max number of connection with clients
100
Instance Attribute Summary collapse
-
#byte_received ⇒ Integer
readonly
received data amout for slave api.
-
#byte_sent ⇒ Integer
readonly
sent data amout for slave api.
Instance Method Summary collapse
-
#build_header ⇒ Header
build header message for service server.
-
#check_header(header) ⇒ Boolean
check header.
-
#initialize(caller_id, service_name, service_type, callback, port = 0, host = ::GServer::DEFAULT_HOST) ⇒ ServiceServer
constructor
A new instance of ServiceServer.
-
#read_and_callback(socket) ⇒ Boolean
main loop of this connection.
-
#send_ng_byte(socket) ⇒ Object
message must send 1 byte for service call result (fail).
-
#send_ok_byte(socket) ⇒ Object
message must send 1 byte for service call result (success).
-
#serve(socket) ⇒ Object
this is called by socket accept.
Methods included from Message
#protocol, #read_all, #read_header, #write_header, #write_msg
Constructor Details
#initialize(caller_id, service_name, service_type, callback, port = 0, host = ::GServer::DEFAULT_HOST) ⇒ ServiceServer
Returns a new instance of ServiceServer.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ros/tcpros/service_server.rb', line 31 def initialize(caller_id, service_name, service_type, callback, port=0, host=::GServer::DEFAULT_HOST) 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
#byte_received ⇒ Integer (readonly)
received data amout for slave api
125 126 127 |
# File 'lib/ros/tcpros/service_server.rb', line 125 def byte_received @byte_received end |
#byte_sent ⇒ Integer (readonly)
sent data amout for slave api
129 130 131 |
# File 'lib/ros/tcpros/service_server.rb', line 129 def byte_sent @byte_sent end |
Instance Method Details
#build_header ⇒ Header
build header message for service server. It contains callerid, type, md5sum.
115 116 117 118 119 120 121 |
# File 'lib/ros/tcpros/service_server.rb', line 115 def build_header header = Header.new header["callerid"] = @caller_id header['type'] = @service_type.type header['md5sum'] = @service_type.md5sum header end |
#check_header(header) ⇒ Boolean
check header. check md5sum only.
107 108 109 |
# File 'lib/ros/tcpros/service_server.rb', line 107 def check_header(header) header.valid?('md5sum', @service_type.md5sum) end |
#read_and_callback(socket) ⇒ Boolean
main loop of this connection. read data and do callback.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ros/tcpros/service_server.rb', line 61 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 |
#send_ng_byte(socket) ⇒ Object
message must send 1 byte for service call result (fail)
52 53 54 |
# File 'lib/ros/tcpros/service_server.rb', line 52 def send_ng_byte(socket) socket.write([0].pack('c')) end |
#send_ok_byte(socket) ⇒ Object
message must send 1 byte for service call result (success)
45 46 47 |
# File 'lib/ros/tcpros/service_server.rb', line 45 def send_ok_byte(socket) socket.write([1].pack('c')) end |
#serve(socket) ⇒ Object
this is called by socket accept
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ros/tcpros/service_server.rb', line 83 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 |