Class: Telegraph::Operator

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/telegraph/operator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, logger

Constructor Details

#initialize(socket, switchboard) ⇒ Operator

Returns a new instance of Operator.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/telegraph/operator.rb', line 13

def initialize(socket, switchboard)
  @socket = socket
  @switchboard = switchboard
  @accept_thread = Thread.new do
    @socket.listen 100
    loop do
      if @should_shutdown
        @socket.close
        @switchboard.close_all_wires
        break
      end

      begin
        client = @socket.accept_nonblock
        debug { "Accepted connection: #{client.inspect}" }
        @switchboard.add_wire Wire.new(client)
      rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR
        connection_ready, = IO.select([@socket], nil, nil, 0.25)
        retry if connection_ready
      end
    end
  end
end

Instance Attribute Details

#switchboardObject (readonly)

Returns the value of attribute switchboard.



7
8
9
# File 'lib/telegraph/operator.rb', line 7

def switchboard
  @switchboard
end

Class Method Details

.listen(host, port, switchboard = Switchboard.new) ⇒ Object



9
10
11
# File 'lib/telegraph/operator.rb', line 9

def self.listen(host, port, switchboard = Switchboard.new)
  new TCPServer.new(host, port), switchboard
end

Instance Method Details

#portObject



37
38
39
# File 'lib/telegraph/operator.rb', line 37

def port
  @socket.addr[1]
end

#shutdownObject



41
42
43
44
45
# File 'lib/telegraph/operator.rb', line 41

def shutdown
  debug { "Shutting down" }
  @should_shutdown = true
  @accept_thread.join
end