Class: Whiskey::Server

Inherits:
Object
  • Object
show all
Includes:
Celluloid::IO
Defined in:
lib/whiskey/server.rb,
lib/whiskey/server/cycle.rb,
lib/whiskey/server/error.rb,
lib/whiskey/server/action.rb,
lib/whiskey/server/router.rb,
lib/whiskey/server/control.rb,
lib/whiskey/server/handler.rb,
lib/whiskey/server/receiver.rb,
lib/whiskey/server/responder.rb,
lib/whiskey/server/connection.rb,
lib/whiskey/server/serializer.rb,
lib/whiskey/server/interpretor.rb,
lib/whiskey/server/deserializer.rb,
lib/whiskey/server/configuration.rb

Defined Under Namespace

Modules: Control Classes: Action, Configuration, Connection, Cycle, Deserializer, Error, Handler, Interpretor, Receiver, Responder, Router, Serializer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Server

Returns a new instance of Server.



48
49
50
51
52
53
# File 'lib/whiskey/server.rb', line 48

def initialize(host, port)
  Whiskey.logger.info "Initializing a server at #{@@configuration}..."
  @server = TCPServer.new(host, port)
  Whiskey.logger.info "Successfully bound host and port!"
  async.run
end

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



46
47
48
# File 'lib/whiskey/server.rb', line 46

def server
  @server
end

Class Method Details

.configure(options = {}, &block) ⇒ Object

This method is the start point for the server, allowing the user to pass in settings for the server. It can be done like this:

Whiskey::Server.configure(host: "localhost", port: 4222)

or with a block:

Whiskey::Server.configure do |config|
  config.host = "localhost"
  config.port = 4222
end


32
33
34
35
36
# File 'lib/whiskey/server.rb', line 32

def self.configure(options = {}, &block)
  @@configuration = Configuration.new(options)
  instance_exec(@@configuration, &block) if block_given?
  start
end

.startObject

This method starts the server according to the configuration object and then follows the instructions set by Celluloid-Io examples.



40
41
42
43
44
# File 'lib/whiskey/server.rb', line 40

def self.start
  supervise(@@configuration.host, @@configuration.port)
  trap("INT") { supervisor.terminate; exit }
  sleep
end

Instance Method Details

#finalizer(callback) ⇒ Object



59
60
61
# File 'lib/whiskey/server.rb', line 59

def finalizer(callback)
  server.close if server
end

#handle_connection(socket) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/whiskey/server.rb', line 63

def handle_connection(socket)
  connection = Connection.new(socket)
  Whiskey.logger.info "New connection created from #{connection.id}"
  begin
    Handler.new(connection).handle
  rescue => connection_error
    Whiskey.logger.error connection_error
  ensure
    # If the connection has been broken, this might not work: Errno::EPIPE
    begin
      socket.write Serializer.new(Error.new(:internal_server_error).to_hash).data
    rescue => socket_error
      Whiskey.logger.error socket_error
    end
    socket.close
  end
end

#runObject



55
56
57
# File 'lib/whiskey/server.rb', line 55

def run
  loop { async.handle_connection server.accept }
end