Class: ROS::GraphManager

Inherits:
Object
  • Object
show all
Includes:
Name
Defined in:
lib/ros/graph_manager.rb

Overview

Manager of ROS graph

This contains all subscribers, publishers, service_servers of a node. It connects with master and manage pub/sub and services.

Constant Summary collapse

@@all_nodes =

current running all nodes. This is used for shutdown all nodes.

[]

Constants included from Name

Name::SEP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Name

#anonymous_name, #canonicalize_name, #expand_local_name, #resolve_name_with_call_id

Constructor Details

#initialize(caller_id, master_uri, host) ⇒ GraphManager

add xmlrpc handlers for slave connections. Then start serve thread.

Parameters:

  • caller_id (String)

    caller_id of this node

  • master_uri (String)

    URI of ROS Master

  • host (String)

    hostname of this node



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ros/graph_manager.rb', line 55

def initialize(caller_id, master_uri, host)
  @caller_id = caller_id
  @host = host
  @port = get_available_port
  @master_uri = master_uri
  @is_ok = true
  @master = MasterProxy.new(@caller_id, @master_uri, get_uri)
  @server = XMLRPCServer.new(@port, @host)
  @publishers = []
  @subscribers = []
  @service_servers = []
  @parameter_subscribers = []

  add_handlers

  @thread = Thread.new do
    @server.serve
  end

  @@all_nodes.push(self)
end

Instance Attribute Details

#hostString (readonly)

Returns value hostname of this node.

Returns:

  • (String)

    value hostname of this node



45
46
47
# File 'lib/ros/graph_manager.rb', line 45

def host
  @host
end

#parameter_subscribersArray (readonly)

Returns all ParameterSubscriber of this node.

Returns:

  • (Array)

    all ParameterSubscriber of this node



37
38
39
# File 'lib/ros/graph_manager.rb', line 37

def parameter_subscribers
  @parameter_subscribers
end

#portInteger (readonly)

Returns value port number of this node.

Returns:

  • (Integer)

    value port number of this node



47
48
49
# File 'lib/ros/graph_manager.rb', line 47

def port
  @port
end

#publishersArray (readonly)

Returns all Publisher of this node.

Returns:

  • (Array)

    all Publisher of this node



31
32
33
# File 'lib/ros/graph_manager.rb', line 31

def publishers
  @publishers
end

#service_serversArray (readonly)

Returns all ServiceServer of this node.

Returns:

  • (Array)

    all ServiceServer of this node



35
36
37
# File 'lib/ros/graph_manager.rb', line 35

def service_servers
  @service_servers
end

#subscribersArray (readonly)

Returns all Subscriber of this node.

Returns:

  • (Array)

    all Subscriber of this node



33
34
35
# File 'lib/ros/graph_manager.rb', line 33

def subscribers
  @subscribers
end

Class Method Details

.shutdown_allObject

shutdown all nodes



78
79
80
81
82
83
84
# File 'lib/ros/graph_manager.rb', line 78

def self.shutdown_all
  @@all_nodes.each do |node|
    if node.is_ok?
      node.shutdown
    end
  end
end

Instance Method Details

#add_parameter_subscriber(subscriber) ⇒ ParameterSubscriber

register callback for paramUpdate

Parameters:

Returns:



170
171
172
173
174
175
# File 'lib/ros/graph_manager.rb', line 170

def add_parameter_subscriber(subscriber)
  subscriber.set_manager(self)
  @parameter_subscribers.push(subscriber)
  @master.subscribe_param(subscriber.key)
  subscriber
end

#add_publisher(publisher) ⇒ Publisher

register a publisher. raise if fail.

Parameters:

  • publisher (Publisher)

    Publisher instance to be added

Returns:



181
182
183
184
185
186
187
# File 'lib/ros/graph_manager.rb', line 181

def add_publisher(publisher)
  @master.register_publisher(publisher.topic_name,
                             publisher.topic_type.type)
  publisher.set_manager(self)
  @publishers.push(publisher)
  publisher
end

#add_service_server(service_server) ⇒ ServiceServer

register a service to master, and add it in the controlling server list. raise if fail.

Parameters:

Returns:



142
143
144
145
146
147
148
# File 'lib/ros/graph_manager.rb', line 142

def add_service_server(service_server)
  @master.register_service(service_server.service_name,
                           service_server.service_uri)
  service_server.set_manager(self)
  @service_servers.push(service_server)
  service_server
end

#add_subscriber(subscriber) ⇒ Subscriber

register a subscriber to master. raise if fail.

Parameters:

  • subscriber (Subscriber)

    Subscriber to be added

Returns:



155
156
157
158
159
160
161
162
163
164
# File 'lib/ros/graph_manager.rb', line 155

def add_subscriber(subscriber)
  uris = @master.register_subscriber(subscriber.topic_name,
                                     subscriber.topic_type.type)
  subscriber.set_manager(self)
  uris.each do |publisher_uri|
    subscriber.add_connection(publisher_uri)
  end
  @subscribers.push(subscriber)
  subscriber
end

#get_available_portInteger

get available port number by opening port 0.

Returns:

  • (Integer)

    port_num



97
98
99
100
101
102
103
# File 'lib/ros/graph_manager.rb', line 97

def get_available_port
  server = TCPServer.open(0)
  saddr = server.getsockname
  port = Socket.unpack_sockaddr_in(saddr)[0]
  server.close
  port
end

#get_uriString

get this slave node’s URI

Returns:



109
110
111
# File 'lib/ros/graph_manager.rb', line 109

def get_uri
  "http://" + @host + ":" + @port.to_s + "/"
end

#is_ok?Boolean

check if this node is running or not.

Returns:

  • (Boolean)

    true if node is running.



89
90
91
# File 'lib/ros/graph_manager.rb', line 89

def is_ok?
  @is_ok
end

#shutdownGraphManager

shutdown this slave node. shutdown the xmlrpc server and all pub/sub connections. and delelte all pub/sub instance from connection list

Returns:



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/ros/graph_manager.rb', line 256

def shutdown
  begin
    @is_ok = false
    @server.shutdown
    if not @thread.join(0.1)
      Thread::kill(@thread)
    end
  rescue
    puts 'fail while shutdown'
    Thread::kill(@thread)
  end

  begin
    @publishers.each do |publisher|
      @master.unregister_publisher(publisher.topic_name)
      publisher.close
    end
  rescue
  ensure
    @publishers = nil
  end

  begin
    @subscribers.each do |subscriber|
      @master.unregister_subscriber(subscriber.topic_name)
      subscriber.close
    end
  rescue
  ensure
    @subscribers = nil
  end
  begin
    @service_servers.each do |service|
      @master.unregister_service(service.service_name,
                                 service.service_uri)
      service.close
    end
  rescue
  ensure
    @service_servers = nil
  end
  begin
    @parameter_subscribers.each do |subscriber|
      @master.unsubscribe_param(subscriber.key)
    end
  rescue
  ensure
    @parameter_subscribers = nil
  end
  @@all_nodes.delete(self)

  self
end

#shutdown_parameter_subscriber(subscriber) ⇒ Object

shutdown a parameter subscriber.

Parameters:

  • subscriber (Subscriber)

    ParameterSubscriber to be shutdown



242
243
244
245
246
247
248
249
# File 'lib/ros/graph_manager.rb', line 242

def shutdown_parameter_subscriber(subscriber)
  begin
    @master.unsubscribe_param(subscriber.key)
    @parameter_subscribers.delete(subscriber) do |sub|
      raise "parameter server not found"
    end
  end
end

#shutdown_publisher(publisher) ⇒ Object

shutdown a publisher.

Parameters:

  • publisher (Publisher)

    Publisher to be shutdown



199
200
201
202
203
204
205
206
207
208
# File 'lib/ros/graph_manager.rb', line 199

def shutdown_publisher(publisher)
  begin
    @master.unregister_publisher(publisher.topic_name)
  ensure
    @publishers.delete(publisher) do |pub|
      raise "publisher not found"
    end
    publisher.close
  end
end

#shutdown_service_server(service) ⇒ Object

shutdown a service server.

Parameters:



227
228
229
230
231
232
233
234
235
236
237
# File 'lib/ros/graph_manager.rb', line 227

def shutdown_service_server(service)
  begin
    @master.unregister_service(service.service_name,
                               service.service_uri)
    @service_servers.delete(service) do |pub|
      raise "service_server not found"
    end
  ensure
    service.close
  end
end

#shutdown_subscriber(subscriber) ⇒ Object

shutdown a subscriber.

Parameters:

  • subscriber (Subscriber)

    Subscriber to be shutdown



213
214
215
216
217
218
219
220
221
222
# File 'lib/ros/graph_manager.rb', line 213

def shutdown_subscriber(subscriber)
  begin
    @master.unregister_subscriber(subscriber.topic_name)
    @subscribers.delete(subscriber) do |pub|
      raise "subscriber not found"
    end
  ensure
    subscriber.close
  end
end

#spin_onceObject

process all messages of subscribers. This means that callbacks for all queued messages are called.



192
193
194
# File 'lib/ros/graph_manager.rb', line 192

def spin_once
  @subscribers.each {|subscriber| subscriber.process_queue}
end

#wait_for_service(service_name, timeout_sec) ⇒ Boolean

wait until service is available

Parameters:

  • service_name (String)

    name of service for waiting

  • timeout_sec (Float)

    wait for this seconds, then time out

Returns:

  • (Boolean)

    true: available, false: time out



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ros/graph_manager.rb', line 118

def wait_for_service(service_name, timeout_sec)
  begin
    timeout(timeout_sec) do
      while @is_ok
        if @master.lookup_service(service_name)
          return true
        end
        sleep(0.1)
      end
    end
  rescue Timeout::Error
    puts "time outed for wait service #{service_name}"
    return nil
  rescue
    raise "connection with master failed. master = #{@master_uri}"
  end
end