class StickyElephant::Server

Attributes

configuration[R]
logger[R]

Public Class Methods

new(configuration) click to toggle source
# File lib/sticky_elephant/server.rb, line 3
def initialize(configuration)
  @configuration = configuration

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

Public Instance Methods

listen() click to toggle source
# 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

Private Instance Methods

log_name() click to toggle source
# File lib/sticky_elephant/server.rb, line 37
def log_name
  "SE server"
end