Class: Raval::Server

Inherits:
Object
  • Object
show all
Includes:
Celluloid::IO
Defined in:
lib/raval/server.rb

Overview

The beginning of things. Listens on a TCP socket for incoming connections and spins off a new agent to handle each connection.

Instance Method Summary collapse

Constructor Details

#initialize(host, port, driver, driver_opts) ⇒ Server

Returns a new instance of Server.



13
14
15
16
17
18
19
20
21
22
# File 'lib/raval/server.rb', line 13

def initialize(host, port, driver, driver_opts)
  @driver, @driver_opts = driver, driver_opts
  # Since we included Celluloid::IO, we're actually making a
  # Celluloid::IO::TCPServer here
  @server = TCPServer.new(host, port)
  run!
rescue Errno::EACCES
  $stderr.puts "Unable to listen on port #{port}"
  exit
end

Instance Method Details

#finalizeObject



24
25
26
# File 'lib/raval/server.rb', line 24

def finalize
  @server.close if @server
end

#handle_connection(socket) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/raval/server.rb', line 32

def handle_connection(socket)
  if @driver_opts
    driver = @driver.new(@driver_opts)
  else
    driver = @driver.new
  end
  handler = Handler.new(driver)
  connection = Connection.new(handler, socket)
  connection.read_commands
rescue EOFError, IOError
  puts "*** #{connection.host}:#{connection.port} disconnected"
end

#runObject



28
29
30
# File 'lib/raval/server.rb', line 28

def run
  loop { handle_connection! @server.accept }
end