Class: Rlyeh::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

crash, format_exception

Constructor Details

#initialize(app_class, options = {}) ⇒ Server

Returns a new instance of Server.



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

def initialize(app_class, options = {})
  @app_class = app_class
  @options = options.dup
  @host = @options.delete(:host) { '127.0.0.1' }
  @port = @options.delete(:port) { 46667 }
  @sessions = {}
  @server = Celluloid::IO::TCPServer.new @host, @port

  info "Rlyeh has emerged on #{@host}:#{@port}"
  async.run
end

Instance Attribute Details

#app_classObject (readonly)

Returns the value of attribute app_class.



11
12
13
# File 'lib/rlyeh/server.rb', line 11

def app_class
  @app_class
end

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/rlyeh/server.rb', line 10

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/rlyeh/server.rb', line 10

def options
  @options
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/rlyeh/server.rb', line 10

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



11
12
13
# File 'lib/rlyeh/server.rb', line 11

def server
  @server
end

#sessionsObject (readonly)

Returns the value of attribute sessions.



11
12
13
# File 'lib/rlyeh/server.rb', line 11

def sessions
  @sessions
end

Instance Method Details

#finalizeObject



25
26
27
28
# File 'lib/rlyeh/server.rb', line 25

def finalize
  @server.close
  info 'Rlyeh has sunk...'
end

#handle_connection(socket) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/rlyeh/server.rb', line 37

def handle_connection(socket)
  connection = Connection.new(self, socket)
  connection.run
rescue Exception => e
  crash e
ensure
  connection.close rescue nil
end

#load_session(session_id) ⇒ Object



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

def load_session(session_id)
  @sessions[session_id] ||= Rlyeh::Session.new(session_id)
end

#runObject



30
31
32
33
34
35
# File 'lib/rlyeh/server.rb', line 30

def run
  loop do
    socket = @server.accept
    async.handle_connection socket
  end
end