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.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/harnex/adapters/codex_appserver.rb', line 250

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.



248
249
250
# File 'lib/harnex/adapters/codex_appserver.rb', line 248

def pid
  @pid
end

Instance Method Details

#closeObject



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/harnex/adapters/codex_appserver.rb', line 301

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



295
296
297
298
299
# File 'lib/harnex/adapters/codex_appserver.rb', line 295

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

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

#on_disconnect(&block) ⇒ Object



269
270
271
# File 'lib/harnex/adapters/codex_appserver.rb', line 269

def on_disconnect(&block)
  @disconnect_handler = block
end

#on_notification(&block) ⇒ Object



265
266
267
# File 'lib/harnex/adapters/codex_appserver.rb', line 265

def on_notification(&block)
  @notification_handler = block
end

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



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/harnex/adapters/codex_appserver.rb', line 277

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



273
274
275
# File 'lib/harnex/adapters/codex_appserver.rb', line 273

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