asterism 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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.13.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
data/lib/asterism.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # Asterism for CRuby: Ruby objects on other machines called like local ones
2
- # (Asterism.connect / expose / [] / poll), and a ROS 2 node over rmw_zenoh's
3
- # wire format (Asterism::ROS, Asterism::CDR, generated message types).
2
+ # (Asterism.connect, then expose / [] / poll on the connection), and a ROS 2
3
+ # node over rmw_zenoh's wire format (Asterism::ROS, Asterism::CDR, generated
4
+ # message types).
4
5
  #
5
6
  # The Ruby code is the one the mruby / PicoRuby boards run: mrblib/ of this
6
7
  # repository, which is also the mrbgem (mrbgem.rake). Nothing is copied:
@@ -12,7 +13,7 @@ require "asterism/zenoh"
12
13
  require_relative "asterism/version"
13
14
 
14
15
  # The same order as an mrbgem's mrblib (alphabetical).
15
- %w[asterism cdr credentials future profile proxy ros status].each do |f|
16
+ %w[asterism cdr credentials future profile proxy ros status topic].each do |f|
16
17
  require_relative "../mrblib/#{f}"
17
18
  end
18
19
  require_relative "asterism/cruby"