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
-
#host ⇒ String
readonly
host of this connection.
-
#port ⇒ Integer
readonly
port number of this socket.
Instance Method Summary collapse
-
#build_header ⇒ Header
build henader message for service client.
-
#call(srv_request, srv_response) ⇒ Boolean
call the service by sending srv request message, and receive response message.
-
#check_header(header) ⇒ Boolean
check md5sum only.
-
#initialize(host, port, caller_id, service_name, service_type, persistent) ⇒ ServiceClient
constructor
A new instance of ServiceClient.
-
#read_ok_byte ⇒ Integer
read ok byte for boolean service result.
-
#shutdown ⇒ Object
close the socket.
Methods included from Message
#protocol, #read_all, #read_header, #write_header, #write_msg
Constructor Details
#initialize(host, port, caller_id, service_name, service_type, persistent) ⇒ ServiceClient
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
#host ⇒ String (readonly)
host of this connection
101 102 103 |
# File 'lib/ros/tcpros/service_client.rb', line 101 def host @host end |
#port ⇒ Integer (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
#build_header ⇒ 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 |
#call(srv_request, srv_response) ⇒ Boolean
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 |
#check_header(header) ⇒ Boolean
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 |
#read_ok_byte ⇒ Integer
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 |
#shutdown ⇒ Object
close the socket
91 92 93 |
# File 'lib/ros/tcpros/service_client.rb', line 91 def shutdown @socket.close end |