Class: ROS::TCPROS::Server

Inherits:
GServer
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • caller_id (String)

    caller id of this node

  • topic_name (String)

    name of this topic

  • topic_type (Class)

    type of topic

  • options (Hash) (defaults to: {})

    :latched, :port, host, :last_published_msg

  • [Boolean] (Hash)

    a customizable set of options

  • [Integer] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Message] (Hash)

    a customizable set of options



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, options={})
  if options[:port]
    port = options[:port]
  else
    port = 0
  end
  if options[:host]
    host = options[: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 = options[:latched]
  @last_published_msg = options[:last_published_msg]
  @byte_sent = 0
  @num_sent = 0
end

Instance Attribute Details

#byte_sentObject (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_idObject (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

#idString

id for slave API

Returns:

  • (String)


108
109
110
# File 'lib/ros/tcpros/server.rb', line 108

def id
  @id
end

#last_published_msgObject

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_queueObject (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_sentObject (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_headerHeader

build Header message for this publisher. It contains callerid, topic, md5sum, type, latching.

Returns:



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

Parameters:

  • header (Header)

    for checking

Returns:

  • (Boolean)

    ok(true) or not



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?

Returns:

  • (Boolean)

    is latched or not



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

Parameters:

  • socket (IO)

    socket for writing

  • msg (Class)

    msg class instance



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

Parameters:

  • socket (IO)

    given socket



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