Class: ROS::TCPROS::Client
- Inherits:
-
Object
- Object
- ROS::TCPROS::Client
- Includes:
- Message
- Defined in:
- lib/ros/tcpros/client.rb
Overview
rosrpc’s client for subscriber
Instance Attribute Summary collapse
-
#byte_received ⇒ Integer
readonly
Received byte.
-
#host ⇒ String
readonly
Host name.
-
#id ⇒ String
Id for slave API.
-
#msg_queue ⇒ Queue
readonly
Message queue.
-
#port ⇒ Integer
readonly
Port number of this client.
-
#target_uri ⇒ String
readonly
URI of connection target.
Instance Method Summary collapse
-
#build_header ⇒ TCPROS::Header
build header data for subscription.
-
#initialize(host, port, caller_id, topic_name, topic_type, target_uri, tcp_no_delay) ⇒ Client
constructor
A new instance of Client.
-
#shutdown ⇒ Object
close the connection.
-
#start ⇒ Object
start recieving data.
Methods included from Message
#protocol, #read_all, #read_header, #write_header, #write_msg
Constructor Details
#initialize(host, port, caller_id, topic_name, topic_type, target_uri, tcp_no_delay) ⇒ Client
Returns a new instance of Client.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ros/tcpros/client.rb', line 29 def initialize(host, port, caller_id, topic_name, topic_type, target_uri, tcp_no_delay) @caller_id = caller_id @topic_name = topic_name @topic_type = topic_type @port = port @host = host @target_uri = target_uri @msg_queue = Queue.new @socket = TCPSocket.open(@host, @port) @tcp_no_delay = tcp_no_delay if tcp_no_delay @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) end @byte_received = 0 @is_running = true end |
Instance Attribute Details
#byte_received ⇒ Integer (readonly)
Returns received byte.
102 103 104 |
# File 'lib/ros/tcpros/client.rb', line 102 def byte_received @byte_received end |
#host ⇒ String (readonly)
Returns host name.
98 99 100 |
# File 'lib/ros/tcpros/client.rb', line 98 def host @host end |
#id ⇒ String
Returns id for slave API.
106 107 108 |
# File 'lib/ros/tcpros/client.rb', line 106 def id @id end |
#msg_queue ⇒ Queue (readonly)
Returns message queue.
100 101 102 |
# File 'lib/ros/tcpros/client.rb', line 100 def msg_queue @msg_queue end |
#port ⇒ Integer (readonly)
Returns port number of this client.
96 97 98 |
# File 'lib/ros/tcpros/client.rb', line 96 def port @port end |
#target_uri ⇒ String (readonly)
Returns URI of connection target.
104 105 106 |
# File 'lib/ros/tcpros/client.rb', line 104 def target_uri @target_uri end |
Instance Method Details
#build_header ⇒ TCPROS::Header
build header data for subscription.
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ros/tcpros/client.rb', line 51 def build_header header = Header.new header.push_data("callerid", @caller_id) header.push_data("topic", @topic_name) header.push_data("md5sum", @topic_type.md5sum) header.push_data("type", @topic_type.type) if @tcp_no_delay header.push_data("tcp_nodelay", '1') else header.push_data("tcp_nodelay", '0') end header end |
#shutdown ⇒ Object
close the connection. kill the thread if it is not response.
85 86 87 88 89 90 91 92 93 |
# File 'lib/ros/tcpros/client.rb', line 85 def shutdown @is_running = false if not @thread.join(0.1) Thread::kill(@thread) end if not @socket.closed? @socket.close end end |
#start ⇒ Object
start recieving data. The received messages are pushed into a message queue.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ros/tcpros/client.rb', line 68 def start write_header(@socket, build_header) read_header(@socket) @thread = Thread.start do while @is_running data = read_all(@socket) msg = @topic_type.new msg.deserialize(data) @byte_received += data.length @msg_queue.push(msg) end end end |