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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +92 -0
- data/README.md +416 -183
- data/data/transfer/transfer.rb +7 -5
- data/lib/asterism/cli/commands.rb +10 -2
- data/lib/asterism/cli/complete.rb +2 -1
- data/lib/asterism/cli/mcp.rb +90 -24
- data/lib/asterism/cli/nouns.rb +175 -22
- data/lib/asterism/cli/survey.rb +84 -19
- data/lib/asterism/cli/values.rb +249 -35
- data/lib/asterism/cli.rb +71 -22
- 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 +2 -1
data/lib/asterism/cli/survey.rb
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
# What `asterism nodes / objects / tree` and the MCP bridge read from the
|
|
2
|
-
# network, on a connection that is already open (Asterism
|
|
2
|
+
# network, on a connection that is already open (an Asterism::Connection): the
|
|
3
3
|
# applications and objects from the liveliness tokens, then the meta of
|
|
4
|
-
# the objects, the nodes' profiles and the applications'
|
|
5
|
-
# (docs/protocol.md 5, 7, 8
|
|
4
|
+
# the objects, the nodes' profiles and the applications' own profiles
|
|
5
|
+
# (their statuses, topics and types: docs/protocol.md 5, 7, 8, 10, 11)
|
|
6
|
+
# with one get for each node or application.
|
|
6
7
|
#
|
|
7
|
-
# Not one wildcard get for all (asterism/*/*/@
|
|
8
|
+
# Not one wildcard get for all (asterism/*/*/@profile): behind a router's
|
|
8
9
|
# access control a query must fit the rules of every node it would reach
|
|
9
|
-
# (a game host may answer asterism/games/*/@
|
|
10
|
+
# (a game host may answer asterism/games/*/@profile, not asterism/*/*/@profile),
|
|
10
11
|
# so a wildcard over the nodes is refused where the plain keys pass. The
|
|
11
12
|
# gets go out at once and are read as their replies come (zenoh-c fills
|
|
12
13
|
# them on its own threads), so a listing costs one time limit at most,
|
|
@@ -23,10 +24,12 @@ module Asterism
|
|
|
23
24
|
|
|
24
25
|
attr_reader :timeout
|
|
25
26
|
|
|
26
|
-
#
|
|
27
|
-
|
|
27
|
+
# net: the connection to read on (an Asterism::Connection). own: this
|
|
28
|
+
# connection's "<node>/<app>", left out of the listings.
|
|
29
|
+
def initialize(net, timeout: 2.0, own: nil)
|
|
30
|
+
@net = net
|
|
28
31
|
@timeout = timeout
|
|
29
|
-
@own = own ||
|
|
32
|
+
@own = own || net.path
|
|
30
33
|
@object_docs = {}
|
|
31
34
|
end
|
|
32
35
|
|
|
@@ -38,7 +41,7 @@ module Asterism
|
|
|
38
41
|
last = nil
|
|
39
42
|
since = now
|
|
40
43
|
loop do
|
|
41
|
-
cur = [
|
|
44
|
+
cur = [@net.apps, @net.each("**").size]
|
|
42
45
|
if cur != last
|
|
43
46
|
last = cur
|
|
44
47
|
since = now
|
|
@@ -50,7 +53,7 @@ module Asterism
|
|
|
50
53
|
|
|
51
54
|
# The applications alive, "<node>/<app>", sorted (this one left out).
|
|
52
55
|
def apps(pattern = "*/*")
|
|
53
|
-
(
|
|
56
|
+
(@net.apps(pattern) - [@own]).sort
|
|
54
57
|
end
|
|
55
58
|
|
|
56
59
|
# The nodes that have an application or an object alive (sorted).
|
|
@@ -61,7 +64,7 @@ module Asterism
|
|
|
61
64
|
# The objects alive, "<node>/<app>/<object>", sorted (this
|
|
62
65
|
# application's own left out).
|
|
63
66
|
def objects(pattern = "**")
|
|
64
|
-
|
|
67
|
+
@net.each(pattern).map(&:asterism_path).reject { _1.start_with?("#{@own}/") }.sort
|
|
65
68
|
end
|
|
66
69
|
|
|
67
70
|
# {path => [{"name", "arity"}, ...]} for the paths given (an object
|
|
@@ -102,19 +105,74 @@ module Asterism
|
|
|
102
105
|
end
|
|
103
106
|
|
|
104
107
|
# {"<node>/<app>" => status} of the applications that answered (one
|
|
105
|
-
# get per application, all at once).
|
|
108
|
+
# get per application on its own profile, ?status, all at once).
|
|
106
109
|
def statuses(list = apps)
|
|
110
|
+
app_profiles(list, "status").filter_map do |path, raw|
|
|
111
|
+
st = ::Asterism::Status.check(raw["status"])
|
|
112
|
+
[path, st] if st
|
|
113
|
+
end.to_h
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# {"<node>/<app>" => {"topics" => [...], "types" => {...}}} of the
|
|
117
|
+
# applications that answered (one get per application on its own
|
|
118
|
+
# profile, ?pubs;types): the topics as Survey.topic_entry gives them,
|
|
119
|
+
# sorted by name, and the application's own types
|
|
120
|
+
# (Asterism::Types.check). An application without either is there
|
|
121
|
+
# with both empty.
|
|
122
|
+
def topics(list = apps)
|
|
123
|
+
app_profiles(list, "pubs;types").to_h do |path, raw|
|
|
124
|
+
[path, { "topics" => Survey.topic_list(::Asterism::Pubs.check(raw["pubs"]) || {}),
|
|
125
|
+
"types" => ::Asterism::Types.check(raw["types"]) || {} }]
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# {"<node>/<app>" => the answer (a Hash)} of the applications whose
|
|
130
|
+
# profile (asterism/<node>/<app>/@profile, docs/protocol.md 8)
|
|
131
|
+
# answered, asked with params (nil: the status and the topics).
|
|
132
|
+
def app_profiles(list, params)
|
|
107
133
|
out = {}
|
|
108
|
-
gets(list.map { "asterism/#{_1}/@
|
|
109
|
-
|
|
134
|
+
gets(list.map { "asterism/#{_1}/@profile" }, params: params).each do |key, replies|
|
|
135
|
+
raw = replies.first && ::Asterism::Codec.unpack(replies.first[1])
|
|
110
136
|
parts = key.split("/")
|
|
111
|
-
out["#{parts[1]}/#{parts[2]}"] =
|
|
137
|
+
out["#{parts[1]}/#{parts[2]}"] = raw if raw.is_a?(Hash)
|
|
112
138
|
end
|
|
113
139
|
out
|
|
114
140
|
end
|
|
115
141
|
|
|
142
|
+
# A topic's declaration (docs/protocol.md 10.1, cleaned) as the
|
|
143
|
+
# command line and the MCP bridge show it: {"name", "type" (a built-in
|
|
144
|
+
# type's name, or the application type's; absent: no type),
|
|
145
|
+
# "encoding" (absent: MessagePack), "unit", "desc", "ts" (true: the
|
|
146
|
+
# sender's time comes with each value)}. A shared topic's name is its
|
|
147
|
+
# entry ("<node>/@shared/<name>", "@shared/<name>") and "shared" says
|
|
148
|
+
# its place ("node", "global"; docs/protocol.md 17).
|
|
149
|
+
def self.topic_entry(name, decl)
|
|
150
|
+
h = { "name" => name }
|
|
151
|
+
pl = ::Asterism::Pubs.place(name)
|
|
152
|
+
h["shared"] = pl[0].to_s if pl && pl[0]
|
|
153
|
+
t = decl["t"]
|
|
154
|
+
if t.is_a?(Integer)
|
|
155
|
+
h["type"] = ::Asterism::Types::NAMES[t - 1] || t
|
|
156
|
+
elsif t
|
|
157
|
+
h["type"] = t
|
|
158
|
+
end
|
|
159
|
+
if decl["e"]
|
|
160
|
+
h["encoding"] = ::Asterism::Pubs.encoding_name(decl["e"]) || decl["e"]
|
|
161
|
+
end
|
|
162
|
+
h["unit"] = decl["u"] if decl["u"]
|
|
163
|
+
h["desc"] = decl["d"] if decl["d"]
|
|
164
|
+
h["ts"] = true if decl["ts"]
|
|
165
|
+
h
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# The declarations ({name => declaration}) as topic entries, sorted.
|
|
169
|
+
def self.topic_list(pubs)
|
|
170
|
+
pubs.sort.map { |name, d| topic_entry(name, d) }
|
|
171
|
+
end
|
|
172
|
+
|
|
116
173
|
# The tree: [{"node", "profile", "apps" => [{"app", "status",
|
|
117
|
-
# "objects" => [{"name", "methods"}]}]}]
|
|
174
|
+
# "topics" => [topic entries], "objects" => [{"name", "methods"}]}]}]
|
|
175
|
+
# ("status" and "topics" only when the application has them). meta: false leaves the
|
|
118
176
|
# methods out (no meta gets); node: (and app:) narrow it to one node
|
|
119
177
|
# (one application).
|
|
120
178
|
def tree(meta: true, node: nil, app: nil)
|
|
@@ -124,7 +182,7 @@ module Asterism
|
|
|
124
182
|
threads = []
|
|
125
183
|
node_list = (app_list.map { _1.split("/")[0] } + objs.map { _1.split("/")[0] }).uniq
|
|
126
184
|
threads << Thread.new { profiles(node_list) }
|
|
127
|
-
threads << Thread.new {
|
|
185
|
+
threads << Thread.new { app_profiles(app_list, nil) }
|
|
128
186
|
threads << Thread.new { meta ? metas(objs) : {} }
|
|
129
187
|
prof, stat, met = threads.map(&:value)
|
|
130
188
|
by_node = Hash.new { |h, k| h[k] = {} }
|
|
@@ -136,7 +194,14 @@ module Asterism
|
|
|
136
194
|
by_node.keys.sort.map do |n|
|
|
137
195
|
apps = by_node[n].keys.sort.map do |ap|
|
|
138
196
|
h = { "app" => ap }
|
|
139
|
-
|
|
197
|
+
# Another protocol than this program's: said (its token's number).
|
|
198
|
+
v = @net.protocol_of("#{n}/#{ap}")
|
|
199
|
+
h["protocol"] = v if v && v != ::Asterism::PROTOCOL
|
|
200
|
+
pr = stat["#{n}/#{ap}"]
|
|
201
|
+
st = pr && ::Asterism::Status.check(pr["status"])
|
|
202
|
+
h["status"] = st if st
|
|
203
|
+
pubs = pr && ::Asterism::Pubs.check(pr["pubs"])
|
|
204
|
+
h["topics"] = Survey.topic_list(pubs) if pubs && !pubs.empty?
|
|
140
205
|
h["objects"] = by_node[n][ap].sort.map do |name|
|
|
141
206
|
e = { "name" => name }
|
|
142
207
|
e["methods"] = met["#{n}/#{ap}/#{name}"] if met.key?("#{n}/#{ap}/#{name}")
|
|
@@ -156,7 +221,7 @@ module Asterism
|
|
|
156
221
|
# Sends a get on each key at once and waits for all of them (at most
|
|
157
222
|
# the time limit): {key => [[reply key, payload], ...]}.
|
|
158
223
|
def gets(keys, params: nil)
|
|
159
|
-
s =
|
|
224
|
+
s = @net.session
|
|
160
225
|
ms = (@timeout * 1000).round
|
|
161
226
|
pending = keys.to_h { |k| [k, s.get(k, timeout_ms: ms, params: params)] }
|
|
162
227
|
out = keys.to_h { [_1, []] }
|
data/lib/asterism/cli/values.rb
CHANGED
|
@@ -1,30 +1,55 @@
|
|
|
1
|
-
# The values the applications put on their own keys
|
|
2
|
-
#
|
|
3
|
-
#
|
|
1
|
+
# The values the applications put on their own keys (their topics,
|
|
2
|
+
# asterism/<node>/<app>/<name>, docs/protocol.md 9: a game table's state, a
|
|
3
|
+
# log, a camera's pictures), for `asterism object get / echo` and the MCP
|
|
4
4
|
# bridge's asterism_values. A put is not kept anywhere and a get has no
|
|
5
5
|
# answer for it (an application's queryable answers only the key shapes of
|
|
6
6
|
# 4.3), so the latest value is only what a subscriber received and kept:
|
|
7
7
|
# Values::Store keeps it, a key at a time, within limits.
|
|
8
|
+
#
|
|
9
|
+
# Each value is read with what came along (9.2, 9.3): its encoding (none:
|
|
10
|
+
# MessagePack; else the bytes as they are, image/jpeg) and the attachment
|
|
11
|
+
# [seq, run id] or [seq, run id, sender's ms], from which Values::Seq counts
|
|
12
|
+
# the values lost between two of a key (Asterism::Sequences: a new run id
|
|
13
|
+
# is a sender that connected again). This reads the Zenoh samples itself rather
|
|
14
|
+
# than through Connection#subscribe: it shows what is on the wire, the
|
|
15
|
+
# payload's size and a value that is not MessagePack too (as text or its
|
|
16
|
+
# size), where Connection#subscribe leaves those out.
|
|
17
|
+
#
|
|
18
|
+
# The shared places (docs/protocol.md 17: asterism/<node>/@shared/<name>
|
|
19
|
+
# and asterism/@shared/<name>) are read too: a key there has no owner
|
|
20
|
+
# application, several applications may put on it, and each value names
|
|
21
|
+
# its sender ("<node>/<app>", the attachment's fourth element). A shared
|
|
22
|
+
# value is shown with its sender ("from"), its numbers are counted per
|
|
23
|
+
# key and sender, and Store keeps the latest value of each sender. As in
|
|
24
|
+
# Zenoh, * and ** never match a part that starts with @: a pattern names
|
|
25
|
+
# @shared itself (games/@shared/**, */@shared/spectate/**, @shared/**).
|
|
8
26
|
require "json"
|
|
9
27
|
|
|
10
28
|
module Asterism
|
|
11
29
|
class CLI
|
|
12
30
|
module Values
|
|
13
31
|
PREFIX = "asterism/"
|
|
32
|
+
# The part of a shared place's keys (docs/protocol.md 17).
|
|
33
|
+
SHARED = "@shared"
|
|
14
34
|
# Characters a part of a key cannot have (2.1); * and ** are whole
|
|
15
35
|
# parts.
|
|
16
36
|
BAD = /[*$?#]/
|
|
17
37
|
|
|
18
38
|
module_function
|
|
19
39
|
|
|
20
|
-
# The pattern (node/app/object/more, * and ** as whole parts
|
|
40
|
+
# The pattern (node/app/object/more, * and ** as whole parts; or a
|
|
41
|
+
# shared place's node/@shared/name..., @shared/name...) checked;
|
|
21
42
|
# ArgumentError otherwise.
|
|
22
43
|
def check_pattern!(pattern)
|
|
23
44
|
parts = pattern.to_s.split("/", -1)
|
|
24
|
-
bad = parts.empty? || parts.any? do |p|
|
|
45
|
+
bad = parts.empty? || parts.each_with_index.any? do |p, i|
|
|
46
|
+
next false if p == SHARED && (i.zero? || (i == 1 && parts[0] != SHARED))
|
|
25
47
|
p.empty? || p.start_with?("@") || (p.match?(BAD) && !%w[* **].include?(p))
|
|
26
48
|
end
|
|
27
|
-
|
|
49
|
+
if bad
|
|
50
|
+
raise ArgumentError, "#{pattern.inspect}: a key's parts are names, * (one part) or ** (any parts); " \
|
|
51
|
+
"a shared place's keys are node/@shared/... or @shared/..."
|
|
52
|
+
end
|
|
28
53
|
pattern
|
|
29
54
|
end
|
|
30
55
|
|
|
@@ -33,15 +58,89 @@ module Asterism
|
|
|
33
58
|
"#{PREFIX}#{pattern}"
|
|
34
59
|
end
|
|
35
60
|
|
|
61
|
+
# Whether key (without asterism/) matches pattern: * one part, ** any
|
|
62
|
+
# parts, neither a part that starts with @ (verbatim, as Zenoh's).
|
|
36
63
|
def match?(pattern, key)
|
|
37
|
-
|
|
64
|
+
glob_parts?(pattern.split("/"), 0, key.split("/"), 0)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def glob_parts?(pat, i, parts, j)
|
|
68
|
+
while i < pat.size
|
|
69
|
+
if pat[i] == "**"
|
|
70
|
+
k = j
|
|
71
|
+
while k <= parts.size
|
|
72
|
+
return true if glob_parts?(pat, i + 1, parts, k)
|
|
73
|
+
break if k < parts.size && parts[k].start_with?("@")
|
|
74
|
+
k += 1
|
|
75
|
+
end
|
|
76
|
+
return false
|
|
77
|
+
end
|
|
78
|
+
return false if j >= parts.size
|
|
79
|
+
ok = pat[i] == parts[j] || (pat[i] == "*" && !parts[j].start_with?("@"))
|
|
80
|
+
return false unless ok
|
|
81
|
+
i += 1
|
|
82
|
+
j += 1
|
|
83
|
+
end
|
|
84
|
+
j == parts.size
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Whether a key (without asterism/) is in a shared place: @shared its
|
|
88
|
+
# first part (the network's) or its second (a node's).
|
|
89
|
+
def shared?(key)
|
|
90
|
+
parts = key.split("/", 3)
|
|
91
|
+
parts[0] == SHARED || parts[1] == SHARED
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# The sender "<node>/<app>" of a value on key, from its attachment
|
|
95
|
+
# (the fourth element, docs/protocol.md 17.3); nil when key is not a
|
|
96
|
+
# shared one (an application's own keys do not read it), or there is
|
|
97
|
+
# no such sender (on a node's place, one of another node is none).
|
|
98
|
+
def sender(key, att)
|
|
99
|
+
got = read(key, att)
|
|
100
|
+
got ? got[3] : nil
|
|
38
101
|
end
|
|
39
102
|
|
|
40
|
-
# [
|
|
41
|
-
#
|
|
42
|
-
|
|
43
|
-
|
|
103
|
+
# [seq, run id, ms, sender] of an attachment on key
|
|
104
|
+
# (Asterism.read_attachment), or nil when it has no sequence number.
|
|
105
|
+
def read(key, att)
|
|
106
|
+
return nil if att.nil? || att.empty?
|
|
107
|
+
parts = key.delete_prefix(PREFIX).split("/", 3)
|
|
108
|
+
place = if parts[0] == SHARED then :global
|
|
109
|
+
elsif parts[1] == SHARED then :node
|
|
110
|
+
end
|
|
111
|
+
::Asterism.read_attachment(att.to_s.b, place, place == :node ? parts[0] : nil)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Where a value's numbers are counted and its latest value kept: the
|
|
115
|
+
# key, and for a shared one with a sender the key and the sender.
|
|
116
|
+
def lane(key, from)
|
|
117
|
+
from ? "#{key}\n#{from}" : key
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Zenoh's default encoding: what a MessagePack value goes with (it is
|
|
121
|
+
# sent with none).
|
|
122
|
+
DEFAULT_ENCODING = "zenoh/bytes"
|
|
123
|
+
# The encodings whose bytes are JSON text, and those that are text.
|
|
124
|
+
JSON_ENCODINGS = %w[application/json text/json application/json-seq application/json-patch+json
|
|
125
|
+
text/json5].freeze
|
|
126
|
+
TEXT_ENCODINGS = %w[application/yaml application/xml application/sql application/jsonpath
|
|
127
|
+
application/jwt].freeze
|
|
128
|
+
# The encodings an MCP client takes as an image (MCP's image content).
|
|
129
|
+
IMAGE_ENCODINGS = %w[image/png image/jpeg image/gif image/webp].freeze
|
|
130
|
+
|
|
131
|
+
# [value, format] of a payload. No encoding (or Zenoh's default):
|
|
132
|
+
# MessagePack (Asterism's own, 3.1), format "msgpack"; else, for a
|
|
133
|
+
# sender that does not follow the protocol, JSON text ("json"), UTF-8
|
|
134
|
+
# text ("text"), else "(n bytes)" ("bytes"). With an encoding (9.2):
|
|
135
|
+
# format is the encoding without its schema ("image/jpeg"), and the
|
|
136
|
+
# value JSON for the JSON ones, the text for text ones, else
|
|
137
|
+
# "(n bytes)". The value is what JSON can hold (bytes inside as
|
|
138
|
+
# "(n bytes)").
|
|
139
|
+
def decode(payload, encoding = nil)
|
|
44
140
|
bytes = payload.to_s.b
|
|
141
|
+
enc = encoding.to_s
|
|
142
|
+
enc = "" if enc == DEFAULT_ENCODING || enc.start_with?("#{DEFAULT_ENCODING};")
|
|
143
|
+
return decode_encoded(bytes, enc) unless enc.empty?
|
|
45
144
|
return [nil, "empty"] if bytes.empty?
|
|
46
145
|
begin
|
|
47
146
|
return [json_safe(::MessagePack.unpack(bytes)), "msgpack"]
|
|
@@ -57,6 +156,23 @@ module Asterism
|
|
|
57
156
|
end
|
|
58
157
|
end
|
|
59
158
|
|
|
159
|
+
# [value, format] of the bytes of a value with an encoding (enc, not
|
|
160
|
+
# Zenoh's default).
|
|
161
|
+
def decode_encoded(bytes, enc)
|
|
162
|
+
name = enc.split(";", 2)[0]
|
|
163
|
+
text = bytes.dup.force_encoding(Encoding::UTF_8)
|
|
164
|
+
texty = name.start_with?("text/") || JSON_ENCODINGS.include?(name) || TEXT_ENCODINGS.include?(name)
|
|
165
|
+
return ["(#{bytes.bytesize} bytes)", name] unless texty && text.valid_encoding?
|
|
166
|
+
if JSON_ENCODINGS.include?(name)
|
|
167
|
+
begin
|
|
168
|
+
return [json_safe(JSON.parse(text)), name]
|
|
169
|
+
rescue JSON::ParserError
|
|
170
|
+
nil
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
[text, name]
|
|
174
|
+
end
|
|
175
|
+
|
|
60
176
|
def json_safe(v)
|
|
61
177
|
case v
|
|
62
178
|
when Hash then v.to_h { |k, x| [k.is_a?(String) ? k : k.to_s, json_safe(x)] }
|
|
@@ -74,15 +190,64 @@ module Asterism
|
|
|
74
190
|
end
|
|
75
191
|
|
|
76
192
|
# One put as the command line and the MCP tool show it: {"key"
|
|
77
|
-
# (without asterism/), "t", "bytes", "format", "value"}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
193
|
+
# (without asterism/), "t", "bytes", "format", "value"} and, from the
|
|
194
|
+
# attachment, "from" (a shared value's sender), "seq", "run_id",
|
|
195
|
+
# "sent" (the sender's time) and, when seq (a Values::Seq) is given, "lost" (when
|
|
196
|
+
# some were) and "restarted" (the sender started its numbers again),
|
|
197
|
+
# counted per sender on a shared key.
|
|
198
|
+
def entry(key, payload, t = Time.now, encoding: nil, attachment: nil, seq: nil)
|
|
199
|
+
value, format = decode(payload, encoding)
|
|
200
|
+
e = { "key" => key.delete_prefix(PREFIX), "t" => time(t), "bytes" => payload.to_s.bytesize,
|
|
201
|
+
"format" => format, "value" => value }
|
|
202
|
+
e.merge!(numbers(e["key"], attachment, seq))
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# {"from", "seq", "run_id", "sent", "lost", "restarted"} of an
|
|
206
|
+
# attachment, each when there is one.
|
|
207
|
+
def numbers(key, attachment, seq = nil)
|
|
208
|
+
got = Values.read(key, attachment)
|
|
209
|
+
out = {}
|
|
210
|
+
return out if got.nil?
|
|
211
|
+
n, run, ms, from = got
|
|
212
|
+
out["from"] = from if from
|
|
213
|
+
out["seq"] = n
|
|
214
|
+
out["run_id"] = run if run
|
|
215
|
+
out["sent"] = time(Time.at(ms / 1000, ms % 1000, :millisecond)) if ms
|
|
216
|
+
if seq
|
|
217
|
+
app = from || (shared?(key) ? nil : key.split("/", 3)[0, 2].join("/"))
|
|
218
|
+
lost, restarted = seq.see(Values.lane(key, from), run, n, app)
|
|
219
|
+
out["lost"] = lost if lost.positive?
|
|
220
|
+
out["restarted"] = true if restarted
|
|
221
|
+
end
|
|
222
|
+
out
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# The sequence numbers seen, key by key (a shared key's sender by
|
|
226
|
+
# sender, Values.lane): how many values were lost before each
|
|
227
|
+
# (docs/protocol.md 9.4: Asterism::Sequences, as a subscription reads
|
|
228
|
+
# them). Thread safe.
|
|
229
|
+
class Seq
|
|
230
|
+
def initialize(max = nil)
|
|
231
|
+
@seqs = ::Asterism::Sequences.new(max)
|
|
232
|
+
@lock = Mutex.new
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# [lost, restarted] of number n of run id run (nil: none) on lane,
|
|
236
|
+
# from app ("<node>/<app>" or nil).
|
|
237
|
+
def see(lane, run, n, app = nil)
|
|
238
|
+
@lock.synchronize { @seqs.see(lane, run, n, app) }
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# The application app left: its numbers are forgotten (its next
|
|
242
|
+
# value is a first one).
|
|
243
|
+
def forget(app)
|
|
244
|
+
@lock.synchronize { @seqs.forget(app) }
|
|
245
|
+
end
|
|
82
246
|
end
|
|
83
247
|
|
|
84
|
-
# The latest put of each key
|
|
85
|
-
#
|
|
248
|
+
# The latest put of each key (of each sender on a shared key), as
|
|
249
|
+
# received since it was made: at most max_keys keys and max_total
|
|
250
|
+
# payload bytes (the key received
|
|
86
251
|
# longest ago is forgotten first); a payload over max_bytes is kept
|
|
87
252
|
# as its size only. Thread safe; wait blocks until a key matches.
|
|
88
253
|
class Store
|
|
@@ -95,6 +260,8 @@ module Asterism
|
|
|
95
260
|
MAX_ANSWER = 100
|
|
96
261
|
# The longest wait an answer may ask for (seconds).
|
|
97
262
|
MAX_WAIT = 30
|
|
263
|
+
# The most images an answer hands over (answer_images).
|
|
264
|
+
MAX_IMAGES = 4
|
|
98
265
|
|
|
99
266
|
attr_reader :since, :forgotten
|
|
100
267
|
|
|
@@ -112,26 +279,38 @@ module Asterism
|
|
|
112
279
|
@keys = {} # key => record, the oldest received first
|
|
113
280
|
@total = 0
|
|
114
281
|
@forgotten = 0
|
|
282
|
+
@seq = Seq.new
|
|
283
|
+
@lost = {} # key => the values lost on it since the start
|
|
115
284
|
@lock = Mutex.new
|
|
116
285
|
@cond = ConditionVariable.new
|
|
117
286
|
end
|
|
118
287
|
|
|
119
|
-
# A put received (key with or without asterism/)
|
|
120
|
-
|
|
288
|
+
# A put received (key with or without asterism/), with its encoding
|
|
289
|
+
# and attachment when it had them. An image (IMAGE_ENCODINGS) kept
|
|
290
|
+
# keeps its bytes too, for answer_images.
|
|
291
|
+
def put(key, payload, encoding: nil, attachment: nil)
|
|
121
292
|
key = key.delete_prefix(PREFIX)
|
|
122
293
|
payload = payload.to_s
|
|
123
294
|
size = payload.bytesize
|
|
124
295
|
rec = if size > @max_bytes
|
|
125
296
|
{ "value" => "(#{size} bytes: too large to keep)", "format" => "dropped", "kept" => false }
|
|
126
297
|
else
|
|
127
|
-
v, f = Values.decode(payload)
|
|
128
|
-
{ "value" => v, "format" => f }
|
|
298
|
+
v, f = Values.decode(payload, encoding)
|
|
299
|
+
r = { "value" => v, "format" => f }
|
|
300
|
+
r["raw"] = payload.b if IMAGE_ENCODINGS.include?(f)
|
|
301
|
+
r
|
|
129
302
|
end
|
|
130
303
|
rec.merge!("key" => key, "bytes" => size, "t" => @now.call, "mono" => @clock.call, "left" => false)
|
|
304
|
+
num = Values.numbers(key, attachment, @seq)
|
|
305
|
+
%w[from seq run_id sent].each { |k| rec[k] = num[k] if num[k] }
|
|
306
|
+
lane = Values.lane(key, num["from"])
|
|
131
307
|
@lock.synchronize do
|
|
132
|
-
|
|
308
|
+
lost = (@lost[lane] || 0) + num["lost"].to_i
|
|
309
|
+
@lost[lane] = lost
|
|
310
|
+
rec["lost"] = lost if lost.positive?
|
|
311
|
+
old = @keys.delete(lane)
|
|
133
312
|
@total -= old["kept"] == false ? 0 : old["bytes"] if old
|
|
134
|
-
@keys[
|
|
313
|
+
@keys[lane] = rec
|
|
135
314
|
@total += size unless rec["kept"] == false
|
|
136
315
|
while @keys.size > @max_keys || (@total > @max_total && @keys.size > 1)
|
|
137
316
|
_, gone = @keys.shift
|
|
@@ -144,19 +323,31 @@ module Asterism
|
|
|
144
323
|
end
|
|
145
324
|
|
|
146
325
|
# The application "<node>/<app>" left the network: its values are
|
|
147
|
-
# from before (kept, marked)
|
|
326
|
+
# from before (kept, marked), those on its own keys and those it
|
|
327
|
+
# sent to a shared place.
|
|
148
328
|
def left(app)
|
|
149
329
|
prefix = "#{app}/"
|
|
150
|
-
@
|
|
330
|
+
@seq.forget(app)
|
|
331
|
+
@lock.synchronize do
|
|
332
|
+
@keys.each_value { |r| r["left"] = true if Store.app_of(r) == app || (r["from"].nil? && r["key"].start_with?(prefix)) }
|
|
333
|
+
end
|
|
151
334
|
self
|
|
152
335
|
end
|
|
153
336
|
|
|
337
|
+
# The application a record is of: its sender on a shared key (nil
|
|
338
|
+
# when it named none), else its key's "<node>/<app>".
|
|
339
|
+
def self.app_of(rec)
|
|
340
|
+
return rec["from"] if rec["from"] || Values.shared?(rec["key"])
|
|
341
|
+
rec["key"].split("/")[0, 2].join("/")
|
|
342
|
+
end
|
|
343
|
+
|
|
154
344
|
def size
|
|
155
345
|
@lock.synchronize { @keys.size }
|
|
156
346
|
end
|
|
157
347
|
|
|
348
|
+
# The keys kept (a shared key once for each of its senders).
|
|
158
349
|
def keys
|
|
159
|
-
@lock.synchronize { @keys.
|
|
350
|
+
@lock.synchronize { @keys.values.map { _1["key"] } }
|
|
160
351
|
end
|
|
161
352
|
|
|
162
353
|
# The records whose keys match (sorted by key); with wait (seconds),
|
|
@@ -166,7 +357,7 @@ module Asterism
|
|
|
166
357
|
@lock.synchronize do
|
|
167
358
|
loop do
|
|
168
359
|
got = @keys.values.select { Values.match?(pattern, _1["key"]) }
|
|
169
|
-
return got.sort_by { _1["key"] } unless got.empty?
|
|
360
|
+
return got.sort_by { [_1["key"], _1["from"].to_s] } unless got.empty?
|
|
170
361
|
left = deadline - @clock.call
|
|
171
362
|
return [] if left <= 0
|
|
172
363
|
@cond.wait(@lock, left)
|
|
@@ -175,22 +366,45 @@ module Asterism
|
|
|
175
366
|
end
|
|
176
367
|
|
|
177
368
|
# What asterism_values answers: {"since", "values" => [{"key", "t",
|
|
178
|
-
# "age_s", "app", "bytes", "format", "value"(, "
|
|
369
|
+
# "age_s", "app", "bytes", "format", "value"(, "from", "seq",
|
|
370
|
+
# "run_id", "sent", "lost", "truncated")}]} (from the sender of a
|
|
371
|
+
# shared key's value, one entry for each sender; seq, run_id and
|
|
372
|
+
# sent from the attachment of
|
|
373
|
+
# the latest value, lost the values missed on the key, or of that
|
|
374
|
+
# sender, since the start; app of the sender, left out for a shared
|
|
375
|
+
# value that named none), with
|
|
179
376
|
# "more" when more keys matched than an answer lists, "forgotten"
|
|
180
377
|
# when the limits made it forget keys, and "note" when nothing came.
|
|
181
378
|
def answer(pattern = "**", wait: 0)
|
|
379
|
+
answer_images(pattern, wait: wait, images: 0)[0]
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
# [answer, images]: the answer, and the bytes of at most images
|
|
383
|
+
# values that are images (IMAGE_ENCODINGS) among those it lists,
|
|
384
|
+
# [[format, bytes], ...]; each such value's entry says "image" =>
|
|
385
|
+
# its place in that list (0 first).
|
|
386
|
+
def answer_images(pattern = "**", wait: 0, images: MAX_IMAGES)
|
|
182
387
|
Values.check_pattern!(pattern)
|
|
183
388
|
wait = wait.to_f.clamp(0, MAX_WAIT)
|
|
184
389
|
recs = match(pattern, wait: wait)
|
|
390
|
+
pics = []
|
|
185
391
|
now = @clock.call
|
|
186
392
|
alive = {}
|
|
187
393
|
out = { "since" => Values.time(@since) }
|
|
188
394
|
out["values"] = recs.first(MAX_ANSWER).map do |r|
|
|
189
|
-
app =
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
395
|
+
app = Store.app_of(r)
|
|
396
|
+
h = { "key" => r["key"], "t" => Values.time(r["t"]), "age_s" => (now - r["mono"]).round(1) }
|
|
397
|
+
if app
|
|
398
|
+
here = alive.key?(app) ? alive[app] : (alive[app] = @alive.call(app) ? true : false)
|
|
399
|
+
h["app"] = here ? (r["left"] ? "restarted" : "alive") : "gone"
|
|
400
|
+
end
|
|
401
|
+
h["bytes"] = r["bytes"]
|
|
402
|
+
h["format"] = r["format"]
|
|
403
|
+
%w[from seq run_id sent lost].each { |k| h[k] = r[k] if r.key?(k) }
|
|
404
|
+
if r["raw"] && pics.size < images
|
|
405
|
+
h["image"] = pics.size
|
|
406
|
+
pics << [r["format"], r["raw"]]
|
|
407
|
+
end
|
|
194
408
|
text = JSON.generate(r["value"])
|
|
195
409
|
if text.bytesize > VALUE_CUT
|
|
196
410
|
h["value"] = "#{text.byteslice(0, VALUE_CUT).scrub('')}…"
|
|
@@ -208,7 +422,7 @@ module Asterism
|
|
|
208
422
|
"it has not been put since this server started (puts come only when a value " \
|
|
209
423
|
"changes), or the router does not let this server read it"
|
|
210
424
|
end
|
|
211
|
-
out
|
|
425
|
+
[out, pics]
|
|
212
426
|
end
|
|
213
427
|
end
|
|
214
428
|
end
|