Class: Encom::ServerTransport::Stdio

Inherits:
Base
  • Object
show all
Defined in:
lib/encom/server_transport/stdio.rb

Instance Attribute Summary

Attributes inherited from Base

#server

Instance Method Summary collapse

Methods inherited from Base

#initialize, #process_message

Constructor Details

This class inherits a constructor from Encom::ServerTransport::Base

Instance Method Details

#debug(message) ⇒ Object



53
54
55
56
57
# File 'lib/encom/server_transport/stdio.rb', line 53

def debug(message)
  return unless @debug

  warn "[Encom::ServerTransport::Stdio] #{message}"
end

#send_message(message) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/encom/server_transport/stdio.rb', line 45

def send_message(message)
  json = JSON.generate(message)
  debug "Sending: #{message.inspect}"
  puts json # Write to stdout
  $stdout.flush
  true
end

#startObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/encom/server_transport/stdio.rb', line 9

def start
  debug 'Starting StdIO transport server'
  debug 'Listening on stdin, writing to stdout...'

  # Enable line buffering for stdout
  $stdout.sync = true

  # Set up signal handlers for graceful shutdown
  setup_signal_handlers

  @running = true

  # Process messages until stdin is closed or shutdown is requested
  while @running && (line = $stdin.gets)
    begin
      message = JSON.parse(line, symbolize_names: true)
      debug "Received: #{message.inspect}"
      process_message(message)
    rescue JSON::ParserError => e
      debug "Error parsing message: #{e.message}"
      next
    rescue StandardError => e
      debug "Error processing message: #{e.message}"
      next
    end
  end

  debug 'StdIO transport server stopped'
end

#stopObject



39
40
41
42
43
# File 'lib/encom/server_transport/stdio.rb', line 39

def stop
  debug 'Stopping StdIO transport server'
  @running = false
  # No specific cleanup needed for stdio beyond setting running to false
end