lib/reel/server.rb in reel-0.4.0 vs lib/reel/server.rb in reel-0.5.0.pre
- old
+ new
@@ -1,21 +1,34 @@
module Reel
+ # Base class for Reel servers.
+ #
+ # This class is a Celluloid::IO actor which provides a barebones server
+ # which does not open a socket itself, it just begin handling connections once
+ # initialized with a specific kind of protocol-based server.
+
+ # For specific protocol support, use:
+
+ # Reel::Server::HTTP
+ # Reel::Server::HTTPS
+ # Coming soon: Reel::Server::UNIX
+
class Server
include Celluloid::IO
-
# How many connections to backlog in the TCP accept queue
DEFAULT_BACKLOG = 100
execute_block_on_receiver :initialize
finalizer :shutdown
- def initialize(host, port, backlog = DEFAULT_BACKLOG, &callback)
- # This is actually an evented Celluloid::IO::TCPServer
- @server = TCPServer.new(host, port)
- @server.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
- @server.listen(backlog)
+ 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
def shutdown
@server.close if @server
@@ -23,10 +36,21 @@
def run
loop { async.handle_connection @server.accept }
end
+ def optimize(socket)
+ if socket.is_a? TCPSocket
+ socket.setsockopt(Socket::IPPROTO_TCP, :TCP_NODELAY, 1)
+ end
+ end
+
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