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 +4 -4
- data/CHANGELOG.md +7 -1
- data/README.md +56 -0
- data/data/transfer/sha256.rb +124 -0
- data/data/transfer/transfer.rb +1027 -0
- data/lib/asterism/transfer/endpoint.rb +129 -0
- data/lib/asterism/transfer/file_inbox.rb +159 -0
- data/lib/asterism/transfer.rb +32 -0
- data/lib/asterism/version.rb +1 -1
- metadata +6 -1
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# Asterism::Transfer::Endpoint (CRuby): an Inbox answering at
|
|
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.
|
|
5
|
+
#
|
|
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.
|
|
13
|
+
#
|
|
14
|
+
# conn = Asterism::Zenoh.open("tcp/127.0.0.1:7447") # or Asterism.net.session
|
|
15
|
+
# ep = Asterism::Transfer::Endpoint.new(conn, inbox, node: "console").start
|
|
16
|
+
# conn.start # its receiving thread answers
|
|
17
|
+
# loop { ep.tick; sleep 0.5 }
|
|
18
|
+
# ep.close
|
|
19
|
+
#
|
|
20
|
+
# The inbox's calls run under one lock (the receiving thread answers, the
|
|
21
|
+
# program ticks and reads it from its own): synchronize { ... } for more.
|
|
22
|
+
module Asterism
|
|
23
|
+
module Transfer
|
|
24
|
+
class Endpoint
|
|
25
|
+
DOC = "Files in pieces with resending (Asterism::Transfer): begin, chunk, commit, cancel"
|
|
26
|
+
METHOD_DOCS = {
|
|
27
|
+
"begin" => { args: %w[kind meta], doc: "starts a file: meta {name, size, sha256, ...}; -> {id, piece, window, pos}" },
|
|
28
|
+
"chunk" => { args: %w[id pos bytes], doc: "a piece at pos; -> the position this receiver holds" },
|
|
29
|
+
"commit" => { args: %w[id], doc: "the file is all here: checks its sha256 and places it" },
|
|
30
|
+
"cancel" => { args: %w[id], doc: "drops what is held of the file" }
|
|
31
|
+
}.freeze
|
|
32
|
+
|
|
33
|
+
attr_reader :inbox, :path
|
|
34
|
+
|
|
35
|
+
def initialize(conn, inbox, node:, app: "transfer", object: "inbox")
|
|
36
|
+
@conn = conn
|
|
37
|
+
@inbox = inbox
|
|
38
|
+
@node = node
|
|
39
|
+
@app = app
|
|
40
|
+
@object = object
|
|
41
|
+
@path = "#{node}/#{app}/#{object}"
|
|
42
|
+
@prefix = "#{::Asterism::ROOT}/#{@path}/"
|
|
43
|
+
@lock = Monitor.new
|
|
44
|
+
@tokens = []
|
|
45
|
+
@queryable = nil
|
|
46
|
+
inbox.path = @path
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def start
|
|
50
|
+
@tokens << @conn.liveliness("#{::Asterism::ROOT}/#{@node}/#{@app}")
|
|
51
|
+
@tokens << @conn.liveliness("#{::Asterism::ROOT}/#{@path}")
|
|
52
|
+
@queryable = @conn.queryable("#{::Asterism::ROOT}/#{@path}/**", depth: 32) { |q| answer(q) }
|
|
53
|
+
self
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def tick
|
|
57
|
+
synchronize { @inbox.tick }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def synchronize(&blk)
|
|
61
|
+
@lock.synchronize(&blk)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def close
|
|
65
|
+
@queryable&.close
|
|
66
|
+
@queryable = nil
|
|
67
|
+
@tokens.each { _1.close rescue nil }
|
|
68
|
+
@tokens.clear
|
|
69
|
+
synchronize { @inbox.close }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# One query: <prefix>meta, or <prefix>call/<caller>.
|
|
73
|
+
def answer(q)
|
|
74
|
+
rest = q.key.to_s.delete_prefix(@prefix)
|
|
75
|
+
return if rest == q.key.to_s
|
|
76
|
+
if rest == "meta"
|
|
77
|
+
q.reply("#{@prefix}meta", ::Asterism::Codec.pack(meta))
|
|
78
|
+
elsif rest.start_with?("call/")
|
|
79
|
+
from = rest.delete_prefix("call/")
|
|
80
|
+
return unless ::Asterism.valid_name?(from)
|
|
81
|
+
q.reply("#{@prefix}call/#{from}", dispatch(q.payload.to_s, from))
|
|
82
|
+
elsif rest == "call"
|
|
83
|
+
q.reply("#{@prefix}call", ::Asterism.error_reply("protocol", "Asterism::Error",
|
|
84
|
+
"#{@node}/#{@app} speaks Asterism protocol " \
|
|
85
|
+
"#{::Asterism::PROTOCOL}: call #{@prefix}call/<caller>"))
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# The meta (docs/protocol.md 5).
|
|
90
|
+
def meta
|
|
91
|
+
methods = Inbox::METHODS.map do |m, n|
|
|
92
|
+
d = METHOD_DOCS[m]
|
|
93
|
+
{ "name" => m, "arity" => n, "args" => d[:args], "doc" => d[:doc] }
|
|
94
|
+
end
|
|
95
|
+
{ "methods" => methods, "doc" => DOC }
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# The packed reply to a call from the node from.
|
|
99
|
+
def dispatch(payload, from)
|
|
100
|
+
req = ::Asterism.parse_request(::Asterism::Codec.unpack(payload))
|
|
101
|
+
return ::Asterism.error_reply("malformed", "ArgumentError", ::Asterism::MALFORMED) unless req
|
|
102
|
+
name, args, kw = req
|
|
103
|
+
arity = Inbox::METHODS[name]
|
|
104
|
+
unless arity
|
|
105
|
+
return ::Asterism.error_reply("not_exposed", "NoMethodError",
|
|
106
|
+
"undefined method '#{name}' for #{@path} (not exposed)")
|
|
107
|
+
end
|
|
108
|
+
if args.size != arity
|
|
109
|
+
return ::Asterism.error_reply("arguments", "ArgumentError",
|
|
110
|
+
"wrong number of arguments (given #{args.size}, expected #{arity})")
|
|
111
|
+
end
|
|
112
|
+
unless kw.empty?
|
|
113
|
+
return ::Asterism.error_reply("arguments", "ArgumentError", "#{@path} takes no keyword arguments")
|
|
114
|
+
end
|
|
115
|
+
value =
|
|
116
|
+
begin
|
|
117
|
+
synchronize { @inbox.public_send(name, *args, from: from) }
|
|
118
|
+
rescue StandardError => e
|
|
119
|
+
return ::Asterism.error_reply("raised", e.class.to_s, e.message)
|
|
120
|
+
end
|
|
121
|
+
begin
|
|
122
|
+
::Asterism::Codec.pack({ "value" => value })
|
|
123
|
+
rescue ::Asterism::EncodeError => e
|
|
124
|
+
::Asterism.error_reply("unsendable", "Asterism::EncodeError", "the return value: #{e.message}")
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# Asterism::Transfer::FileInbox (CRuby): the receiver of the kind "file",
|
|
2
|
+
# with the rules a Family mruby machine's transfer app keeps for it, under a
|
|
3
|
+
# directory the program gives.
|
|
4
|
+
#
|
|
5
|
+
# - files of the kind "file" only, from the senders allowed (patterns,
|
|
6
|
+
# * matching any run of characters; or a block);
|
|
7
|
+
# - a name that stays in the directory (Transfer.file_name?: no "/", "\\",
|
|
8
|
+
# "..", not hidden, 1 to 48 bytes);
|
|
9
|
+
# - a name already there: the sender chooses (meta "overwrite" => true
|
|
10
|
+
# replaces it once the new one is whole and its sha256 matches), else
|
|
11
|
+
# it goes beside it as name(1), name(2), ...;
|
|
12
|
+
# - room: max_bytes for the directory in all (nil: no limit of its own);
|
|
13
|
+
# a file that does not fit is refused at begin;
|
|
14
|
+
# - one sender at a time, resending from this receiver's position, the
|
|
15
|
+
# sha256 of the whole (Inbox);
|
|
16
|
+
# - the pieces are written beside where the file goes, hidden
|
|
17
|
+
# ("<dir>.<name>.file.part"), so the commit only renames; the notes of
|
|
18
|
+
# the files coming in go to work (a directory of their own), so a
|
|
19
|
+
# program started again goes on with a file it held (start).
|
|
20
|
+
#
|
|
21
|
+
# The meta's "inbox" (a Family mruby machine's SD card, "sd") names a place
|
|
22
|
+
# this receiver does not have: refused, as on a machine without a card.
|
|
23
|
+
#
|
|
24
|
+
# inbox = Asterism::Transfer::FileInbox.new("storage/inbox/", work: "storage/work/", from: ["fmruby-*"])
|
|
25
|
+
# inbox.start # takes up a held file, deletes stray pieces
|
|
26
|
+
# endpoint = Asterism::Transfer::Endpoint.new(conn, inbox, node: "mypc").start
|
|
27
|
+
# # ... inbox.tick now and then (endpoint.tick)
|
|
28
|
+
module Asterism
|
|
29
|
+
module Transfer
|
|
30
|
+
class FileInbox < Inbox
|
|
31
|
+
KIND = "file"
|
|
32
|
+
# A program has memory for more pieces on their way than a board.
|
|
33
|
+
WINDOW_FILE = 8
|
|
34
|
+
|
|
35
|
+
attr_reader :inbox_dir, :max_bytes
|
|
36
|
+
attr_accessor :from, :label
|
|
37
|
+
|
|
38
|
+
# dir: where the files go (made when missing; a trailing "/" is
|
|
39
|
+
# added). work: where the notes go (default dir + ".work/"). from: the
|
|
40
|
+
# senders allowed (an Array of node IDs and patterns, "*" for any), or
|
|
41
|
+
# a block |node| -> true / false. max_bytes: the most the directory
|
|
42
|
+
# holds in all. label: this receiver's name in the reasons it gives.
|
|
43
|
+
def initialize(dir, work: nil, from: [], max_bytes: nil, label: "this machine", window: WINDOW_FILE,
|
|
44
|
+
**opts, &allowed)
|
|
45
|
+
@inbox_dir = dir.end_with?("/") ? dir : "#{dir}/"
|
|
46
|
+
work ||= "#{@inbox_dir}.work/"
|
|
47
|
+
work = "#{work}/" unless work.end_with?("/")
|
|
48
|
+
::FileUtils.mkdir_p(@inbox_dir)
|
|
49
|
+
::FileUtils.mkdir_p(work)
|
|
50
|
+
super(work, window: window, **opts)
|
|
51
|
+
@from = from
|
|
52
|
+
@allowed = allowed
|
|
53
|
+
@max_bytes = max_bytes
|
|
54
|
+
@label = label
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Takes up a file held when the program last ended (its note in work)
|
|
58
|
+
# and deletes the hidden pieces no note speaks for.
|
|
59
|
+
def start
|
|
60
|
+
resume_held
|
|
61
|
+
sweep
|
|
62
|
+
self
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Whether node may send files here.
|
|
66
|
+
def sender_ok?(node)
|
|
67
|
+
return @allowed.call(node.to_s) ? true : false if @allowed
|
|
68
|
+
Array(@from).any? { |pat| FileInbox.match?(pat.to_s, node.to_s) }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# pattern with * (any run of characters) against s.
|
|
72
|
+
def self.match?(pattern, s)
|
|
73
|
+
Regexp.new("\\A#{Regexp.escape(pattern).gsub('\\*', '.*')}\\z").match?(s)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# The files here: [{"name", "size", "mtime"}] (hidden ones left out),
|
|
77
|
+
# newest first.
|
|
78
|
+
def files
|
|
79
|
+
::Dir.children(@inbox_dir).reject { _1.start_with?(".") }.filter_map do |n|
|
|
80
|
+
path = "#{@inbox_dir}#{n}"
|
|
81
|
+
next unless ::File.file?(path)
|
|
82
|
+
st = ::File.stat(path)
|
|
83
|
+
{ "name" => n, "size" => st.size, "mtime" => st.mtime }
|
|
84
|
+
end.sort_by { -_1["mtime"].to_f }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The bytes the directory holds (pieces coming in included).
|
|
88
|
+
def used_bytes
|
|
89
|
+
::Dir.children(@inbox_dir).sum do |n|
|
|
90
|
+
path = "#{@inbox_dir}#{n}"
|
|
91
|
+
::File.file?(path) ? ::File.size(path) : 0
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# ---- the hooks of Inbox ----
|
|
96
|
+
|
|
97
|
+
def refused(kind, from, meta)
|
|
98
|
+
return "#{@label} takes no #{kind}" unless kind == KIND
|
|
99
|
+
return "#{@label} takes no file from #{from}" unless sender_ok?(from)
|
|
100
|
+
place = meta.is_a?(Hash) ? meta["inbox"] : nil
|
|
101
|
+
return nil if place.nil? || place == "home"
|
|
102
|
+
return "#{@label} has no SD card to write to" if place == "sd"
|
|
103
|
+
"#{@label} has no inbox #{place} (home only)"
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def name_ok?(kind, name)
|
|
107
|
+
::Asterism::Transfer.file_name?(name)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def room(kind, meta)
|
|
111
|
+
return nil if @max_bytes.nil?
|
|
112
|
+
left = @max_bytes - used_bytes
|
|
113
|
+
left.negative? ? 0 : left
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def part_path(kind, name, meta)
|
|
117
|
+
"#{@inbox_dir}.#{name}.#{kind}.part"
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# The file is whole (its sha256 matches): its hidden piece file is
|
|
121
|
+
# renamed to its name, or to a free name beside it when that one is
|
|
122
|
+
# taken and overwrite was not asked. An overwritten file is replaced
|
|
123
|
+
# only now.
|
|
124
|
+
def received(t)
|
|
125
|
+
over = t["meta"]["overwrite"] == true
|
|
126
|
+
part = t["part"]
|
|
127
|
+
name = over ? t["name"] : ::Asterism::Transfer.free_name(@inbox_dir, t["name"])
|
|
128
|
+
if name.nil?
|
|
129
|
+
remove(part)
|
|
130
|
+
return { "status" => "refused", "reason" => "no free name for #{t['name']} in #{@label}'s inbox" }
|
|
131
|
+
end
|
|
132
|
+
dest = "#{@inbox_dir}#{name}"
|
|
133
|
+
begin
|
|
134
|
+
::File.delete(dest) if over && ::File.exist?(dest)
|
|
135
|
+
::File.rename(part, dest)
|
|
136
|
+
rescue SystemCallError => e
|
|
137
|
+
remove(part)
|
|
138
|
+
warn("transfer: #{dest} from #{t['from']}: #{e.message}")
|
|
139
|
+
return { "status" => "refused", "reason" => "cannot place #{name} (#{e.message})" }
|
|
140
|
+
end
|
|
141
|
+
info("transfer: placed #{dest} (#{t['size']} bytes) from #{t['from']}#{over ? ' (overwrite)' : ''}")
|
|
142
|
+
{ "status" => "placed", "name" => name, "path" => name }
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
private
|
|
146
|
+
|
|
147
|
+
def sweep
|
|
148
|
+
keep = current_file && current_file["part"]
|
|
149
|
+
::Dir.children(@inbox_dir).each do |n|
|
|
150
|
+
next unless n.start_with?(".") && n.end_with?(".part")
|
|
151
|
+
path = "#{@inbox_dir}#{n}"
|
|
152
|
+
next if path == keep
|
|
153
|
+
info("transfer: #{path} left from before; deleted")
|
|
154
|
+
remove(path)
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Asterism::Transfer for CRuby: files carried in pieces between machines,
|
|
2
|
+
# with resending (data/transfer/transfer.rb, the same file the boards load
|
|
3
|
+
# at run time; see there). Loaded by `require "asterism/transfer"`.
|
|
4
|
+
#
|
|
5
|
+
# CRuby only, besides: FileInbox (the receiver of the kind "file", with the
|
|
6
|
+
# rules of a Family mruby machine) and Endpoint (an inbox answering at
|
|
7
|
+
# <node>/transfer/inbox beside the program's own application).
|
|
8
|
+
require "digest/sha2"
|
|
9
|
+
require "fileutils"
|
|
10
|
+
require "json"
|
|
11
|
+
require "monitor"
|
|
12
|
+
require_relative "../../data/transfer/transfer"
|
|
13
|
+
require_relative "transfer/file_inbox"
|
|
14
|
+
require_relative "transfer/endpoint"
|
|
15
|
+
|
|
16
|
+
module Asterism
|
|
17
|
+
module Transfer
|
|
18
|
+
# CRuby: the piece files are written as bytes. In text mode a program
|
|
19
|
+
# that sets Encoding.default_internal (Rails sets UTF-8) would have
|
|
20
|
+
# Ruby transcode each piece, and a piece that is not UTF-8 fails to be
|
|
21
|
+
# written. (The boards' File has no such mode.)
|
|
22
|
+
module BinaryPieces
|
|
23
|
+
def open_part(t)
|
|
24
|
+
super
|
|
25
|
+
f = t["file"]
|
|
26
|
+
f.binmode if f.respond_to?(:binmode)
|
|
27
|
+
nil
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
Inbox.prepend(BinaryPieces)
|
|
31
|
+
end
|
|
32
|
+
end
|
data/lib/asterism/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: asterism
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.12.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Katsuhiko Kageyama
|
|
@@ -143,6 +143,8 @@ files:
|
|
|
143
143
|
- data/msgs/visualization_msgs/msg/MarkerArray.rb
|
|
144
144
|
- data/msgs/visualization_msgs/msg/MeshFile.rb
|
|
145
145
|
- data/msgs/visualization_msgs/msg/UVCoordinate.rb
|
|
146
|
+
- data/transfer/sha256.rb
|
|
147
|
+
- data/transfer/transfer.rb
|
|
146
148
|
- exe/asterism
|
|
147
149
|
- lib/asterism.rb
|
|
148
150
|
- lib/asterism/cli.rb
|
|
@@ -161,6 +163,9 @@ files:
|
|
|
161
163
|
- lib/asterism/cruby/ros.rb
|
|
162
164
|
- lib/asterism/cruby/runner.rb
|
|
163
165
|
- lib/asterism/cruby/zenoh.rb
|
|
166
|
+
- lib/asterism/transfer.rb
|
|
167
|
+
- lib/asterism/transfer/endpoint.rb
|
|
168
|
+
- lib/asterism/transfer/file_inbox.rb
|
|
164
169
|
- lib/asterism/version.rb
|
|
165
170
|
- mrblib/asterism.rb
|
|
166
171
|
- mrblib/cdr.rb
|