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