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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +92 -0
- data/README.md +416 -183
- data/data/transfer/transfer.rb +7 -5
- data/lib/asterism/cli/commands.rb +10 -2
- data/lib/asterism/cli/complete.rb +2 -1
- data/lib/asterism/cli/mcp.rb +90 -24
- data/lib/asterism/cli/nouns.rb +175 -22
- data/lib/asterism/cli/survey.rb +84 -19
- data/lib/asterism/cli/values.rb +249 -35
- data/lib/asterism/cli.rb +71 -22
- data/lib/asterism/cruby/objects.rb +138 -247
- data/lib/asterism/cruby/ros.rb +1 -1
- data/lib/asterism/cruby/runner.rb +2 -1
- data/lib/asterism/cruby/zenoh.rb +8 -8
- data/lib/asterism/cruby.rb +10 -6
- data/lib/asterism/transfer/endpoint.rb +29 -12
- data/lib/asterism/version.rb +3 -3
- data/lib/asterism.rb +4 -3
- data/mrblib/asterism.rb +1123 -804
- data/mrblib/future.rb +13 -8
- data/mrblib/profile.rb +286 -117
- data/mrblib/proxy.rb +17 -9
- data/mrblib/status.rb +153 -108
- data/mrblib/topic.rb +1254 -0
- metadata +2 -1
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:
|
|
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::
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
398
|
-
in [:leave, blk] then
|
|
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.
|
|
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
|
-
|
|
518
|
-
|
|
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.
|
|
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.
|
|
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
|
-
|
|
669
|
+
@net[path, timeout_ms: ms].asterism_call(@opts[:call], *args)
|
|
658
670
|
elsif chunks.size == 3
|
|
659
|
-
raw =
|
|
671
|
+
raw = @net.meta(path, ms)
|
|
660
672
|
raise ::Asterism::Error, "malformed meta from #{path}" unless raw.is_a?(Hash)
|
|
661
673
|
else
|
|
662
|
-
|
|
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 |
|
|
679
|
-
px =
|
|
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:",
|
|
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
|
-
|
|
745
|
-
|
|
746
|
-
|
|
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 /
|
|
2
|
-
# receiving thread (CRuby only).
|
|
3
|
-
#
|
|
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
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
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
|
-
#
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
|
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
|
-
#
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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(
|
|
51
|
+
blk.call(conn)
|
|
62
52
|
ensure
|
|
63
|
-
|
|
53
|
+
conn.close
|
|
64
54
|
end
|
|
65
55
|
end
|
|
56
|
+
end
|
|
57
|
+
singleton_class.prepend(LockedModule)
|
|
66
58
|
|
|
67
|
-
|
|
68
|
-
#
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
128
|
+
prepend Locked
|
|
129
129
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
-
#
|
|
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.
|
|
147
|
-
# stays
|
|
148
|
-
# closes it. Disconnected when not connected.
|
|
149
|
-
def
|
|
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 =
|
|
152
|
-
@
|
|
153
|
-
@
|
|
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
|
-
|
|
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
|
-
#
|
|
218
|
-
|
|
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
|
-
|
|
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
|
-
|
|
185
|
+
runner.start
|
|
295
186
|
self
|
|
296
187
|
end
|
|
297
188
|
|
|
298
189
|
def stop
|
|
299
|
-
@runner
|
|
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
|
-
|
|
197
|
+
runner.run
|
|
307
198
|
end
|
|
308
199
|
|
|
309
200
|
def running?
|
|
310
|
-
@runner
|
|
311
|
-
|
|
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
|
-
|
|
324
|
-
|
|
325
|
-
|
|
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
|
-
|
|
329
|
-
|
|
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
|
data/lib/asterism/cruby/ros.rb
CHANGED
|
@@ -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.
|
|
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
|