Class: ROS::TCPROS::Client

Inherits:
Object
  • Object
show all
Includes:
Message
Defined in:
lib/ros/tcpros/client.rb

Overview

rosrpc’s client for subscriber

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • host (String)

    host name

  • port (Integer)

    port number

  • caller_id (String)

    caller id of this node

  • topic_name (String)

    name of this topic

  • topic_type (Class)

    type of topic

  • target_uri (String)

    URI of connection target

  • tcp_no_delay (Boolean)

    use tcp no delay option or not



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_receivedInteger (readonly)

Returns received byte.

Returns:

  • (Integer)

    received byte



102
103
104
# File 'lib/ros/tcpros/client.rb', line 102

def byte_received
  @byte_received
end

#hostString (readonly)

Returns host name.

Returns:

  • (String)

    host name



98
99
100
# File 'lib/ros/tcpros/client.rb', line 98

def host
  @host
end

#idString

Returns id for slave API.

Returns:

  • (String)

    id for slave API



106
107
108
# File 'lib/ros/tcpros/client.rb', line 106

def id
  @id
end

#msg_queueQueue (readonly)

Returns message queue.

Returns:

  • (Queue)

    message queue



100
101
102
# File 'lib/ros/tcpros/client.rb', line 100

def msg_queue
  @msg_queue
end

#portInteger (readonly)

Returns port number of this client.

Returns:

  • (Integer)

    port number of this client



96
97
98
# File 'lib/ros/tcpros/client.rb', line 96

def port
  @port
end

#target_uriString (readonly)

Returns URI of connection target.

Returns:

  • (String)

    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_headerTCPROS::Header

build header data for subscription.

Returns:



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

#shutdownObject

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

#startObject

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