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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +98 -0
- data/README.md +449 -181
- data/data/transfer/transfer.rb +7 -5
- data/lib/asterism/cli/commands.rb +99 -0
- data/lib/asterism/cli/complete.rb +2 -1
- data/lib/asterism/cli/mcp.rb +90 -24
- data/lib/asterism/cli/nouns.rb +195 -25
- data/lib/asterism/cli/survey.rb +84 -19
- data/lib/asterism/cli/values.rb +249 -35
- data/lib/asterism/cli.rb +223 -82
- 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 +3 -1
data/mrblib/status.rb
CHANGED
|
@@ -2,21 +2,24 @@
|
|
|
2
2
|
# runs (how many tables are open, who is seated, a battery level), told by
|
|
3
3
|
# the application itself.
|
|
4
4
|
#
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
5
|
+
# net.status(tables: 3, humans: 2) # sets those fields, keeps the others
|
|
6
|
+
# net.status({ "mahjong1" => {...} }) # the whole status (a Hash)
|
|
7
|
+
# net.status # => {"tables" => 3, ...} or nil
|
|
8
|
+
# net.status_of("games", "games") # another application's, or nil
|
|
9
9
|
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
# status
|
|
10
|
+
# The status is one part of the application's own profile,
|
|
11
|
+
# asterism/<node>/<app>/@profile (docs/protocol.md 8): the application
|
|
12
|
+
# answers queries there with {"status" => ..., "pubs" => ...} (the topics,
|
|
13
|
+
# topic.rb), or with the one part a query names (?status, ?pubs, ?types).
|
|
14
|
+
# It puts the status on asterism/<node>/<app>/@status when it changes (only
|
|
15
|
+
# then: the same status again puts nothing) and after a lost
|
|
16
|
+
# connection comes back. An application with neither a status, a topic nor
|
|
17
|
+
# a type of its own declares nothing there and answers nothing.
|
|
15
18
|
#
|
|
16
19
|
# The value is a MessagePack map with str keys; the values are any values
|
|
17
20
|
# of the protocol nested at most MAX_DEPTH levels below the map, and the
|
|
18
|
-
# whole map packs into MAX_BYTES at most. It is not the profile
|
|
19
|
-
# (profile.rb):
|
|
21
|
+
# whole map packs into MAX_BYTES at most. It is not the node's profile
|
|
22
|
+
# (profile.rb): that says once what a node is; a status changes.
|
|
20
23
|
module Asterism
|
|
21
24
|
module Status
|
|
22
25
|
KEY = "@status"
|
|
@@ -46,7 +49,7 @@ module Asterism
|
|
|
46
49
|
st ? [parts[1], parts[2], st] : nil
|
|
47
50
|
end
|
|
48
51
|
|
|
49
|
-
# The checks of
|
|
52
|
+
# The checks of Connection#status: a copy with String keys, or
|
|
50
53
|
# ArgumentError with the reason.
|
|
51
54
|
# @api private
|
|
52
55
|
def self.check!(h)
|
|
@@ -83,114 +86,156 @@ module Asterism
|
|
|
83
86
|
end
|
|
84
87
|
end
|
|
85
88
|
|
|
86
|
-
#
|
|
87
|
-
#
|
|
88
|
-
#
|
|
89
|
-
#
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if h.nil? && fields.empty?
|
|
97
|
-
return @status ? @status.dup : nil
|
|
98
|
-
end
|
|
99
|
-
base = h.nil? ? (@status ? @status.dup : {}) : Status.check!(h)
|
|
100
|
-
fields.each do |k, v|
|
|
101
|
-
if v.nil?
|
|
102
|
-
base.delete(k.to_s)
|
|
103
|
-
else
|
|
104
|
-
base[k.to_s] = v
|
|
89
|
+
# The parts a query of an application's profile asks for: its
|
|
90
|
+
# parameters split on ";", each part's name before "="; the known names
|
|
91
|
+
# are status, pubs and types. None of them: status and pubs.
|
|
92
|
+
# @api private
|
|
93
|
+
def self.app_profile_parts(params)
|
|
94
|
+
want = []
|
|
95
|
+
unless params.nil? || params.empty?
|
|
96
|
+
params.split(";").each do |pair|
|
|
97
|
+
n = pair.split("=")[0].to_s
|
|
98
|
+
want << n if (n == "status" || n == "pubs" || n == "types") && !want.include?(n)
|
|
105
99
|
end
|
|
106
100
|
end
|
|
107
|
-
|
|
108
|
-
tell_status(false)
|
|
109
|
-
@status.dup
|
|
101
|
+
want.empty? ? ["status", "pubs"] : want
|
|
110
102
|
end
|
|
111
103
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
104
|
+
class Connection
|
|
105
|
+
# This application's status. With a Hash, it is the whole status; with
|
|
106
|
+
# keywords, those fields change (nil removes one) and the others stay;
|
|
107
|
+
# both: the keywords on top of the Hash. Returns the status. Without
|
|
108
|
+
# arguments, returns it (nil when none was set). Raises ArgumentError for
|
|
109
|
+
# a status it does not accept (see Asterism::Status).
|
|
110
|
+
#
|
|
111
|
+
# The status is the connection's (each connection is one application).
|
|
112
|
+
# A change is put on asterism/<node>/<app>/@status; setting the same
|
|
113
|
+
# status again puts nothing. After close it is still there to read and
|
|
114
|
+
# set, but goes nowhere.
|
|
115
|
+
def status(h = nil, **fields)
|
|
116
|
+
if h.nil? && fields.empty?
|
|
117
|
+
return @status ? @status.dup : nil
|
|
118
|
+
end
|
|
119
|
+
base = h.nil? ? (@status ? @status.dup : {}) : ::Asterism::Status.check!(h)
|
|
120
|
+
fields.each do |k, v|
|
|
121
|
+
if v.nil?
|
|
122
|
+
base.delete(k.to_s)
|
|
123
|
+
else
|
|
124
|
+
base[k.to_s] = v
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
@status = ::Asterism::Status.check!(base)
|
|
128
|
+
tell_status(false)
|
|
129
|
+
@status.dup
|
|
126
130
|
end
|
|
127
|
-
Status.check(raw)
|
|
128
|
-
end
|
|
129
131
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
132
|
+
# Another application's status (Asterism::Status.check has cleaned it),
|
|
133
|
+
# or nil when it does not answer in time or has no status (an
|
|
134
|
+
# application that is gone, one without a status). timeout: in seconds
|
|
135
|
+
# (or timeout_ms:), 2.0 by default. Waits like a call (polling, answering
|
|
136
|
+
# calls meanwhile); raises Disconnected when not connected. This
|
|
137
|
+
# application's own status comes without the network.
|
|
138
|
+
def status_of(node, app, timeout: nil, timeout_ms: nil)
|
|
139
|
+
raw = app_profile_part(node, app, "status", timeout, timeout_ms)
|
|
140
|
+
raw.nil? ? nil : ::Asterism::Status.check(raw["status"])
|
|
139
141
|
end
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
142
|
+
|
|
143
|
+
# A query of asterism/<node>/<app>/@profile (part: "status", "pubs",
|
|
144
|
+
# "types", or nil for the default reply) -> a Future.
|
|
145
|
+
# @api private
|
|
146
|
+
def app_profile_async(node, app, part, timeout, timeout_ms)
|
|
147
|
+
node = ::Asterism.check_name("node", node)
|
|
148
|
+
app = ::Asterism.check_name("app", app)
|
|
149
|
+
ms = ::Asterism.time_ms("Connection##{part || 'profile'}_of", timeout, timeout_ms, nil,
|
|
150
|
+
::Asterism::DEFAULT_TIMEOUT_MS)
|
|
151
|
+
path = "#{node}/#{app}/#{::Asterism::Profile::KEY}"
|
|
152
|
+
what = part || "profile"
|
|
153
|
+
if @session && node == @node_id && app == @app
|
|
154
|
+
return ::Asterism::Future.answered(path, what, ::Asterism::Codec.pack(app_profile_hash(part)))
|
|
155
|
+
end
|
|
156
|
+
begin
|
|
157
|
+
g = session!.get("#{::Asterism::ROOT}/#{path}", timeout_ms: ms, params: part)
|
|
158
|
+
rescue ::Asterism::Zenoh::Error => e
|
|
159
|
+
lost_if_closed(e.message)
|
|
160
|
+
raise ::Asterism::Disconnected, e.message
|
|
161
|
+
end
|
|
162
|
+
::Asterism::Future.new(self, path, what, g, ::Asterism.now_ms + ms, ms)
|
|
145
163
|
end
|
|
146
|
-
Future.new(path, "status", g, now_ms + ms, ms)
|
|
147
|
-
end
|
|
148
164
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
165
|
+
# The applications alive now ("<node>/<app>", from their liveliness
|
|
166
|
+
# tokens; this one included, and the program's other connections as
|
|
167
|
+
# the applications they are) whose path matches the pattern (* is one
|
|
168
|
+
# chunk, ** any number).
|
|
169
|
+
def apps(pattern = "*/*")
|
|
170
|
+
list = []
|
|
171
|
+
return list unless @session
|
|
172
|
+
own = "#{@node_id}/#{@app}"
|
|
173
|
+
list << own if ::Asterism.glob?(pattern, own)
|
|
174
|
+
@apps.each_key { |path| list << path if path != own && ::Asterism.glob?(pattern, path) }
|
|
175
|
+
list
|
|
176
|
+
end
|
|
160
177
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
178
|
+
# Puts the status on asterism/<node>/<app>/@status while connected: when
|
|
179
|
+
# it changed since the last put of this connection, or always with force
|
|
180
|
+
# (a connection that came back). Declares the application's profile
|
|
181
|
+
# queryable with the first put.
|
|
182
|
+
# @api private
|
|
183
|
+
def tell_status(force)
|
|
184
|
+
s = @session
|
|
185
|
+
return unless s && @status
|
|
186
|
+
bytes = ::Asterism::Codec.pack(@status)
|
|
187
|
+
return if !force && !@status_put.nil? && @status_put == bytes
|
|
188
|
+
app_queryable(s)
|
|
189
|
+
s.put("#{::Asterism::ROOT}/#{@node_id}/#{@app}/#{::Asterism::Status::KEY}", bytes)
|
|
190
|
+
@status_put = bytes
|
|
191
|
+
rescue ::Asterism::Zenoh::Error
|
|
192
|
+
nil # the next poll notices a lost connection
|
|
193
|
+
end
|
|
177
194
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
195
|
+
# Declares the queryable asterism/<node>/<app>/@profile once this
|
|
196
|
+
# application has something to tell there (a status, a topic, a type):
|
|
197
|
+
# an application with none of them declares nothing (and costs nothing).
|
|
198
|
+
# @api private
|
|
199
|
+
def app_queryable(s)
|
|
200
|
+
@app_queryable ||= s.queryable("#{::Asterism::ROOT}/#{@node_id}/#{@app}/#{::Asterism::Profile::KEY}", depth: 4)
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# The reply of asterism/<node>/<app>/@profile: the parts asked for
|
|
204
|
+
# (part: one name, or the parameters of a query), each left out when
|
|
205
|
+
# this application has none.
|
|
206
|
+
# @api private
|
|
207
|
+
def app_profile_hash(params)
|
|
208
|
+
h = {}
|
|
209
|
+
::Asterism.app_profile_parts(params).each do |n|
|
|
210
|
+
if n == "status"
|
|
211
|
+
h["status"] = @status if @status
|
|
212
|
+
elsif n == "pubs"
|
|
213
|
+
h["pubs"] = pubs_hash if @topics && !@topics.empty?
|
|
214
|
+
elsif @types && !@types.empty?
|
|
215
|
+
h["types"] = @types
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
h
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# @api private
|
|
222
|
+
def answer_app_profile
|
|
223
|
+
qa = @app_queryable
|
|
224
|
+
return unless qa && qa.pending > 0
|
|
225
|
+
queries = qa.each_pending
|
|
226
|
+
i = 0
|
|
227
|
+
while i < queries.size
|
|
228
|
+
q = queries[i]
|
|
229
|
+
i += 1
|
|
190
230
|
begin
|
|
191
|
-
q.
|
|
192
|
-
|
|
193
|
-
|
|
231
|
+
q.reply("#{::Asterism::ROOT}/#{@node_id}/#{@app}/#{::Asterism::Profile::KEY}",
|
|
232
|
+
::Asterism::Codec.pack(app_profile_hash(q.params)))
|
|
233
|
+
ensure
|
|
234
|
+
begin
|
|
235
|
+
q.finish
|
|
236
|
+
rescue ::Asterism::Zenoh::Error
|
|
237
|
+
# the session went away meanwhile
|
|
238
|
+
end
|
|
194
239
|
end
|
|
195
240
|
end
|
|
196
241
|
end
|