Skip to content

Instantly share code, notes, and snippets.

@hinrik
Created February 16, 2013 02:49
Show Gist options
  • Save hinrik/4965286 to your computer and use it in GitHub Desktop.
Save hinrik/4965286 to your computer and use it in GitHub Desktop.
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)
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