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/data/transfer/transfer.rb
CHANGED
|
@@ -241,7 +241,7 @@ module Asterism
|
|
|
241
241
|
|
|
242
242
|
# The receiving side. Expose it (or a subclass) with caller: :from:
|
|
243
243
|
#
|
|
244
|
-
#
|
|
244
|
+
# net.expose("inbox", inbox, methods: Asterism::Transfer::Inbox::METHODS, caller: :from)
|
|
245
245
|
#
|
|
246
246
|
# and call tick from the update loop. The hooks to override:
|
|
247
247
|
#
|
|
@@ -626,8 +626,9 @@ module Asterism
|
|
|
626
626
|
|
|
627
627
|
# The sending side of one file. Made with the receiver's path
|
|
628
628
|
# ("<node>/<app>/<object>"), the kind, the local file and the meta the
|
|
629
|
-
# kind wants
|
|
630
|
-
#
|
|
629
|
+
# kind wants, and net: the Asterism::Connection the calls go out on;
|
|
630
|
+
# call step from the update loop (which polls net) until done?. Then
|
|
631
|
+
# ok? and answer (commit's answer: the kind's), or reason.
|
|
631
632
|
#
|
|
632
633
|
# The receiver's position is the truth: every answer says where it
|
|
633
634
|
# stands and the next piece goes from there. A call that fails (no
|
|
@@ -649,8 +650,9 @@ module Asterism
|
|
|
649
650
|
attr_accessor :logger
|
|
650
651
|
attr_reader :path, :size, :sha256, :answer, :reason, :stats, :pos, :id, :kind, :piece, :window
|
|
651
652
|
|
|
652
|
-
def initialize(to, kind, path, meta: {}, name: nil, piece: PIECE_MAX, window: WINDOW_MAX,
|
|
653
|
+
def initialize(to, kind, path, net:, meta: {}, name: nil, piece: PIECE_MAX, window: WINDOW_MAX,
|
|
653
654
|
timeout_ms: CALL_TIMEOUT_MS, tries: TRIES, sha256: nil)
|
|
655
|
+
@net = net
|
|
654
656
|
@to = to
|
|
655
657
|
@kind = kind
|
|
656
658
|
@path = path
|
|
@@ -781,7 +783,7 @@ module Asterism
|
|
|
781
783
|
end
|
|
782
784
|
|
|
783
785
|
def proxy
|
|
784
|
-
|
|
786
|
+
@net[@to, timeout_ms: @timeout_ms].async
|
|
785
787
|
end
|
|
786
788
|
|
|
787
789
|
def call(method, *args)
|
|
@@ -15,7 +15,7 @@ module Asterism
|
|
|
15
15
|
},
|
|
16
16
|
"app" => {
|
|
17
17
|
"list" => ["", "the applications, node/app a line"],
|
|
18
|
-
"info" => ["NODE/APP", "one application: its status and
|
|
18
|
+
"info" => ["NODE/APP", "one application: its status, topics, types and objects"]
|
|
19
19
|
},
|
|
20
20
|
"object" => {
|
|
21
21
|
"list" => ["[PATTERN]", "the objects, node/app/object a line (PATTERN: * and ** for whole parts)"],
|
|
@@ -26,6 +26,14 @@ module Asterism
|
|
|
26
26
|
"echo" => ["PATH [KEY]", "the values it puts there as they come (the text: what changed; --once: " \
|
|
27
27
|
"the first; --raw: each whole)"]
|
|
28
28
|
},
|
|
29
|
+
"topic" => {
|
|
30
|
+
"list" => ["[NODE/APP]", "the topics the applications put values on, node/app/name a line, with their " \
|
|
31
|
+
"types (NODE/APP: * for a part; */* by default); a shared place's " \
|
|
32
|
+
"(node/@shared/name, @shared/name) with the application that declares it"],
|
|
33
|
+
"info" => ["PATH", "one topic (node/app/name): its type, encoding, unit and text, and the definition " \
|
|
34
|
+
"of its type when the application defines it; for a shared key (node/@shared/name, " \
|
|
35
|
+
"@shared/name) each application that declares it, with its own"]
|
|
36
|
+
},
|
|
29
37
|
"status" => {
|
|
30
38
|
"get" => ["NODE/APP", "an application's status, now"],
|
|
31
39
|
"echo" => ["NODE/APP", "its status, then each change as it comes (--once: now, once)"],
|
|
@@ -67,7 +75,7 @@ module Asterism
|
|
|
67
75
|
LOCAL = %w[mcp completion].freeze
|
|
68
76
|
|
|
69
77
|
# What a command does on the network. Every command only reads (the
|
|
70
|
-
# tokens, metas, profiles, statuses, the values the applications put),
|
|
78
|
+
# tokens, metas, profiles, statuses, topics, the values the applications put),
|
|
71
79
|
# except call, and ping with --call: those run a method of an object.
|
|
72
80
|
#
|
|
73
81
|
# :call path:, method: say which
|
|
@@ -81,7 +81,8 @@ module Asterism
|
|
|
81
81
|
i = args.size
|
|
82
82
|
case [key, i]
|
|
83
83
|
when ["node info", 0] then nodes
|
|
84
|
-
when ["app info", 0], ["status get", 0], ["status echo", 0], ["status hz", 0]
|
|
84
|
+
when ["app info", 0], ["status get", 0], ["status echo", 0], ["status hz", 0], ["topic list", 0],
|
|
85
|
+
["topic info", 0] then staged(cur, apps, 2)
|
|
85
86
|
when ["object list", 0], ["object info", 0], ["object get", 0], ["object echo", 0], ["objects", 0], ["call", 0]
|
|
86
87
|
staged(cur, objects, 3)
|
|
87
88
|
when ["ping", 0], ["echo", 0] then (cur.include?("/") ? [] : nodes) + staged(cur, objects, 3)
|
data/lib/asterism/cli/mcp.rb
CHANGED
|
@@ -9,10 +9,15 @@
|
|
|
9
9
|
# and objects (always; reads)
|
|
10
10
|
# asterism_objects the objects and their methods (meta) (always; reads)
|
|
11
11
|
# asterism_status the applications' statuses (always; reads)
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
12
|
+
# asterism_topics the topics the applications declare (their types,
|
|
13
|
+
# shared places included,
|
|
14
|
+
# encodings, units) and their own types' definitions
|
|
15
|
+
# (@profile?pubs;types) (always; reads)
|
|
16
|
+
# asterism_values the latest value of each topic the applications put
|
|
17
|
+
# on (asterism/<node>/<app>/<name>), as this server
|
|
18
|
+
# received them since it started, with their
|
|
19
|
+
# encodings and sequence numbers, pictures as images
|
|
20
|
+
# when asked (always; reads; lib/asterism/cli/values.rb)
|
|
16
21
|
# <node>.<app>.<object>.<method>
|
|
17
22
|
# one per method that an --allow pattern names, made
|
|
18
23
|
# from the object's meta: the arguments' names the
|
|
@@ -34,8 +39,10 @@ module Asterism
|
|
|
34
39
|
These tools look at an Asterism network: Ruby programs and boards that expose objects to each other over
|
|
35
40
|
Zenoh. Paths are <node>/<app>/<object>. asterism_nodes lists the nodes and their applications (with the
|
|
36
41
|
status each application tells about itself, a JSON object of its own), asterism_objects the objects and
|
|
37
|
-
their methods, asterism_status the statuses,
|
|
38
|
-
their
|
|
42
|
+
their methods, asterism_status the statuses, asterism_topics the topics the applications put values on
|
|
43
|
+
(with their types), asterism_values the latest values put on them (a game table's state) and in the
|
|
44
|
+
shared places (<node>/@shared/..., @shared/...; each value with its sender, from). The other
|
|
45
|
+
tools call one method each, on the object their
|
|
39
46
|
name says (<node>.<app>.<object>.<method>); only the methods this server was started to allow are there.
|
|
40
47
|
A call's answer is {"value": ...}; an error says its kind (raised, not_exposed, arguments, ...).
|
|
41
48
|
TEXT
|
|
@@ -48,7 +55,7 @@ module Asterism
|
|
|
48
55
|
@call_tools = {} # tool name => [path, method, arity, meta entry]
|
|
49
56
|
@signature = nil
|
|
50
57
|
@lock = Mutex.new
|
|
51
|
-
@values = Values::Store.new(alive: ->(app) {
|
|
58
|
+
@values = Values::Store.new(alive: ->(app) { !@net.apps(app).empty? })
|
|
52
59
|
end
|
|
53
60
|
|
|
54
61
|
attr_reader :values
|
|
@@ -68,12 +75,23 @@ module Asterism
|
|
|
68
75
|
@refresher&.kill
|
|
69
76
|
end
|
|
70
77
|
|
|
71
|
-
#
|
|
72
|
-
# does not match @profile
|
|
73
|
-
#
|
|
78
|
+
# The key expressions remember_values subscribes to: the
|
|
79
|
+
# applications' own keys (asterism/** does not match @profile,
|
|
80
|
+
# @status nor @shared), the nodes' shared places and the network's
|
|
81
|
+
# (docs/protocol.md 17).
|
|
82
|
+
VALUE_KEYS = ["#{Values::PREFIX}**", "#{Values::PREFIX}*/#{Values::SHARED}/**",
|
|
83
|
+
"#{Values::PREFIX}#{Values::SHARED}/**"].freeze
|
|
84
|
+
|
|
85
|
+
# From now on, the puts on the applications' own keys and in the
|
|
86
|
+
# shared places, and the applications that leave (their values are
|
|
87
|
+
# marked).
|
|
74
88
|
def remember_values
|
|
75
|
-
|
|
76
|
-
|
|
89
|
+
VALUE_KEYS.each do |k|
|
|
90
|
+
@net.zenoh.subscribe(k, depth: 1024) do |s|
|
|
91
|
+
@values.put(s.key, s.payload, encoding: s.encoding, attachment: s.attachment)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
@net.zenoh.liveliness_watch("#{Values::PREFIX}*/*", depth: 1024) do |key, alive|
|
|
77
95
|
@values.left(key.delete_prefix(Values::PREFIX)) unless alive
|
|
78
96
|
end
|
|
79
97
|
end
|
|
@@ -173,44 +191,92 @@ module Asterism
|
|
|
173
191
|
end
|
|
174
192
|
end
|
|
175
193
|
end,
|
|
194
|
+
::MCP::Tool.define(
|
|
195
|
+
name: "asterism_topics",
|
|
196
|
+
description: "The topics the applications put values on (asterism/<node>/<app>/<name>), as they " \
|
|
197
|
+
"declare them: {\"<node>/<app>\": {\"topics\": [{\"name\", \"type\", \"encoding\", " \
|
|
198
|
+
"\"unit\", \"desc\", \"ts\"}], \"types\": {...}}}. type: a built-in type (number, " \
|
|
199
|
+
"vector3 [x, y, z], pose [x, y, z, qx, qy, qz, qw], image, state, log, text) or the name of " \
|
|
200
|
+
"an application's own type, whose definition is in types ({\"f\": [[field, type], ...], " \
|
|
201
|
+
"\"d\": what it is}); no type: not declared. encoding: absent for MessagePack, else how " \
|
|
202
|
+
"the bytes are (image/jpeg). ts: the sender's time comes with each value. shared: the topic " \
|
|
203
|
+
"is on a shared place, its name the key below asterism/ (\"node\": " \
|
|
204
|
+
"<node>/@shared/<name>, \"global\": @shared/<name>); several applications may declare " \
|
|
205
|
+
"one shared key, each with its own declaration and types (read a value's type from the " \
|
|
206
|
+
"application that sent it). An application that declares nothing is not listed. The " \
|
|
207
|
+
"values are asterism_values's.",
|
|
208
|
+
input_schema: { properties: { node: { type: "string" }, app: { type: "string" } } },
|
|
209
|
+
annotations: { read_only_hint: true }
|
|
210
|
+
) do |server_context:, node: nil, app: nil|
|
|
211
|
+
bridge.answer do
|
|
212
|
+
list = sv.apps("#{node || '*'}/#{app || '*'}")
|
|
213
|
+
sv.topics(list).reject { |_, h| h["topics"].empty? && h["types"].empty? }
|
|
214
|
+
end
|
|
215
|
+
end,
|
|
176
216
|
::MCP::Tool.define(
|
|
177
217
|
name: "asterism_values",
|
|
178
218
|
description: VALUES_DOC,
|
|
179
219
|
input_schema: {
|
|
180
220
|
properties: {
|
|
181
221
|
pattern: { type: "string",
|
|
182
|
-
description: "keys as node/app
|
|
183
|
-
"
|
|
184
|
-
"games/games/othello1/** or games/*/*/state, not
|
|
222
|
+
description: "keys as node/app/<topic name> (a name may have several parts), with * " \
|
|
223
|
+
"(one part) and ** (any parts); default ** (every application's key). " \
|
|
224
|
+
"The whole name: games/games/othello1/** or games/*/*/state, not " \
|
|
225
|
+
"games/games/othello1 for the topic othello1/state. The shared places: " \
|
|
226
|
+
"node/@shared/<name> (*/@shared/spectate/** every node's) and " \
|
|
227
|
+
"@shared/<name> (the network's); * and ** never match @shared itself" },
|
|
185
228
|
wait: { type: "number",
|
|
186
229
|
description: "seconds (at most #{Values::Store::MAX_WAIT}) to wait for the first put " \
|
|
187
|
-
"that matches when none has come yet; default 0 (answer now)" }
|
|
230
|
+
"that matches when none has come yet; default 0 (answer now)" },
|
|
231
|
+
images: { type: "boolean",
|
|
232
|
+
description: "true: the values that are pictures (image/png, image/jpeg, image/gif, " \
|
|
233
|
+
"image/webp) come along as images, at most #{Values::Store::MAX_IMAGES}; " \
|
|
234
|
+
"default false (their size only)" }
|
|
188
235
|
}
|
|
189
236
|
},
|
|
190
237
|
annotations: { read_only_hint: true }
|
|
191
|
-
) do |server_context:, pattern: "**", wait: 0|
|
|
192
|
-
bridge.
|
|
238
|
+
) do |server_context:, pattern: "**", wait: 0, images: false|
|
|
239
|
+
bridge.values_answer(pattern, wait: wait, images: images)
|
|
193
240
|
rescue ArgumentError => e
|
|
194
241
|
bridge.error(e.message)
|
|
195
242
|
end
|
|
196
243
|
]
|
|
197
244
|
end
|
|
198
245
|
|
|
199
|
-
VALUES_DOC = "The latest value each application put on its
|
|
200
|
-
"a game table's state, a log
|
|
201
|
-
"
|
|
246
|
+
VALUES_DOC = "The latest value each application put on its topics (asterism/<node>/<app>/<name>: " \
|
|
247
|
+
"a game table's state, a log, a camera's pictures) and in the shared places (asterism/<node>/" \
|
|
248
|
+
"@shared/<name>, asterism/@shared/<name>: data no single application owns, such as a game " \
|
|
249
|
+
"host's spectate views), as this server received them: {\"since\", " \
|
|
250
|
+
"\"values\": [{\"key\", \"value\", \"t\", \"age_s\", \"app\", \"bytes\", \"format\", " \
|
|
251
|
+
"\"from\", \"seq\", \"run_id\", \"sent\", \"lost\"}]}. " \
|
|
252
|
+
"from: a shared value's sender (<node>/<app>); a shared key has one entry for each sender, " \
|
|
253
|
+
"with that sender's numbers and app. " \
|
|
202
254
|
"A put comes only when the value changes, and this server remembers only what it received " \
|
|
203
255
|
"since it started (since): a key that has not been put since then is not listed. Give wait " \
|
|
204
256
|
"(seconds) to wait for the first put that matches when none has come yet (a quiet table, " \
|
|
205
257
|
"just after the start). age_s: seconds since it came. app: alive; gone (the application is " \
|
|
206
258
|
"not on the network now: this is its last value, not the present); restarted (it left and " \
|
|
207
|
-
"came back after this value).
|
|
259
|
+
"came back after this value). format: msgpack (the value as it is), or the encoding the " \
|
|
260
|
+
"value came with (image/jpeg: the value is its size; JSON and text encodings as they are). " \
|
|
261
|
+
"seq: the value's sequence number, run_id: the sender's connection (another one: it " \
|
|
262
|
+
"connected again and numbers from 0), sent: the sender's time (when it sends it), lost: how " \
|
|
263
|
+
"many values of the key this server missed since it started. value: a " \
|
|
208
264
|
"value whose JSON is longer than #{Values::Store::VALUE_CUT} bytes is cut (truncated: " \
|
|
209
265
|
"true, value: the start of its JSON text). It keeps at most #{Values::Store::MAX_KEYS} keys " \
|
|
210
266
|
"(those received longest ago are forgotten first) and lists at most " \
|
|
211
267
|
"#{Values::Store::MAX_ANSWER} (more: how many it left out). Nothing came: the note says so " \
|
|
212
268
|
"(not put since the start, or the router does not let this server read it). The statuses " \
|
|
213
|
-
"(@status) are asterism_status's."
|
|
269
|
+
"(@status) are asterism_status's, what each topic is asterism_topics's. images: true hands the " \
|
|
270
|
+
"pictures over as images (the entry's image: its place among them)."
|
|
271
|
+
|
|
272
|
+
# asterism_values's answer: its JSON, then the pictures asked for as
|
|
273
|
+
# MCP images.
|
|
274
|
+
def values_answer(pattern, wait:, images:)
|
|
275
|
+
out, pics = @values.answer_images(pattern, wait: wait, images: images ? Values::Store::MAX_IMAGES : 0)
|
|
276
|
+
content = [{ type: "text", text: JSON.generate(json_safe(out)) }]
|
|
277
|
+
pics.each { |fmt, bytes| content << { type: "image", data: [bytes].pack("m0"), mimeType: fmt } }
|
|
278
|
+
::MCP::Tool::Response.new(content)
|
|
279
|
+
end
|
|
214
280
|
|
|
215
281
|
def objects(pattern)
|
|
216
282
|
paths = @survey.objects(pattern)
|
|
@@ -289,7 +355,7 @@ module Asterism
|
|
|
289
355
|
unless @lock.synchronize { @call_tools.any? { |_, t| t[0] == path && t[1] == method } }
|
|
290
356
|
return error("#{path}.#{method} is not callable here (not allowed, or gone)")
|
|
291
357
|
end
|
|
292
|
-
v =
|
|
358
|
+
v = @net[path, timeout: @survey.timeout].asterism_call(method, *pos, **kw)
|
|
293
359
|
text({ "value" => v })
|
|
294
360
|
rescue ::Asterism::RemoteError => e
|
|
295
361
|
error({ "error" => { "kind" => e.remote_kind, "class" => e.remote_class, "message" => e.remote_message } })
|
data/lib/asterism/cli/nouns.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# The noun-verb commands of the command line (node, app, object, status:
|
|
1
|
+
# The noun-verb commands of the command line (node, app, object, topic, status:
|
|
2
2
|
# lib/asterism/cli.rb's NOUNS), `completion` and the hidden `__complete`
|
|
3
3
|
# (lib/asterism/cli/complete.rb).
|
|
4
4
|
module Asterism
|
|
@@ -57,7 +57,12 @@ module Asterism
|
|
|
57
57
|
if a.nil?
|
|
58
58
|
@err.puts "asterism: no application #{node}/#{app} is visible"
|
|
59
59
|
code = 1
|
|
60
|
-
|
|
60
|
+
next
|
|
61
|
+
end
|
|
62
|
+
# Its own types too (one more get, ?types), when it defines some.
|
|
63
|
+
types = a["topics"] ? sv.topics(["#{node}/#{app}"]).dig("#{node}/#{app}", "types") : nil
|
|
64
|
+
a = a.except("objects").merge("types" => types, "objects" => a["objects"]) if types && !types.empty?
|
|
65
|
+
if structured?
|
|
61
66
|
emit({ "node" => node }.merge(a))
|
|
62
67
|
else
|
|
63
68
|
app_block("#{node}/#{app}", a, 0)
|
|
@@ -126,18 +131,18 @@ module Asterism
|
|
|
126
131
|
code = 0
|
|
127
132
|
connect do |net, _sv|
|
|
128
133
|
q = Queue.new
|
|
129
|
-
hold net.
|
|
134
|
+
hold net.zenoh.subscribe(Values.key_expr(pattern), depth: 16) { |s| q << [s, Time.now] }
|
|
130
135
|
got = pop_until(q, wait)
|
|
131
136
|
if got.nil?
|
|
132
137
|
@err.puts nothing_came(pattern, wait)
|
|
133
138
|
code = 1
|
|
134
139
|
next
|
|
135
140
|
end
|
|
136
|
-
e =
|
|
141
|
+
e = sample_entry(*got)
|
|
137
142
|
if structured?
|
|
138
143
|
emit(e)
|
|
139
144
|
else
|
|
140
|
-
put_lines(got[
|
|
145
|
+
put_lines(got[1], e, nil).each { @out.puts _1 }
|
|
141
146
|
end
|
|
142
147
|
end
|
|
143
148
|
code
|
|
@@ -151,21 +156,24 @@ module Asterism
|
|
|
151
156
|
code = 0
|
|
152
157
|
connect do |net, _sv|
|
|
153
158
|
q = Queue.new
|
|
154
|
-
hold net.
|
|
159
|
+
hold net.zenoh.subscribe(Values.key_expr(pattern), depth: 256) { |s| q << [s, Time.now] }
|
|
155
160
|
seen = 0
|
|
156
161
|
last = {}
|
|
162
|
+
seq = Values::Seq.new
|
|
157
163
|
until_stopped(net) do |left|
|
|
158
164
|
got = q.pop(timeout: [left || 0.2, 0.2].min)
|
|
159
165
|
next unless got
|
|
160
166
|
seen += 1
|
|
161
|
-
e =
|
|
167
|
+
e = sample_entry(*got, seq: seq)
|
|
162
168
|
if structured?
|
|
163
169
|
emit_line(e)
|
|
164
170
|
else
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
171
|
+
# A shared key's senders each their own (Values.lane).
|
|
172
|
+
lane = Values.lane(e["key"], e["from"])
|
|
173
|
+
whole = @opts[:raw] || !last.key?(lane)
|
|
174
|
+
old = last[lane]
|
|
175
|
+
last[lane] = e["value"]
|
|
176
|
+
lines = whole ? put_lines(got[1], e, nil) : put_lines(got[1], e, old, again: true)
|
|
169
177
|
lines&.each { @out.puts _1 }
|
|
170
178
|
end
|
|
171
179
|
@out.flush
|
|
@@ -179,6 +187,113 @@ module Asterism
|
|
|
179
187
|
code
|
|
180
188
|
end
|
|
181
189
|
|
|
190
|
+
# ------------------------------------------------------------ topic
|
|
191
|
+
|
|
192
|
+
# The topics of the applications that match NODE/APP (* for a part;
|
|
193
|
+
# */* by default): node/app/name a line, with the type.
|
|
194
|
+
def cmd_topic_list(rest)
|
|
195
|
+
pattern = rest.shift || "*/*"
|
|
196
|
+
parts = pattern.split("/", -1)
|
|
197
|
+
unless parts.size == 2 && parts.none? { _1.empty? || (_1.include?("*") && _1 != "*") }
|
|
198
|
+
raise ArgumentError, "topic list: NODE/APP, * for a part (asterism topic list 'games/*')"
|
|
199
|
+
end
|
|
200
|
+
connect do |_net, sv|
|
|
201
|
+
list = sv.topics(sv.apps(pattern)).sort.flat_map do |path, h|
|
|
202
|
+
h["topics"].map do |t|
|
|
203
|
+
n, a = path.split("/")
|
|
204
|
+
e = { "path" => ::Asterism::Pubs.path(n, a, t["name"]) }
|
|
205
|
+
e["by"] = path if t["shared"] # a shared place: the application that declares it
|
|
206
|
+
e.merge(t.except("name"))
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
if structured?
|
|
210
|
+
emit(list)
|
|
211
|
+
else
|
|
212
|
+
list.each do |t|
|
|
213
|
+
by = t["by"] ? " #{paint("(#{t['shared']}, by #{t['by']})", :note)}" : ""
|
|
214
|
+
@out.puts "#{paint_path(t['path'])} #{paint(topic_about(t), :note)}#{by}"
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
0
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# One topic (node/app/name, the name may have more parts): its
|
|
222
|
+
# declaration, and the definition of its type when it is one of the
|
|
223
|
+
# application's own. A shared key (node/@shared/name, @shared/name):
|
|
224
|
+
# each application that declares it, with its own declaration and the
|
|
225
|
+
# definition of its type from its own types (senders may define one
|
|
226
|
+
# type name differently).
|
|
227
|
+
def cmd_topic_info(rest)
|
|
228
|
+
path = rest.shift.to_s
|
|
229
|
+
pl = ::Asterism::Pubs.place(path)
|
|
230
|
+
return shared_topic_info(path, pl) if pl && pl[0]
|
|
231
|
+
parts = path.split("/", -1)
|
|
232
|
+
if parts.size < 3 || parts.any? { _1.empty? || _1.include?("*") }
|
|
233
|
+
raise ArgumentError, "topic info: PATH is node/app/name (asterism topic info games/games/othello1/state)"
|
|
234
|
+
end
|
|
235
|
+
app = parts[0, 2].join("/")
|
|
236
|
+
name = parts[2..].join("/")
|
|
237
|
+
code = 0
|
|
238
|
+
connect do |_net, sv|
|
|
239
|
+
h = sv.topics([app])[app]
|
|
240
|
+
t = h && h["topics"].find { _1["name"] == name }
|
|
241
|
+
if t.nil?
|
|
242
|
+
@err.puts "asterism: no topic #{path} is declared#{h ? '' : " (#{app} does not answer)"}"
|
|
243
|
+
code = 1
|
|
244
|
+
next
|
|
245
|
+
end
|
|
246
|
+
out = { "path" => path }.merge(t.except("name"))
|
|
247
|
+
d = t["type"].is_a?(String) && h["types"][t["type"]]
|
|
248
|
+
out["definition"] = d if d
|
|
249
|
+
if structured?
|
|
250
|
+
emit(out)
|
|
251
|
+
else
|
|
252
|
+
@out.puts paint_path(path)
|
|
253
|
+
@out.puts " #{paint('type:', :note)} #{t['type'] || '(no type)'}"
|
|
254
|
+
@out.puts " #{paint('encoding:', :note)} #{t['encoding'] || 'msgpack'}"
|
|
255
|
+
@out.puts " #{paint('unit:', :note)} #{t['unit']}" if t["unit"]
|
|
256
|
+
@out.puts " #{paint('time:', :note)} the sender's, with each value" if t["ts"]
|
|
257
|
+
Text.wrap_text(t["desc"], style.width, first: " ").each { @out.puts _1 } if t["desc"]
|
|
258
|
+
types_block({ t["type"] => d }, 2, label: "definition:") if d
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
code
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# topic info for a shared key: the publishers, each with its own
|
|
265
|
+
# declaration and type definition.
|
|
266
|
+
def shared_topic_info(path, pl)
|
|
267
|
+
code = 0
|
|
268
|
+
connect do |_net, sv|
|
|
269
|
+
apps = sv.apps(pl[0] == :node ? "#{pl[1]}/*" : "*/*")
|
|
270
|
+
pubs = sv.topics(apps).sort.filter_map do |app, h|
|
|
271
|
+
t = h["topics"].find { _1["name"] == path }
|
|
272
|
+
next unless t
|
|
273
|
+
e = { "by" => app }.merge(t.except("name", "shared"))
|
|
274
|
+
d = t["type"].is_a?(String) && h["types"][t["type"]]
|
|
275
|
+
e["definition"] = d if d
|
|
276
|
+
e
|
|
277
|
+
end
|
|
278
|
+
if pubs.empty?
|
|
279
|
+
@err.puts "asterism: no application declares #{path}"
|
|
280
|
+
code = 1
|
|
281
|
+
next
|
|
282
|
+
end
|
|
283
|
+
if structured?
|
|
284
|
+
emit({ "path" => path, "shared" => pl[0].to_s, "publishers" => pubs })
|
|
285
|
+
else
|
|
286
|
+
@out.puts "#{paint_path(path)} #{paint("(#{pl[0]})", :note)}"
|
|
287
|
+
pubs.each do |t|
|
|
288
|
+
@out.puts " #{paint('by', :note)} #{paint_path(t['by'])} #{paint(topic_about(t), :note)}"
|
|
289
|
+
Text.wrap_text(t["desc"], style.width, first: " ").each { @out.puts _1 } if t["desc"]
|
|
290
|
+
types_block({ t["type"] => t["definition"] }, 4, label: "definition:") if t["definition"]
|
|
291
|
+
end
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
code
|
|
295
|
+
end
|
|
296
|
+
|
|
182
297
|
# ------------------------------------------------------------ status
|
|
183
298
|
|
|
184
299
|
def cmd_status_get(rest)
|
|
@@ -381,18 +496,29 @@ module Asterism
|
|
|
381
496
|
end
|
|
382
497
|
|
|
383
498
|
# PATH (node/app/object, * for a part) and KEY (the rest, ** by
|
|
384
|
-
# default) as one pattern.
|
|
499
|
+
# default) as one pattern. A shared place's keys (docs/protocol.md
|
|
500
|
+
# 17): PATH node/@shared/name... (a node's, * for the node) or
|
|
501
|
+
# @shared/name... (the network's), then KEY the same way.
|
|
385
502
|
def put_pattern!(rest, what)
|
|
386
503
|
path = rest.shift
|
|
387
504
|
key = rest.shift || "**"
|
|
388
505
|
parts = path.to_s.split("/", -1)
|
|
389
|
-
|
|
506
|
+
shared = parts[0] == Values::SHARED || parts[1] == Values::SHARED
|
|
507
|
+
ok = shared ? parts.size >= 1 : parts.size == 3
|
|
508
|
+
unless ok && parts.none? { _1.empty? || (_1.include?("*") && _1 != "*") }
|
|
390
509
|
raise ArgumentError, "#{what}: PATH is node/app/object (* for a part), then KEY (asterism #{what} " \
|
|
391
|
-
"games/games/othello1 state)"
|
|
510
|
+
"games/games/othello1 state); a shared place's: node/@shared/name or @shared/name " \
|
|
511
|
+
"(asterism #{what} games/@shared/spectate '*/state')"
|
|
392
512
|
end
|
|
393
513
|
Values.check_pattern!("#{path}/#{key}")
|
|
394
514
|
end
|
|
395
515
|
|
|
516
|
+
# Values.entry of a Zenoh sample received at t (its encoding and
|
|
517
|
+
# attachment too).
|
|
518
|
+
def sample_entry(sample, t, seq: nil)
|
|
519
|
+
Values.entry(sample.key, sample.payload, t, encoding: sample.encoding, attachment: sample.attachment, seq: seq)
|
|
520
|
+
end
|
|
521
|
+
|
|
396
522
|
def nothing_came(pattern, seconds)
|
|
397
523
|
"asterism: nothing came on #{pattern}#{seconds ? " in #{format('%g', seconds)} s" : ''} " \
|
|
398
524
|
"(the router does not let this node read it, or it has not changed)"
|
|
@@ -400,19 +526,45 @@ module Asterism
|
|
|
400
526
|
|
|
401
527
|
# The lines of one put received at t: the time, the key and the value
|
|
402
528
|
# whole; again (the key's value before was old): a line for each thing
|
|
403
|
-
# that changed in a Hash, the value whole otherwise (nil: no change).
|
|
529
|
+
# that changed in a Hash, the value whole otherwise (nil: no change). A
|
|
530
|
+
# value that is bytes in an encoding (a picture) shows as its encoding
|
|
531
|
+
# and size. A line comes first when values were lost before this one,
|
|
532
|
+
# or the sender started its numbers again (Values.entry). A shared
|
|
533
|
+
# value's key is followed by its sender (from games/room01).
|
|
404
534
|
def put_lines(t, e, old, again: false)
|
|
405
535
|
head = "#{paint(t.strftime('%H:%M:%S.%L'), :time)} "
|
|
406
536
|
v = e["value"]
|
|
537
|
+
gap = put_gap(head, e)
|
|
407
538
|
if again && old.is_a?(Hash) && v.is_a?(Hash)
|
|
408
539
|
changes = Text.status_diff(old, v)
|
|
409
|
-
return
|
|
410
|
-
return changes.map { |c| "#{head}#{
|
|
540
|
+
return gap if changes.empty?
|
|
541
|
+
return gap.to_a + changes.map { |c| "#{head}#{put_key(e)} #{change_text(c)}" }
|
|
411
542
|
end
|
|
412
|
-
return
|
|
413
|
-
first = "#{head}#{
|
|
414
|
-
return ["#{first} #{
|
|
415
|
-
[
|
|
543
|
+
return gap if again && old == v && !e["format"].include?("/")
|
|
544
|
+
first = "#{head}#{put_key(e)}"
|
|
545
|
+
return [*gap, "#{first} #{paint("(#{e['format']}, #{e['bytes']} bytes)", :note)}"] if bytes_value?(e)
|
|
546
|
+
return [*gap, "#{first} #{Text.inline(v)}"] unless v.is_a?(Hash)
|
|
547
|
+
[*gap, first, *Text.status_lines(v, style.width, Text.width(head))]
|
|
548
|
+
end
|
|
549
|
+
|
|
550
|
+
# Whether a value is bytes in an encoding (shown by its size).
|
|
551
|
+
def bytes_value?(e)
|
|
552
|
+
e["format"].include?("/") && e["value"] == "(#{e['bytes']} bytes)"
|
|
553
|
+
end
|
|
554
|
+
|
|
555
|
+
# [the line] saying what was lost before a value, or nil.
|
|
556
|
+
def put_gap(head, e)
|
|
557
|
+
if e["restarted"]
|
|
558
|
+
["#{head}#{put_key(e)} #{paint("(started again at #{e['seq']})", :note)}"]
|
|
559
|
+
elsif e["lost"]
|
|
560
|
+
["#{head}#{put_key(e)} #{paint("(#{e['lost']} lost)", :note)}"]
|
|
561
|
+
end
|
|
562
|
+
end
|
|
563
|
+
|
|
564
|
+
# A value's key as a line shows it, with a shared value's sender.
|
|
565
|
+
def put_key(e)
|
|
566
|
+
k = paint(e["key"], :object)
|
|
567
|
+
e["from"] ? "#{k} #{paint('from', :note)} #{paint(e['from'], :app)}" : k
|
|
416
568
|
end
|
|
417
569
|
|
|
418
570
|
def no_status(path)
|
|
@@ -432,6 +584,7 @@ module Asterism
|
|
|
432
584
|
pad = " " * indent
|
|
433
585
|
@out.puts "#{pad}#{paint(name, :app)}"
|
|
434
586
|
status_block(a, indent + 2)
|
|
587
|
+
types_block(a["types"], indent + 2)
|
|
435
588
|
return if a["objects"].empty?
|
|
436
589
|
@out.puts "#{pad} #{paint('objects:', :note)}"
|
|
437
590
|
a["objects"].each do |o|
|
|
@@ -443,7 +596,7 @@ module Asterism
|
|
|
443
596
|
|
|
444
597
|
# The status's puts of one application, cleaned (Status.parse).
|
|
445
598
|
def subscribe_status(net, path)
|
|
446
|
-
hold(net.
|
|
599
|
+
hold(net.zenoh.subscribe("asterism/#{path}/@status") do |sample|
|
|
447
600
|
got = ::Asterism::Status.parse(sample.key, sample.payload)
|
|
448
601
|
yield got[2] if got
|
|
449
602
|
end)
|