asterism 0.11.0 → 0.12.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3f9045afdf176405f969bbf97ec703a00f6dc97b777224dfe71d449ebfcda29
4
- data.tar.gz: '0748cc260662719332fa6a17030577ceb55ca53d137a9723ad100214c0518d72'
3
+ metadata.gz: da843ff3a157644e6b93edbcb6d8e7be7f9964af7599bb033d0d5a0b4a3c7757
4
+ data.tar.gz: 4d4a45ca03b5615decdef1f9f683550ec670c7a7347b36e9a7411788762935c6
5
5
  SHA512:
6
- metadata.gz: afc211c874d21e5d73bbb94d0e5bd870bd8aa3d840ccd57f7aaf072d9ecebb0bae72abd7a9d3e9fdb742498cbb3eb3220fa0a35fd213a9ce727f1e65ca8b738c
7
- data.tar.gz: e12f369efedb87918e86d1eb70346e0da7404d08f5b9fdb37b8ce244be3bc90c85cabf9e9d548a928ad48f683e26574a5062eaf55cba9167361cbe05735301ab
6
+ metadata.gz: 945450cf13d5343477e9994295e09e0f449d5012a6e4893628b9775b7100261fe60fa6e8e378a67c122c4f2192f8f409e65d650341216f7cd4e9a36c59ed2236
7
+ data.tar.gz: dbec4a48913f223c5742068259f78d7bdfe4b92c222e2290379c80a8252741023ce7c1b7598bdff48914e3d39552a3cb7a6ba46a1768a0085f43f00c6e12a73e
data/CHANGELOG.md CHANGED
@@ -6,7 +6,13 @@ the earlier ones are only in this repository. From 0.4.0 the two Zenoh
6
6
  bindings (asterism-zenoh, picoruby-asterism-zenoh) carry the same minor
7
7
  version (a patch release of one may come without the other).
8
8
 
9
- ## Unreleased
9
+ ## 0.12.0
10
+
11
+ - `Asterism::Transfer` (`data/transfer/`, `require "asterism/transfer"` on CRuby): a file carried in pieces over calls, resent from the receiver's position after lost, repeated or reordered pieces, a dropped connection or a receiver started again, checked whole by SHA-256. Plain Ruby loaded at run time on the boards (not part of the mrbgem). `rake test:transfer`.
12
+ - `Asterism::Transfer`: the receiver's hooks `name_ok?(kind, name)` (the names a kind takes; `plain_name?` by default) and `room(kind, meta)` (was `room`); `Transfer.file_name?` (a file name that stays in the receiver's directory: no `/`, `\`, `..`, not hidden, up to 48 bytes), `Transfer.numbered` / `Transfer.free_name` (`foo(1).txt` beside a `foo.txt`); `Sender#cancel` (the receiver drops what it holds of the file) and `Sender#kind`.
13
+
14
+ - `Asterism::Transfer::FileInbox` (CRuby): the receiver of the kind `file` with the rules of a Family mruby machine's transfer app, under a directory the program gives: the senders allowed (patterns or a block), names that stay in the directory, a name already there kept (`foo(1).txt` beside it) unless the sender asks to overwrite, a limit for the directory in all (`max_bytes:`, refused at `begin`), the pieces hidden beside where the file goes, the notes in a directory of their own (`start` goes on with a held file and deletes stray pieces). The SD card's inbox (`"inbox" => "sd"`) is refused as on a machine without a card.
15
+ - `Asterism::Transfer::Endpoint` (CRuby): an inbox answering at `<node>/transfer/inbox` (where the machines send: `asend <node> <file>`) on a Zenoh connection of the program's, beside the application `Asterism.connect` made (the object layer is one application per process). Its application's and object's tokens, its calls and its meta in the object layer's encoding, the caller's node ID as `from:`.
10
16
 
11
17
  ## 0.11.0
12
18
 
data/README.md CHANGED
@@ -128,6 +128,7 @@ these repositories.
128
128
  ```
129
129
  rake # every suite
130
130
  rake test:msgs # generator, type hashes, CDR, bundled types (CRuby only; no Zenoh, no docker)
131
+ rake test:transfer # files in pieces (Asterism::Transfer) over a stand-in for the calls: lost, repeated, swapped pieces, a connection down, a receiver started again
131
132
  rake test:objects # objects and Asterism::ROS between CRuby sessions over a local peer link
132
133
  rake test:api # the CRuby API (blocks, receiving threads, Enumerators)
133
134
  rake test:cli # the command line (exe/asterism), its Tab completion and its MCP bridge (the mcp gem's client; skipped without it)
@@ -1165,6 +1166,61 @@ publishing from `node.every` and receiving through a `node.subscribe` block
1165
1166
  keeps 6.9 KB free, the same as polling `each_pending` by hand. Loading a
1166
1167
  nested type (`geometry_msgs/msg/Twist`) at start-up took 0.6 KB more.
1167
1168
 
1169
+ ## Files in pieces: `Asterism::Transfer`
1170
+
1171
+ A file carried from one machine to another over calls, that comes through
1172
+ lost, repeated and reordered pieces, a connection that drops for a while
1173
+ and a receiver started again, and arrives whole (its SHA-256 checked) or
1174
+ not at all. The receiver's position is the truth: every answer says how
1175
+ many bytes it holds, and the sender goes on from there. Plain Ruby that is
1176
+ not compiled into the mrbgem (an mruby VM that carries no files pays
1177
+ nothing for it): `data/transfer/transfer.rb`, which the boards load at run
1178
+ time (`require "/usr/share/asterism/transfer/transfer"` on Family mruby)
1179
+ and CRuby with `require "asterism/transfer"`. Its head describes the calls
1180
+ (`begin` / `chunk` / `commit` / `cancel`).
1181
+
1182
+ ```ruby
1183
+ # the receiver: what it takes and where a whole file goes
1184
+ class Inbox < Asterism::Transfer::Inbox
1185
+ def refused(kind, from, meta) = (kind == "file" ? nil : "takes no #{kind}")
1186
+ def received(t) = (File.rename(t["part"], "inbox/#{t['name']}"); { "status" => "placed" })
1187
+ end
1188
+ inbox = Inbox.new("tmp/") # the piece files and their notes go here
1189
+ inbox.resume_held # a file held when it last ended goes on
1190
+ Asterism.expose("inbox", inbox, methods: Asterism::Transfer::Inbox::METHODS, caller: :from)
1191
+ # ... inbox.tick in the update loop
1192
+
1193
+ # the sender: stepped from the update loop, never waiting in one go
1194
+ s = Asterism::Transfer::Sender.new("node-b/app/inbox", "file", "photo.png", name: "photo")
1195
+ until s.done?
1196
+ Asterism.poll
1197
+ s.step
1198
+ end
1199
+ s.ok? ? s.answer : s.reason
1200
+ ```
1201
+
1202
+ On CRuby, `Asterism::Transfer::FileInbox` is the receiver of the kind
1203
+ `file` (the rules of a Family mruby machine: the senders allowed, names
1204
+ that stay in its directory, `foo(1).txt` beside a name already there unless
1205
+ the sender asks to overwrite, a limit for the directory), and
1206
+ `Asterism::Transfer::Endpoint` answers for it at `<node>/transfer/inbox`,
1207
+ where the machines send, beside the program's own application:
1208
+
1209
+ ```ruby
1210
+ conn = Asterism::Zenoh.open("tcp/127.0.0.1:7447")
1211
+ inbox = Asterism::Transfer::FileInbox.new("inbox/", work: "work/", from: ["fmruby-*"],
1212
+ max_bytes: 1 << 30, label: "mypc").start
1213
+ ep = Asterism::Transfer::Endpoint.new(conn, inbox, node: "mypc").start
1214
+ conn.start
1215
+ loop { ep.tick; sleep 0.5 } # a file that stopped coming is deleted after a minute
1216
+ ```
1217
+
1218
+ A piece is 3,072 bytes by default: one call carries about 4 KB in all on
1219
+ zenoh-pico (`Z_FRAG_MAX_SIZE` 4096; a String argument of 4,044 bytes still
1220
+ went through between two Family mruby programs, 4,048 did not and was not
1221
+ answered at all). SHA-256 is CRuby's Digest, mbedTLS (`MbedTLS::Digest`)
1222
+ where the build has it, else plain Ruby (`data/transfer/sha256.rb`, slow).
1223
+
1168
1224
  ## Limits
1169
1225
 
1170
1226
  - Objects cannot be passed by reference (a return value is a copy), blocks
@@ -0,0 +1,124 @@
1
+ # SHA-256 (FIPS 180-4) in plain Ruby, for the builds that have neither
2
+ # CRuby's Digest nor mbedTLS (the browser build of Family mruby). Slow: it
3
+ # is there so that every machine can check a whole file the same way.
4
+ # Needs 64-bit Integers (MRB_INT64 on mruby). Loaded by transfer.rb only
5
+ # when it is the one to use.
6
+ module Asterism
7
+ module Transfer
8
+ class PlainSHA256
9
+ K = [
10
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
11
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
12
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
13
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
14
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
15
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
16
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
17
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
18
+ ]
19
+ M = 0xffffffff
20
+
21
+ def initialize
22
+ @h = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]
23
+ @buf = String.new
24
+ @len = 0
25
+ @w = Array.new(64, 0)
26
+ end
27
+
28
+ def update(bytes)
29
+ s = bytes.to_s
30
+ @len += s.bytesize
31
+ @buf << s
32
+ n = @buf.bytesize - (@buf.bytesize % 64)
33
+ off = 0
34
+ while off < n
35
+ block(@buf, off)
36
+ off += 64
37
+ end
38
+ @buf = String.new << @buf.byteslice(n, @buf.bytesize - n).to_s if n > 0
39
+ self
40
+ end
41
+
42
+ def hexdigest
43
+ bits = @len * 8
44
+ tail = @buf.dup
45
+ tail << 0x80.chr
46
+ tail << 0.chr while tail.bytesize % 64 != 56
47
+ i = 7
48
+ while i >= 0
49
+ tail << ((bits >> (i * 8)) & 0xff).chr
50
+ i -= 1
51
+ end
52
+ h = @h.dup
53
+ saved = @h
54
+ @h = h
55
+ off = 0
56
+ while off < tail.bytesize
57
+ block(tail, off)
58
+ off += 64
59
+ end
60
+ out = ""
61
+ i = 0
62
+ while i < 8
63
+ out << format("%08x", @h[i])
64
+ i += 1
65
+ end
66
+ @h = saved
67
+ out
68
+ end
69
+
70
+ def block(s, off)
71
+ w = @w
72
+ i = 0
73
+ while i < 16
74
+ j = off + i * 4
75
+ w[i] = (s.getbyte(j) << 24) | (s.getbyte(j + 1) << 16) | (s.getbyte(j + 2) << 8) | s.getbyte(j + 3)
76
+ i += 1
77
+ end
78
+ while i < 64
79
+ x = w[i - 15]
80
+ y = w[i - 2]
81
+ s0 = (((x >> 7) | (x << 25)) ^ ((x >> 18) | (x << 14)) ^ (x >> 3)) & M
82
+ s1 = (((y >> 17) | (y << 15)) ^ ((y >> 19) | (y << 13)) ^ (y >> 10)) & M
83
+ w[i] = (w[i - 16] + s0 + w[i - 7] + s1) & M
84
+ i += 1
85
+ end
86
+ h = @h
87
+ a = h[0]
88
+ b = h[1]
89
+ c = h[2]
90
+ d = h[3]
91
+ e = h[4]
92
+ f = h[5]
93
+ g = h[6]
94
+ hh = h[7]
95
+ i = 0
96
+ while i < 64
97
+ s1 = (((e >> 6) | (e << 26)) ^ ((e >> 11) | (e << 21)) ^ ((e >> 25) | (e << 7))) & M
98
+ ch = (e & f) ^ ((~e) & g)
99
+ t1 = (hh + s1 + ch + K[i] + w[i]) & M
100
+ s0 = (((a >> 2) | (a << 30)) ^ ((a >> 13) | (a << 19)) ^ ((a >> 22) | (a << 10))) & M
101
+ maj = (a & b) ^ (a & c) ^ (b & c)
102
+ t2 = (s0 + maj) & M
103
+ hh = g
104
+ g = f
105
+ f = e
106
+ e = (d + t1) & M
107
+ d = c
108
+ c = b
109
+ b = a
110
+ a = (t1 + t2) & M
111
+ i += 1
112
+ end
113
+ h[0] = (h[0] + a) & M
114
+ h[1] = (h[1] + b) & M
115
+ h[2] = (h[2] + c) & M
116
+ h[3] = (h[3] + d) & M
117
+ h[4] = (h[4] + e) & M
118
+ h[5] = (h[5] + f) & M
119
+ h[6] = (h[6] + g) & M
120
+ h[7] = (h[7] + hh) & M
121
+ end
122
+ end
123
+ end
124
+ end