Class: ROS::TCPROS::ServiceClient
- Inherits:
-
Object
- Object
- ROS::TCPROS::ServiceClient
- Includes:
- Message
- Defined in:
- lib/ros/tcpros/service_client.rb
Overview
TCPROS protocol service client connection
Instance Attribute Summary (collapse)
-
- (String) host
readonly
host of this connection.
-
- (Integer) port
readonly
port number of this socket.
Instance Method Summary (collapse)
-
- (Header) build_header
build henader message for service client.
-
- (Boolean) call(srv_request, srv_response)
call the service by sending srv request message, and receive response message.
-
- (Boolean) check_header(header)
check md5sum only.
-
- (ServiceClient) initialize(host, port, caller_id, service_name, service_type, persistent)
constructor
A new instance of ServiceClient.
-
- (Integer) read_ok_byte
read ok byte for boolean service result.
-
- (Object) shutdown
close the socket.
Methods included from Message
#protocol, #read_all, #read_header, #write_header, #write_msg
Constructor Details
- (ServiceClient) initialize(host, port, caller_id, service_name, service_type, persistent)
Returns a new instance of ServiceClient
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ros/tcpros/service_client.rb', line 26 def initialize(host, port, caller_id, service_name, service_type, persistent) @caller_id = caller_id @service_name = service_name @service_type = service_type @port = port @host = host @socket = TCPSocket.open(@host, @port) @persistent = persistent @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) end |
Instance Attribute Details
- (String) host (readonly)
host of this connection
101 102 103 |
# File 'lib/ros/tcpros/service_client.rb', line 101 def host @host end |
- (Integer) port (readonly)
port number of this socket
97 98 99 |
# File 'lib/ros/tcpros/service_client.rb', line 97 def port @port end |
Instance Method Details
- (Header) build_header
build henader message for service client. It contains callerid, service, md5sum, type, persistent.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ros/tcpros/service_client.rb', line 41 def build_header header = Header.new header.push_data("callerid", @caller_id) header.push_data("service", @service_name) header.push_data("md5sum", @service_type.md5sum) header.push_data("type", @service_type.type) if @persistent header.push_data("persistent", '1') end header end |
- (Boolean) call(srv_request, srv_response)
call the service by sending srv request message, and receive response message.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ros/tcpros/service_client.rb', line 59 def call(srv_request, srv_response) write_header(@socket, build_header) if check_header(read_header(@socket)) write_msg(@socket, srv_request) @socket.flush ok_byte = read_ok_byte if ok_byte == 1 srv_response.deserialize(read_all(@socket)) return true end false end false end |
- (Boolean) check_header(header)
check md5sum only.
85 86 87 |
# File 'lib/ros/tcpros/service_client.rb', line 85 def check_header(header) header.valid?('md5sum', @service_type.md5sum) end |
- (Integer) read_ok_byte
read ok byte for boolean service result.
77 78 79 |
# File 'lib/ros/tcpros/service_client.rb', line 77 def read_ok_byte @socket.recv(1).unpack('c')[0] end |
- (Object) shutdown
close the socket
91 92 93 |
# File 'lib/ros/tcpros/service_client.rb', line 91 def shutdown @socket.close end |