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.
@@ -1,6 +1,6 @@
1
- # The object layer (Asterism.connect / expose / []) with blocks and a
2
- # receiving thread (CRuby only). Asterism.connect without a block,
3
- # Asterism.poll and the rest of the portable API work as on the boards.
1
+ # The object layer (Asterism.connect / Connection) with blocks and a
2
+ # receiving thread (CRuby only). A connection without a block, polled with
3
+ # net.poll, works as on the boards.
4
4
  #
5
5
  # Asterism.connect("tcp/192.0.2.2:7447", node: "mypc", app: "demo") do |net|
6
6
  # net.expose("screen", Screen.new, methods: [:say])
@@ -11,196 +11,154 @@
11
11
  # net.run # answer calls until Ctrl-C
12
12
  # end
13
13
  #
14
- # Locking: the object layer is one connection per process with its state in
15
- # the Asterism module, written for one thread. Every entry into it (polling,
16
- # answering, calling, exposing, listing) holds Asterism::LOCK, so the
17
- # receiving thread and the application's threads take turns. A call that
18
- # waits for its answer while the receiving thread runs does not poll; it
19
- # waits for the receiving thread's ticks (Asterism::Runner#wait_until).
14
+ # Each connection has its own receiving thread (start / run). A program may
15
+ # hold several connections, each one application.
16
+ #
17
+ # Locking: the object layer is written for one thread. Every entry into a
18
+ # connection (polling, answering, calling, exposing, listing) holds
19
+ # Asterism::LOCK, one for all the connections of the process (a call
20
+ # waiting on one connection answers the calls of the others, so they share
21
+ # it), so the receiving threads and the application's threads take turns.
22
+ # A call that waits for its answer while its connection's receiving thread
23
+ # runs does not poll; it waits for that thread's ticks
24
+ # (Asterism::Runner#wait_until).
20
25
  require "monitor"
21
26
 
22
27
  module Asterism
23
- # Held by every entry into the object layer (reentrant).
28
+ # Held by every entry into the object layer (reentrant), for every
29
+ # connection of the process.
24
30
  LOCK = Monitor.new
25
31
 
26
- # The portable API, entered under LOCK. Prepended to the Asterism module's
27
- # singleton class, so the shared methods themselves are not changed.
28
- module Locked
29
- %i[expose unexpose poll nodes [] call_async meta connected? reconnecting? exposed on_join on_leave off_join off_leave
30
- on_app_join on_app_leave off_app_join off_app_leave profile profile_async status status_async apps session].each do |m|
32
+ # Module functions that read or change the connections, under LOCK.
33
+ module LockedModule
34
+ %i[connections poll_all profile_defaults register unregister connection_of pump_others claim unclaim
35
+ warn_text_once].each do |m|
31
36
  define_method(m) do |*args, **kw, &blk|
32
37
  ::Asterism::LOCK.synchronize { super(*args, **kw, &blk) }
33
38
  end
34
39
  end
35
40
 
36
- # With a block: yields an Asterism::Net and closes when the block ends
41
+ # With a block: yields the connection and closes it when the block ends
37
42
  # (also on an exception); returns the block's value. Without a block,
38
- # as before (returns Asterism).
39
- #
40
- # credentials: a .tar from an Asterism console (Asterism::Credentials):
41
- # its certificates go to zenoh (mutual TLS), and the locator and node:
42
- # default to its router and name (given ones win).
43
+ # returns the connection (as on the boards).
43
44
  def connect(locator = nil, node: nil, app:, mode: nil, listen: nil, config: nil, connect_timeout: nil,
44
45
  check_timeout: nil, credentials: nil, &blk)
45
- if credentials
46
- locator, config, creds = ::Asterism::Credentials.merge(credentials, locator, config)
47
- node ||= creds.name
48
- end
49
- ::Asterism::LOCK.synchronize do
50
- super(locator, node: node, app: app, mode: mode, listen: listen, config: config,
51
- connect_timeout: connect_timeout, check_timeout: check_timeout)
52
- # The Net of a connection that was lost (not closed) belongs to the
53
- # old session.
54
- old = @net
55
- @net = nil
56
- old&.release
57
- end
58
- return self unless blk
59
- net = ::Asterism.net
46
+ conn = super(locator, node: node, app: app, mode: mode, listen: listen, config: config,
47
+ connect_timeout: connect_timeout, check_timeout: check_timeout, credentials: credentials)
48
+ return conn unless blk
49
+ conn.runner # what the blocks raise goes to on_error from here on
60
50
  begin
61
- blk.call(net)
51
+ blk.call(conn)
62
52
  ensure
63
- net.close
53
+ conn.close
64
54
  end
65
55
  end
56
+ end
57
+ singleton_class.prepend(LockedModule)
66
58
 
67
- # Stops the receiving thread first (outside the lock: it may be inside
68
- # a tick), then closes.
69
- def close
70
- net = @net
71
- @net = nil
72
- net&.release
73
- ::Asterism::LOCK.synchronize { super }
74
- end
59
+ class Connection
60
+ # The portable API, entered under LOCK. Prepended, so the shared methods
61
+ # themselves are not changed.
62
+ module Locked
63
+ # initialize is not among them: Asterism.connect looks for another
64
+ # copy of the application on the network (up to check_timeout:)
65
+ # without the lock, so that the program's other connections go on
66
+ # meanwhile; attach (the declarations, joining the program) holds it.
67
+ %i[attach expose unexpose poll pump nodes [] call_async meta connected? reconnecting? exposed
68
+ on_join on_leave off_join off_leave on_app_join on_app_leave off_app_join off_app_leave
69
+ profile profile_async status app_profile_async apps session publish unpublish published
70
+ topic_put define_type subscribe unsubscribe pubs_get protocol_known protocol_store
71
+ sender_cached sender_store watch_pubs].each do |m|
72
+ define_method(m) do |*args, **kw, &blk|
73
+ ::Asterism::LOCK.synchronize { super(*args, **kw, &blk) }
74
+ end
75
+ end
75
76
 
76
- # The on_join / on_leave (and on_app_*) blocks (called from
77
- # Asterism.poll): with a Net, what they raise goes to its on_error and
78
- # the other blocks still run.
79
- def tell(where, blk, node, app = nil)
80
- net = @net
81
- return super unless net
82
- net.runner.guard(where) { super }
83
- end
77
+ # Stops the receiving thread first (outside the lock: it may be inside
78
+ # a tick), then closes.
79
+ def close
80
+ r = @runner
81
+ @runner = nil
82
+ @zenoh = nil
83
+ r&.close
84
+ ::Asterism::LOCK.synchronize { super }
85
+ end
84
86
 
85
- # Lists under the lock, yields outside it (the block may make calls).
86
- def each(pattern = "**", &blk)
87
- list = ::Asterism::LOCK.synchronize { super(pattern, &nil) }
88
- return list unless blk
89
- list.each(&blk)
90
- list.size
91
- end
87
+ # The on_join / on_leave (and on_app_*) blocks (called from poll): once
88
+ # the connection has its receiving side (runner: a connect with a
89
+ # block, start, run, on_error), what they raise goes to its on_error
90
+ # and the other blocks still run.
91
+ def tell(where, blk, node, app = nil)
92
+ r = @runner
93
+ return super unless r
94
+ r.guard(where) { super }
95
+ end
92
96
 
93
- # A call waiting for its answer. On the receiving thread, or when it
94
- # does not run: polls, as on the boards. On another thread while it
95
- # runs: waits for its ticks to bring the answer.
96
- def wait_until(&cond)
97
- loop do
98
- r = @net&.runner
99
- if r && r.running? && !r.current?
100
- r.wait_until(&cond)
101
- return if ::Asterism::LOCK.synchronize(&cond)
102
- # The receiving thread stopped before the answer: poll here.
103
- else
104
- return ::Asterism::LOCK.synchronize { super(&cond) }
105
- end
97
+ # A subscription's block (called from poll): the same.
98
+ def tell_sample(blk, value, info)
99
+ r = @runner
100
+ return super unless r
101
+ r.guard("subscribe") { super }
106
102
  end
107
- end
108
- end
109
- singleton_class.prepend(Locked)
110
103
 
111
- class Future
112
- # Prepended: done? reads the connection state, under the lock.
113
- module Locked
114
- def done?
115
- ::Asterism::LOCK.synchronize { super }
104
+ # Lists under the lock, yields outside it (the block may make calls).
105
+ def each(pattern = "**", &blk)
106
+ list = ::Asterism::LOCK.synchronize { super(pattern, &nil) }
107
+ return list unless blk
108
+ list.each(&blk)
109
+ list.size
116
110
  end
117
- end
118
- prepend Locked
119
- end
120
111
 
121
- # The connection of this process (Asterism is one per process) as an
122
- # object, with a receiving thread. nil when not connected.
123
- def self.net
124
- LOCK.synchronize do
125
- return nil unless connected?
126
- @net ||= Net.new
112
+ # A call waiting for its answer. On the receiving thread, or when it
113
+ # does not run: polls, as on the boards. On another thread while it
114
+ # runs: waits for its ticks to bring the answer.
115
+ def wait_until(&cond)
116
+ loop do
117
+ r = @runner
118
+ if r && r.running? && !r.current?
119
+ r.wait_until(&cond)
120
+ return if ::Asterism::LOCK.synchronize(&cond)
121
+ # The receiving thread stopped before the answer: poll here.
122
+ else
123
+ return ::Asterism::LOCK.synchronize { super(&cond) }
124
+ end
125
+ end
126
+ end
127
127
  end
128
- end
128
+ prepend Locked
129
129
 
130
- # The block API of the object layer. Made by Asterism.connect with a
131
- # block, or Asterism.net after a connect without one.
132
- class Net
133
- attr_reader :runner
130
+ # Enumerable over the exposed objects alive now, as proxies
131
+ # (net.map(&:asterism_path), net.count): each with the default pattern.
132
+ include Enumerable
134
133
 
135
- def initialize
136
- @runner = Runner.new(::Asterism.instance_variable_get(:@session), lock: ::Asterism::LOCK, name: "asterism")
137
- # One tick: Asterism.poll (answers the calls, tells on_join /
138
- # on_leave); false once the connection is gone.
139
- @runner.add { ::Asterism.poll ? 0 : false }
134
+ # The receiving side of this connection (made on first use): one tick
135
+ # is poll (answers the calls, tells on_join / on_leave, hands values to
136
+ # the subscriptions); false once the connection is gone. @api private
137
+ def runner
138
+ ::Asterism::LOCK.synchronize do
139
+ @runner ||= begin
140
+ r = Runner.new(@session, lock: ::Asterism::LOCK, name: "asterism #{@node_id}/#{@app}")
141
+ r.add { poll ? 0 : false }
142
+ r
143
+ end
144
+ end
140
145
  end
141
146
 
142
- # Asterism.session as a Zenoh::Connection (the block API of
147
+ # The session as a Zenoh::Connection (the block API of
143
148
  # Asterism::Zenoh.open) on the same receiving thread as the objects:
144
149
  # subscribe / queryable blocks run there, between the calls answered.
145
150
  # For a ROS 2 node on the same session:
146
- # Asterism::ROS::Connection.new(net.session).node("name"). The session
147
- # stays Asterism's: net.session.close does nothing (shared?), net.close
148
- # closes it. Disconnected when not connected.
149
- def session
151
+ # Asterism::ROS::Connection.new(net.zenoh).node("name"). The session
152
+ # stays the connection's: net.zenoh.close does nothing (shared?),
153
+ # net.close closes it. Disconnected when not connected.
154
+ def zenoh
150
155
  ::Asterism::LOCK.synchronize do
151
- s = ::Asterism.session
152
- @session = nil if @session && !@session.session.equal?(s)
153
- @session ||= ::Asterism::Zenoh::Connection.new(s, runner: @runner)
156
+ s = session
157
+ @zenoh = nil if @zenoh && !@zenoh.session.equal?(s)
158
+ @zenoh ||= ::Asterism::Zenoh::Connection.new(s, runner: runner)
154
159
  end
155
160
  end
156
161
 
157
- def node_id = ::Asterism.node_id
158
- def app = ::Asterism.app
159
- def connected? = ::Asterism.connected?
160
- def reconnecting? = ::Asterism.reconnecting?
161
- def lost_reason = ::Asterism.lost_reason
162
- def nodes = ::Asterism.nodes
163
- def exposed = ::Asterism.exposed
164
-
165
- # Asterism.expose: methods: an Array of names or a Hash name => number
166
- # of arguments (or => {args: [names], doc: "text"}); doc: the object's
167
- # own text (both go to the meta); caller: :from passes the calling node's ID as that
168
- # keyword. The object's methods run on the receiving thread (or in
169
- # run, or in whatever polls).
170
- def expose(name, obj, methods:, caller: nil, doc: nil)
171
- ::Asterism.expose(name, obj, methods: methods, caller: caller, doc: doc)
172
- end
173
-
174
- # Asterism.current_call: the call being answered on this thread, or nil.
175
- def current_call = ::Asterism.current_call
176
-
177
- def unexpose(name)
178
- ::Asterism.unexpose(name)
179
- end
180
-
181
- # Asterism.profile: this node's profile (with keywords, sets fields).
182
- def profile(**fields)
183
- ::Asterism.profile(**fields)
184
- end
185
-
186
- # Asterism.profile_of: another node's profile, or nil. Waits like a
187
- # call (for the receiving thread's ticks while it runs).
188
- def profile_of(node, icon: true, timeout: nil, timeout_ms: nil)
189
- ::Asterism.profile_of(node, icon: icon, timeout: timeout, timeout_ms: timeout_ms)
190
- end
191
-
192
- # Asterism.status: this application's status (a Hash: the whole
193
- # status; keywords: those fields). Without arguments, returns it.
194
- def status(h = nil, **fields)
195
- ::Asterism.status(h, **fields)
196
- end
197
-
198
- # Asterism.status_of: another application's status, or nil. Waits like
199
- # a call (for the receiving thread's ticks while it runs).
200
- def status_of(node, app, timeout: nil, timeout_ms: nil)
201
- ::Asterism.status_of(node, app, timeout: timeout, timeout_ms: timeout_ms)
202
- end
203
-
204
162
  # on_status(pattern = "*/*") { |node, app, status| }: the statuses put
205
163
  # by the applications whose "<node>/<app>" matches the pattern (Zenoh
206
164
  # wildcards: * one chunk, ** several), as they change, on the receiving
@@ -208,125 +166,58 @@ module Asterism
208
166
  # Returns the subscription (close it to stop).
209
167
  def on_status(pattern = "*/*", &blk)
210
168
  raise ArgumentError, "on_status needs a block" unless blk
211
- session.subscribe("#{::Asterism::ROOT}/#{pattern}/#{::Asterism::Status::KEY}") do |sample|
169
+ zenoh.subscribe("#{::Asterism::ROOT}/#{pattern}/#{::Asterism::Status::KEY}") do |sample|
212
170
  got = ::Asterism::Status.parse(sample.key, sample.payload)
213
171
  blk.call(*got) if got
214
172
  end
215
173
  end
216
174
 
217
- # Asterism.apps: the applications alive now, "<node>/<app>".
218
- def apps(pattern = "*/*") = ::Asterism.apps(pattern)
219
-
220
- # A proxy for <node>/<app>/<object>; timeout: in seconds (or
221
- # timeout_ms:).
222
- def [](path, timeout: nil, timeout_ms: nil)
223
- ::Asterism[path, timeout: timeout, timeout_ms: timeout_ms]
224
- end
225
- alias proxy []
226
-
227
- # The exposed objects alive now matching the pattern, as proxies. With
228
- # a block, yields each and returns their number (1.0: returns self, as
229
- # Ruby's each does; net.count is the number); without, an Enumerator
230
- # (net.each("*/demo/info").map(&:status)).
231
- def each(pattern = "**", &blk)
232
- return enum_for(:each, pattern) unless blk
233
- ::Asterism.each(pattern, &blk)
234
- end
235
- include Enumerable
236
-
237
- # on_join { |node| } / on_leave { |node| }: Asterism.on_join /
238
- # on_leave, the same as on the boards (called from Asterism.poll), here
239
- # on the receiving thread. What they raise goes to on_error.
240
- def on_join(&blk)
241
- ::Asterism.on_join(&blk)
242
- self
243
- end
244
-
245
- def on_leave(&blk)
246
- ::Asterism.on_leave(&blk)
247
- self
248
- end
249
-
250
- # on_app_join { |node, app| } / on_app_leave { |node, app| }: an
251
- # application appeared on the network / is gone (Asterism.on_app_join),
252
- # on the receiving thread. What they raise goes to on_error.
253
- def on_app_join(&blk)
254
- ::Asterism.on_app_join(&blk)
255
- self
256
- end
257
-
258
- def on_app_leave(&blk)
259
- ::Asterism.on_app_leave(&blk)
260
- self
261
- end
262
-
263
- def off_app_join(blk)
264
- ::Asterism.off_app_join(blk)
265
- self
266
- end
267
-
268
- def off_app_leave(blk)
269
- ::Asterism.off_app_leave(blk)
270
- self
271
- end
272
-
273
- # Removes a block given to on_join / on_leave (keep the Proc).
274
- def off_join(blk)
275
- ::Asterism.off_join(blk)
276
- self
277
- end
278
-
279
- def off_leave(blk)
280
- ::Asterism.off_leave(blk)
281
- self
282
- end
283
-
284
- # on_error { |error, where| }: what on_join / on_leave blocks raise.
285
- # (An exposed method that raises is answered to its caller as a
286
- # RemoteError, as before; it does not come here.)
175
+ # on_error { |error, where| }: what the on_join / on_leave and
176
+ # subscription blocks raise. (An exposed method that raises is answered
177
+ # to its caller as a RemoteError; it does not come here.)
287
178
  def on_error(&blk)
288
- @runner.on_error(&blk)
179
+ runner.on_error(&blk)
289
180
  self
290
181
  end
291
182
 
292
183
  # Answers calls on a thread of its own. Returns self.
293
184
  def start
294
- @runner.start
185
+ runner.start
295
186
  self
296
187
  end
297
188
 
298
189
  def stop
299
- @runner.stop
190
+ @runner&.stop
300
191
  self
301
192
  end
302
193
 
303
194
  # Answers calls on this thread until stop, the connection closing, or
304
195
  # Ctrl-C. Returns nil.
305
196
  def run
306
- @runner.run
197
+ runner.run
307
198
  end
308
199
 
309
200
  def running?
310
- @runner.running?
311
- end
312
-
313
- def poll
314
- ::Asterism.poll
315
- end
316
-
317
- # Stops the receiving thread and closes the connection (Asterism.close).
318
- def close
319
- ::Asterism.close
320
- nil
201
+ r = @runner
202
+ r ? r.running? : false
321
203
  end
322
204
 
323
- # For Asterism.close: stop receiving and forget the session.
324
- def release
325
- @runner.close
205
+ def inspect
206
+ state = if @session then running? ? " running" : ""
207
+ elsif @lost then " lost"
208
+ else " closed"
209
+ end
210
+ "#<Asterism::Connection #{@node_id}/#{@app}#{state}>"
326
211
  end
212
+ end
327
213
 
328
- def inspect
329
- "#<Asterism::Net #{node_id}/#{app}#{running? ? ' running' : ''}>"
214
+ class Future
215
+ # Prepended: done? reads the connection state, under the lock.
216
+ module Locked
217
+ def done?
218
+ ::Asterism::LOCK.synchronize { super }
219
+ end
330
220
  end
221
+ prepend Locked
331
222
  end
332
223
  end
@@ -96,7 +96,7 @@ module Asterism
96
96
  end
97
97
 
98
98
  # Stops spinning, withdraws the nodes and closes the session. On
99
- # Asterism's session (ROS::Connection.new(net.session)) it only
99
+ # an Asterism connection's session (ROS::Connection.new(net.zenoh)) it only
100
100
  # withdraws the nodes: the receiving thread and the session stay the
101
101
  # object layer's.
102
102
  def close
@@ -36,6 +36,7 @@ module Asterism
36
36
  end
37
37
 
38
38
  def register(session, runner)
39
+ return if session.nil?
39
40
  @registry_lock.synchronize { @registry[session] = runner }
40
41
  end
41
42
 
@@ -48,7 +49,7 @@ module Asterism
48
49
 
49
50
  # session: the Asterism::Zenoh::Session the tasks read from. lock: the
50
51
  # Monitor guarding the layer the tasks call into (the object layer has a
51
- # process-wide one). on_closed is called once, on the ticking thread,
52
+ # process-wide one for all its connections). on_closed is called once, on the ticking thread,
52
53
  # when the session is found closed.
53
54
  def initialize(session, lock: Monitor.new, interval: DEFAULT_INTERVAL, name: "asterism")
54
55
  @session = session
@@ -164,7 +164,7 @@ module Asterism
164
164
  attr_reader :session, :runner
165
165
 
166
166
  # runner: an existing Runner to receive on instead of a new one
167
- # (Asterism::Net#session gives the object layer's: the session and
167
+ # (Asterism::Connection#zenoh gives the object layer's: the session and
168
168
  # its receiving thread are Asterism's, see shared?). @api private
169
169
  def initialize(session, interval: Runner::DEFAULT_INTERVAL, runner: nil)
170
170
  @session = session
@@ -179,10 +179,10 @@ module Asterism
179
179
  end
180
180
  end
181
181
 
182
- # True for the Connection of Asterism's own session (net.session):
183
- # its close does nothing, start / stop / run are the object layer's
184
- # receiving thread (net.start / net.stop / net.run), and
185
- # Asterism.close (or the end of the connect block) closes the session
182
+ # True for the Connection of an Asterism connection's session
183
+ # (net.zenoh): its close does nothing, start / stop / run are that
184
+ # connection's receiving thread (net.start / net.stop / net.run), and
185
+ # net.close (or the end of the connect block) closes the session
186
186
  # with everything declared on it.
187
187
  def shared?
188
188
  @shared
@@ -399,12 +399,12 @@ module Asterism
399
399
  def declare_keyexpr(key) = @session.declare_keyexpr(key)
400
400
 
401
401
  # Stops receiving and closes the session (with everything declared on
402
- # it). Idempotent. On a shared? Connection (net.session) it does
402
+ # it). Idempotent. On a shared? Connection (net.zenoh) it does
403
403
  # nothing: close what you declared on it (subscriptions, publishers,
404
- # ROS nodes) one by one, or let Asterism.close close them.
404
+ # ROS nodes) one by one, or let net.close close them.
405
405
  def close
406
406
  if @shared
407
- ::Asterism.warn_once(self, "asterism: net.session belongs to Asterism; its close is ignored " \
407
+ ::Asterism.warn_once(self, "asterism: net.zenoh belongs to its Asterism connection; its close is ignored " \
408
408
  "(net.close closes it)")
409
409
  return nil
410
410
  end
@@ -66,7 +66,8 @@ module Asterism
66
66
  end
67
67
 
68
68
  # The profile's defaults on CRuby (mrblib/profile.rb): kind "cruby" and
69
- # the icon bundled with the gem (replace it with Asterism.profile(icon:)).
69
+ # the icon bundled with the gem (replace it with net.profile(icon:) or
70
+ # Asterism.profile_defaults(icon:)).
70
71
  ICON_FILE = File.expand_path("../../data/icon.png", __dir__)
71
72
 
72
73
  def self.default_profile_kind
@@ -103,10 +104,13 @@ module Asterism
103
104
  end
104
105
  end
105
106
 
106
- # The calls being answered, per thread: an exposed method runs on the
107
- # thread that answers (the receiving thread, or whatever polls), and
108
- # Asterism.current_call on another thread is not inside that call.
109
- def self.call_stack
110
- Thread.current[:asterism_call_stack] ||= []
107
+ class Connection
108
+ # The calls being answered, per thread: an exposed method runs on the
109
+ # thread that answers (the receiving thread, or whatever polls), and
110
+ # current_call on another thread is not inside that call.
111
+ def call_stack
112
+ stacks = (Thread.current[:asterism_call_stacks] ||= {}.compare_by_identity)
113
+ stacks[self] ||= []
114
+ end
111
115
  end
112
116
  end
@@ -1,17 +1,23 @@
1
1
  # Asterism::Transfer::Endpoint (CRuby): an Inbox answering at
2
2
  # <node>/<app>/<object> (default <node>/transfer/inbox, where a Family
3
- # mruby machine's transfer app answers) on a Zenoh connection of the
4
- # program's own, beside the application Asterism.connect made.
3
+ # mruby machine's transfer app answers) on a plain Zenoh connection.
5
4
  #
6
- # Asterism's object layer is one application per process; a program whose
7
- # application has another name (a console, app "console") still wants to
8
- # receive where the machines send (`asend <node> <file>` sends to
9
- # <node>/transfer/inbox). The endpoint declares that application's and that
10
- # object's liveliness tokens and answers its calls and its meta with the
11
- # object layer's encoding (docs/protocol.md), the caller's node ID from the
12
- # key passed as from: (caller: :from). Nothing else is exposed there.
5
+ # A program whose application has another name (a console, app "console")
6
+ # still wants to receive where the machines send (`asend <node> <file>`
7
+ # sends to <node>/transfer/inbox). A second Asterism connection does that
8
+ # with the object layer itself:
13
9
  #
14
- # conn = Asterism::Zenoh.open("tcp/127.0.0.1:7447") # or Asterism.net.session
10
+ # rx = Asterism.connect(router, node: "console", app: "transfer")
11
+ # rx.expose("inbox", inbox, methods: Asterism::Transfer::Inbox::METHODS, caller: :from)
12
+ #
13
+ # The endpoint is the same on a Zenoh connection that is there already
14
+ # (one without an Asterism application of its own): it declares that
15
+ # application's and that object's liveliness tokens and answers its calls
16
+ # and its meta with the object layer's encoding (docs/protocol.md), the
17
+ # caller's node ID from the key passed as from: (caller: :from). Nothing
18
+ # else is exposed there.
19
+ #
20
+ # conn = Asterism::Zenoh.open("tcp/127.0.0.1:7447") # or net.zenoh
15
21
  # ep = Asterism::Transfer::Endpoint.new(conn, inbox, node: "console").start
16
22
  # conn.start # its receiving thread answers
17
23
  # loop { ep.tick; sleep 0.5 }
@@ -47,7 +53,9 @@ module Asterism
47
53
  end
48
54
 
49
55
  def start
50
- @tokens << @conn.liveliness("#{::Asterism::ROOT}/#{@node}/#{@app}")
56
+ # The application's token says the protocol (docs/protocol.md 6.1),
57
+ # as Asterism.connect's does.
58
+ @tokens << @conn.liveliness(::Asterism.app_token_key(@node, @app))
51
59
  @tokens << @conn.liveliness("#{::Asterism::ROOT}/#{@path}")
52
60
  @queryable = @conn.queryable("#{::Asterism::ROOT}/#{@path}/**", depth: 32) { |q| answer(q) }
53
61
  self
@@ -97,7 +105,16 @@ module Asterism
97
105
 
98
106
  # The packed reply to a call from the node from.
99
107
  def dispatch(payload, from)
100
- req = ::Asterism.parse_request(::Asterism::Codec.unpack(payload))
108
+ raw = ::Asterism::Codec.unpack(payload)
109
+ # The protocol first, as the object layer's callee does (docs/protocol.md 4.3 step 3).
110
+ if raw.is_a?(Hash) && !(raw["protocol"].is_a?(Integer) && raw["protocol"] == ::Asterism::PROTOCOL)
111
+ pr = raw["protocol"]
112
+ said = pr.is_a?(Integer) ? "protocol #{pr}" : "no protocol (protocol 1 or older)"
113
+ return ::Asterism.error_reply("protocol", "Asterism::Error",
114
+ "#{@node}/#{@app} speaks Asterism protocol #{::Asterism::PROTOCOL}, " \
115
+ "the call says #{said}")
116
+ end
117
+ req = ::Asterism.parse_request(raw)
101
118
  return ::Asterism.error_reply("malformed", "ArgumentError", ::Asterism::MALFORMED) unless req
102
119
  name, args, kw = req
103
120
  arity = Inbox::METHODS[name]
@@ -1,7 +1,7 @@
1
1
  module Asterism
2
2
  # The gem's version (the CRuby gem asterism). The Zenoh binding it needs
3
3
  # is in asterism.gemspec (asterism-zenoh ~> 0.4.0 for 0.5.0 to 0.7.0:
4
- # the bindings did not change; see CHANGELOG.md). 0.7.0 speaks wire
5
- # protocol 1 only (Asterism::PROTOCOL, docs/protocol.md).
6
- VERSION = "0.12.0"
4
+ # the bindings did not change; see CHANGELOG.md). It speaks wire
5
+ # protocol 2 only (Asterism::PROTOCOL, docs/protocol.md).
6
+ VERSION = "0.14.0"
7
7
  end