Created
February 16, 2013 02:49
-
-
Save hinrik/4965286 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'celluloid' | |
require 'celluloid/io' | |
class EchoServer | |
include Celluloid::IO | |
finalizer :echo_finalize | |
def initialize(host, port) | |
puts "*** Starting echo server on #{host}:#{port}" | |
# Since we included Celluloid::IO, we're actually making a | |
# Celluloid::IO::TCPServer here | |
@server = TCPServer.new(host, port) | |
async.run | |
end | |
def echo_finalize | |
@server.close if @server | |
end | |
private | |
def run | |
loop { async.handle_connection @server.accept } | |
end | |
def handle_connection(socket) | |
_, port, host = socket.peeraddr | |
puts "*** Received connection from #{host}:#{port}" | |
loop { socket.write socket.readpartial(4096) } | |
rescue EOFError | |
puts "*** #{host}:#{port} disconnected" | |
end | |
end | |
server = EchoServer.new('localhost', 9000) | |
Celluloid.join(server) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
D, [2013-02-15T21:48:41.958060 #27596] DEBUG -- : Terminating 5 actors... | |
D, [2013-02-15T21:48:41.959060 #27596] DEBUG -- : Shutdown completed cleanly | |
echo.rb:37:in `<main>': undefined method `join' for Celluloid:Module (NoMethodError) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment