asterism 0.13.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/lib/asterism/cli.rb CHANGED
@@ -8,6 +8,8 @@
8
8
  # asterism object list 'games/games/*' / object info games/games/othello1
9
9
  # asterism object find join # the objects that have join
10
10
  # asterism status get games/games # echo: as it changes, hz: how often
11
+ # asterism topic list 'games/*' # the topics put on, with their types
12
+ # asterism topic info games/games/othello1/state
11
13
  # asterism object get games/games/othello1 state # the next put there
12
14
  # asterism call games/games/othello1 view '"<token>"'
13
15
  # asterism ping / watch / tree / mcp # as before
@@ -25,11 +27,11 @@
25
27
  # Inside another program (a console's bridge) it runs on that program's
26
28
  # connection instead of opening one:
27
29
  #
28
- # cli = Asterism::CLI.new(out: io, err: io, env: {}, net: Asterism.net,
30
+ # cli = Asterism::CLI.new(out: io, err: io, env: {}, net: conn,
29
31
  # stop: -> { asked_to_stop }, permit: ->(plan) { nil })
30
32
  # cli.run(%w[node list])
31
33
  #
32
- # net: the Asterism::Net to use (it is neither started nor closed here; what
34
+ # net: the Asterism::Connection to use (it is neither started nor closed here; what
33
35
  # a command subscribes to or hooks onto it is released when the command
34
36
  # ends); stop: asked often while a command streams (watch, echo, hz, get,
35
37
  # ping), true ends it as Ctrl-C does; permit: asked once the words are read
@@ -340,7 +342,7 @@ module Asterism
340
342
  end
341
343
 
342
344
  # Connects, starts the receiving thread, waits for the tokens and yields
343
- # the Net and a Survey.
345
+ # the connection and a Survey.
344
346
  def connect(app_prefix: "cli", &blk)
345
347
  return connect_given(&blk) if embedded?
346
348
  router, creds, peer = network
@@ -358,16 +360,18 @@ module Asterism
358
360
  locator = peer
359
361
  kw[:mode] = :peer
360
362
  end
361
- ::Asterism.profile(name: app_prefix == "mcp" ? "asterism MCP" : "asterism CLI")
362
363
  ::Asterism.connect(locator, **kw) do |net|
364
+ net.profile(name: app_prefix == "mcp" ? "asterism MCP" : "asterism CLI")
363
365
  net.on_error { |e, where| @err.puts "asterism: #{where}: #{e.class}: #{e.message}" }
364
366
  net.start
365
- survey = Survey.new(timeout: @opts[:timeout])
367
+ @net = net
368
+ survey = Survey.new(net, timeout: @opts[:timeout])
366
369
  survey.settle(@opts[:settle])
367
370
  begin
368
371
  yield net, survey
369
372
  ensure
370
373
  release_held
374
+ @net = nil
371
375
  end
372
376
  end
373
377
  end
@@ -377,7 +381,8 @@ module Asterism
377
381
  # the command subscribed to or hooked onto it is released at the end.
378
382
  def connect_given
379
383
  raise ::Asterism::Disconnected, "the connection is not there" unless @given_net.connected?
380
- yield @given_net, Survey.new(timeout: @opts[:timeout])
384
+ @net = @given_net
385
+ yield @given_net, Survey.new(@given_net, timeout: @opts[:timeout])
381
386
  ensure
382
387
  release_held
383
388
  end
@@ -394,8 +399,8 @@ module Asterism
394
399
  held = @lock.synchronize { @held.slice!(0..) }
395
400
  held.reverse_each do |h|
396
401
  case h
397
- in [:join, blk] then (@given_net || ::Asterism).off_join(blk)
398
- in [:leave, blk] then (@given_net || ::Asterism).off_leave(blk)
402
+ in [:join, blk] then @net&.off_join(blk)
403
+ in [:leave, blk] then @net&.off_leave(blk)
399
404
  else h.close
400
405
  end
401
406
  rescue StandardError
@@ -509,19 +514,26 @@ module Asterism
509
514
  sv.statuses.sort.each { |path, st| event("status", path, status: st, now: true) }
510
515
  net.on_join(&hold([:join, proc { |n| event("join", n, what: "node") }])[1])
511
516
  net.on_leave(&hold([:leave, proc { |n| event("leave", n, what: "node") }])[1])
512
- hold(net.session.liveliness_watch("asterism/**") do |key, alive|
517
+ hold(net.zenoh.liveliness_watch("asterism/**") do |key, alive|
513
518
  # The tokens alive at the start come first, in one burst: not news.
514
519
  next unless @watch_started
515
520
  path = key.delete_prefix("asterism/")
516
521
  next if path == own || path.start_with?("#{own}/")
517
- what = { 2 => "app", 3 => "object" }[path.count("/") + 1]
518
- event(alive ? "join" : "leave", path, what: what) if what
522
+ parts = path.split("/")
523
+ if parts.size == 3
524
+ event(alive ? "join" : "leave", path, what: "object")
525
+ elsif (pr = ::Asterism.token_protocol(parts))
526
+ # An application's token says its protocol (docs/protocol.md 6.1).
527
+ app = parts[0, 2].join("/")
528
+ what = pr == ::Asterism::PROTOCOL ? "app" : "app (protocol #{pr})"
529
+ event(alive ? "join" : "leave", app, what: what)
530
+ end
519
531
  end)
520
- hold(net.session.subscribe(Survey::STATUS_ALL) do |sample|
532
+ hold(net.zenoh.subscribe(Survey::STATUS_ALL) do |sample|
521
533
  got = ::Asterism::Status.parse(sample.key, sample.payload)
522
534
  event("status", "#{got[0]}/#{got[1]}", status: got[2]) if got && "#{got[0]}/#{got[1]}" != own
523
535
  end)
524
- hold(net.session.subscribe(Survey::PROFILES_ALL) do |sample|
536
+ hold(net.zenoh.subscribe(Survey::PROFILES_ALL) do |sample|
525
537
  node = sample.key.split("/")[1]
526
538
  pr = ::Asterism::Profile.check(::Asterism::Codec.unpack(sample.payload))
527
539
  event("profile", node, profile: pr) if pr && node != net.node_id
@@ -654,12 +666,12 @@ module Asterism
654
666
 
655
667
  def ping_once(path, chunks, args, ms)
656
668
  if @opts[:call]
657
- ::Asterism[path, timeout_ms: ms].asterism_call(@opts[:call], *args)
669
+ @net[path, timeout_ms: ms].asterism_call(@opts[:call], *args)
658
670
  elsif chunks.size == 3
659
- raw = ::Asterism.meta(path, ms)
671
+ raw = @net.meta(path, ms)
660
672
  raise ::Asterism::Error, "malformed meta from #{path}" unless raw.is_a?(Hash)
661
673
  else
662
- ::Asterism.profile_async(path, false, nil, ms).raw_value
674
+ @net.profile_async(path, false, nil, ms).raw_value
663
675
  end
664
676
  end
665
677
 
@@ -675,8 +687,8 @@ module Asterism
675
687
  raise ArgumentError, "--kwargs: expected a JSON object or a Ruby Hash" unless kwargs.is_a?(Hash)
676
688
  kwargs = kwargs.transform_keys(&:to_sym)
677
689
  code = 0
678
- connect do |_net, _sv|
679
- px = ::Asterism[path, timeout: @opts[:timeout]]
690
+ connect do |net, _sv|
691
+ px = net[path, timeout: @opts[:timeout]]
680
692
  begin
681
693
  v = px.asterism_call(method, *args, **kwargs)
682
694
  structured? ? emit_line({ "value" => json_safe(v) }) : @out.puts(v.inspect)
@@ -739,11 +751,48 @@ module Asterism
739
751
  "#{paint(n['node'], :node)}#{profile_text(n['profile'])}"
740
752
  end
741
753
 
742
- # An application's status (when it has one) under "status:", indented.
754
+ # An application's status (when it has one) under "status:", and its
755
+ # topics (when it has some) under "topics:", indented.
743
756
  def status_block(app, indent)
744
- return unless app.key?("status")
745
- @out.puts "#{' ' * indent}#{paint('status:', :note)}"
746
- Text.status_lines(json_safe(app["status"]), style.width, indent + 2).each { @out.puts _1 }
757
+ if app.key?("status")
758
+ @out.puts "#{' ' * indent}#{paint('status:', :note)}"
759
+ Text.status_lines(json_safe(app["status"]), style.width, indent + 2).each { @out.puts _1 }
760
+ end
761
+ topics_block(app["topics"], indent)
762
+ end
763
+
764
+ # Topics (Survey.topic_entry) under "topics:", one a line: the name and
765
+ # what topic_about says.
766
+ def topics_block(topics, indent)
767
+ return if topics.nil? || topics.empty?
768
+ pad = " " * indent
769
+ @out.puts "#{pad}#{paint('topics:', :note)}"
770
+ topics.each do |t|
771
+ about = topic_about(t)
772
+ @out.puts "#{pad} #{paint(t['name'], :object)}#{" #{paint(about, :note)}" unless about.empty?}"
773
+ Text.wrap_text(t["desc"], style.width, first: "#{pad} ").each { @out.puts _1 } if t["desc"]
774
+ end
775
+ end
776
+
777
+ # A topic in a few words: its type, then its encoding, unit and
778
+ # "ts" (the sender's time comes along) in parentheses.
779
+ def topic_about(t)
780
+ more = [t["encoding"], t["unit"], t["ts"] ? "ts" : nil].compact
781
+ [t["type"] || "(no type)", more.empty? ? nil : "(#{more.join(', ')})"].compact.join(" ")
782
+ end
783
+
784
+ # An application's own types (Asterism::Types.check) under label:
785
+ # the name, then the fields as name type.
786
+ def types_block(types, indent, label: "types:")
787
+ return if types.nil? || types.empty?
788
+ pad = " " * indent
789
+ @out.puts "#{pad}#{paint(label, :note)}"
790
+ types.sort.each do |name, d|
791
+ @out.puts "#{pad} #{paint(name, :object)}"
792
+ Text.wrap_text(d["d"], style.width, first: "#{pad} ").each { @out.puts _1 } if d["d"]
793
+ fields = d["f"].map { |f, t| "#{f} #{paint(t.is_a?(Integer) ? (::Asterism::Types::NAMES[t - 1] || t.to_s) : t, :note)}" }
794
+ Text.wrap(fields, style.width, first: "#{pad} ", rest: "#{pad} ", sep: " ").each { @out.puts _1 } unless fields.empty?
795
+ end
747
796
  end
748
797
 
749
798
  # The methods' signatures wrapped to the width, indented.
@@ -1,6 +1,6 @@
1
- # The object layer (Asterism.connect / expose / []) with blocks and a
2
- # receiving thread (CRuby only). Asterism.connect without a block,
3
- # Asterism.poll and the rest of the portable API work as on the boards.
1
+ # The object layer (Asterism.connect / Connection) with blocks and a
2
+ # receiving thread (CRuby only). A connection without a block, polled with
3
+ # net.poll, works as on the boards.
4
4
  #
5
5
  # Asterism.connect("tcp/192.0.2.2:7447", node: "mypc", app: "demo") do |net|
6
6
  # net.expose("screen", Screen.new, methods: [:say])
@@ -11,196 +11,154 @@
11
11
  # net.run # answer calls until Ctrl-C
12
12
  # end
13
13
  #
14
- # Locking: the object layer is one connection per process with its state in
15
- # the Asterism module, written for one thread. Every entry into it (polling,
16
- # answering, calling, exposing, listing) holds Asterism::LOCK, so the
17
- # receiving thread and the application's threads take turns. A call that
18
- # waits for its answer while the receiving thread runs does not poll; it
19
- # waits for the receiving thread's ticks (Asterism::Runner#wait_until).
14
+ # Each connection has its own receiving thread (start / run). A program may
15
+ # hold several connections, each one application.
16
+ #
17
+ # Locking: the object layer is written for one thread. Every entry into a
18
+ # connection (polling, answering, calling, exposing, listing) holds
19
+ # Asterism::LOCK, one for all the connections of the process (a call
20
+ # waiting on one connection answers the calls of the others, so they share
21
+ # it), so the receiving threads and the application's threads take turns.
22
+ # A call that waits for its answer while its connection's receiving thread
23
+ # runs does not poll; it waits for that thread's ticks
24
+ # (Asterism::Runner#wait_until).
20
25
  require "monitor"
21
26
 
22
27
  module Asterism
23
- # Held by every entry into the object layer (reentrant).
28
+ # Held by every entry into the object layer (reentrant), for every
29
+ # connection of the process.
24
30
  LOCK = Monitor.new
25
31
 
26
- # The portable API, entered under LOCK. Prepended to the Asterism module's
27
- # singleton class, so the shared methods themselves are not changed.
28
- module Locked
29
- %i[expose unexpose poll nodes [] call_async meta connected? reconnecting? exposed on_join on_leave off_join off_leave
30
- on_app_join on_app_leave off_app_join off_app_leave profile profile_async status status_async apps session].each do |m|
32
+ # Module functions that read or change the connections, under LOCK.
33
+ module LockedModule
34
+ %i[connections poll_all profile_defaults register unregister connection_of pump_others claim unclaim
35
+ warn_text_once].each do |m|
31
36
  define_method(m) do |*args, **kw, &blk|
32
37
  ::Asterism::LOCK.synchronize { super(*args, **kw, &blk) }
33
38
  end
34
39
  end
35
40
 
36
- # With a block: yields an Asterism::Net and closes when the block ends
41
+ # With a block: yields the connection and closes it when the block ends
37
42
  # (also on an exception); returns the block's value. Without a block,
38
- # as before (returns Asterism).
39
- #
40
- # credentials: a .tar from an Asterism console (Asterism::Credentials):
41
- # its certificates go to zenoh (mutual TLS), and the locator and node:
42
- # default to its router and name (given ones win).
43
+ # returns the connection (as on the boards).
43
44
  def connect(locator = nil, node: nil, app:, mode: nil, listen: nil, config: nil, connect_timeout: nil,
44
45
  check_timeout: nil, credentials: nil, &blk)
45
- if credentials
46
- locator, config, creds = ::Asterism::Credentials.merge(credentials, locator, config)
47
- node ||= creds.name
48
- end
49
- ::Asterism::LOCK.synchronize do
50
- super(locator, node: node, app: app, mode: mode, listen: listen, config: config,
51
- connect_timeout: connect_timeout, check_timeout: check_timeout)
52
- # The Net of a connection that was lost (not closed) belongs to the
53
- # old session.
54
- old = @net
55
- @net = nil
56
- old&.release
57
- end
58
- return self unless blk
59
- net = ::Asterism.net
46
+ conn = super(locator, node: node, app: app, mode: mode, listen: listen, config: config,
47
+ connect_timeout: connect_timeout, check_timeout: check_timeout, credentials: credentials)
48
+ return conn unless blk
49
+ conn.runner # what the blocks raise goes to on_error from here on
60
50
  begin
61
- blk.call(net)
51
+ blk.call(conn)
62
52
  ensure
63
- net.close
53
+ conn.close
64
54
  end
65
55
  end
56
+ end
57
+ singleton_class.prepend(LockedModule)
66
58
 
67
- # Stops the receiving thread first (outside the lock: it may be inside
68
- # a tick), then closes.
69
- def close
70
- net = @net
71
- @net = nil
72
- net&.release
73
- ::Asterism::LOCK.synchronize { super }
74
- end
59
+ class Connection
60
+ # The portable API, entered under LOCK. Prepended, so the shared methods
61
+ # themselves are not changed.
62
+ module Locked
63
+ # initialize is not among them: Asterism.connect looks for another
64
+ # copy of the application on the network (up to check_timeout:)
65
+ # without the lock, so that the program's other connections go on
66
+ # meanwhile; attach (the declarations, joining the program) holds it.
67
+ %i[attach expose unexpose poll pump nodes [] call_async meta connected? reconnecting? exposed
68
+ on_join on_leave off_join off_leave on_app_join on_app_leave off_app_join off_app_leave
69
+ profile profile_async status app_profile_async apps session publish unpublish published
70
+ topic_put define_type subscribe unsubscribe pubs_get protocol_known protocol_store
71
+ sender_cached sender_store watch_pubs].each do |m|
72
+ define_method(m) do |*args, **kw, &blk|
73
+ ::Asterism::LOCK.synchronize { super(*args, **kw, &blk) }
74
+ end
75
+ end
75
76
 
76
- # The on_join / on_leave (and on_app_*) blocks (called from
77
- # Asterism.poll): with a Net, what they raise goes to its on_error and
78
- # the other blocks still run.
79
- def tell(where, blk, node, app = nil)
80
- net = @net
81
- return super unless net
82
- net.runner.guard(where) { super }
83
- end
77
+ # Stops the receiving thread first (outside the lock: it may be inside
78
+ # a tick), then closes.
79
+ def close
80
+ r = @runner
81
+ @runner = nil
82
+ @zenoh = nil
83
+ r&.close
84
+ ::Asterism::LOCK.synchronize { super }
85
+ end
84
86
 
85
- # Lists under the lock, yields outside it (the block may make calls).
86
- def each(pattern = "**", &blk)
87
- list = ::Asterism::LOCK.synchronize { super(pattern, &nil) }
88
- return list unless blk
89
- list.each(&blk)
90
- list.size
91
- end
87
+ # The on_join / on_leave (and on_app_*) blocks (called from poll): once
88
+ # the connection has its receiving side (runner: a connect with a
89
+ # block, start, run, on_error), what they raise goes to its on_error
90
+ # and the other blocks still run.
91
+ def tell(where, blk, node, app = nil)
92
+ r = @runner
93
+ return super unless r
94
+ r.guard(where) { super }
95
+ end
92
96
 
93
- # A call waiting for its answer. On the receiving thread, or when it
94
- # does not run: polls, as on the boards. On another thread while it
95
- # runs: waits for its ticks to bring the answer.
96
- def wait_until(&cond)
97
- loop do
98
- r = @net&.runner
99
- if r && r.running? && !r.current?
100
- r.wait_until(&cond)
101
- return if ::Asterism::LOCK.synchronize(&cond)
102
- # The receiving thread stopped before the answer: poll here.
103
- else
104
- return ::Asterism::LOCK.synchronize { super(&cond) }
105
- end
97
+ # A subscription's block (called from poll): the same.
98
+ def tell_sample(blk, value, info)
99
+ r = @runner
100
+ return super unless r
101
+ r.guard("subscribe") { super }
106
102
  end
107
- end
108
- end
109
- singleton_class.prepend(Locked)
110
103
 
111
- class Future
112
- # Prepended: done? reads the connection state, under the lock.
113
- module Locked
114
- def done?
115
- ::Asterism::LOCK.synchronize { super }
104
+ # Lists under the lock, yields outside it (the block may make calls).
105
+ def each(pattern = "**", &blk)
106
+ list = ::Asterism::LOCK.synchronize { super(pattern, &nil) }
107
+ return list unless blk
108
+ list.each(&blk)
109
+ list.size
116
110
  end
117
- end
118
- prepend Locked
119
- end
120
111
 
121
- # The connection of this process (Asterism is one per process) as an
122
- # object, with a receiving thread. nil when not connected.
123
- def self.net
124
- LOCK.synchronize do
125
- return nil unless connected?
126
- @net ||= Net.new
112
+ # A call waiting for its answer. On the receiving thread, or when it
113
+ # does not run: polls, as on the boards. On another thread while it
114
+ # runs: waits for its ticks to bring the answer.
115
+ def wait_until(&cond)
116
+ loop do
117
+ r = @runner
118
+ if r && r.running? && !r.current?
119
+ r.wait_until(&cond)
120
+ return if ::Asterism::LOCK.synchronize(&cond)
121
+ # The receiving thread stopped before the answer: poll here.
122
+ else
123
+ return ::Asterism::LOCK.synchronize { super(&cond) }
124
+ end
125
+ end
126
+ end
127
127
  end
128
- end
128
+ prepend Locked
129
129
 
130
- # The block API of the object layer. Made by Asterism.connect with a
131
- # block, or Asterism.net after a connect without one.
132
- class Net
133
- attr_reader :runner
130
+ # Enumerable over the exposed objects alive now, as proxies
131
+ # (net.map(&:asterism_path), net.count): each with the default pattern.
132
+ include Enumerable
134
133
 
135
- def initialize
136
- @runner = Runner.new(::Asterism.instance_variable_get(:@session), lock: ::Asterism::LOCK, name: "asterism")
137
- # One tick: Asterism.poll (answers the calls, tells on_join /
138
- # on_leave); false once the connection is gone.
139
- @runner.add { ::Asterism.poll ? 0 : false }
134
+ # The receiving side of this connection (made on first use): one tick
135
+ # is poll (answers the calls, tells on_join / on_leave, hands values to
136
+ # the subscriptions); false once the connection is gone. @api private
137
+ def runner
138
+ ::Asterism::LOCK.synchronize do
139
+ @runner ||= begin
140
+ r = Runner.new(@session, lock: ::Asterism::LOCK, name: "asterism #{@node_id}/#{@app}")
141
+ r.add { poll ? 0 : false }
142
+ r
143
+ end
144
+ end
140
145
  end
141
146
 
142
- # Asterism.session as a Zenoh::Connection (the block API of
147
+ # The session as a Zenoh::Connection (the block API of
143
148
  # Asterism::Zenoh.open) on the same receiving thread as the objects:
144
149
  # subscribe / queryable blocks run there, between the calls answered.
145
150
  # For a ROS 2 node on the same session:
146
- # Asterism::ROS::Connection.new(net.session).node("name"). The session
147
- # stays Asterism's: net.session.close does nothing (shared?), net.close
148
- # closes it. Disconnected when not connected.
149
- def session
151
+ # Asterism::ROS::Connection.new(net.zenoh).node("name"). The session
152
+ # stays the connection's: net.zenoh.close does nothing (shared?),
153
+ # net.close closes it. Disconnected when not connected.
154
+ def zenoh
150
155
  ::Asterism::LOCK.synchronize do
151
- s = ::Asterism.session
152
- @session = nil if @session && !@session.session.equal?(s)
153
- @session ||= ::Asterism::Zenoh::Connection.new(s, runner: @runner)
156
+ s = session
157
+ @zenoh = nil if @zenoh && !@zenoh.session.equal?(s)
158
+ @zenoh ||= ::Asterism::Zenoh::Connection.new(s, runner: runner)
154
159
  end
155
160
  end
156
161
 
157
- def node_id = ::Asterism.node_id
158
- def app = ::Asterism.app
159
- def connected? = ::Asterism.connected?
160
- def reconnecting? = ::Asterism.reconnecting?
161
- def lost_reason = ::Asterism.lost_reason
162
- def nodes = ::Asterism.nodes
163
- def exposed = ::Asterism.exposed
164
-
165
- # Asterism.expose: methods: an Array of names or a Hash name => number
166
- # of arguments (or => {args: [names], doc: "text"}); doc: the object's
167
- # own text (both go to the meta); caller: :from passes the calling node's ID as that
168
- # keyword. The object's methods run on the receiving thread (or in
169
- # run, or in whatever polls).
170
- def expose(name, obj, methods:, caller: nil, doc: nil)
171
- ::Asterism.expose(name, obj, methods: methods, caller: caller, doc: doc)
172
- end
173
-
174
- # Asterism.current_call: the call being answered on this thread, or nil.
175
- def current_call = ::Asterism.current_call
176
-
177
- def unexpose(name)
178
- ::Asterism.unexpose(name)
179
- end
180
-
181
- # Asterism.profile: this node's profile (with keywords, sets fields).
182
- def profile(**fields)
183
- ::Asterism.profile(**fields)
184
- end
185
-
186
- # Asterism.profile_of: another node's profile, or nil. Waits like a
187
- # call (for the receiving thread's ticks while it runs).
188
- def profile_of(node, icon: true, timeout: nil, timeout_ms: nil)
189
- ::Asterism.profile_of(node, icon: icon, timeout: timeout, timeout_ms: timeout_ms)
190
- end
191
-
192
- # Asterism.status: this application's status (a Hash: the whole
193
- # status; keywords: those fields). Without arguments, returns it.
194
- def status(h = nil, **fields)
195
- ::Asterism.status(h, **fields)
196
- end
197
-
198
- # Asterism.status_of: another application's status, or nil. Waits like
199
- # a call (for the receiving thread's ticks while it runs).
200
- def status_of(node, app, timeout: nil, timeout_ms: nil)
201
- ::Asterism.status_of(node, app, timeout: timeout, timeout_ms: timeout_ms)
202
- end
203
-
204
162
  # on_status(pattern = "*/*") { |node, app, status| }: the statuses put
205
163
  # by the applications whose "<node>/<app>" matches the pattern (Zenoh
206
164
  # wildcards: * one chunk, ** several), as they change, on the receiving
@@ -208,125 +166,58 @@ module Asterism
208
166
  # Returns the subscription (close it to stop).
209
167
  def on_status(pattern = "*/*", &blk)
210
168
  raise ArgumentError, "on_status needs a block" unless blk
211
- session.subscribe("#{::Asterism::ROOT}/#{pattern}/#{::Asterism::Status::KEY}") do |sample|
169
+ zenoh.subscribe("#{::Asterism::ROOT}/#{pattern}/#{::Asterism::Status::KEY}") do |sample|
212
170
  got = ::Asterism::Status.parse(sample.key, sample.payload)
213
171
  blk.call(*got) if got
214
172
  end
215
173
  end
216
174
 
217
- # Asterism.apps: the applications alive now, "<node>/<app>".
218
- def apps(pattern = "*/*") = ::Asterism.apps(pattern)
219
-
220
- # A proxy for <node>/<app>/<object>; timeout: in seconds (or
221
- # timeout_ms:).
222
- def [](path, timeout: nil, timeout_ms: nil)
223
- ::Asterism[path, timeout: timeout, timeout_ms: timeout_ms]
224
- end
225
- alias proxy []
226
-
227
- # The exposed objects alive now matching the pattern, as proxies. With
228
- # a block, yields each and returns their number (1.0: returns self, as
229
- # Ruby's each does; net.count is the number); without, an Enumerator
230
- # (net.each("*/demo/info").map(&:status)).
231
- def each(pattern = "**", &blk)
232
- return enum_for(:each, pattern) unless blk
233
- ::Asterism.each(pattern, &blk)
234
- end
235
- include Enumerable
236
-
237
- # on_join { |node| } / on_leave { |node| }: Asterism.on_join /
238
- # on_leave, the same as on the boards (called from Asterism.poll), here
239
- # on the receiving thread. What they raise goes to on_error.
240
- def on_join(&blk)
241
- ::Asterism.on_join(&blk)
242
- self
243
- end
244
-
245
- def on_leave(&blk)
246
- ::Asterism.on_leave(&blk)
247
- self
248
- end
249
-
250
- # on_app_join { |node, app| } / on_app_leave { |node, app| }: an
251
- # application appeared on the network / is gone (Asterism.on_app_join),
252
- # on the receiving thread. What they raise goes to on_error.
253
- def on_app_join(&blk)
254
- ::Asterism.on_app_join(&blk)
255
- self
256
- end
257
-
258
- def on_app_leave(&blk)
259
- ::Asterism.on_app_leave(&blk)
260
- self
261
- end
262
-
263
- def off_app_join(blk)
264
- ::Asterism.off_app_join(blk)
265
- self
266
- end
267
-
268
- def off_app_leave(blk)
269
- ::Asterism.off_app_leave(blk)
270
- self
271
- end
272
-
273
- # Removes a block given to on_join / on_leave (keep the Proc).
274
- def off_join(blk)
275
- ::Asterism.off_join(blk)
276
- self
277
- end
278
-
279
- def off_leave(blk)
280
- ::Asterism.off_leave(blk)
281
- self
282
- end
283
-
284
- # on_error { |error, where| }: what on_join / on_leave blocks raise.
285
- # (An exposed method that raises is answered to its caller as a
286
- # RemoteError, as before; it does not come here.)
175
+ # on_error { |error, where| }: what the on_join / on_leave and
176
+ # subscription blocks raise. (An exposed method that raises is answered
177
+ # to its caller as a RemoteError; it does not come here.)
287
178
  def on_error(&blk)
288
- @runner.on_error(&blk)
179
+ runner.on_error(&blk)
289
180
  self
290
181
  end
291
182
 
292
183
  # Answers calls on a thread of its own. Returns self.
293
184
  def start
294
- @runner.start
185
+ runner.start
295
186
  self
296
187
  end
297
188
 
298
189
  def stop
299
- @runner.stop
190
+ @runner&.stop
300
191
  self
301
192
  end
302
193
 
303
194
  # Answers calls on this thread until stop, the connection closing, or
304
195
  # Ctrl-C. Returns nil.
305
196
  def run
306
- @runner.run
197
+ runner.run
307
198
  end
308
199
 
309
200
  def running?
310
- @runner.running?
311
- end
312
-
313
- def poll
314
- ::Asterism.poll
315
- end
316
-
317
- # Stops the receiving thread and closes the connection (Asterism.close).
318
- def close
319
- ::Asterism.close
320
- nil
201
+ r = @runner
202
+ r ? r.running? : false
321
203
  end
322
204
 
323
- # For Asterism.close: stop receiving and forget the session.
324
- def release
325
- @runner.close
205
+ def inspect
206
+ state = if @session then running? ? " running" : ""
207
+ elsif @lost then " lost"
208
+ else " closed"
209
+ end
210
+ "#<Asterism::Connection #{@node_id}/#{@app}#{state}>"
326
211
  end
212
+ end
327
213
 
328
- def inspect
329
- "#<Asterism::Net #{node_id}/#{app}#{running? ? ' running' : ''}>"
214
+ class Future
215
+ # Prepended: done? reads the connection state, under the lock.
216
+ module Locked
217
+ def done?
218
+ ::Asterism::LOCK.synchronize { super }
219
+ end
330
220
  end
221
+ prepend Locked
331
222
  end
332
223
  end
@@ -96,7 +96,7 @@ module Asterism
96
96
  end
97
97
 
98
98
  # Stops spinning, withdraws the nodes and closes the session. On
99
- # Asterism's session (ROS::Connection.new(net.session)) it only
99
+ # an Asterism connection's session (ROS::Connection.new(net.zenoh)) it only
100
100
  # withdraws the nodes: the receiving thread and the session stay the
101
101
  # object layer's.
102
102
  def close