Class: Reel::Server

Inherits:
Object
  • Object
show all
Includes:
Celluloid::IO
Defined in:
lib/reel/server.rb,
lib/reel/server/http.rb,
lib/reel/server/https.rb

Overview

Reel::Server::HTTP Reel::Server::HTTPS Coming soon: Reel::Server::UNIX

Direct Known Subclasses

HTTP, HTTPS

Defined Under Namespace

Classes: HTTP, HTTPS

Constant Summary collapse

DEFAULT_BACKLOG =

How many connections to backlog in the TCP accept queue

100

Instance Method Summary collapse

Constructor Details

#initialize(server, options = {}, &callback) ⇒ Server

Returns a new instance of Server.



22
23
24
25
26
27
28
29
30
31
# File 'lib/reel/server.rb', line 22

def initialize(server, options={}, &callback)
  @spy      = STDOUT if options[:spy]
  @options  = options
  @callback = callback
  @server   = server

  @server.listen(options.fetch(:backlog, DEFAULT_BACKLOG))

  async.run
end

Instance Method Details

#handle_connection(socket) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/reel/server.rb', line 41

def handle_connection(socket)
  if @spy
    require 'reel/spy'
    socket = Reel::Spy.new(socket, @spy)
  end

  connection = Connection.new(socket)

  begin
    @callback.call(connection)
  ensure
    if connection.attached?
      connection.close rescue nil
    end
  end
rescue RequestError, EOFError
  # Client disconnected prematurely
  # TODO: log this?
end

#runObject



37
38
39
# File 'lib/reel/server.rb', line 37

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

#shutdownObject



33
34
35
# File 'lib/reel/server.rb', line 33

def shutdown
  @server.close if @server
end