asterism 0.12.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/mrblib/future.rb CHANGED
@@ -1,18 +1,22 @@
1
1
  module Asterism
2
2
  # The answer to a call that is on its way (Proxy#async). done? never
3
- # waits; value waits (polling, and answering calls meanwhile) until the
4
- # answer or the time limit, then returns the value or raises.
3
+ # waits; value waits (polling its connection, and answering calls
4
+ # meanwhile) until the answer or the time limit, then returns the value
5
+ # or raises.
5
6
  class Future
6
7
  attr_reader :path, :method_name
7
8
 
8
9
  # @api private
9
10
  def self.answered(path, method, reply)
10
- f = new(path, method, nil, 0, 0)
11
+ f = new(nil, path, method, nil, 0, 0)
11
12
  f.set_reply(reply)
12
13
  f
13
14
  end
14
15
 
15
- def initialize(path, method, get, deadline_ms, timeout_ms)
16
+ # connection: the Asterism::Connection the call went out on (nil for an
17
+ # answer that is there already).
18
+ def initialize(connection, path, method, get, deadline_ms, timeout_ms)
19
+ @connection = connection
16
20
  @path = path
17
21
  @method_name = method.to_s
18
22
  @get = get
@@ -42,7 +46,7 @@ module Asterism
42
46
  end
43
47
 
44
48
  # True once the answer came, the time ran out or the connection closed.
45
- # Call Asterism.poll in the update loop for it to move on.
49
+ # Poll the connection in the update loop for it to move on.
46
50
  def done?
47
51
  collect
48
52
  @finished
@@ -65,9 +69,10 @@ module Asterism
65
69
 
66
70
  # The decoded answer (meta and profile replies are not value maps).
67
71
  def raw_value
68
- ::Asterism.wait_until { done? } unless done?
72
+ @connection.wait_until { done? } unless done?
69
73
  if @reply.nil?
70
- raise ::Asterism::Disconnected, (::Asterism.lost_reason || "not connected") unless ::Asterism.connected?
74
+ c = @connection
75
+ raise ::Asterism::Disconnected, (c.lost_reason || "not connected") unless c.connected?
71
76
  if @took && @took < @timeout_ms
72
77
  # Finished early without a reply: nobody answers that key (the
73
78
  # object is not exposed, or its application is gone).
@@ -94,7 +99,7 @@ module Asterism
94
99
  # interpreter entry on the C stack, and this runs inside nested waits.
95
100
  replies = g.each_reply
96
101
  @reply = replies[0][1] if @reply.nil? && replies.size > 0
97
- if !@reply.nil? || ended || ::Asterism.now_ms >= @deadline || !::Asterism.connected?
102
+ if !@reply.nil? || ended || ::Asterism.now_ms >= @deadline || !@connection.connected?
98
103
  @finished = true
99
104
  @took = ::Asterism.now_ms - @started
100
105
  end
data/mrblib/profile.rb CHANGED
@@ -1,13 +1,14 @@
1
1
  # A node's profile: what it runs on, where, a display name and an icon,
2
2
  # told by the node itself (0.5.0).
3
3
  #
4
- # Asterism.profile(name: "Rover", icon: png) # this node's; returns it
5
- # Asterism.profile # => {"kind" => "picoruby", "name" => "Rover", ...}
6
- # Asterism.profile_of("fmruby-aaaaaa") # another node's, or nil
4
+ # Asterism.profile_defaults(kind: :family_mruby, host: "tab5") # the program's defaults
5
+ # net.profile(name: "Rover", icon: png) # this node's (net = Asterism.connect(...)); returns it
6
+ # net.profile # => {"kind" => "picoruby", "name" => "Rover", ...}
7
+ # net.profile_of("fmruby-aaaaaa") # another node's, or nil
7
8
  #
8
- # Key: asterism/<node>/@profile (a queryable of every application connected
9
- # as that node). The reply is a MessagePack Hash:
10
- # "protocol" the wire protocol the node speaks (Asterism::PROTOCOL, 1);
9
+ # Key: asterism/<node>/@profile (a queryable of every connection made as
10
+ # that node; all of them answer the same profile). The reply is a MessagePack Hash:
11
+ # "protocol" the wire protocol the node speaks (Asterism::PROTOCOL, 2);
11
12
  # a node without it speaks protocol 0
12
13
  # "kind" what Ruby runs the node: "family_mruby", "picoruby",
13
14
  # "cruby", "console" (others are allowed)
@@ -21,8 +22,9 @@
21
22
  # Each connect, and each change while connected, also puts the profile
22
23
  # without its icon on the same key, for those who follow it (a console).
23
24
  #
24
- # Room left for later: an application's own profile would be
25
- # asterism/<node>/<app>/@profile, with the same Hash.
25
+ # An application's own profile (its status, topics and types) is one chunk
26
+ # further down, asterism/<node>/<app>/@profile (status.rb, topic.rb;
27
+ # docs/protocol.md 8).
26
28
  module Asterism
27
29
  module Profile
28
30
  # The kinds this version knows. Others pass: a kind is any lower-case
@@ -162,7 +164,7 @@ module Asterism
162
164
  true
163
165
  end
164
166
 
165
- # The checks of Asterism.profile (ArgumentError, with the reason).
167
+ # The checks of Connection#profile and Asterism.profile_defaults (ArgumentError, with the reason).
166
168
  # @api private
167
169
  def self.kind!(v)
168
170
  s = v.is_a?(Symbol) ? v.to_s : v
@@ -209,93 +211,93 @@ module Asterism
209
211
  end
210
212
  end
211
213
 
212
- # This node's profile. With keywords, sets those fields and returns the
213
- # profile; without, returns it. The fields: kind: (a Symbol or String;
214
- # nil goes back to the default), name:, host: (Strings; nil leaves them
215
- # out), icon: (the bytes of a PNG; nil for no icon). The fields not given
216
- # stay as they are. Raises ArgumentError for a field it does not accept
217
- # (see Asterism::Profile).
214
+ # The profile of a node is the node's, not one application's: every
215
+ # connection of this program connected as that node answers the same
216
+ # profile (docs/protocol.md 7.3). It is kept here, by node, and outlives
217
+ # the connections (the next connection as that node finds it).
218
218
  #
219
- # The defaults: kind "picoruby" on mruby / PicoRuby, "cruby" on CRuby; no
220
- # host, no name; the icon is the file at Asterism.default_icon_path when
221
- # there is one (the boards: /usr/share/asterism/icon.png; CRuby: the gem's
222
- # data/icon.png). A board's firmware may set its own defaults with this
223
- # same call before the application runs (Family mruby does).
224
- #
225
- # Kept across connect and close (it belongs to the process, or the VM on
226
- # a board). While connected, a change is also put on the profile's key.
227
- def self.profile(**fields)
228
- return profile_hash(true) if fields.empty?
229
- changes = {}
230
- fields.each do |k, v|
231
- key = k.to_s
232
- if key == "kind"
233
- changes[key] = v.nil? ? nil : Profile.kind!(v)
234
- elsif key == "name"
235
- changes[key] = v.nil? ? nil : Profile.text!("name", v, Profile::NAME_MAX)
236
- elsif key == "host"
237
- changes[key] = v.nil? ? nil : Profile.text!("host", v, Profile::HOST_MAX)
238
- elsif key == "icon"
239
- changes[key] = v.nil? ? nil : Profile.icon!(v)
240
- else
241
- raise ArgumentError, "unknown profile field #{key} (kind, name, host, icon)"
219
+ # The fields of a node's profile are, in order of precedence: those set
220
+ # on a connection of that node (Connection#profile), those set for the
221
+ # whole program (Asterism.profile_defaults), the built-in defaults.
222
+
223
+ # The fields every node of this program starts with: what a board's
224
+ # firmware says about the board before the application connects (Family
225
+ # mruby does). With keywords, sets those fields (kind:, name:, host:,
226
+ # icon:, as Connection#profile takes them; nil removes one, icon: nil is
227
+ # no icon) and the others stay; returns the profile they make (a node
228
+ # that set nothing of its own has it). Without keywords, returns it.
229
+ # The built-in defaults: kind "picoruby" on mruby / PicoRuby, "cruby" on
230
+ # CRuby; no host, no name; the icon is the file at
231
+ # Asterism.default_icon_path when there is one (the boards:
232
+ # /usr/share/asterism/icon.png; CRuby: the gem's data/icon.png). Every
233
+ # node connected now puts its changed profile.
234
+ def self.profile_defaults(**fields)
235
+ @profile_defaults ||= {}
236
+ unless fields.empty?
237
+ changes = Profile.fields!(fields)
238
+ set = @profile_defaults
239
+ changes.each do |k, v|
240
+ if v.nil? && k != "icon"
241
+ set.delete(k)
242
+ else
243
+ set[k] = v # icon: nil stays, so the default file is not used
244
+ end
242
245
  end
246
+ @profile_icons = nil
247
+ tell_node_profile(nil)
243
248
  end
244
- set = (@profile_set ||= {})
249
+ profile_hash(nil, true)
250
+ end
251
+
252
+ # The fields a connection set for its node: node => {field => value};
253
+ # a nil value leaves the field out (it hides the program's default).
254
+ # @api private
255
+ def self.node_fields(node)
256
+ (@node_profiles ||= {})[node] ||= {}
257
+ end
258
+
259
+ # Sets fields of a node's profile (Connection#profile) and puts it.
260
+ # @api private
261
+ def self.set_node_profile(node, fields)
262
+ changes = Profile.fields!(fields)
263
+ set = node_fields(node)
245
264
  changes.each do |k, v|
246
- if v.nil? && k != "icon"
247
- set.delete(k)
265
+ if k == "kind" && v.nil?
266
+ set.delete(k) # back to the program's default kind
248
267
  else
249
- set[k] = v # icon: nil stays, so the default file is not used
268
+ set[k] = v # name: nil, host: nil, icon: nil leave the field out
250
269
  end
251
270
  end
252
- @profile_icon = nil if changes.key?("icon")
253
- tell_profile
254
- profile_hash(true)
255
- end
256
-
257
- # Another node's profile (Asterism::Profile.check has cleaned it), or nil
258
- # when it does not answer in time (an older version of Asterism, a ROS 2
259
- # node, a node that is gone). icon: false asks for it without the icon
260
- # bytes (icon_hash still says which icon it is). timeout: in seconds (or
261
- # timeout_ms:), 2.0 by default. Waits like a call (polling, answering
262
- # calls meanwhile); raises Disconnected when not connected. This node's
263
- # own profile comes without the network.
264
- def self.profile_of(node, icon: true, timeout: nil, timeout_ms: nil)
265
- f = profile_async(node, icon, timeout, timeout_ms)
266
- begin
267
- raw = f.raw_value
268
- rescue Disconnected
269
- raise
270
- rescue Error
271
- return nil # no answer in time, or not MessagePack
272
- end
273
- Profile.check(raw)
271
+ @profile_icons = nil
272
+ tell_node_profile(node)
273
+ profile_hash(node, true)
274
274
  end
275
275
 
276
+ # Puts the profile of node (nil: of every node) on its key, once, from
277
+ # one of its connections.
276
278
  # @api private
277
- def self.profile_async(node, icon, timeout, timeout_ms)
278
- node = check_name("node", node)
279
- ms = time_ms("Asterism.profile_of", timeout, timeout_ms, nil, DEFAULT_TIMEOUT_MS)
280
- path = "#{node}/#{Profile::KEY}"
281
- return Future.answered(path, "profile", Codec.pack(profile_hash(icon))) if @session && node == @node
282
- begin
283
- g = session!.get("#{ROOT}/#{path}", timeout_ms: ms, params: icon ? nil : "icon=0")
284
- rescue Asterism::Zenoh::Error => e
285
- lost_if_closed(e.message)
286
- raise Disconnected, e.message
279
+ def self.tell_node_profile(node)
280
+ told = {}
281
+ list = connections
282
+ i = 0
283
+ while i < list.size
284
+ c = list[i]
285
+ i += 1
286
+ n = c.node_id
287
+ next if told[n] || (!node.nil? && n != node)
288
+ told[n] = true
289
+ c.tell_profile
287
290
  end
288
- Future.new(path, "profile", g, now_ms + ms, ms)
289
291
  end
290
292
 
291
- # The kind when the application did not set one.
293
+ # The kind when neither the node nor the program set one.
292
294
  # @api private
293
295
  def self.default_profile_kind
294
296
  "picoruby"
295
297
  end
296
298
 
297
- # Where the default icon is read from (when the application gave none);
298
- # nil for none. CRuby: the gem's data/icon.png.
299
+ # Where the default icon is read from (when nobody gave one); nil for
300
+ # none. CRuby: the gem's data/icon.png.
299
301
  def self.default_icon_path
300
302
  "/usr/share/asterism/icon.png"
301
303
  end
@@ -309,14 +311,18 @@ module Asterism
309
311
  nil
310
312
  end
311
313
 
312
- # The profile as sent: String keys, the icon only when with_icon.
314
+ # The profile of node (nil: the program's defaults alone) as sent:
315
+ # String keys, the icon only when with_icon.
313
316
  # @api private
314
- def self.profile_hash(with_icon)
315
- set = @profile_set || {}
316
- h = { "protocol" => PROTOCOL, "kind" => set["kind"] || default_profile_kind }
317
- h["host"] = set["host"] if set["host"]
318
- h["name"] = set["name"] if set["name"]
319
- icon = profile_icon
317
+ def self.profile_hash(node, with_icon)
318
+ d = @profile_defaults || {}
319
+ n = node.nil? ? {} : node_fields(node)
320
+ h = { "protocol" => PROTOCOL, "kind" => n["kind"] || d["kind"] || default_profile_kind }
321
+ host = n.key?("host") ? n["host"] : d["host"]
322
+ h["host"] = host if host
323
+ name = n.key?("name") ? n["name"] : d["name"]
324
+ h["name"] = name if name
325
+ icon = profile_icon(node)
320
326
  if icon
321
327
  h["icon"] = icon[0] if with_icon
322
328
  h["icon_hash"] = icon[1]
@@ -324,50 +330,213 @@ module Asterism
324
330
  h
325
331
  end
326
332
 
327
- # [bytes, icon_hash] of this node's icon, or nil. The default file is
328
- # read once, when the profile is first needed.
333
+ # [bytes, icon_hash] of node's icon, or nil. The default file is read
334
+ # once, when a profile is first needed.
329
335
  # @api private
330
- def self.profile_icon
331
- if @profile_icon.nil?
332
- set = @profile_set || {}
333
- bytes = set.key?("icon") ? set["icon"] : nil
334
- unless set.key?("icon")
335
- path = default_icon_path
336
- bytes = path ? read_icon_file(path) : nil
336
+ def self.profile_icon(node)
337
+ cache = (@profile_icons ||= {})
338
+ key = node.nil? ? "" : node
339
+ got = cache[key]
340
+ if got.nil?
341
+ d = @profile_defaults || {}
342
+ n = node.nil? ? {} : node_fields(node)
343
+ if n.key?("icon")
344
+ bytes = n["icon"]
345
+ elsif d.key?("icon")
346
+ bytes = d["icon"]
347
+ else
348
+ bytes = default_icon_bytes
337
349
  end
338
350
  info = bytes ? Profile.png_info(bytes) : nil
339
351
  # A binary String: it goes as a MessagePack bin.
340
- @profile_icon = info ? [Profile.binary(bytes), info[2]] : false
352
+ got = info ? [Profile.binary(bytes), info[2]] : false
353
+ cache[key] = got
341
354
  end
342
- @profile_icon ? @profile_icon : nil
355
+ got ? got : nil
343
356
  end
344
357
 
345
- # Puts the profile without its icon on its key (while connected).
358
+ # The bytes of the default icon file (read once), or nil.
346
359
  # @api private
347
- def self.tell_profile
348
- s = @session
349
- return unless s
350
- s.put("#{ROOT}/#{@node}/#{Profile::KEY}", Codec.pack(profile_hash(false)))
351
- rescue Asterism::Zenoh::Error
352
- nil # the next poll notices a lost connection
360
+ def self.default_icon_bytes
361
+ if @default_icon.nil?
362
+ path = default_icon_path
363
+ bytes = path ? read_icon_file(path) : nil
364
+ @default_icon = bytes ? bytes : false
365
+ end
366
+ @default_icon ? @default_icon : nil
353
367
  end
354
368
 
355
- # @api private
356
- def self.answer_profile
357
- qa = @profile_queryable
358
- return unless qa && qa.pending > 0
359
- queries = qa.each_pending
360
- i = 0
361
- while i < queries.size
362
- q = queries[i]
363
- i += 1
369
+ module Profile
370
+ # The fields of Connection#profile / Asterism.profile_defaults, checked:
371
+ # {field => value or nil}, or ArgumentError with the reason.
372
+ # @api private
373
+ def self.fields!(fields)
374
+ changes = {}
375
+ fields.each do |k, v|
376
+ key = k.to_s
377
+ if key == "kind"
378
+ changes[key] = v.nil? ? nil : kind!(v)
379
+ elsif key == "name"
380
+ changes[key] = v.nil? ? nil : text!("name", v, NAME_MAX)
381
+ elsif key == "host"
382
+ changes[key] = v.nil? ? nil : text!("host", v, HOST_MAX)
383
+ elsif key == "icon"
384
+ changes[key] = v.nil? ? nil : icon!(v)
385
+ else
386
+ raise ArgumentError, "unknown profile field #{key} (kind, name, host, icon)"
387
+ end
388
+ end
389
+ changes
390
+ end
391
+ end
392
+
393
+ class Connection
394
+ # This node's profile (the node this connection is). With keywords,
395
+ # sets those fields and returns the profile; without, returns it. The
396
+ # fields: kind: (a Symbol or String; nil goes back to the program's
397
+ # default), name:, host: (Strings; nil leaves them out), icon: (the
398
+ # bytes of a PNG; nil for no icon). The fields not given stay as they
399
+ # are. Raises ArgumentError for a field it does not accept (see
400
+ # Asterism::Profile).
401
+ #
402
+ # The profile belongs to the node: every connection of this program
403
+ # connected as the same node answers it, and a change made on one of
404
+ # them is theirs too. It is kept after close (a connection opened again
405
+ # as that node has it). A change is put on the profile's key.
406
+ def profile(**fields)
407
+ return ::Asterism.profile_hash(@node_id, true) if fields.empty?
408
+ ::Asterism.set_node_profile(@node_id, fields)
409
+ end
410
+
411
+ # Another node's profile (Asterism::Profile.check has cleaned it), or nil
412
+ # when it does not answer in time (an older version of Asterism, a ROS 2
413
+ # node, a node that is gone). icon: false asks for it without the icon
414
+ # bytes (icon_hash still says which icon it is). timeout: in seconds (or
415
+ # timeout_ms:), 2.0 by default. Waits like a call (polling, answering
416
+ # calls meanwhile); raises Disconnected when not connected. This node's
417
+ # own profile comes without the network.
418
+ def profile_of(node, icon: true, timeout: nil, timeout_ms: nil)
419
+ f = profile_async(node, icon, timeout, timeout_ms)
364
420
  begin
365
- q.reply("#{ROOT}/#{@node}/#{Profile::KEY}", Codec.pack(profile_hash(Profile.wants_icon?(q.params))))
366
- ensure
421
+ raw = f.raw_value
422
+ rescue ::Asterism::Disconnected
423
+ raise
424
+ rescue ::Asterism::Error
425
+ return nil # no answer in time, or not MessagePack
426
+ end
427
+ ::Asterism::Profile.check(raw)
428
+ end
429
+
430
+ # @api private
431
+ def profile_async(node, icon, timeout, timeout_ms)
432
+ node = ::Asterism.check_name("node", node)
433
+ ms = ::Asterism.time_ms("Connection#profile_of", timeout, timeout_ms, nil, ::Asterism::DEFAULT_TIMEOUT_MS)
434
+ path = "#{node}/#{::Asterism::Profile::KEY}"
435
+ if @session && node == @node_id
436
+ return ::Asterism::Future.answered(path, "profile", ::Asterism::Codec.pack(::Asterism.profile_hash(node, icon)))
437
+ end
438
+ begin
439
+ g = session!.get("#{::Asterism::ROOT}/#{path}", timeout_ms: ms, params: icon ? nil : "icon=0")
440
+ rescue ::Asterism::Zenoh::Error => e
441
+ lost_if_closed(e.message)
442
+ raise ::Asterism::Disconnected, e.message
443
+ end
444
+ ::Asterism::Future.new(self, path, "profile", g, ::Asterism.now_ms + ms, ms)
445
+ end
446
+
447
+ # The protocol a node or an application speaks. protocol_of("node/app"):
448
+ # the number its application token says (docs/protocol.md 6.1), nil
449
+ # while it is not there. protocol_of(node): the number its applications'
450
+ # tokens say (the lowest when they differ); for a node none of whose
451
+ # applications is known, the "protocol" of its profile (7.3; 0 when the
452
+ # profile has none, nil when it does not answer in time; asked once,
453
+ # without the icon, and kept until the node leaves). An application of
454
+ # another protocol than this one (Asterism::PROTOCOL) is not refused:
455
+ # its values still flow, calls between them are refused with the error
456
+ # "protocol" (4.3); each connection says so once per application
457
+ # (app_protocol_seen).
458
+ def protocol_of(node, timeout: nil, timeout_ms: nil)
459
+ node = node.to_s
460
+ cut = node.index("/")
461
+ unless cut.nil?
462
+ app = ::Asterism.check_name("app", node[cut + 1, node.size])
463
+ node = ::Asterism.check_name("node", node[0, cut])
464
+ return ::Asterism::PROTOCOL if node == @node_id && app == @app
465
+ return @app_protocols ? @app_protocols["#{node}/#{app}"] : nil
466
+ end
467
+ node = ::Asterism.check_name("node", node)
468
+ return ::Asterism::PROTOCOL if node == @node_id
469
+ got = protocol_known(node)
470
+ return got unless got.nil?
471
+ h = profile_of(node, icon: false, timeout: timeout, timeout_ms: timeout_ms)
472
+ return nil if h.nil?
473
+ protocol_store(node, h["protocol"])
474
+ end
475
+
476
+ # Keeps what a node's profile says of its protocol (0: nothing).
477
+ # @api private
478
+ def protocol_store(node, pr)
479
+ pr = 0 unless pr.is_a?(Integer)
480
+ @protocols[node] = pr if @protocols
481
+ pr
482
+ end
483
+
484
+ # The protocol of a node known without asking: the lowest its
485
+ # applications' tokens say, else what its profile said before.
486
+ # @api private
487
+ def protocol_known(node)
488
+ low = nil
489
+ list = @app_protocols
490
+ if list
491
+ prefix = "#{node}/"
492
+ list.each do |name, pr|
493
+ low = pr if name.start_with?(prefix) && (low.nil? || pr < low)
494
+ end
495
+ end
496
+ return low unless low.nil?
497
+ @protocols ? @protocols[node] : nil
498
+ end
499
+
500
+ # An application token came: says once (per program) about an
501
+ # application that speaks another protocol than this one.
502
+ # @api private
503
+ def app_protocol_seen(name, pr)
504
+ return if pr == ::Asterism::PROTOCOL
505
+ ::Asterism.warn_text_once("protocol #{name} #{pr}",
506
+ "asterism: #{name} speaks Asterism protocol #{pr}, this program " \
507
+ "#{::Asterism::PROTOCOL}: calls between them are refused (error \"protocol\"), " \
508
+ "its values still flow but may be read wrong (docs/protocol.md 6.1)")
509
+ end
510
+
511
+ # Puts the node's profile without its icon on its key (while connected).
512
+ # @api private
513
+ def tell_profile
514
+ s = @session
515
+ return unless s
516
+ s.put("#{::Asterism::ROOT}/#{@node_id}/#{::Asterism::Profile::KEY}",
517
+ ::Asterism::Codec.pack(::Asterism.profile_hash(@node_id, false)))
518
+ rescue ::Asterism::Zenoh::Error
519
+ nil # the next poll notices a lost connection
520
+ end
521
+
522
+ # @api private
523
+ def answer_profile
524
+ qa = @profile_queryable
525
+ return unless qa && qa.pending > 0
526
+ queries = qa.each_pending
527
+ i = 0
528
+ while i < queries.size
529
+ q = queries[i]
530
+ i += 1
367
531
  begin
368
- q.finish
369
- rescue Asterism::Zenoh::Error
370
- # the session went away meanwhile
532
+ h = ::Asterism.profile_hash(@node_id, ::Asterism::Profile.wants_icon?(q.params))
533
+ q.reply("#{::Asterism::ROOT}/#{@node_id}/#{::Asterism::Profile::KEY}", ::Asterism::Codec.pack(h))
534
+ ensure
535
+ begin
536
+ q.finish
537
+ rescue ::Asterism::Zenoh::Error
538
+ # the session went away meanwhile
539
+ end
371
540
  end
372
541
  end
373
542
  end
data/mrblib/proxy.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Asterism
2
- # A stand-in for an object on another machine (Asterism["node/app/obj"]).
2
+ # A stand-in for an object on another machine (net["node/app/obj"], made
3
+ # by a connection: its calls go out on that connection).
3
4
  # Any method it does not have itself becomes a call that waits for the
4
5
  # answer. respond_to? and remote_methods come from the object's meta.
5
6
  #
@@ -11,7 +12,8 @@ module Asterism
11
12
  # forwarded.
12
13
  LOCAL_ONLY = [:to_ary, :to_str, :to_hash, :to_proc, :to_a, :to_io]
13
14
 
14
- def initialize(path, timeout_ms)
15
+ def initialize(connection, path, timeout_ms)
16
+ @connection = connection
15
17
  @path = path
16
18
  @timeout_ms = timeout_ms
17
19
  @meta = nil
@@ -21,10 +23,15 @@ module Asterism
21
23
  @path
22
24
  end
23
25
 
26
+ # The Asterism::Connection the calls go out on.
27
+ def asterism_connection
28
+ @connection
29
+ end
30
+
24
31
  def method_missing(name, *args, **kw, &blk)
25
32
  return super if ::Asterism::Proxy::LOCAL_ONLY.include?(name)
26
33
  raise ::ArgumentError, "a block cannot be sent to #{@path}" if blk
27
- ::Asterism.call(@path, name, args, kw, @timeout_ms)
34
+ @connection.call(@path, name, args, kw, @timeout_ms)
28
35
  end
29
36
 
30
37
  # Calls the remote method by its name (a String or Symbol): for the
@@ -33,7 +40,7 @@ module Asterism
33
40
  # identifiers (from objects written in other languages).
34
41
  def asterism_call(name, *args, **kw, &blk)
35
42
  raise ::ArgumentError, "a block cannot be sent to #{@path}" if blk
36
- ::Asterism.call(@path, name.to_s, args, kw, @timeout_ms)
43
+ @connection.call(@path, name.to_s, args, kw, @timeout_ms)
37
44
  end
38
45
 
39
46
  # In Ruby rather than through respond_to_missing?: Object#respond_to? is
@@ -64,7 +71,7 @@ module Asterism
64
71
  # The meta reply, fetched once:
65
72
  # {"methods" => [{"name" => name, "arity" => n}, ...]}.
66
73
  def asterism_meta
67
- @meta ||= ::Asterism.meta(@path, @timeout_ms)
74
+ @meta ||= @connection.meta(@path, @timeout_ms)
68
75
  end
69
76
 
70
77
  # Forget the meta (the object may have been exposed again).
@@ -84,7 +91,7 @@ module Asterism
84
91
 
85
92
  # The same object, calls that do not wait: each returns a Future.
86
93
  def async
87
- AsyncProxy.new(@path, @timeout_ms)
94
+ AsyncProxy.new(@connection, @path, @timeout_ms)
88
95
  end
89
96
 
90
97
  def inspect
@@ -97,7 +104,8 @@ module Asterism
97
104
  end
98
105
 
99
106
  class AsyncProxy
100
- def initialize(path, timeout_ms)
107
+ def initialize(connection, path, timeout_ms)
108
+ @connection = connection
101
109
  @path = path
102
110
  @timeout_ms = timeout_ms
103
111
  end
@@ -105,7 +113,7 @@ module Asterism
105
113
  def method_missing(name, *args, **kw, &blk)
106
114
  return super if ::Asterism::Proxy::LOCAL_ONLY.include?(name)
107
115
  raise ::ArgumentError, "a block cannot be sent to #{@path}" if blk
108
- ::Asterism.call_async(@path, name, args, kw, @timeout_ms)
116
+ @connection.call_async(@path, name, args, kw, @timeout_ms)
109
117
  end
110
118
 
111
119
  def respond_to_missing?(name, _include_private = false)
@@ -115,7 +123,7 @@ module Asterism
115
123
  # Proxy#asterism_call that does not wait: returns a Future.
116
124
  def asterism_call(name, *args, **kw, &blk)
117
125
  raise ::ArgumentError, "a block cannot be sent to #{@path}" if blk
118
- ::Asterism.call_async(@path, name.to_s, args, kw, @timeout_ms)
126
+ @connection.call_async(@path, name.to_s, args, kw, @timeout_ms)
119
127
  end
120
128
 
121
129
  def inspect