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.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/harnex/adapters/codex_appserver.rb', line 229

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.



227
228
229
# File 'lib/harnex/adapters/codex_appserver.rb', line 227

def pid
  @pid
end

Instance Method Details

#closeObject



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/harnex/adapters/codex_appserver.rb', line 280

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



274
275
276
277
278
# File 'lib/harnex/adapters/codex_appserver.rb', line 274

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

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

#on_disconnect(&block) ⇒ Object



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

def on_disconnect(&block)
  @disconnect_handler = block
end

#on_notification(&block) ⇒ Object



244
245
246
# File 'lib/harnex/adapters/codex_appserver.rb', line 244

def on_notification(&block)
  @notification_handler = block
end

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



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/harnex/adapters/codex_appserver.rb', line 256

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



252
253
254
# File 'lib/harnex/adapters/codex_appserver.rb', line 252

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