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/topic.rb
ADDED
|
@@ -0,0 +1,1254 @@
|
|
|
1
|
+
# Data an application streams on its own keys (docs/protocol.md 9-11):
|
|
2
|
+
# topics it declares, the values it puts on them, and the subscriptions of
|
|
3
|
+
# the readers.
|
|
4
|
+
#
|
|
5
|
+
# net = Asterism.connect(router, node: "linux", app: "demo")
|
|
6
|
+
# imu = net.publish("imu", type: :vector3, unit: "m/s2", ts: true)
|
|
7
|
+
# imu.put([0.1, 0.0, 9.8]) # MessagePack, no encoding
|
|
8
|
+
# cam = net.publish("camera", type: :image, encoding: "image/jpeg")
|
|
9
|
+
# cam.put(jpeg_bytes) # the bytes as they are
|
|
10
|
+
# net.define_type("mygame/board.strokes", [["pts", "int[]"], ["color", "str"]])
|
|
11
|
+
#
|
|
12
|
+
# net.subscribe("*/demo/imu") { |v, info| p [v, info.seq, info.lost] }
|
|
13
|
+
# net.pubs_of("linux", "demo") # => {"imu" => {"t" => 2, ...}}
|
|
14
|
+
#
|
|
15
|
+
# Keys: a value goes on asterism/<node>/<app>/<name> (a declared Zenoh
|
|
16
|
+
# publisher: the key travels as a number), with no encoding when it is
|
|
17
|
+
# MessagePack and the encoding given at publish otherwise; the attachment
|
|
18
|
+
# is the MessagePack array [seq, run id] or [seq, run id, sender's time in
|
|
19
|
+
# ms since 1970] (ts: true). The run id is the connection's: 16 random bits
|
|
20
|
+
# picked when it opens, so that a reader tells a sender that started again
|
|
21
|
+
# from one whose numbers went on (docs/protocol.md 9.3, 9.4). The declarations are put on asterism/<node>/<app>/@pubs each
|
|
22
|
+
# time they change, and answered by the application's own profile,
|
|
23
|
+
# asterism/<node>/<app>/@profile (status.rb): ?status, ?pubs, ?types.
|
|
24
|
+
#
|
|
25
|
+
# Shared places (docs/protocol.md 17): data with no single owner goes on
|
|
26
|
+
# a node's asterism/<node>/@shared/<name> or the network's
|
|
27
|
+
# asterism/@shared/<name>. The sender declares it in its own @pubs
|
|
28
|
+
# ("<node>/@shared/<name>", "@shared/<name>"), and each value's attachment
|
|
29
|
+
# is [seq, run id, ms or nil, "<node>/<app>"], so that the readers count
|
|
30
|
+
# the numbers per sender and read the type from the sender's declarations.
|
|
31
|
+
#
|
|
32
|
+
# t = net.publish("spectate/t1/state", shared: :node, type: "mygame/table")
|
|
33
|
+
# net.subscribe("*/@shared/spectate/**") { |v, info| p [info.from, info.lost] }
|
|
34
|
+
# net.publishers_of("linux/@shared/spectate/t1/state") # => {"linux/room01" => {...}}
|
|
35
|
+
# net.type_of(info) # the sender's type of that key
|
|
36
|
+
#
|
|
37
|
+
# Subscription blocks run from Connection#poll only, never from the polling
|
|
38
|
+
# inside a waiting call (the values wait in the subscription's queue).
|
|
39
|
+
module Asterism
|
|
40
|
+
# The types of a topic's values: the built-in ones by number (their
|
|
41
|
+
# shapes are in docs/protocol.md 10), an application's own by name.
|
|
42
|
+
module Types
|
|
43
|
+
NUMBER = 1 # a Float or an Integer
|
|
44
|
+
VECTOR3 = 2 # [x, y, z]
|
|
45
|
+
POSE = 3 # [x, y, z, qx, qy, qz, qw] (ROS 2's Pose order)
|
|
46
|
+
IMAGE = 4 # the bytes, in the topic's encoding (image/jpeg, image/png)
|
|
47
|
+
STATE = 5 # a String or an Integer (a scene, on / off)
|
|
48
|
+
LOG = 6 # a log record: level, msg, time (ms since 1970, an int)
|
|
49
|
+
TEXT = 7 # a String
|
|
50
|
+
# The built-in types by name; the number is the index + 1.
|
|
51
|
+
NAMES = ["number", "vector3", "pose", "image", "state", "log", "text"]
|
|
52
|
+
# The basic types of a field of an application's own type.
|
|
53
|
+
BASIC = ["int", "float", "str", "bin", "bool", "any"]
|
|
54
|
+
NAME_MAX = 128 # bytes of a type's name and a field's name
|
|
55
|
+
FIELDS_MAX = 64 # fields of one type
|
|
56
|
+
|
|
57
|
+
# The number of a built-in type from its name (a Symbol or String),
|
|
58
|
+
# or nil.
|
|
59
|
+
def self.number(name)
|
|
60
|
+
s = name.to_s
|
|
61
|
+
i = 0
|
|
62
|
+
while i < NAMES.size
|
|
63
|
+
return i + 1 if NAMES[i] == s
|
|
64
|
+
i += 1
|
|
65
|
+
end
|
|
66
|
+
nil
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# The "t" of a declaration from publish's type: (nil: no type).
|
|
70
|
+
# @api private
|
|
71
|
+
def self.topic_type!(t)
|
|
72
|
+
return nil if t.nil?
|
|
73
|
+
if t.is_a?(Integer)
|
|
74
|
+
return t if t >= 1 && t <= NAMES.size
|
|
75
|
+
raise ArgumentError, "type: #{t} is not a built-in type (1 to #{NAMES.size})"
|
|
76
|
+
end
|
|
77
|
+
if t.is_a?(Symbol)
|
|
78
|
+
n = number(t)
|
|
79
|
+
return n if n
|
|
80
|
+
raise ArgumentError, "type: :#{t} is not a built-in type (#{NAMES.join(', ')}); " \
|
|
81
|
+
"an application's own type is a String"
|
|
82
|
+
end
|
|
83
|
+
return name!("type:", t) if t.is_a?(String)
|
|
84
|
+
raise ArgumentError, "type: expects a Symbol, an Integer or a String, got #{t.class}"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# @api private
|
|
88
|
+
def self.name!(what, s)
|
|
89
|
+
s = s.to_s if s.is_a?(Symbol)
|
|
90
|
+
raise ArgumentError, "#{what} expects a String, got #{s.class}" unless s.is_a?(String)
|
|
91
|
+
raise ArgumentError, "#{what} empty" if s.empty?
|
|
92
|
+
raise ArgumentError, "#{what} #{s.bytesize} bytes (#{NAME_MAX} at most)" if s.bytesize > NAME_MAX
|
|
93
|
+
s
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# A field's type as it is sent: a name, a String (a Symbol as its
|
|
97
|
+
# name): a built-in type's name ("vector3"), a basic type ("int"),
|
|
98
|
+
# another application type; "[]" at the end for an array of it
|
|
99
|
+
# ("vector3[]", "int[]"). A number is not a field's type (a topic's
|
|
100
|
+
# type: is; docs/protocol.md 11.2).
|
|
101
|
+
# @api private
|
|
102
|
+
def self.field_type!(t)
|
|
103
|
+
if t.is_a?(Integer)
|
|
104
|
+
n = t >= 1 && t <= NAMES.size ? " (#{NAMES[t - 1].inspect})" : ""
|
|
105
|
+
raise ArgumentError, "field type #{t}: a field's type is a name#{n}, not a number"
|
|
106
|
+
end
|
|
107
|
+
s = name!("field type:", t)
|
|
108
|
+
base = s
|
|
109
|
+
base = base[0, base.size - 2] while base.end_with?("[]")
|
|
110
|
+
raise ArgumentError, "field type #{s.inspect}: no type before []" if base.empty?
|
|
111
|
+
s
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# A definition from define_type: {"f" => [[name, type], ...], "d" => desc}.
|
|
115
|
+
# @api private
|
|
116
|
+
def self.definition!(fields, desc)
|
|
117
|
+
raise ArgumentError, "define_type: fields: expects an Array of [name, type]" unless fields.is_a?(Array)
|
|
118
|
+
raise ArgumentError, "define_type: more than #{FIELDS_MAX} fields" if fields.size > FIELDS_MAX
|
|
119
|
+
list = []
|
|
120
|
+
fields.each do |f|
|
|
121
|
+
unless f.is_a?(Array) && f.size == 2
|
|
122
|
+
raise ArgumentError, "define_type: each field is [name, type], got #{f.inspect}"
|
|
123
|
+
end
|
|
124
|
+
list << [name!("field name:", f[0]), field_type!(f[1])]
|
|
125
|
+
end
|
|
126
|
+
d = { "f" => list }
|
|
127
|
+
unless desc.nil?
|
|
128
|
+
raise ArgumentError, "define_type: desc: expects a String" unless desc.is_a?(String)
|
|
129
|
+
raise ArgumentError, "define_type: desc: at most #{::Asterism::DOC_MAX} bytes" if desc.bytesize > ::Asterism::DOC_MAX
|
|
130
|
+
d["d"] = desc
|
|
131
|
+
end
|
|
132
|
+
d
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# A received map of definitions, cleaned: the definitions with a str
|
|
136
|
+
# name and an "f" that is an array of [str, non-empty str] pairs, and
|
|
137
|
+
# "d" when it is a str. nil when it is not a map.
|
|
138
|
+
def self.check(h)
|
|
139
|
+
return nil unless h.is_a?(Hash)
|
|
140
|
+
out = {}
|
|
141
|
+
h.each do |name, d|
|
|
142
|
+
next unless name.is_a?(String) && !name.empty? && d.is_a?(Hash) && d["f"].is_a?(Array)
|
|
143
|
+
ok = true
|
|
144
|
+
d["f"].each do |f|
|
|
145
|
+
ok = false unless f.is_a?(Array) && f.size == 2 && f[0].is_a?(String) &&
|
|
146
|
+
f[1].is_a?(String) && !f[1].empty?
|
|
147
|
+
end
|
|
148
|
+
next unless ok
|
|
149
|
+
c = { "f" => d["f"] }
|
|
150
|
+
c["d"] = d["d"] if d["d"].is_a?(String)
|
|
151
|
+
out[name] = c
|
|
152
|
+
end
|
|
153
|
+
out
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# The declarations of the topics (asterism/<node>/<app>/@pubs).
|
|
158
|
+
module Pubs
|
|
159
|
+
KEY = "@pubs"
|
|
160
|
+
# The chunk of the shared places: asterism/@shared/... (the network's)
|
|
161
|
+
# and asterism/<node>/@shared/... (a node's).
|
|
162
|
+
SHARED = "@shared"
|
|
163
|
+
UNIT_MAX = 64 # bytes of a unit
|
|
164
|
+
# Zenoh's encodings with a fixed number (Zenoh 1.x), the number is the
|
|
165
|
+
# position: zenoh/bytes is 0, image/jpeg 17. One String, split when an
|
|
166
|
+
# encoding is first looked up.
|
|
167
|
+
ENCODINGS = "zenoh/bytes zenoh/string zenoh/serialized application/octet-stream text/plain " \
|
|
168
|
+
"application/json text/json application/cdr application/cbor application/yaml text/yaml " \
|
|
169
|
+
"text/json5 application/python-serialized-object application/protobuf " \
|
|
170
|
+
"application/java-serialized-object application/openmetrics-text image/png image/jpeg " \
|
|
171
|
+
"image/gif image/bmp image/webp application/xml application/x-www-form-urlencoded " \
|
|
172
|
+
"text/html text/xml text/css text/javascript text/markdown text/csv application/sql " \
|
|
173
|
+
"application/coap-payload application/json-patch+json application/json-seq " \
|
|
174
|
+
"application/jsonpath application/jwt application/mp4 application/soap+xml " \
|
|
175
|
+
"application/yang audio/aac audio/flac audio/mp4 audio/ogg audio/vorbis video/h261 " \
|
|
176
|
+
"video/h263 video/h264 video/h265 video/h266 video/mp4 video/ogg video/raw video/vp8 " \
|
|
177
|
+
"video/vp9"
|
|
178
|
+
|
|
179
|
+
# The fixed number of an encoding ("image/jpeg" or with a schema,
|
|
180
|
+
# "text/plain;charset=utf-8"), or nil when it has none.
|
|
181
|
+
def self.encoding_id(name)
|
|
182
|
+
s = name.to_s
|
|
183
|
+
cut = s.index(";")
|
|
184
|
+
s = s[0, cut] if cut
|
|
185
|
+
list = (@names ||= ENCODINGS.split(" "))
|
|
186
|
+
i = 0
|
|
187
|
+
while i < list.size
|
|
188
|
+
return i if list[i] == s
|
|
189
|
+
i += 1
|
|
190
|
+
end
|
|
191
|
+
nil
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# The name of an encoding number, or nil.
|
|
195
|
+
def self.encoding_name(id)
|
|
196
|
+
list = (@names ||= ENCODINGS.split(" "))
|
|
197
|
+
id.is_a?(Integer) && id >= 0 && id < list.size ? list[id] : nil
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Where a declaration's entry puts its values (docs/protocol.md 17):
|
|
201
|
+
# [nil, nil, name] for a topic of the application's own ("imu",
|
|
202
|
+
# "board1/strokes": asterism/<node>/<app>/<name>), [:node, node, name]
|
|
203
|
+
# for "<node>/@shared/<name>" (asterism/<node>/@shared/<name>),
|
|
204
|
+
# [:global, nil, name] for "@shared/<name>" (asterism/@shared/<name>).
|
|
205
|
+
# nil for anything else (an empty chunk, a chunk that is not a name,
|
|
206
|
+
# another verbatim chunk).
|
|
207
|
+
def self.place(entry)
|
|
208
|
+
return nil unless entry.is_a?(String) && !entry.empty?
|
|
209
|
+
return nil if entry.start_with?("/") || entry.end_with?("/")
|
|
210
|
+
parts = entry.split("/")
|
|
211
|
+
if parts[0] == SHARED
|
|
212
|
+
rest = parts[1, parts.size - 1]
|
|
213
|
+
return names?(rest) ? [:global, nil, rest.join("/")] : nil
|
|
214
|
+
end
|
|
215
|
+
if parts.size > 2 && parts[1] == SHARED
|
|
216
|
+
rest = parts[2, parts.size - 2]
|
|
217
|
+
return ::Asterism.valid_name?(parts[0]) && names?(rest) ? [:node, parts[0], rest.join("/")] : nil
|
|
218
|
+
end
|
|
219
|
+
names?(parts) ? [nil, nil, entry] : nil
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# The entry of a topic in the declarations: its name for the
|
|
223
|
+
# application's own, "<node>/@shared/<name>" or "@shared/<name>".
|
|
224
|
+
def self.entry(shared, node, name)
|
|
225
|
+
return name if shared.nil?
|
|
226
|
+
shared == :global ? "#{SHARED}/#{name}" : "#{node}/#{SHARED}/#{name}"
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# The key a declared entry of <node>/<app> puts its values on, below
|
|
230
|
+
# asterism/ ("linux/demo/imu", "linux/@shared/spectate/t1/state",
|
|
231
|
+
# "@shared/lobby"), or nil when the entry is not one.
|
|
232
|
+
def self.path(node, app, entry)
|
|
233
|
+
pl = place(entry)
|
|
234
|
+
return nil if pl.nil?
|
|
235
|
+
pl[0].nil? ? "#{node}/#{app}/#{entry}" : entry
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# @api private
|
|
239
|
+
def self.names?(list)
|
|
240
|
+
return false if list.empty?
|
|
241
|
+
list.each { |c| return false unless ::Asterism.valid_name?(c) }
|
|
242
|
+
true
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# A received map of declarations, cleaned: the entries whose name is a
|
|
246
|
+
# topic of the application's own or a shared one (place), whose
|
|
247
|
+
# declaration is a map, with the known keys that pass ("t" an int above
|
|
248
|
+
# 0 or a str, "e" an int, "u" and "d" str, "ts" true). With node (the
|
|
249
|
+
# node of the application that declared them), an entry of another
|
|
250
|
+
# node's shared place is left out too. nil when it is not a map.
|
|
251
|
+
def self.check(h, node = nil)
|
|
252
|
+
return nil unless h.is_a?(Hash)
|
|
253
|
+
out = {}
|
|
254
|
+
h.each do |name, d|
|
|
255
|
+
next unless d.is_a?(Hash)
|
|
256
|
+
pl = place(name)
|
|
257
|
+
next if pl.nil? || (node && pl[0] == :node && pl[1] != node)
|
|
258
|
+
c = {}
|
|
259
|
+
t = d["t"]
|
|
260
|
+
c["t"] = t if (t.is_a?(Integer) && t > 0) || (t.is_a?(String) && !t.empty?)
|
|
261
|
+
e = d["e"]
|
|
262
|
+
c["e"] = e if e.is_a?(Integer) && e > 0
|
|
263
|
+
c["u"] = d["u"] if d["u"].is_a?(String)
|
|
264
|
+
c["d"] = d["d"] if d["d"].is_a?(String)
|
|
265
|
+
c["ts"] = true if d["ts"] == true
|
|
266
|
+
out[name] = c
|
|
267
|
+
end
|
|
268
|
+
out
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# [node, app, declarations] of a put on asterism/<node>/<app>/@pubs, or
|
|
272
|
+
# nil when it is not one.
|
|
273
|
+
def self.parse(key, payload)
|
|
274
|
+
parts = key.to_s.split("/")
|
|
275
|
+
return nil unless parts.size == 4 && parts[0] == ::Asterism::ROOT && parts[3] == KEY
|
|
276
|
+
d = check(::Asterism::Codec.unpack(payload), parts[1])
|
|
277
|
+
d ? [parts[1], parts[2], d] : nil
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# A declared topic (Connection#publish). put sends a value; close withdraws
|
|
282
|
+
# the declaration.
|
|
283
|
+
class Topic
|
|
284
|
+
# The sequence number goes back to 0 after this (below 2^31, so that
|
|
285
|
+
# every board's Integer holds it).
|
|
286
|
+
SEQ_MAX = 2147483647
|
|
287
|
+
|
|
288
|
+
# name: as given to publish (below @shared/ for a shared one); key: the
|
|
289
|
+
# whole key; shared: nil (the application's own), :node or :global;
|
|
290
|
+
# entry: the name in the declarations ("imu", "<node>/@shared/<name>",
|
|
291
|
+
# "@shared/<name>").
|
|
292
|
+
attr_reader :name, :key, :encoding, :connection, :shared, :entry
|
|
293
|
+
|
|
294
|
+
# @api private
|
|
295
|
+
# seq: the number of the first put (a topic declared again on the same
|
|
296
|
+
# connection goes on from the one it replaces); run: the connection's
|
|
297
|
+
# run id.
|
|
298
|
+
def initialize(connection, name, key, publisher, decl, encoding, ts, shared = nil, entry = nil, from = nil,
|
|
299
|
+
seq = 0, run = 0)
|
|
300
|
+
@connection = connection
|
|
301
|
+
@name = name
|
|
302
|
+
@key = key
|
|
303
|
+
@publisher = publisher
|
|
304
|
+
@decl = decl
|
|
305
|
+
@encoding = encoding
|
|
306
|
+
@ts = ts
|
|
307
|
+
@seq = seq
|
|
308
|
+
@run = run
|
|
309
|
+
@shared = shared
|
|
310
|
+
@entry = entry || name
|
|
311
|
+
@from = from
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# The declaration as sent in @pubs ({"t" => 2, "u" => "m/s2", ...}).
|
|
315
|
+
def declaration
|
|
316
|
+
@decl.dup
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# The sequence number the next put sends.
|
|
320
|
+
def seq
|
|
321
|
+
@seq
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# Whether the sender's time goes with each value (ts: true).
|
|
325
|
+
def ts?
|
|
326
|
+
@ts
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
# Sends a value: any value MessagePack carries (EncodeError otherwise),
|
|
330
|
+
# or for a topic with an encoding, the bytes (a String) as they are.
|
|
331
|
+
# Raises Disconnected when the connection is closed (or the topic is).
|
|
332
|
+
def put(value)
|
|
333
|
+
@connection.topic_put(self, value)
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
def close
|
|
337
|
+
@connection.unpublish(self)
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
def closed?
|
|
341
|
+
@publisher.nil?
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def inspect
|
|
345
|
+
"#<Asterism::Topic #{@key}#{closed? ? ' closed' : ''}>"
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
# @api private
|
|
349
|
+
def publisher
|
|
350
|
+
@publisher
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
# @api private
|
|
354
|
+
def decl_raw
|
|
355
|
+
@decl
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
# The attachment of the next value.
|
|
359
|
+
# @api private
|
|
360
|
+
# [seq, run] or [seq, run, ms] for the application's own topic; [seq,
|
|
361
|
+
# run, ms or nil, "<node>/<app>"] for a shared one (docs/protocol.md
|
|
362
|
+
# 9.3).
|
|
363
|
+
def attachment
|
|
364
|
+
ms = @ts ? ::Asterism.wall_ms : nil
|
|
365
|
+
return ::MessagePack.pack([@seq, @run, ms, @from]) if @shared
|
|
366
|
+
::MessagePack.pack(ms.nil? ? [@seq, @run] : [@seq, @run, ms])
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
# @api private
|
|
370
|
+
def advance
|
|
371
|
+
@seq = @seq >= SEQ_MAX ? 0 : @seq + 1
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
# @api private
|
|
375
|
+
def closed!
|
|
376
|
+
@publisher = nil
|
|
377
|
+
end
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
# What comes with each value to a subscription's block (|v, info|): where
|
|
381
|
+
# it came from, its number and times, and how many were missed before it.
|
|
382
|
+
class SampleInfo
|
|
383
|
+
# key: the whole key (asterism/<node>/<app>/<name>, or a shared place's
|
|
384
|
+
# asterism/<node>/@shared/<name> / asterism/@shared/<name>); path: the
|
|
385
|
+
# key below asterism/. shared: nil for an application's own topic,
|
|
386
|
+
# :node or :global for a shared place. name: the topic's name (below
|
|
387
|
+
# @shared/ for a shared one; it may hold "/"). from: the sender,
|
|
388
|
+
# "<node>/<app>" (for a shared place as its attachment tells it, nil
|
|
389
|
+
# when it does not); node, app: its parts (a node's shared place without
|
|
390
|
+
# a sender: the key's node, app nil). seq: the sender's sequence number
|
|
391
|
+
# (nil when the value came without one). run_id: the run id of the
|
|
392
|
+
# sender's connection (16 bits, a new one each time it connects; nil
|
|
393
|
+
# when the value came without one). time: the sender's time in ms
|
|
394
|
+
# since 1970 (nil unless the topic sends it, ts: true). encoding:
|
|
395
|
+
# "msgpack" for a MessagePack value, else the Zenoh encoding
|
|
396
|
+
# ("image/jpeg"). timestamp: the router's time (an
|
|
397
|
+
# Asterism::Zenoh::Timestamp), nil when no router stamped it. lost: how
|
|
398
|
+
# many values of this key (and, on a shared place, of this sender) did
|
|
399
|
+
# not come between the last one and this (0 for the first). restarted:
|
|
400
|
+
# true when the sender started a new run: its run id changed (it
|
|
401
|
+
# connected again; lost is 0, the new run's earlier values are not
|
|
402
|
+
# counted), or, with the same run id, its number went back (lost then
|
|
403
|
+
# counts the values of the run before this one; docs/protocol.md 9.4).
|
|
404
|
+
attr_reader :key, :path, :node, :app, :name, :seq, :run_id, :time, :encoding, :timestamp, :lost, :shared, :from
|
|
405
|
+
|
|
406
|
+
# @api private
|
|
407
|
+
def initialize(key, node, app, name, seq, time, encoding, timestamp, lost, restarted,
|
|
408
|
+
path = nil, shared = nil, from = nil, run_id = nil)
|
|
409
|
+
@key = key
|
|
410
|
+
@node = node
|
|
411
|
+
@app = app
|
|
412
|
+
@name = name
|
|
413
|
+
@seq = seq
|
|
414
|
+
@time = time
|
|
415
|
+
@encoding = encoding
|
|
416
|
+
@timestamp = timestamp
|
|
417
|
+
@lost = lost
|
|
418
|
+
@restarted = restarted
|
|
419
|
+
@path = path || "#{node}/#{app}/#{name}"
|
|
420
|
+
@shared = shared
|
|
421
|
+
@from = from || (shared.nil? && node && app ? "#{node}/#{app}" : nil)
|
|
422
|
+
@run_id = run_id
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
def restarted?
|
|
426
|
+
@restarted
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
def to_h
|
|
430
|
+
{ key: @key, path: @path, shared: @shared, from: @from, node: @node, app: @app, name: @name, seq: @seq,
|
|
431
|
+
run_id: @run_id, time: @time, encoding: @encoding, timestamp: @timestamp, lost: @lost, restarted: @restarted }
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
def inspect
|
|
435
|
+
by = @shared ? " from #{@from || '?'}" : ""
|
|
436
|
+
"#<Asterism::SampleInfo #{@path}#{by} seq=#{@seq.inspect} lost=#{@lost}#{@restarted ? ' restarted' : ''} " \
|
|
437
|
+
"#{@encoding}>"
|
|
438
|
+
end
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
# A reader's memory of the sequence numbers (docs/protocol.md 9.4): for
|
|
442
|
+
# each lane (a key, or a shared key and its sender) the last run id and
|
|
443
|
+
# number, and the application its values come from ("<node>/<app>"), so
|
|
444
|
+
# that the lanes of an application that leaves are forgotten (forget).
|
|
445
|
+
# At most max lanes: past it, the lane seen longest ago is forgotten (its
|
|
446
|
+
# next value counts as a first one). Not thread safe.
|
|
447
|
+
class Sequences
|
|
448
|
+
# Sequence numbers are compared within half of their range: a step
|
|
449
|
+
# forward smaller than this is a step, anything else a restart (the
|
|
450
|
+
# numbers wrap at ::Asterism::Topic::SEQ_MAX).
|
|
451
|
+
HALF = 1073741824
|
|
452
|
+
|
|
453
|
+
def initialize(max = nil)
|
|
454
|
+
@max = max || ::Asterism.lanes_max
|
|
455
|
+
@last = {} # lane => [run id, seq, app]; the lane seen longest ago first
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
# [lost, restarted] of the value numbered seq, of run id run (nil: it
|
|
459
|
+
# came without one), on lane; app: the application it comes from
|
|
460
|
+
# ("<node>/<app>", or nil when it is not known).
|
|
461
|
+
def see(lane, run, seq, app = nil)
|
|
462
|
+
prev = @last.delete(lane)
|
|
463
|
+
if prev.nil?
|
|
464
|
+
while @last.size >= @max
|
|
465
|
+
@last.delete(@last.keys[0])
|
|
466
|
+
end
|
|
467
|
+
@last[lane] = [run, seq, app]
|
|
468
|
+
return [0, false]
|
|
469
|
+
end
|
|
470
|
+
@last[lane] = [run, seq, app]
|
|
471
|
+
# Another run id: the sender connected again. Its earlier values are
|
|
472
|
+
# not counted.
|
|
473
|
+
return [0, true] unless prev[0] == run
|
|
474
|
+
d = seq - prev[1]
|
|
475
|
+
d += ::Asterism::Topic::SEQ_MAX + 1 if d < 0
|
|
476
|
+
return [d - 1, false] if d > 0 && d < HALF
|
|
477
|
+
[seq, true]
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
# Forgets the lanes whose values come from app ("<node>/<app>"): it left
|
|
481
|
+
# the network. Returns how many.
|
|
482
|
+
def forget(app)
|
|
483
|
+
gone = []
|
|
484
|
+
@last.each { |lane, v| gone << lane if v[2] == app }
|
|
485
|
+
i = 0
|
|
486
|
+
while i < gone.size
|
|
487
|
+
@last.delete(gone[i])
|
|
488
|
+
i += 1
|
|
489
|
+
end
|
|
490
|
+
gone.size
|
|
491
|
+
end
|
|
492
|
+
|
|
493
|
+
# The lanes remembered now.
|
|
494
|
+
def size
|
|
495
|
+
@last.size
|
|
496
|
+
end
|
|
497
|
+
|
|
498
|
+
def clear
|
|
499
|
+
@last = {}
|
|
500
|
+
self
|
|
501
|
+
end
|
|
502
|
+
end
|
|
503
|
+
|
|
504
|
+
# The lanes a reader remembers (Sequences): fewer on a board.
|
|
505
|
+
# @api private
|
|
506
|
+
def self.lanes_max
|
|
507
|
+
machine? ? 64 : 4096
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
# The senders whose declarations and types a connection keeps
|
|
511
|
+
# (Connection#type_of): fewer on a board.
|
|
512
|
+
# @api private
|
|
513
|
+
def self.senders_max
|
|
514
|
+
machine? ? 8 : 256
|
|
515
|
+
end
|
|
516
|
+
|
|
517
|
+
# The parts of a value's attachment (docs/protocol.md 9.3): [seq, run id,
|
|
518
|
+
# time, from], or nil when it has no sequence number (no attachment, or
|
|
519
|
+
# not an array that starts with an int from 0 to 2^31 - 1). The run id
|
|
520
|
+
# and the time are nil when they are not ints. place: nil for an
|
|
521
|
+
# application's own key (the sender is the key's, the fourth element is
|
|
522
|
+
# not read), :node or :global for a shared place: from is the fourth
|
|
523
|
+
# element when it is "<node>/<app>" (on a node's place, of node, the
|
|
524
|
+
# key's), else nil.
|
|
525
|
+
def self.read_attachment(att, place = nil, node = nil)
|
|
526
|
+
return nil if att.nil? || att.empty?
|
|
527
|
+
a = ::Asterism::Codec.unpack(att)
|
|
528
|
+
return nil unless a.is_a?(Array) && a[0].is_a?(Integer) && a[0] >= 0 && a[0] <= ::Asterism::Topic::SEQ_MAX
|
|
529
|
+
run = a[1].is_a?(Integer) ? a[1] : nil
|
|
530
|
+
time = a[2].is_a?(Integer) ? a[2] : nil
|
|
531
|
+
from = nil
|
|
532
|
+
unless place.nil?
|
|
533
|
+
from = ::Asterism::Subscription.sender(a[3])
|
|
534
|
+
# A node's place is that node's: a sender of another node is none.
|
|
535
|
+
from = nil if from && place == :node && !from.start_with?("#{node}/")
|
|
536
|
+
end
|
|
537
|
+
[a[0], run, time, from]
|
|
538
|
+
end
|
|
539
|
+
|
|
540
|
+
# A subscription (Connection#subscribe). close ends it.
|
|
541
|
+
class Subscription
|
|
542
|
+
HALF = ::Asterism::Sequences::HALF
|
|
543
|
+
|
|
544
|
+
attr_reader :path, :connection
|
|
545
|
+
|
|
546
|
+
# @api private
|
|
547
|
+
def initialize(connection, path, zsub, blk)
|
|
548
|
+
@connection = connection
|
|
549
|
+
@path = path
|
|
550
|
+
@zsub = zsub
|
|
551
|
+
@blk = blk
|
|
552
|
+
@seqs = ::Asterism::Sequences.new
|
|
553
|
+
@received = 0
|
|
554
|
+
@malformed = 0
|
|
555
|
+
end
|
|
556
|
+
|
|
557
|
+
# The values handed to the block so far.
|
|
558
|
+
def received
|
|
559
|
+
@received
|
|
560
|
+
end
|
|
561
|
+
|
|
562
|
+
# Values left out because they were not MessagePack (no encoding, and
|
|
563
|
+
# not one value of docs/protocol.md 3.1).
|
|
564
|
+
def malformed
|
|
565
|
+
@malformed
|
|
566
|
+
end
|
|
567
|
+
|
|
568
|
+
# Values the queue had no room for (it holds depth: values between two
|
|
569
|
+
# polls).
|
|
570
|
+
def dropped
|
|
571
|
+
@zsub ? @zsub.dropped : 0
|
|
572
|
+
end
|
|
573
|
+
|
|
574
|
+
def close
|
|
575
|
+
@connection.unsubscribe(self)
|
|
576
|
+
end
|
|
577
|
+
|
|
578
|
+
def closed?
|
|
579
|
+
@zsub.nil?
|
|
580
|
+
end
|
|
581
|
+
|
|
582
|
+
def inspect
|
|
583
|
+
"#<Asterism::Subscription #{@path}#{closed? ? ' closed' : ''}>"
|
|
584
|
+
end
|
|
585
|
+
|
|
586
|
+
# The memory of the sequence numbers (an Asterism::Sequences).
|
|
587
|
+
# @api private
|
|
588
|
+
def sequences
|
|
589
|
+
@seqs
|
|
590
|
+
end
|
|
591
|
+
|
|
592
|
+
# Forgets the numbers of the values from app ("<node>/<app>"), which
|
|
593
|
+
# left the network (Connection: on its token's leave).
|
|
594
|
+
# @api private
|
|
595
|
+
def forget(app)
|
|
596
|
+
@seqs.forget(app)
|
|
597
|
+
end
|
|
598
|
+
|
|
599
|
+
# @api private
|
|
600
|
+
def zsub
|
|
601
|
+
@zsub
|
|
602
|
+
end
|
|
603
|
+
|
|
604
|
+
# @api private
|
|
605
|
+
def block
|
|
606
|
+
@blk
|
|
607
|
+
end
|
|
608
|
+
|
|
609
|
+
# @api private
|
|
610
|
+
def closed!
|
|
611
|
+
@zsub = nil
|
|
612
|
+
end
|
|
613
|
+
|
|
614
|
+
# The values waiting, as [[value, info], ...] (the bad ones left out).
|
|
615
|
+
# @api private
|
|
616
|
+
def take
|
|
617
|
+
z = @zsub
|
|
618
|
+
out = []
|
|
619
|
+
return out unless z && z.pending > 0
|
|
620
|
+
samples = z.each_sample
|
|
621
|
+
i = 0
|
|
622
|
+
while i < samples.size
|
|
623
|
+
got = unwrap(samples[i])
|
|
624
|
+
i += 1
|
|
625
|
+
out << got if got
|
|
626
|
+
end
|
|
627
|
+
out
|
|
628
|
+
end
|
|
629
|
+
|
|
630
|
+
# [value, info] of one Zenoh sample, or nil (a delete, or not
|
|
631
|
+
# MessagePack).
|
|
632
|
+
# @api private
|
|
633
|
+
def unwrap(sm)
|
|
634
|
+
return nil if sm.kind == :delete
|
|
635
|
+
payload = sm.payload
|
|
636
|
+
enc = sm.encoding
|
|
637
|
+
if enc.nil? || enc == "" || enc == "zenoh/bytes"
|
|
638
|
+
value = ::Asterism::Codec.unpack(payload)
|
|
639
|
+
if value.nil? && !(payload && payload.bytesize == 1 && payload.getbyte(0) == 0xc0)
|
|
640
|
+
@malformed += 1
|
|
641
|
+
return nil
|
|
642
|
+
end
|
|
643
|
+
enc = "msgpack"
|
|
644
|
+
else
|
|
645
|
+
value = payload
|
|
646
|
+
end
|
|
647
|
+
key = sm.key
|
|
648
|
+
root = ::Asterism::ROOT
|
|
649
|
+
rest = key.start_with?("#{root}/") ? key[root.size + 1, key.size] : key
|
|
650
|
+
parts = rest.split("/")
|
|
651
|
+
shared = nil
|
|
652
|
+
if parts[0] == ::Asterism::Pubs::SHARED
|
|
653
|
+
shared = :global
|
|
654
|
+
node = nil
|
|
655
|
+
app = nil
|
|
656
|
+
name = parts.size > 1 ? parts[1, parts.size - 1].join("/") : nil
|
|
657
|
+
elsif parts.size > 2 && parts[1] == ::Asterism::Pubs::SHARED
|
|
658
|
+
shared = :node
|
|
659
|
+
node = parts[0]
|
|
660
|
+
app = nil
|
|
661
|
+
name = parts[2, parts.size - 2].join("/")
|
|
662
|
+
else
|
|
663
|
+
node = parts[0]
|
|
664
|
+
app = parts[1]
|
|
665
|
+
name = parts.size > 2 ? parts[2, parts.size - 2].join("/") : nil
|
|
666
|
+
end
|
|
667
|
+
seq = nil
|
|
668
|
+
run = nil
|
|
669
|
+
time = nil
|
|
670
|
+
from = nil
|
|
671
|
+
got = ::Asterism.read_attachment(sm.attachment, shared, node)
|
|
672
|
+
if got
|
|
673
|
+
seq = got[0]
|
|
674
|
+
run = got[1]
|
|
675
|
+
time = got[2]
|
|
676
|
+
from = got[3]
|
|
677
|
+
end
|
|
678
|
+
if from
|
|
679
|
+
cut = from.index("/")
|
|
680
|
+
node = from[0, cut]
|
|
681
|
+
app = from[cut + 1, from.size - cut - 1]
|
|
682
|
+
end
|
|
683
|
+
# The numbers of a shared key are counted per sender.
|
|
684
|
+
lane = from ? "#{key}\n#{from}" : key
|
|
685
|
+
lost = 0
|
|
686
|
+
restarted = false
|
|
687
|
+
unless seq.nil?
|
|
688
|
+
sender = from || (shared.nil? && node && app ? "#{node}/#{app}" : nil)
|
|
689
|
+
r = @seqs.see(lane, run, seq, sender)
|
|
690
|
+
lost = r[0]
|
|
691
|
+
restarted = r[1]
|
|
692
|
+
end
|
|
693
|
+
@received += 1
|
|
694
|
+
[value, ::Asterism::SampleInfo.new(key, node, app, name, seq, time, enc, sm.timestamp, lost, restarted,
|
|
695
|
+
rest, shared, from, run)]
|
|
696
|
+
end
|
|
697
|
+
|
|
698
|
+
# The sender of a shared value from its attachment: "<node>/<app>" when
|
|
699
|
+
# it is a str of two names, else nil.
|
|
700
|
+
# @api private
|
|
701
|
+
def self.sender(s)
|
|
702
|
+
return nil unless s.is_a?(String)
|
|
703
|
+
cut = s.index("/")
|
|
704
|
+
return nil if cut.nil?
|
|
705
|
+
n = s[0, cut]
|
|
706
|
+
a = s[cut + 1, s.size - cut - 1]
|
|
707
|
+
::Asterism.valid_name?(n) && ::Asterism.valid_name?(a) ? s : nil
|
|
708
|
+
end
|
|
709
|
+
end
|
|
710
|
+
|
|
711
|
+
# A topic's name: key chunks separated by "/", each a name.
|
|
712
|
+
# @api private
|
|
713
|
+
def self.check_topic_name(name)
|
|
714
|
+
s = name.is_a?(Symbol) ? name.to_s : name
|
|
715
|
+
raise ArgumentError, "invalid topic name: #{name.inspect}" unless s.is_a?(String)
|
|
716
|
+
bad = s.empty? || s.start_with?("/") || s.end_with?("/")
|
|
717
|
+
unless bad
|
|
718
|
+
s.split("/").each { |c| bad = true unless valid_name?(c) }
|
|
719
|
+
end
|
|
720
|
+
raise ArgumentError, "invalid topic name: #{name.inspect}" if bad
|
|
721
|
+
s
|
|
722
|
+
end
|
|
723
|
+
|
|
724
|
+
# The place of publish's shared: nil (the application's own), :node or
|
|
725
|
+
# :global (a Symbol or a String).
|
|
726
|
+
# @api private
|
|
727
|
+
def self.shared_place!(shared)
|
|
728
|
+
return nil if shared.nil? || shared == false
|
|
729
|
+
s = shared.to_s
|
|
730
|
+
return :node if s == "node"
|
|
731
|
+
return :global if s == "global"
|
|
732
|
+
raise ArgumentError, "shared: expects :node or :global (or nil), got #{shared.inspect}"
|
|
733
|
+
end
|
|
734
|
+
|
|
735
|
+
# The sender's time: milliseconds since 1970 (nil where there is no
|
|
736
|
+
# clock of the day).
|
|
737
|
+
# @api private
|
|
738
|
+
def self.wall_ms
|
|
739
|
+
return nil unless Object.const_defined?(:Time)
|
|
740
|
+
t = Time.now
|
|
741
|
+
t.to_i * 1000 + t.usec / 1000
|
|
742
|
+
end
|
|
743
|
+
|
|
744
|
+
class Connection
|
|
745
|
+
# Declares a topic of this application, asterism/<node>/<app>/<name>
|
|
746
|
+
# (name: chunks separated by "/", each a name of docs/protocol.md 2.1),
|
|
747
|
+
# and returns it (an Asterism::Topic; topic.put(value) sends). type: a
|
|
748
|
+
# built-in type (:vector3, or its number) or an application's own type
|
|
749
|
+
# (a String, "mygame/board.strokes"); unit:, desc: Strings for the
|
|
750
|
+
# readers; ts: true sends the sender's time with each value; encoding: a
|
|
751
|
+
# Zenoh encoding with a fixed number ("image/jpeg"): the values are then
|
|
752
|
+
# Strings sent as they are, not MessagePack. Publishing a name again
|
|
753
|
+
# replaces the topic; the numbers are the connection's, so they go on
|
|
754
|
+
# from where the replaced topic was (also after unpublish), and start
|
|
755
|
+
# at 0 on a new connection (docs/protocol.md 9.3). The declaration is
|
|
756
|
+
# put on asterism/<node>/<app>/@pubs. Raises ArgumentError for what it
|
|
757
|
+
# does not accept and Disconnected when not connected. The topics end
|
|
758
|
+
# with the connection.
|
|
759
|
+
#
|
|
760
|
+
# shared: puts the values on a shared place instead (docs/protocol.md
|
|
761
|
+
# 17): :node, this node's asterism/<node>/@shared/<name>; :global, the
|
|
762
|
+
# network's asterism/@shared/<name>. The topic is still declared in this
|
|
763
|
+
# application's @pubs ("<node>/@shared/<name>", "@shared/<name>"), with
|
|
764
|
+
# this application's types, and each value says its sender
|
|
765
|
+
# ("<node>/<app>") in the attachment. Other applications may put on the
|
|
766
|
+
# same key; the router decides who may (the node's place: its own node;
|
|
767
|
+
# the network's: whom the roles allow).
|
|
768
|
+
def publish(name, type: nil, unit: nil, desc: nil, ts: false, encoding: nil, shared: nil)
|
|
769
|
+
s = session!
|
|
770
|
+
name = ::Asterism.check_topic_name(name)
|
|
771
|
+
place = ::Asterism.shared_place!(shared)
|
|
772
|
+
entry = ::Asterism::Pubs.entry(place, @node_id, name)
|
|
773
|
+
decl = {}
|
|
774
|
+
t = ::Asterism::Types.topic_type!(type)
|
|
775
|
+
decl["t"] = t unless t.nil?
|
|
776
|
+
enc = nil
|
|
777
|
+
unless encoding.nil?
|
|
778
|
+
enc = encoding.to_s
|
|
779
|
+
id = ::Asterism::Pubs.encoding_id(enc)
|
|
780
|
+
if id.nil?
|
|
781
|
+
raise ArgumentError, "encoding: #{enc.inspect} is not one of Zenoh's encodings with a number " \
|
|
782
|
+
"(image/jpeg, image/png, text/plain, application/cdr, ...)"
|
|
783
|
+
end
|
|
784
|
+
if id == 0
|
|
785
|
+
raise ArgumentError, "encoding: zenoh/bytes is how a MessagePack value goes; leave encoding: out " \
|
|
786
|
+
"(a binary String is sent as a MessagePack bin)"
|
|
787
|
+
end
|
|
788
|
+
decl["e"] = id
|
|
789
|
+
end
|
|
790
|
+
unless unit.nil?
|
|
791
|
+
raise ArgumentError, "unit: expects a String, got #{unit.class}" unless unit.is_a?(String)
|
|
792
|
+
raise ArgumentError, "unit: at most #{::Asterism::Pubs::UNIT_MAX} bytes" if unit.bytesize > ::Asterism::Pubs::UNIT_MAX
|
|
793
|
+
decl["u"] = unit unless unit.empty?
|
|
794
|
+
end
|
|
795
|
+
unless desc.nil?
|
|
796
|
+
raise ArgumentError, "desc: expects a String, got #{desc.class}" unless desc.is_a?(String)
|
|
797
|
+
raise ArgumentError, "desc: at most #{::Asterism::DOC_MAX} bytes" if desc.bytesize > ::Asterism::DOC_MAX
|
|
798
|
+
decl["d"] = desc unless desc.empty?
|
|
799
|
+
end
|
|
800
|
+
raise ArgumentError, "ts: expects true or false" unless ts == true || ts == false
|
|
801
|
+
decl["ts"] = true if ts
|
|
802
|
+
old = @topics[entry] # replaced in place: the order of @pubs stays
|
|
803
|
+
seq = old ? old.seq : (@seq_of.delete(entry) || 0)
|
|
804
|
+
key = place ? "#{::Asterism::ROOT}/#{entry}" : "#{::Asterism::ROOT}/#{@node_id}/#{@app}/#{name}"
|
|
805
|
+
if old && old.encoding == enc && old.decl_raw == decl
|
|
806
|
+
# The same declaration again: the same publisher goes on.
|
|
807
|
+
zp = old.publisher
|
|
808
|
+
old.closed!
|
|
809
|
+
else
|
|
810
|
+
close_topic(old) if old
|
|
811
|
+
begin
|
|
812
|
+
zp = enc ? s.publisher(key, encoding: enc) : s.publisher(key)
|
|
813
|
+
rescue ::Asterism::Zenoh::Error
|
|
814
|
+
if old
|
|
815
|
+
@topics.delete(entry)
|
|
816
|
+
@seq_of[entry] = seq
|
|
817
|
+
tell_pubs(false)
|
|
818
|
+
end
|
|
819
|
+
raise
|
|
820
|
+
end
|
|
821
|
+
end
|
|
822
|
+
topic = ::Asterism::Topic.new(self, name, key, zp, decl, enc, ts, place, entry, place ? path : nil,
|
|
823
|
+
seq, @run_id)
|
|
824
|
+
@topics[entry] = topic
|
|
825
|
+
app_queryable(s)
|
|
826
|
+
tell_pubs(false)
|
|
827
|
+
topic
|
|
828
|
+
rescue ::Asterism::Zenoh::Error => e
|
|
829
|
+
lost_if_closed(e.message)
|
|
830
|
+
raise ::Asterism::Disconnected, e.message
|
|
831
|
+
end
|
|
832
|
+
|
|
833
|
+
# Withdraws a topic (a Topic, or its name with the shared: it was
|
|
834
|
+
# published with, or its entry as published lists it): its declaration
|
|
835
|
+
# leaves @pubs. True when there was one.
|
|
836
|
+
def unpublish(topic, shared: nil)
|
|
837
|
+
tbl = @topics || {}
|
|
838
|
+
is_topic = topic.is_a?(::Asterism::Topic)
|
|
839
|
+
name = if is_topic
|
|
840
|
+
topic.entry
|
|
841
|
+
else
|
|
842
|
+
::Asterism::Pubs.entry(::Asterism.shared_place!(shared), @node_id, topic.to_s)
|
|
843
|
+
end
|
|
844
|
+
cur = tbl[name]
|
|
845
|
+
if cur.nil? || (is_topic && !cur.equal?(topic))
|
|
846
|
+
close_topic(topic) if is_topic
|
|
847
|
+
return false
|
|
848
|
+
end
|
|
849
|
+
tbl.delete(name)
|
|
850
|
+
@seq_of[name] = cur.seq # a topic declared again goes on from here
|
|
851
|
+
close_topic(cur)
|
|
852
|
+
tell_pubs(false)
|
|
853
|
+
true
|
|
854
|
+
end
|
|
855
|
+
|
|
856
|
+
# The entries of this application's topics: the names of its own, and
|
|
857
|
+
# "<node>/@shared/<name>" / "@shared/<name>" for the shared ones.
|
|
858
|
+
def published
|
|
859
|
+
@topics ? @topics.keys : []
|
|
860
|
+
end
|
|
861
|
+
|
|
862
|
+
# @api private
|
|
863
|
+
def close_topic(topic)
|
|
864
|
+
zp = topic.publisher
|
|
865
|
+
topic.closed!
|
|
866
|
+
zp.close if zp
|
|
867
|
+
rescue ::Asterism::Zenoh::Error
|
|
868
|
+
nil
|
|
869
|
+
end
|
|
870
|
+
|
|
871
|
+
# @api private
|
|
872
|
+
def topic_put(topic, value)
|
|
873
|
+
zp = topic.publisher
|
|
874
|
+
raise ::Asterism::Disconnected, "topic #{topic.entry} is closed" if zp.nil?
|
|
875
|
+
if topic.encoding
|
|
876
|
+
unless value.is_a?(String)
|
|
877
|
+
raise ArgumentError, "topic #{topic.name} (#{topic.encoding}): put expects the bytes, a String"
|
|
878
|
+
end
|
|
879
|
+
bytes = value
|
|
880
|
+
else
|
|
881
|
+
bytes = ::Asterism::Codec.pack(value)
|
|
882
|
+
end
|
|
883
|
+
begin
|
|
884
|
+
zp.put(bytes, attachment: topic.attachment)
|
|
885
|
+
rescue ::Asterism::Zenoh::Error => e
|
|
886
|
+
lost_if_closed(e.message)
|
|
887
|
+
raise ::Asterism::Disconnected, e.message
|
|
888
|
+
end
|
|
889
|
+
topic.advance
|
|
890
|
+
nil
|
|
891
|
+
end
|
|
892
|
+
|
|
893
|
+
# The declarations as sent: {name => {"t", "e", "u", "d", "ts"}}.
|
|
894
|
+
# @api private
|
|
895
|
+
def pubs_hash
|
|
896
|
+
h = {}
|
|
897
|
+
(@topics || {}).each { |name, t| h[name] = t.decl_raw }
|
|
898
|
+
h
|
|
899
|
+
end
|
|
900
|
+
|
|
901
|
+
# Puts the declarations on asterism/<node>/<app>/@pubs while connected:
|
|
902
|
+
# when they changed since the last put of this connection, or always with
|
|
903
|
+
# force (a connection that came back) once there were any.
|
|
904
|
+
# @api private
|
|
905
|
+
def tell_pubs(force)
|
|
906
|
+
s = @session
|
|
907
|
+
return unless s
|
|
908
|
+
tbl = @topics || {}
|
|
909
|
+
return if tbl.empty? && @pubs_put.nil?
|
|
910
|
+
bytes = ::Asterism::Codec.pack(pubs_hash)
|
|
911
|
+
return if !force && !@pubs_put.nil? && @pubs_put == bytes
|
|
912
|
+
s.put("#{::Asterism::ROOT}/#{@node_id}/#{@app}/#{::Asterism::Pubs::KEY}", bytes)
|
|
913
|
+
@pubs_put = bytes
|
|
914
|
+
rescue ::Asterism::Zenoh::Error
|
|
915
|
+
nil # the next poll notices a lost connection
|
|
916
|
+
end
|
|
917
|
+
|
|
918
|
+
# Defines an application type (docs/protocol.md 10), told to the readers
|
|
919
|
+
# by asterism/<node>/<app>/@profile?types. name: a String
|
|
920
|
+
# ("mygame/board.strokes"); fields: [[name, type], ...], each type a
|
|
921
|
+
# name (a String or a Symbol, never a number): a built-in type's
|
|
922
|
+
# ("vector3", :vector3), a basic one ("int", "float", "str", "bin",
|
|
923
|
+
# "bool") or another application type's, with "[]" at the end for an
|
|
924
|
+
# array ("vector3[]", "int[]", "mygame/point[]"), repeated for arrays
|
|
925
|
+
# of arrays ("int[][]"); "any" for a value of any shape. The values are
|
|
926
|
+
# maps with the field names as keys: short names (one or two letters)
|
|
927
|
+
# keep each value small (docs/protocol.md 11.2). desc: a text.
|
|
928
|
+
# Defining a name again replaces it. The types are the connection's
|
|
929
|
+
# (kept after close, to read). Returns the definition as sent
|
|
930
|
+
# ({"f" => [...], "d" => ...}).
|
|
931
|
+
def define_type(name, fields, desc: nil)
|
|
932
|
+
name = ::Asterism::Types.name!("define_type: name:", name)
|
|
933
|
+
d = ::Asterism::Types.definition!(fields, desc)
|
|
934
|
+
@types[name] = d
|
|
935
|
+
s = @session
|
|
936
|
+
app_queryable(s) if s
|
|
937
|
+
d
|
|
938
|
+
end
|
|
939
|
+
|
|
940
|
+
# Another application's topics: {entry => {"t", "e", "u", "d", "ts"}}
|
|
941
|
+
# (Asterism::Pubs.check has cleaned it; an entry is a topic's name, or
|
|
942
|
+
# "<node>/@shared/<name>" / "@shared/<name>" for a shared one: place
|
|
943
|
+
# tells which), {} when it answers without any,
|
|
944
|
+
# nil when nobody answers in time. Waits like status_of.
|
|
945
|
+
def pubs_of(node, app, timeout: nil, timeout_ms: nil)
|
|
946
|
+
raw = app_profile_part(node, app, "pubs", timeout, timeout_ms)
|
|
947
|
+
return nil if raw.nil?
|
|
948
|
+
::Asterism::Pubs.check(raw.key?("pubs") ? raw["pubs"] : {}, node.to_s)
|
|
949
|
+
end
|
|
950
|
+
|
|
951
|
+
# Another application's own types: {name => {"f" => [...], "d" => ...}}
|
|
952
|
+
# (Asterism::Types.check), {} when it answers without any, nil when
|
|
953
|
+
# nobody answers in time. Waits like status_of.
|
|
954
|
+
def types_of(node, app, timeout: nil, timeout_ms: nil)
|
|
955
|
+
raw = app_profile_part(node, app, "types", timeout, timeout_ms)
|
|
956
|
+
return nil if raw.nil?
|
|
957
|
+
::Asterism::Types.check(raw.key?("types") ? raw["types"] : {})
|
|
958
|
+
end
|
|
959
|
+
|
|
960
|
+
# The applications that declare a shared key, from their @pubs: path is
|
|
961
|
+
# "@shared/<name>" or "<node>/@shared/<name>" (node may be *, any node).
|
|
962
|
+
# Returns {"<node>/<app>" => declaration} (cleaned as pubs_of does; this
|
|
963
|
+
# application's own among them), {} when nobody declares it. Asks every
|
|
964
|
+
# application's profile (of that node, for a node's place) at once and
|
|
965
|
+
# waits for the answers until timeout: (seconds, 2.0 by default) or the
|
|
966
|
+
# query ends. Raises ArgumentError for a path that is not a shared key and
|
|
967
|
+
# Disconnected when not connected.
|
|
968
|
+
def publishers_of(path, timeout: nil, timeout_ms: nil)
|
|
969
|
+
p = path.to_s
|
|
970
|
+
star = p.start_with?("*/")
|
|
971
|
+
pl = ::Asterism::Pubs.place(star ? "x#{p[1, p.size - 1]}" : p)
|
|
972
|
+
if pl.nil? || pl[0].nil?
|
|
973
|
+
raise ArgumentError, "publishers_of: expected @shared/<name> or <node>/@shared/<name>, got #{path.inspect}"
|
|
974
|
+
end
|
|
975
|
+
node = pl[0] == :node && !star ? pl[1] : nil
|
|
976
|
+
ms = ::Asterism.time_ms("Connection#publishers_of", timeout, timeout_ms, nil, ::Asterism::DEFAULT_TIMEOUT_MS)
|
|
977
|
+
out = {}
|
|
978
|
+
add = lambda do |n, a, pubs|
|
|
979
|
+
return if pubs.nil?
|
|
980
|
+
pubs.each do |entry, d|
|
|
981
|
+
q = ::Asterism::Pubs.place(entry)
|
|
982
|
+
next if q.nil? || q[0] != pl[0] || q[2] != pl[2]
|
|
983
|
+
next if q[0] == :node && node && q[1] != node
|
|
984
|
+
out["#{n}/#{a}"] = d
|
|
985
|
+
end
|
|
986
|
+
end
|
|
987
|
+
add.call(@node_id, @app, ::Asterism::Pubs.check(pubs_hash, @node_id)) if @session
|
|
988
|
+
g = pubs_get(node, ms)
|
|
989
|
+
deadline = ::Asterism.now_ms + ms
|
|
990
|
+
wait_until do
|
|
991
|
+
ended = g.done?
|
|
992
|
+
g.each_reply.each do |r|
|
|
993
|
+
parts = r[0].to_s.split("/")
|
|
994
|
+
next unless parts.size == 4 && parts[0] == ::Asterism::ROOT
|
|
995
|
+
raw = ::Asterism::Codec.unpack(r[1])
|
|
996
|
+
next unless raw.is_a?(Hash)
|
|
997
|
+
add.call(parts[1], parts[2], ::Asterism::Pubs.check(raw["pubs"], parts[1]))
|
|
998
|
+
end
|
|
999
|
+
ended || ::Asterism.now_ms >= deadline || !connected?
|
|
1000
|
+
end
|
|
1001
|
+
out
|
|
1002
|
+
end
|
|
1003
|
+
|
|
1004
|
+
# @api private
|
|
1005
|
+
def pubs_get(node, ms)
|
|
1006
|
+
session!.get("#{::Asterism::ROOT}/#{node || '*'}/*/#{::Asterism::Profile::KEY}", timeout_ms: ms, params: "pubs")
|
|
1007
|
+
rescue ::Asterism::Zenoh::Error => e
|
|
1008
|
+
lost_if_closed(e.message)
|
|
1009
|
+
raise ::Asterism::Disconnected, e.message
|
|
1010
|
+
end
|
|
1011
|
+
|
|
1012
|
+
# The declaration of the topic a value came on, as its sender declared
|
|
1013
|
+
# it (info: the Asterism::SampleInfo of the value): the sender's entry
|
|
1014
|
+
# for that key in its @pubs ({"t", "e", "u", "d", "ts"}). On a shared
|
|
1015
|
+
# key every sender declares the key with its own declaration, so this
|
|
1016
|
+
# asks the sender (info.from). nil when the sender is not known, does not
|
|
1017
|
+
# answer, or does not declare the key. Waits like pubs_of the first
|
|
1018
|
+
# time; the answer is kept for that sender until it puts its @pubs again
|
|
1019
|
+
# or leaves (type_of shares it), so asking for each value is cheap.
|
|
1020
|
+
def declaration_of(info, timeout: nil, timeout_ms: nil)
|
|
1021
|
+
got = sender_parts(info, "pubs", timeout, timeout_ms)
|
|
1022
|
+
got ? got[0] : nil
|
|
1023
|
+
end
|
|
1024
|
+
|
|
1025
|
+
# The type of the values on a key as their sender declared it (info: a
|
|
1026
|
+
# SampleInfo): a built-in type's number, or for an application type
|
|
1027
|
+
# [name, definition] with the definition from the sender's own types
|
|
1028
|
+
# ({"f" => [...], "d" => ...}; nil when it does not tell it). Two senders
|
|
1029
|
+
# on a shared key may give the same type name different definitions;
|
|
1030
|
+
# each value is read with its own sender's. nil when there is no type
|
|
1031
|
+
# or the sender does not answer. Waits like types_of (one query of
|
|
1032
|
+
# @profile?pubs;types), and keeps the answer as declaration_of does (a
|
|
1033
|
+
# type defined again without a change of @pubs is not seen until then).
|
|
1034
|
+
def type_of(info, timeout: nil, timeout_ms: nil)
|
|
1035
|
+
got = sender_parts(info, "pubs;types", timeout, timeout_ms)
|
|
1036
|
+
return nil if got.nil?
|
|
1037
|
+
t = got[0]["t"]
|
|
1038
|
+
return t unless t.is_a?(String)
|
|
1039
|
+
types = got[1] || {}
|
|
1040
|
+
[t, types[t]]
|
|
1041
|
+
end
|
|
1042
|
+
|
|
1043
|
+
# [the sender's declaration of info's key, the sender's types], or nil.
|
|
1044
|
+
# @api private
|
|
1045
|
+
def sender_parts(info, _parts, timeout, timeout_ms)
|
|
1046
|
+
from = info.from
|
|
1047
|
+
return nil if from.nil?
|
|
1048
|
+
got = sender_profile(from, timeout, timeout_ms)
|
|
1049
|
+
return nil if got.nil?
|
|
1050
|
+
entry = info.shared ? info.path : info.name
|
|
1051
|
+
d = got[0][entry]
|
|
1052
|
+
return nil if d.nil?
|
|
1053
|
+
[d, got[1]]
|
|
1054
|
+
end
|
|
1055
|
+
|
|
1056
|
+
# [declarations, types] of the application from ("<node>/<app>"), both
|
|
1057
|
+
# cleaned, from what is kept or one query of its @profile?pubs;types;
|
|
1058
|
+
# nil when it does not answer.
|
|
1059
|
+
# @api private
|
|
1060
|
+
def sender_profile(from, timeout, timeout_ms)
|
|
1061
|
+
own = from == path
|
|
1062
|
+
unless own
|
|
1063
|
+
got = sender_cached(from)
|
|
1064
|
+
return got if got
|
|
1065
|
+
end
|
|
1066
|
+
mark = watch_pubs
|
|
1067
|
+
cut = from.index("/")
|
|
1068
|
+
n = from[0, cut]
|
|
1069
|
+
a = from[cut + 1, from.size - cut - 1]
|
|
1070
|
+
raw = app_profile_part(n, a, "pubs;types", timeout, timeout_ms)
|
|
1071
|
+
return nil if raw.nil?
|
|
1072
|
+
got = [::Asterism::Pubs.check(raw["pubs"], n) || {}, ::Asterism::Types.check(raw["types"]) || {}]
|
|
1073
|
+
sender_store(from, got, mark) unless own
|
|
1074
|
+
got
|
|
1075
|
+
end
|
|
1076
|
+
|
|
1077
|
+
# What is kept for from, or nil.
|
|
1078
|
+
# @api private
|
|
1079
|
+
def sender_cached(from)
|
|
1080
|
+
@senders ? @senders[from] : nil
|
|
1081
|
+
end
|
|
1082
|
+
|
|
1083
|
+
# Keeps from's answer, unless an application's @pubs came (or one left)
|
|
1084
|
+
# since mark (it may be from's: the answer may be older than it).
|
|
1085
|
+
# @api private
|
|
1086
|
+
def sender_store(from, got, mark)
|
|
1087
|
+
return unless @session && @pubs_watch && @sender_changes == mark
|
|
1088
|
+
tbl = (@senders ||= {})
|
|
1089
|
+
while tbl.size >= ::Asterism.senders_max
|
|
1090
|
+
tbl.delete(tbl.keys[0])
|
|
1091
|
+
end
|
|
1092
|
+
tbl[from] = got
|
|
1093
|
+
end
|
|
1094
|
+
|
|
1095
|
+
# Follows the puts on asterism/*/*/@pubs from the first declaration_of /
|
|
1096
|
+
# type_of on (each drops what is kept for its application). Returns
|
|
1097
|
+
# the count of changes so far (sender_store's mark).
|
|
1098
|
+
# @api private
|
|
1099
|
+
def watch_pubs
|
|
1100
|
+
if @pubs_watch.nil? && @session
|
|
1101
|
+
begin
|
|
1102
|
+
@pubs_watch = @session.subscribe("#{::Asterism::ROOT}/*/*/#{::Asterism::Pubs::KEY}", depth: 16)
|
|
1103
|
+
@pubs_watch_dropped = 0
|
|
1104
|
+
rescue ::Asterism::Zenoh::Error
|
|
1105
|
+
@pubs_watch = nil
|
|
1106
|
+
end
|
|
1107
|
+
end
|
|
1108
|
+
@sender_changes ||= 0
|
|
1109
|
+
end
|
|
1110
|
+
|
|
1111
|
+
# Drops what is kept for the applications that put their @pubs (all of
|
|
1112
|
+
# it when some of those puts were dropped from the queue).
|
|
1113
|
+
# @api private
|
|
1114
|
+
def follow_pubs
|
|
1115
|
+
w = @pubs_watch
|
|
1116
|
+
return unless w && w.pending > 0
|
|
1117
|
+
samples = w.each_sample
|
|
1118
|
+
d = w.dropped
|
|
1119
|
+
if d != @pubs_watch_dropped
|
|
1120
|
+
@pubs_watch_dropped = d
|
|
1121
|
+
@senders = {}
|
|
1122
|
+
@sender_changes += 1
|
|
1123
|
+
end
|
|
1124
|
+
i = 0
|
|
1125
|
+
while i < samples.size
|
|
1126
|
+
parts = samples[i].key.to_s.split("/")
|
|
1127
|
+
i += 1
|
|
1128
|
+
forget_sender("#{parts[1]}/#{parts[2]}") if parts.size == 4
|
|
1129
|
+
end
|
|
1130
|
+
end
|
|
1131
|
+
|
|
1132
|
+
# @api private
|
|
1133
|
+
def forget_sender(app)
|
|
1134
|
+
@sender_changes = (@sender_changes || 0) + 1
|
|
1135
|
+
@senders.delete(app) if @senders
|
|
1136
|
+
end
|
|
1137
|
+
|
|
1138
|
+
# The application app ("<node>/<app>") left (its token went away): every
|
|
1139
|
+
# subscription of this connection forgets the numbers of its values, and
|
|
1140
|
+
# what is kept of its declarations and types goes (docs/protocol.md 8.5,
|
|
1141
|
+
# 9.4).
|
|
1142
|
+
# @api private
|
|
1143
|
+
def forget_app(app)
|
|
1144
|
+
list = @subscriptions
|
|
1145
|
+
if list
|
|
1146
|
+
i = 0
|
|
1147
|
+
while i < list.size
|
|
1148
|
+
list[i].forget(app)
|
|
1149
|
+
i += 1
|
|
1150
|
+
end
|
|
1151
|
+
end
|
|
1152
|
+
forget_sender(app)
|
|
1153
|
+
end
|
|
1154
|
+
|
|
1155
|
+
# @api private
|
|
1156
|
+
def app_profile_part(node, app, part, timeout, timeout_ms)
|
|
1157
|
+
f = app_profile_async(node, app, part, timeout, timeout_ms)
|
|
1158
|
+
begin
|
|
1159
|
+
raw = f.raw_value
|
|
1160
|
+
rescue ::Asterism::Disconnected
|
|
1161
|
+
raise
|
|
1162
|
+
rescue ::Asterism::Error
|
|
1163
|
+
return nil # no answer in time, or not MessagePack
|
|
1164
|
+
end
|
|
1165
|
+
raw.is_a?(Hash) ? raw : nil
|
|
1166
|
+
end
|
|
1167
|
+
|
|
1168
|
+
# Subscribes to the values on asterism/<path> (path: <node>/<app>/<name>,
|
|
1169
|
+
# Zenoh wildcards allowed: * one chunk, ** any number; or a shared place,
|
|
1170
|
+
# <node>/@shared/<name> and @shared/<name>: "*/@shared/spectate/**",
|
|
1171
|
+
# "@shared/lobby/**". Wildcards do not reach into @shared: "**" and
|
|
1172
|
+
# "<node>/**" get only applications' own topics). The block gets
|
|
1173
|
+
# each value and an Asterism::SampleInfo (|v, info|; |v| is fine too):
|
|
1174
|
+
# the value decoded from MessagePack, or the bytes (a String) when it
|
|
1175
|
+
# came with an encoding. Blocks run from poll. This application's own
|
|
1176
|
+
# values do not come back to it (another connection of the program gets
|
|
1177
|
+
# them, as any other application). depth: the values kept between two
|
|
1178
|
+
# polls (Asterism::Zenoh::DEFAULT_DEPTH). Returns the
|
|
1179
|
+
# Asterism::Subscription (close it to stop); it ends with the connection.
|
|
1180
|
+
def subscribe(path, depth: nil, &blk)
|
|
1181
|
+
raise ArgumentError, "subscribe needs a block" unless blk
|
|
1182
|
+
s = session!
|
|
1183
|
+
p = path.to_s
|
|
1184
|
+
if p.empty? || p.start_with?("/") || p.end_with?("/")
|
|
1185
|
+
raise ArgumentError, "subscribe: expected <node>/<app>/<name> (wildcards allowed), got #{path.inspect}"
|
|
1186
|
+
end
|
|
1187
|
+
key = "#{::Asterism::ROOT}/#{p}"
|
|
1188
|
+
begin
|
|
1189
|
+
zs = depth.nil? ? s.subscribe(key) : s.subscribe(key, depth: depth)
|
|
1190
|
+
rescue ::Asterism::Zenoh::Error => e
|
|
1191
|
+
lost_if_closed(e.message)
|
|
1192
|
+
raise ::Asterism::Disconnected, e.message
|
|
1193
|
+
end
|
|
1194
|
+
sub = ::Asterism::Subscription.new(self, p, zs, blk)
|
|
1195
|
+
@subscriptions << sub
|
|
1196
|
+
sub
|
|
1197
|
+
end
|
|
1198
|
+
|
|
1199
|
+
# Ends a subscription. True when it was open.
|
|
1200
|
+
def unsubscribe(sub)
|
|
1201
|
+
list = @subscriptions || []
|
|
1202
|
+
z = sub.zsub
|
|
1203
|
+
sub.closed!
|
|
1204
|
+
list.delete(sub)
|
|
1205
|
+
return false if z.nil?
|
|
1206
|
+
begin
|
|
1207
|
+
z.close
|
|
1208
|
+
rescue ::Asterism::Zenoh::Error
|
|
1209
|
+
nil
|
|
1210
|
+
end
|
|
1211
|
+
true
|
|
1212
|
+
end
|
|
1213
|
+
|
|
1214
|
+
# Hands the values that came to the subscriptions' blocks (from poll).
|
|
1215
|
+
# @api private
|
|
1216
|
+
def deliver_samples
|
|
1217
|
+
list = @subscriptions
|
|
1218
|
+
return if list.nil? || list.empty?
|
|
1219
|
+
subs = list.dup
|
|
1220
|
+
i = 0
|
|
1221
|
+
while i < subs.size
|
|
1222
|
+
sub = subs[i]
|
|
1223
|
+
i += 1
|
|
1224
|
+
next if sub.closed?
|
|
1225
|
+
got = sub.take
|
|
1226
|
+
j = 0
|
|
1227
|
+
while j < got.size
|
|
1228
|
+
break if sub.closed?
|
|
1229
|
+
tell_sample(sub.block, got[j][0], got[j][1])
|
|
1230
|
+
j += 1
|
|
1231
|
+
end
|
|
1232
|
+
end
|
|
1233
|
+
end
|
|
1234
|
+
|
|
1235
|
+
# One value for one block (CRuby routes what it raises to on_error).
|
|
1236
|
+
# @api private
|
|
1237
|
+
def tell_sample(blk, value, info)
|
|
1238
|
+
blk.call(value, info)
|
|
1239
|
+
end
|
|
1240
|
+
|
|
1241
|
+
# Forgets the topics and subscriptions of a connection that ends (its
|
|
1242
|
+
# session closes their Zenoh entities).
|
|
1243
|
+
# @api private
|
|
1244
|
+
def release_data
|
|
1245
|
+
(@topics || {}).each_value { |t| t.closed! }
|
|
1246
|
+
@topics = {}
|
|
1247
|
+
(@subscriptions || []).each { |sub| sub.closed! }
|
|
1248
|
+
@subscriptions = []
|
|
1249
|
+
@pubs_put = nil
|
|
1250
|
+
@pubs_watch = nil
|
|
1251
|
+
@senders = nil
|
|
1252
|
+
end
|
|
1253
|
+
end
|
|
1254
|
+
end
|