asterism 0.11.0 → 0.13.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.
@@ -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
@@ -3,5 +3,5 @@ module Asterism
3
3
  # is in asterism.gemspec (asterism-zenoh ~> 0.4.0 for 0.5.0 to 0.7.0:
4
4
  # the bindings did not change; see CHANGELOG.md). 0.7.0 speaks wire
5
5
  # protocol 1 only (Asterism::PROTOCOL, docs/protocol.md).
6
- VERSION = "0.11.0"
6
+ VERSION = "0.13.0"
7
7
  end
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.11.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katsuhiko Kageyama
@@ -143,10 +143,13 @@ 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
149
151
  - lib/asterism/cli/allow.rb
152
+ - lib/asterism/cli/commands.rb
150
153
  - lib/asterism/cli/complete.rb
151
154
  - lib/asterism/cli/hz.rb
152
155
  - lib/asterism/cli/literal.rb
@@ -161,6 +164,9 @@ files:
161
164
  - lib/asterism/cruby/ros.rb
162
165
  - lib/asterism/cruby/runner.rb
163
166
  - lib/asterism/cruby/zenoh.rb
167
+ - lib/asterism/transfer.rb
168
+ - lib/asterism/transfer/endpoint.rb
169
+ - lib/asterism/transfer/file_inbox.rb
164
170
  - lib/asterism/version.rb
165
171
  - mrblib/asterism.rb
166
172
  - mrblib/cdr.rb