Class: Harnex::Adapters::CodexAppServer::JsonRpcClient

Inherits:
Object
  • Object
show all
Defined in:
lib/harnex/adapters/codex_appserver.rb

Overview

Minimal JSON-RPC 2.0 client. One JSON object per line. Responses keyed by id; everything else is a notification.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(read_io:, write_io:, pid: nil) ⇒ JsonRpcClient

Returns a new instance of JsonRpcClient.



264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/harnex/adapters/codex_appserver.rb', line 264

def initialize(read_io:, write_io:, pid: nil)
  @read_io = read_io
  @write_io = write_io
  @pid = pid
  @next_id = 1
  @pending = {}
  @id_mutex = Mutex.new
  @write_mutex = Mutex.new
  @notification_handler = nil
  @disconnect_handler = nil
  @disconnect_signaled = false
  @closed = false
  @reader_thread = nil
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



262
263
264
# File 'lib/harnex/adapters/codex_appserver.rb', line 262

def pid
  @pid
end

Instance Method Details

#closeObject



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/harnex/adapters/codex_appserver.rb', line 315

def close
  return if @closed

  @closed = true

  @id_mutex.synchronize do
    @pending.each_value { |q| q.push(StandardError.new("codex_appserver client closed")) }
    @pending.clear
  end

  begin
    @write_io.close unless @write_io.closed?
  rescue IOError
    nil
  end

  if @pid && process_alive?(@pid)
    sleep 0.05
    begin
      Process.kill("TERM", @pid)
    rescue Errno::ESRCH
      nil
    end
  end

  @reader_thread&.join(2)
end

#notify(method, params = {}) ⇒ Object



309
310
311
312
313
# File 'lib/harnex/adapters/codex_appserver.rb', line 309

def notify(method, params = {})
  return if @closed

  write_line({ jsonrpc: "2.0", method: method, params: params })
end

#on_disconnect(&block) ⇒ Object



283
284
285
# File 'lib/harnex/adapters/codex_appserver.rb', line 283

def on_disconnect(&block)
  @disconnect_handler = block
end

#on_notification(&block) ⇒ Object



279
280
281
# File 'lib/harnex/adapters/codex_appserver.rb', line 279

def on_notification(&block)
  @notification_handler = block
end

#request(method, params = {}) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/harnex/adapters/codex_appserver.rb', line 291

def request(method, params = {})
  raise "codex_appserver client is closed" if @closed

  queue = Queue.new
  id = @id_mutex.synchronize do
    assigned = @next_id
    @next_id += 1
    @pending[assigned] = queue
    assigned
  end

  write_line({ jsonrpc: "2.0", id: id, method: method, params: params })
  result = queue.pop
  raise result if result.is_a?(Exception)

  result
end

#startObject



287
288
289
# File 'lib/harnex/adapters/codex_appserver.rb', line 287

def start
  @reader_thread = Thread.new { read_loop }
end