Class: ROS::TCPROS::Server
- Inherits:
-
GServer
- Object
- GServer
- ROS::TCPROS::Server
- Includes:
- Message
- Defined in:
- lib/ros/tcpros/server.rb
Overview
This is TCPROS connection for Publihser
Constant Summary collapse
- MAX_CONNECTION =
max number of connections with Client
100
Instance Attribute Summary collapse
-
#byte_sent ⇒ Object
readonly
Returns the value of attribute byte_sent.
-
#caller_id ⇒ Object
readonly
Returns the value of attribute caller_id.
-
#id ⇒ String
id for slave API.
-
#last_published_msg ⇒ Object
Returns the value of attribute last_published_msg.
-
#msg_queue ⇒ Object
readonly
Returns the value of attribute msg_queue.
-
#num_sent ⇒ Object
readonly
Returns the value of attribute num_sent.
Instance Method Summary collapse
-
#build_header ⇒ Header
build Header message for this publisher.
-
#check_header(header) ⇒ Boolean
validate header for this publisher.
-
#initialize(caller_id, topic_name, topic_type, options = {}) ⇒ Server
constructor
A new instance of Server.
-
#latching? ⇒ Boolean
Is this latching publisher?.
-
#publish_msg(socket, msg) ⇒ Object
send a message to reciever.
-
#serve(socket) ⇒ Object
this is called if a socket accept a connection.
Methods included from Message
#protocol, #read_all, #read_header, #write_header, #write_msg
Constructor Details
#initialize(caller_id, topic_name, topic_type, options = {}) ⇒ Server
Returns a new instance of Server.
31 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/server.rb', line 31 def initialize(caller_id, topic_name, topic_type, ={}) if [:port] port = [:port] else port = 0 end if [:host] host = [:host] else host = GServer::DEFAULT_HOST end super(port, host, MAX_CONNECTION) @caller_id = caller_id @topic_name = topic_name @topic_type = topic_type @msg_queue = Queue.new @is_latched = [:latched] @last_published_msg = [:last_published_msg] @byte_sent = 0 @num_sent = 0 end |
Instance Attribute Details
#byte_sent ⇒ Object (readonly)
Returns the value of attribute byte_sent.
104 105 106 |
# File 'lib/ros/tcpros/server.rb', line 104 def byte_sent @byte_sent end |
#caller_id ⇒ Object (readonly)
Returns the value of attribute caller_id.
104 105 106 |
# File 'lib/ros/tcpros/server.rb', line 104 def caller_id @caller_id end |
#id ⇒ String
id for slave API
108 109 110 |
# File 'lib/ros/tcpros/server.rb', line 108 def id @id end |
#last_published_msg ⇒ Object
Returns the value of attribute last_published_msg.
109 110 111 |
# File 'lib/ros/tcpros/server.rb', line 109 def last_published_msg @last_published_msg end |
#msg_queue ⇒ Object (readonly)
Returns the value of attribute msg_queue.
104 105 106 |
# File 'lib/ros/tcpros/server.rb', line 104 def msg_queue @msg_queue end |
#num_sent ⇒ Object (readonly)
Returns the value of attribute num_sent.
104 105 106 |
# File 'lib/ros/tcpros/server.rb', line 104 def num_sent @num_sent end |
Instance Method Details
#build_header ⇒ Header
build Header message for this publisher. It contains callerid, topic, md5sum, type, latching.
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/ros/tcpros/server.rb', line 124 def build_header header = Header.new header["callerid"] = @caller_id header["topic"] = @topic_name header["md5sum"] = @topic_type.md5sum header["type"] = @topic_type.type if latching? header["latching"] = '1' end header end |
#check_header(header) ⇒ Boolean
validate header for this publisher
115 116 117 118 |
# File 'lib/ros/tcpros/server.rb', line 115 def check_header(header) header.valid?('type', @topic_type.type) and header.valid?('md5sum', @topic_type.md5sum) end |
#latching? ⇒ Boolean
Is this latching publisher?
57 58 59 |
# File 'lib/ros/tcpros/server.rb', line 57 def latching? @is_latched end |
#publish_msg(socket, msg) ⇒ Object
send a message to reciever
65 66 67 68 69 70 71 |
# File 'lib/ros/tcpros/server.rb', line 65 def publish_msg(socket, msg) data = write_msg(socket, msg) @last_published_msg = msg # for getBusStats @byte_sent += data.length @num_sent += 1 end |
#serve(socket) ⇒ Object
this is called if a socket accept a connection. This is GServer’s function
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ros/tcpros/server.rb', line 77 def serve(socket) #:nodoc: header = read_header(socket) if check_header(header) if header['tcp_nodelay'] == '1' socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) end begin write_header(socket, build_header) if latching? if @last_published_msg publish_msg(socket, @last_published_msg) end end loop do @last_published_msg = @msg_queue.pop publish_msg(socket, @last_published_msg) end rescue socket.shutdown end else socket.shutdown p "header check error: #{header}" raise 'header check error' end end |