libbeachcomber 0.10.0 → 0.11.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: d3c045383cb88693dccb3888f17a5dbed1d5fcea26e582898d59034095d04f98
4
- data.tar.gz: 9bfa55709e0f57da1632fdba84b817836490c65c6fef75a16dd03beb0af78160
3
+ metadata.gz: 900880883418c5a61cf81a0ddbdb7907319fe3c0a13b79fcd0818cb0c7cab02a
4
+ data.tar.gz: 7338b1c6c2d9ad23b9e4bb17f119f7f1e1212903cba0900997b7b3d270235b90
5
5
  SHA512:
6
- metadata.gz: 1fbc30c179de56aea27c151833d8a5934edc1ee5401957695a25191904b30ab6f134420b53df961b8af82bda333331de5a41176574b38ff1a1b1d37242fed6e0
7
- data.tar.gz: 7951707bcabfe101803feef42ccc192dc1c434b9ff9eb2e1f95453fd60556514d759ff431d4f2552566418af12b40e0e6c95a3996465b321828e17092cf5e09d
6
+ metadata.gz: 9be3b9c1db183319dde7549bd2adbf01643b825d297a1d46097ff52c51f4d5c4c462ab5db3e8155b5139d6d0ce6f580402126aee8b97c8be737c963a9d292e13
7
+ data.tar.gz: 8d283cf6eeba0d9e8014cff9aa2d28f548b65b07d06ea6c55df0b828a0e4cbaeb98122b3ca9a11bfef7bf0b8141ddf579965c85521ccde9e2a6d9f06bffb3b67
@@ -19,6 +19,22 @@ module Beachcomber
19
19
  json_or_nil(options.empty? ? nil : options)
20
20
  end
21
21
 
22
+ # The +options_json+ +bc_io+ and +bc_session_io+ take: +get_options+'s,
23
+ # and the put's +ttl+ and +persist+.
24
+ def io_options(force, wait, ttl, persist)
25
+ options = {}
26
+ options['force'] = true if force
27
+ options['wait'] = wait unless wait.nil?
28
+ options['ttl'] = ttl.to_s unless ttl.nil?
29
+ options['persist'] = true if persist
30
+ json_or_nil(options.empty? ? nil : options)
31
+ end
32
+
33
+ # The input as the ABI takes it: JSON text as given, else the object as JSON.
34
+ def io_input(input)
35
+ input.is_a?(String) ? input : JSON.generate(input)
36
+ end
37
+
22
38
  def json_or_nil(value)
23
39
  value ? JSON.generate(value) : nil
24
40
  end
@@ -78,6 +94,12 @@ module Beachcomber
78
94
  def initialize(handle)
79
95
  @handle = handle
80
96
  @closed = false
97
+ @mutex = Mutex.new
98
+ ObjectSpace.define_finalizer(self, self.class.finalizer(handle))
99
+ end
100
+
101
+ def self.finalizer(handle)
102
+ proc { Beachcomber::FFI.close_session(handle) }
81
103
  end
82
104
 
83
105
  # Sets this session's directory: every later {#get} that names no +cwd+,
@@ -114,6 +136,21 @@ module Beachcomber
114
136
  nil
115
137
  end
116
138
 
139
+ # {Client#io}: put +input+, a JSON object, at +put_key+, then read
140
+ # +get_key+ on this session's connection with the input bound as +input+ in templates
141
+ # (+comb io+). +input+ is a Hash or JSON text. <tt>{field}</tt> in either key
142
+ # is filled from the input's top-level string or number +field+; a missing
143
+ # one is a {ParseError} and stores nothing. Input that is not a JSON
144
+ # object stores nothing and the read goes on. +cwd+ addresses the put as
145
+ # well as the read (none: this session's directory); +ttl+ and +persist+ are {#put}'s, +force+
146
+ # and +wait+ {#get}'s.
147
+ #
148
+ # @return [Object, nil] what {#get} returns
149
+ def io(put_key, get_key, input, cwd: nil, env: nil, force: false, wait: nil, ttl: nil, persist: false)
150
+ Beachcomber::FFI.call!(:bc_session_io, handle!, put_key, get_key, io_input(input), cwd, json_or_nil(env),
151
+ io_options(force, wait, ttl, persist))
152
+ end
153
+
117
154
  # {Client#put} on this session's connection; +path: nil+ is this
118
155
  # session's directory.
119
156
  #
@@ -128,13 +165,18 @@ module Beachcomber
128
165
  nil
129
166
  end
130
167
 
131
- # Closes the underlying connection. Idempotent.
168
+ # Closes the underlying connection. Idempotent and thread-safe.
132
169
  def close
133
- return if @closed
134
-
135
- @closed = true
136
- Beachcomber::FFI.close_session(@handle)
137
- @handle = nil
170
+ handle = @mutex.synchronize do
171
+ return if @closed
172
+
173
+ @closed = true
174
+ h = @handle
175
+ @handle = nil
176
+ ObjectSpace.undefine_finalizer(self)
177
+ h
178
+ end
179
+ Beachcomber::FFI.close_session(handle) if handle
138
180
  end
139
181
 
140
182
  private
@@ -168,7 +210,7 @@ module Beachcomber
168
210
  # library's discovery.
169
211
  # @param timeout_ms [Integer, nil] how long one read or write on the
170
212
  # socket may take, in milliseconds; at least 1. nil is the library's
171
- # default (100).
213
+ # default (250).
172
214
  # @param connect_timeout_ms [Integer, nil] how long connecting keeps
173
215
  # retrying before it gives up, in milliseconds; 0 is one attempt. nil
174
216
  # is the library's default (1500).
@@ -177,7 +219,7 @@ module Beachcomber
177
219
  # autostarts when the socket path is auto-discovered — never for an
178
220
  # explicit socket_path.
179
221
  # @param exec [Boolean, nil] whether a template this client renders
180
- # ({#get}, {#eval}) may call +exec()+; nil is the library's default
222
+ # ({#get}, {#eval}) may call +_.exec()+; nil is the library's default
181
223
  # (off), and a template that calls it then fails with
182
224
  # "exec() is disabled in this context".
183
225
  def initialize(socket_path: nil, timeout_ms: nil, connect_timeout_ms: nil, autostart: nil, exec: nil)
@@ -186,6 +228,7 @@ module Beachcomber
186
228
  autostart: autostart, exec: exec,
187
229
  )
188
230
  @handle = Beachcomber::FFI.new_client(options)
231
+ @mutex = Mutex.new
189
232
  ObjectSpace.define_finalizer(self, self.class.finalizer(@handle))
190
233
  end
191
234
 
@@ -277,6 +320,21 @@ module Beachcomber
277
320
  nil
278
321
  end
279
322
 
323
+ # The operation +comb io+ is: put +input+, a JSON object, at +put_key+, then read
324
+ # +get_key+ on a connection of its own with the input bound as +input+ in templates
325
+ # (+comb io+). +input+ is a Hash or JSON text. <tt>{field}</tt> in either key
326
+ # is filled from the input's top-level string or number +field+; a missing
327
+ # one is a {ParseError} and stores nothing. Input that is not a JSON
328
+ # object stores nothing and the read goes on. +cwd+ addresses the put as
329
+ # well as the read (none: the global entry); +ttl+ and +persist+ are {#put}'s, +force+
330
+ # and +wait+ {#get}'s.
331
+ #
332
+ # @return [Object, nil] what {#get} returns
333
+ def io(put_key, get_key, input, cwd: nil, env: nil, force: false, wait: nil, ttl: nil, persist: false)
334
+ Beachcomber::FFI.call!(:bc_io, handle!, put_key, get_key, io_input(input), cwd, json_or_nil(env),
335
+ io_options(force, wait, ttl, persist))
336
+ end
337
+
280
338
  # Forces the daemon to recompute a provider/key.
281
339
  #
282
340
  # @param key [String]
@@ -327,11 +385,15 @@ module Beachcomber
327
385
  # Frees the client's handle. Idempotent; a later operation fails. The
328
386
  # finalizer frees it when a client is never closed.
329
387
  def close
330
- return if @handle.nil?
388
+ handle = @mutex.synchronize do
389
+ return if @handle.nil?
331
390
 
332
- ObjectSpace.undefine_finalizer(self)
333
- Beachcomber::FFI.free_client(@handle)
334
- @handle = nil
391
+ h = @handle
392
+ @handle = nil
393
+ ObjectSpace.undefine_finalizer(self)
394
+ h
395
+ end
396
+ Beachcomber::FFI.free_client(handle) if handle
335
397
  end
336
398
 
337
399
  private
@@ -36,6 +36,7 @@ module Beachcomber
36
36
  define_error :ParseError, 'parse_error'
37
37
  define_error :ServerError, 'server_error'
38
38
  define_error :TimeoutError, 'timeout'
39
+ define_error :ProtocolMismatchError, 'protocol_mismatch'
39
40
  define_error :BusyError, 'busy'
40
41
  define_error :PanicError, 'panic'
41
42
  define_error :LibraryNotFoundError, 'library_not_found'
@@ -35,13 +35,13 @@ module Beachcomber
35
35
  # for the system loader, the bare file name) handed to dlopen.
36
36
  Candidate = Struct.new(:description, :path)
37
37
 
38
- # The 22 bc_* symbols this binding calls, checked at load (not on first
38
+ # The 24 bc_* symbols this binding calls, checked at load (not on first
39
39
  # use) so a version-skewed or partial install fails loudly up front.
40
40
  REQUIRED_SYMBOLS = %w[
41
41
  bc_version bc_client_new bc_client_free bc_string_free
42
- bc_get bc_put bc_refresh bc_status bc_introspect bc_hello
42
+ bc_get bc_put bc_io bc_refresh bc_status bc_introspect bc_hello
43
43
  bc_eval
44
- bc_session_open bc_session_close bc_session_get bc_session_put bc_session_refresh
44
+ bc_session_open bc_session_close bc_session_get bc_session_put bc_session_io bc_session_refresh
45
45
  bc_session_set_context
46
46
  bc_watch_open bc_watch_next bc_watch_cancel bc_watch_free
47
47
  ].freeze
@@ -108,37 +108,38 @@ module Beachcomber
108
108
  end
109
109
 
110
110
  def self.finish_load!(handle, path)
111
+ check_symbols!(handle, path)
112
+ fn = bind_functions!(handle)
111
113
  @handle = handle
112
114
  @library_path = path
113
- check_symbols!
114
- bind_functions!
115
+ @fn = fn
115
116
  @handle
116
117
  end
117
118
  private_class_method :finish_load!
118
119
 
119
- def self.symbol_present?(name)
120
- @handle[name]
120
+ def self.symbol_present?(handle, name)
121
+ handle[name]
121
122
  true
122
123
  rescue Fiddle::DLError
123
124
  false
124
125
  end
125
126
  private_class_method :symbol_present?
126
127
 
127
- def self.check_symbols!
128
- missing = REQUIRED_SYMBOLS.reject { |sym| symbol_present?(sym) }
128
+ def self.check_symbols!(handle, path)
129
+ missing = REQUIRED_SYMBOLS.reject { |sym| symbol_present?(handle, sym) }
129
130
  return if missing.empty?
130
131
 
131
132
  raise Beachcomber::LibrarySymbolError.new(
132
- "#{@library_path} is missing required symbol(s): #{missing.join(', ')}",
133
- library_version: safe_version_for_error,
133
+ "#{path} is missing required symbol(s): #{missing.join(', ')}",
134
+ library_version: safe_version_for_error(handle),
134
135
  )
135
136
  end
136
137
  private_class_method :check_symbols!
137
138
 
138
- def self.safe_version_for_error
139
- return 'unknown' unless symbol_present?('bc_version')
139
+ def self.safe_version_for_error(handle)
140
+ return 'unknown' unless symbol_present?(handle, 'bc_version')
140
141
 
141
- fn = Fiddle::Function.new(@handle['bc_version'], [], VOIDP)
142
+ fn = Fiddle::Function.new(handle['bc_version'], [], VOIDP)
142
143
  ptr = fn.call
143
144
  ptr.null? ? 'unknown' : ptr.to_s
144
145
  rescue StandardError
@@ -146,35 +147,37 @@ module Beachcomber
146
147
  end
147
148
  private_class_method :safe_version_for_error
148
149
 
149
- def self.bind_functions!
150
- @fn = {
151
- bc_version: fnew('bc_version', [], VOIDP),
152
- bc_client_new: fnew('bc_client_new', [VOIDP], VOIDP),
153
- bc_client_free: fnew('bc_client_free', [VOIDP], VOID),
154
- bc_string_free: fnew('bc_string_free', [VOIDP], VOID),
155
- bc_get: fnew('bc_get', [VOIDP, VOIDP, VOIDP, VOIDP, VOIDP], VOIDP),
156
- bc_put: fnew('bc_put', [VOIDP, VOIDP, VOIDP, VOIDP, VOIDP, INT], VOIDP),
157
- bc_refresh: fnew('bc_refresh', [VOIDP, VOIDP, VOIDP], VOIDP),
158
- bc_status: fnew('bc_status', [VOIDP], VOIDP),
159
- bc_introspect: fnew('bc_introspect', [VOIDP, VOIDP, VOIDP], VOIDP),
160
- bc_hello: fnew('bc_hello', [VOIDP], VOIDP),
161
- bc_eval: fnew('bc_eval', [VOIDP, VOIDP, VOIDP, VOIDP], VOIDP),
162
- bc_session_open: fnew('bc_session_open', [VOIDP], VOIDP),
163
- bc_session_close: fnew('bc_session_close', [VOIDP], VOID),
164
- bc_session_get: fnew('bc_session_get', [VOIDP, VOIDP, VOIDP, VOIDP, VOIDP], VOIDP),
165
- bc_session_put: fnew('bc_session_put', [VOIDP, VOIDP, VOIDP, VOIDP, VOIDP, INT], VOIDP),
166
- bc_session_refresh: fnew('bc_session_refresh', [VOIDP, VOIDP, VOIDP], VOIDP),
167
- bc_session_set_context: fnew('bc_session_set_context', [VOIDP, VOIDP], VOIDP),
168
- bc_watch_open: fnew('bc_watch_open', [VOIDP, VOIDP, VOIDP, VOIDP], VOIDP),
169
- bc_watch_next: fnew('bc_watch_next', [VOIDP, INT], VOIDP),
170
- bc_watch_cancel: fnew('bc_watch_cancel', [VOIDP], VOID),
171
- bc_watch_free: fnew('bc_watch_free', [VOIDP], VOID),
150
+ def self.bind_functions!(handle)
151
+ {
152
+ bc_version: fnew(handle, 'bc_version', [], VOIDP),
153
+ bc_client_new: fnew(handle, 'bc_client_new', [VOIDP], VOIDP),
154
+ bc_client_free: fnew(handle, 'bc_client_free', [VOIDP], VOID),
155
+ bc_string_free: fnew(handle, 'bc_string_free', [VOIDP], VOID),
156
+ bc_get: fnew(handle, 'bc_get', [VOIDP, VOIDP, VOIDP, VOIDP, VOIDP], VOIDP),
157
+ bc_put: fnew(handle, 'bc_put', [VOIDP, VOIDP, VOIDP, VOIDP, VOIDP, Fiddle::TYPE_UCHAR], VOIDP),
158
+ bc_io: fnew(handle, 'bc_io', [VOIDP, VOIDP, VOIDP, VOIDP, VOIDP, VOIDP, VOIDP], VOIDP),
159
+ bc_refresh: fnew(handle, 'bc_refresh', [VOIDP, VOIDP, VOIDP], VOIDP),
160
+ bc_status: fnew(handle, 'bc_status', [VOIDP], VOIDP),
161
+ bc_introspect: fnew(handle, 'bc_introspect', [VOIDP, VOIDP, VOIDP], VOIDP),
162
+ bc_hello: fnew(handle, 'bc_hello', [VOIDP], VOIDP),
163
+ bc_eval: fnew(handle, 'bc_eval', [VOIDP, VOIDP, VOIDP, VOIDP], VOIDP),
164
+ bc_session_open: fnew(handle, 'bc_session_open', [VOIDP], VOIDP),
165
+ bc_session_close: fnew(handle, 'bc_session_close', [VOIDP], VOID),
166
+ bc_session_get: fnew(handle, 'bc_session_get', [VOIDP, VOIDP, VOIDP, VOIDP, VOIDP], VOIDP),
167
+ bc_session_put: fnew(handle, 'bc_session_put', [VOIDP, VOIDP, VOIDP, VOIDP, VOIDP, Fiddle::TYPE_UCHAR], VOIDP),
168
+ bc_session_io: fnew(handle, 'bc_session_io', [VOIDP, VOIDP, VOIDP, VOIDP, VOIDP, VOIDP, VOIDP], VOIDP),
169
+ bc_session_refresh: fnew(handle, 'bc_session_refresh', [VOIDP, VOIDP, VOIDP], VOIDP),
170
+ bc_session_set_context: fnew(handle, 'bc_session_set_context', [VOIDP, VOIDP], VOIDP),
171
+ bc_watch_open: fnew(handle, 'bc_watch_open', [VOIDP, VOIDP, VOIDP, VOIDP], VOIDP),
172
+ bc_watch_next: fnew(handle, 'bc_watch_next', [VOIDP, INT], VOIDP),
173
+ bc_watch_cancel: fnew(handle, 'bc_watch_cancel', [VOIDP], VOID),
174
+ bc_watch_free: fnew(handle, 'bc_watch_free', [VOIDP], VOID),
172
175
  }
173
176
  end
174
177
  private_class_method :bind_functions!
175
178
 
176
- def self.fnew(name, args, ret)
177
- Fiddle::Function.new(@handle[name], args, ret)
179
+ def self.fnew(handle, name, args, ret)
180
+ Fiddle::Function.new(handle[name], args, ret)
178
181
  end
179
182
  private_class_method :fnew
180
183
 
@@ -202,9 +205,11 @@ module Beachcomber
202
205
  ptr = fn.call(*args)
203
206
  raise Beachcomber::IoError, "NULL returned from #{sym}" if ptr.nil? || ptr.null?
204
207
 
205
- json = ptr.to_s
206
- @fn[:bc_string_free].call(ptr)
207
- json
208
+ begin
209
+ ptr.to_s
210
+ ensure
211
+ @fn[:bc_string_free].call(ptr)
212
+ end
208
213
  end
209
214
 
210
215
  # Like raw_call, but decodes the {"ok":...} envelope and raises the
@@ -237,8 +242,8 @@ module Beachcomber
237
242
  @fn[:bc_watch_open].call(client_handle, key, cwd, env_json)
238
243
  end
239
244
 
240
- # Void-returning teardown calls (null-safe on the C side; harmless if
241
- # called more than once from a Ruby finalizer racing an explicit close).
245
+ # Void-returning teardown calls (null-safe on the C side; callers must
246
+ # ensure each non-null handle is freed at most once).
242
247
  def self.free_client(handle)
243
248
  load!
244
249
  @fn[:bc_client_free].call(handle)
@@ -16,6 +16,15 @@ module Beachcomber
16
16
  def initialize(handle)
17
17
  @handle = handle
18
18
  @closed = false
19
+ @mutex = Mutex.new
20
+ ObjectSpace.define_finalizer(self, self.class.finalizer(handle))
21
+ end
22
+
23
+ def self.finalizer(handle)
24
+ proc do
25
+ Beachcomber::FFI.cancel_watch(handle)
26
+ Beachcomber::FFI.free_watch(handle)
27
+ end
19
28
  end
20
29
 
21
30
  # Yields a WatchEvent per emitted change until the stream ends.
@@ -65,13 +74,21 @@ module Beachcomber
65
74
  nil
66
75
  end
67
76
 
68
- # Cancels the stream and frees its handle. Idempotent.
77
+ # Cancels the stream and frees its handle. Idempotent and thread-safe.
69
78
  def close
70
- return if @closed
79
+ handle = @mutex.synchronize do
80
+ return if @closed
81
+
82
+ @closed = true
83
+ h = @handle
84
+ @handle = nil
85
+ ObjectSpace.undefine_finalizer(self)
86
+ h
87
+ end
88
+ return unless handle
71
89
 
72
- @closed = true
73
- Beachcomber::FFI.cancel_watch(@handle)
74
- Beachcomber::FFI.free_watch(@handle)
90
+ Beachcomber::FFI.cancel_watch(handle)
91
+ Beachcomber::FFI.free_watch(handle)
75
92
  end
76
93
  end
77
94
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libbeachcomber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - NavistAu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-06 00:00:00.000000000 Z
11
+ date: 2026-12-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Communicates with the beachcomber daemon over a Unix domain socket to
14
14
  query cached shell-environment data (git state, hostname, battery, etc.).