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

@@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

- (GraphManager) initialize(caller_id, master_uri, host)

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

- (String) host (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

- (Array) parameter_subscribers (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

- (Integer) port (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

- (Array) publishers (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

- (Array) service_servers (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

- (Array) subscribers (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

+ (Object) shutdown_all

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

- (ParameterSubscriber) add_parameter_subscriber(subscriber)

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

- (Publisher) add_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

- (ServiceServer) add_service_server(service_server)

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

- (Subscriber) add_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

- (Object) check_master_connection

check connection with master using master API



314
315
316
317
318
319
320
321
322
# File 'lib/ros/graph_manager.rb', line 314

def check_master_connection
  begin
    @master.get_system_state
  rescue
    # some error
    return false
  end
  return true
end

- (Integer) get_available_port

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

- (String) get_uri

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

- (Boolean) is_ok?

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

- (GraphManager) shutdown

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

Returns:



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
309
310
# File 'lib/ros/graph_manager.rb', line 258

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 = []
  end

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

  self
end

- (Object) shutdown_parameter_subscriber(subscriber)

shutdown a parameter subscriber.

Parameters:

  • subscriber (Subscriber)

    ParameterSubscriber to be shutdown



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

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

- (Object) shutdown_publisher(publisher)

shutdown a publisher.

Parameters:

  • publisher (Publisher)

    Publisher to be shutdown



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

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

- (Object) shutdown_service_server(service)

shutdown a service server.

Parameters:



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

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

- (Object) shutdown_subscriber(subscriber)

shutdown a subscriber.

Parameters:

  • subscriber (Subscriber)

    Subscriber to be shutdown



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

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

- (Bool) spin_once

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

Returns:

  • (Bool)

    some message has come or not.



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

def spin_once
  results = @subscribers.map {|subscriber| subscriber.process_queue}
  results.include?(true)
end

- (Boolean) wait_for_service(service_name, timeout_sec)

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 out for wait service #{service_name}"
    return nil
  rescue
    raise "connection with master failed. master = #{@master_uri}"
  end
end