Class: StickyElephant::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/sticky_elephant/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Server

Returns a new instance of Server.



3
4
5
6
7
8
# File 'lib/sticky_elephant/server.rb', line 3

def initialize(configuration)
  @configuration = configuration

  @logger = ElephantLogger.new(configuration)
  @logger.info(log_name) { "Launching" }
end

Instance Method Details

#listenObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sticky_elephant/server.rb', line 10

def listen
  server = TCPServer.open(configuration.host, configuration.port)
  Thread.abort_on_exception = configuration.abort_on_exception
  loop do
    begin
      Thread.start(server.accept) do |socket|
        remote_address = begin
                           socket.remote_address.ip_address.to_s
                         rescue Errno::EINVAL
                           "localhost"
                         end
        logger.info(log_name) { "connection from #{remote_address} accepted" }
        Connection.new(socket, logger: logger).process
      end
    rescue Interrupt
      logger.info(log_name) { "Caught ctrl-c, shutting down" }
      Thread.list.each {|t| t.kill unless t == Thread.current }
      logger.close
      exit(0)
    end
  end
end