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
- MAX_CONNECTION =
max number of connections with Client
100
Instance Attribute Summary (collapse)
-
- (Object) byte_sent
readonly
Returns the value of attribute byte_sent.
-
- (Object) caller_id
readonly
Returns the value of attribute caller_id.
-
- (String) id
id for slave API.
-
- (Object) last_published_msg
Returns the value of attribute last_published_msg.
-
- (Object) msg_queue
readonly
Returns the value of attribute msg_queue.
-
- (Object) num_sent
readonly
Returns the value of attribute num_sent.
Instance Method Summary (collapse)
-
- (Header) build_header
build Header message for this publisher.
-
- (Boolean) check_header(header)
validate header for this publisher.
-
- (Server) initialize(caller_id, topic_name, topic_type, options = {})
constructor
A new instance of Server.
-
- (Boolean) latching?
Is this latching publisher?.
-
- (Object) publish_msg(socket, msg)
send a message to reciever.
-
- (Object) serve(socket)
this is called if a socket accept a connection.
Methods included from Message
#protocol, #read_all, #read_header, #write_header, #write_msg
Constructor Details
- (Server) initialize(caller_id, topic_name, topic_type, options = {})
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
- (Object) byte_sent (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 |
- (Object) caller_id (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 |
- (String) id
id for slave API
108 109 110 |
# File 'lib/ros/tcpros/server.rb', line 108 def id @id end |
- (Object) last_published_msg
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 |
- (Object) msg_queue (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 |
- (Object) num_sent (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
- (Header) build_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 |
- (Boolean) check_header(header)
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 |
- (Boolean) latching?
Is this latching publisher?
57 58 59 |
# File 'lib/ros/tcpros/server.rb', line 57 def latching? @is_latched end |
- (Object) publish_msg(socket, msg)
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 |
- (Object) serve(socket)
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 |