Class: DohLogger::SocketViewer

Inherits:
Object
  • Object
show all
Defined in:
lib/doh/logger/socket_viewer.rb

Overview

this class accepts log events through potentially multiple sockets combining the events from the multiple sockets as they’re received into a single event stream this is typically used in conjunction with socket_acceptor.rb to do remote debugging

Instance Method Summary collapse

Constructor Details

#initialize(port = DohLogger::SocketAcceptor::default_port) ⇒ SocketViewer

Returns a new instance of SocketViewer.



15
16
17
18
# File 'lib/doh/logger/socket_viewer.rb', line 15

def initialize(port = DohLogger::SocketAcceptor::default_port)
  @events = Queue.new
  @server = TCPServer.new(port)
end

Instance Method Details

#accept_connectionsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/doh/logger/socket_viewer.rb', line 25

def accept_connections
  thread_socks = []
  begin
    loop do
      sock = @server.accept
      thread_socks.push([Thread.new{handle_events_for_sock(sock)}, sock])
    end
  rescue IOError => e
  ensure
    thread_socks.each do |elem|
      elem[1].close
      elem[0].join
    end
  end
end

#handle_eventsObject



53
54
55
56
57
58
59
60
61
# File 'lib/doh/logger/socket_viewer.rb', line 53

def handle_events
  thread = Thread.new {accept_connections}
  loop do
    event_str = @events.deq
    break if event_str == :finished
    event = Marshal::load(event_str)
    yield event
  end
end

#handle_events_for_sock(sock) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/doh/logger/socket_viewer.rb', line 41

def handle_events_for_sock(sock)
  begin
    begin
      obj = sock.read_object
      @events.enq(obj)
    end until sock.closed?
  rescue IOError => e
    #silently ignore IOErrors?
  end
  thread.join
end

#shutdownObject



20
21
22
23
# File 'lib/doh/logger/socket_viewer.rb', line 20

def shutdown
  @server.close
  @events.enq(:finished)
end