Class: ROS::Master

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

Overview

ROS Master

See Also:

Defined Under Namespace

Classes: Parameter, ParameterSubscriber, Publisher, ServiceServer, Subscriber

Constant Summary

Constant Summary

Constants included from Name

Name::SEP

Instance Method Summary (collapse)

Methods included from Name

#anonymous_name, #canonicalize_name, #expand_local_name, #resolve_name_with_call_id

Constructor Details

- (Master) initialize(master_uri = ENV['ROS_MASTER_URI'])

Initialize XMLRPC Server. Master#start must be called to be started.

Parameters:

  • master_uri (String) (defaults to: ENV['ROS_MASTER_URI'])

    uri of master



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/ros/master.rb', line 194

def initialize(master_uri=ENV['ROS_MASTER_URI'])
  @master_uri = master_uri
  uri = URI.parse(@master_uri)
  @services = []
  @publishers = []
  @subscribers = []
  @parameters = []
  @param_subscribers = []
  @queue = Queue.new
  @server = XMLRPCServer.new(uri.port, uri.host)

  @server.set_default_handler do |method, *args|
    puts "unhandled call with #{method}, #{args}"
    [0, "I DON'T KNOW", 0]
  end

  @server.add_handler('registerService') do |caller_id, service_name, service_api, caller_api|
    service_name = canonicalize_name(service_name)
    kill_same_name_node(caller_id, caller_api)
    @services.push(ServiceServer.new(caller_id, service_name, caller_api, service_api))
    [1, "registered", 0]
  end

  @server.add_handler('unregisterService') do |caller_id, service_name, service_api|
    service_name = canonicalize_name(service_name)
    before = @services.length
    @services.delete_if {|x| x.name == service_name and x.caller_id == caller_id}
    after = @services.length
    [1, "deleted", before - after]
  end

  @server.add_handler('registerSubscriber') do |caller_id, topic_name, type, api|
    topic_name = canonicalize_name(topic_name)
    kill_same_name_node(caller_id, api)
    @subscribers.push(Subscriber.new(caller_id, topic_name, type, api))
    pub_apis = @publishers.select {|x|x.name == topic_name and x.msg_type == type}.map {|x| x.api }
    [1, "registered", pub_apis]
  end

  @server.add_handler('unregisterSubscriber') do |caller_id, topic_name, api|
    topic_name = canonicalize_name(topic_name)
    before = @subscribers.length
    @subscribers.delete_if {|x| x.name == topic_name and x.caller_id == caller_id}
    after = @subscribers.length
    [1, "deleted", before - after]
  end

  @server.add_handler('registerPublisher') do |caller_id, topic_name, type, api|
    topic_name = canonicalize_name(topic_name)
    kill_same_name_node(caller_id, api)
    @publishers.push(Publisher.new(caller_id, topic_name, type, api))
    sub_apis = @subscribers.select {|x|x.name == topic_name and x.msg_type == type}.map {|x| x.api }
    if not sub_apis
      sub_apis = []
    end
    pub_apis = @publishers.select {|x|x.name == topic_name and x.msg_type == type}.map {|x| x.api }
    @queue.push(proc{
                  sub_apis.each do |s_api|
                    proxy = SlaveProxy.new('/master', s_api)
                    begin
                      proxy.publisher_update(topic_name, pub_apis)
                    rescue => e
                      p e.faultCode
                      p e.faultString
                      delete_connection(s_api)
                    end
                  end
                })
    [1, "registered", sub_apis]
  end

  @server.add_handler('unregisterPublisher') do |caller_id, topic_name, api|
    topic_name = canonicalize_name(topic_name)
    before = @publishers.length
    @publishers.delete_if {|x| x.name == topic_name and x.caller_id == caller_id}
    pub_apis = @publishers.select {|x|x.name == topic_name and x.caller_id == caller_id}.map {|x| x.api }
    sub_apis = @subscribers.select {|x|x.name == topic_name}.map {|x| x.api }
    if not sub_apis
      sub_apis = []
    end
#        @queue.push(proc{
                  sub_apis.each do |s_api|
                    proxy = SlaveProxy.new('/master', s_api)
                    begin
                      proxy.publisher_update(topic_name, pub_apis)
                    rescue => e
                      p e.faultCode
                      p e.faultString
                      delete_connection(s_api)
                    end
                  end
 #                   })
    after = @publishers.length
    [1, "deleted", before - after]
  end

  @server.add_handler('getPid') do |caller_id|
    [1, "ok", $$]
  end

  @server.add_handler('lookupNode') do |caller_id, node_name|
    pub = @publishers.select {|x| x.caller_id == node_name}
    sub = @subscribers.select {|x| x.caller_id == node_name}
    service = @services.select {|x| x.caller_id == node_name}
    if not pub.empty?
      [1, "found", pub.first.api]
    elsif not sub.empty?
      [1, "found", sub.first.api]
    elsif not service.empty?
      [1, "found", service.first.api]
    else
      [0, "not found", 0]
    end
  end

  @server.add_handler('getPublishedTopics') do |caller_id, subgraph|
    if subgraph == ''
      [1, "ok", @publishers.map {|x| [x.name, x.msg_type]}]
    else
      [1, "ok", @publishers.select {|x| not x.caller_id.scan(/^#{subgraph}/).empty?}.map {|x| [x.name, x.msg_type]}]
    end
  end

  @server.add_handler('getSystemState') do |caller_id|
    def convert_info_to_list(info)
      list = []
      info.keys.each do |key|
        list.push([key, info[key]])
      end
      list
    end

    pub_info = {}
    @publishers.each do |pub|
      if pub_info[pub.name]
        pub_info[pub.name].push(pub.caller_id)
      else
        pub_info[pub.name]= [pub.caller_id]
      end
    end

    sub_info = {}
    @subscribers.each do |sub|
      if sub_info[sub.name]
        sub_info[sub.name].push(sub.caller_id)
      else
        sub_info[sub.name]= [sub.caller_id]
      end
    end

    ser_info = {}
    @services.each do |ser|
      if ser_info[ser.name]
        ser_info[ser.name].push(ser.caller_id)
      else
        ser_info[ser.name]= [ser.caller_id]
      end
    end

    [1, "ok", [convert_info_to_list(pub_info),
               convert_info_to_list(sub_info),
               convert_info_to_list(ser_info)]]
  end

  @server.add_handler('getUri') do |caller_id|
    [1, "ok", @master_uri]
  end

  @server.add_handler('lookupService') do |caller_id, service|
    ser = @services.select {|x| x.name == service}
    if ser.empty?
      [0, "fail", 0]
    else
      [1, "ok", ser.first.service_api]
    end
  end

  ## parameters
  @server.add_handler('deleteParam') do |caller_id, key|
    key = canonicalize_name(key)
    before = @parameters.length
    @parameters.delete_if {|x| x.key == key}
    after = @parameters.length

    if before == after
      [0, "[#{key}] is not exists", 0]
    else
      [1, "ok", 0]
    end
  end

  @server.add_handler('setParam') do |caller_id, key, value|
    key = canonicalize_name(key)
    exist_param = @parameters.select {|x| x.key == key}
    if exist_param.empty?
      @parameters.push(Parameter.new(key, value))
    else
      exist_param.first.value = value
    end
    @param_subscribers.each do |x|
      if x.key == key
        begin
          proxy = SlaveProxy.new('/master', x.api)
          proxy.param_update(key, value)
        rescue
        end
      end
    end
    [1, "ok", 0]
  end

  @server.add_handler('getParam') do |caller_id, key|
    key = canonicalize_name(key)
    param = @parameters.select {|x| x.key == key}
    if param.empty?
      [0, "no such param [#{key}]", 0]
    else
      [1, "ok", param.first.value]
    end
  end

  @server.add_handler('searchParam') do |caller_id, key|
    key = canonicalize_name(key)
    if key == ''
      param = @parameters
    else
      param = @parameters.select {|x| not x.key.scan(/#{key}/).empty?}
    end
    if param.empty?
      [-1, "no param", 0]
    else
      [1, "ok", param.first.key]
    end
  end

  @server.add_handler('subscribeParam') do |caller_id, caller_api, key|
    key = canonicalize_name(key)
    @param_subscribers.push(ParameterSubscriber.new(caller_id, caller_api, key))
    params = @parameters.select {|x| x.key == key}
    if params.empty?
      [1, "ok", []]
    else
      [1, "ok", params.map {|x| x.value}]
    end
  end

  @server.add_handler('unsubscribeParam') do |caller_id, caller_api, key|
    key = canonicalize_name(key)
    before = @param_subscribers.length
    @param_subscribers.delete_if {|x| x.api == caller_api and x.key == key}
    after = @param_subscribers.length
    [1, "ok", before - after]
  end

  @server.add_handler('hasParam') do |caller_id, key|
    key = canonicalize_name(key)
    params = @parameters.select {|x| x.key == key}
    if params.empty?
      [0, "no", false]
    else
      [1, "ok", params.first.value]
    end
  end

  @server.add_handler('getParamNames') do |caller_id|
    [1, "ok", @parameters.map{|x| x.key}]
  end

  # not documented api
  @server.add_handler('getTopicTypes') do |caller_id|
    [1, "ok", @publishers.map {|x| [x.name, x.msg_type]} | @subscribers.map {|x| [x.name, x.msg_type]}]
  end

end

Instance Method Details

- (Object) delete_connection(api)

Delete connection of this api (XMLRPC URI).

Parameters:

  • api (String)

    URI of node to delete connection.



185
186
187
188
189
# File 'lib/ros/master.rb', line 185

def delete_connection(api)
  @subscribers.delete_if {|x| x.api == api}
  @publishers.delete_if {|x| x.api == api}
  @services.delete_if {|x| x.api == api}
end

- (Object) kill_same_name_node(caller_id, api)

kill old node if the same caller_id node is exits.

Parameters:

  • caller_id (String)

    new node’s caller_id

  • api (String)

    new node’s XMLRPC URI



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ros/master.rb', line 159

def kill_same_name_node(caller_id, api)
  delete_api = nil
  [@publishers, @subscribers, @services].each do |list|
    list.each do |pub|
      if pub.caller_id == caller_id and pub.api != api
        puts "killing #{caller_id}"
        delete_api = pub.api
        break
      end
    end
  end
  if delete_api
    proxy = SlaveProxy.new('/master', delete_api)
    begin
      proxy.shutdown("registered new node #{delete_api}")
    rescue
    end
    # delete
    [@publishers, @subscribers, @services].each do |list|
      list.delete_if {|x| x.api == delete_api}
    end
  end
end

- (Object) start

start server and set default parameters



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/ros/master.rb', line 470

def start
  puts "=== starting Ruby ROS master @#{@master_uri} ==="
  @parameters.push(Parameter.new('/rosversion', '1.8.6'))
  @parameters.push(Parameter.new('/rosdistro', 'fuerte'))
  @thread = Thread.new do
    while true
      procs = []
      # lock here
      proc_obj = @queue.pop
      procs.push(proc_obj)
      while not @queue.empty?
        proc_obj = @queue.pop
        procs.push(proc_obj)
      end
      # wait until server returns xmlrpc response...
      sleep 0.2
      begin
        procs.each {|x| x.call}
      rescue => e
        p 'error!'
        p e.faultString
      end
    end
  end
  @server.serve
  self
end