Created
January 31, 2014 08:52
-
-
Save HoneyryderChuck/8728622 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/io' | |
require 'celluloid/autostart' | |
class Server | |
include Celluloid::IO | |
finalizer :shutdown | |
def initialize(host, port) | |
puts "*** Starting server" | |
@server = TCPServer.new(host, port) | |
async.run | |
end | |
def shutdown ; @server.close if @server ; end | |
def run | |
loop { async.handle_connection @server.accept } | |
end | |
def handle_connection(sock) | |
while (comm = sock.readpartial(4096)) | |
sock.write "command received: #{comm}" | |
every(0.2) { sock.write "here it goes a bit more!!!" } | |
end | |
rescue EOFError | |
puts "Ups, something went wrong!" | |
sock.close | |
end | |
end | |
class Client | |
def initialize(host, port) | |
puts "*** Started client" | |
@client = Celluloid::IO::TCPSocket.new(host, port) | |
end | |
def cmd(command) | |
puts "sending command #{command}" | |
@client.write command | |
# part I'm not sure about | |
loop { puts @client.readpartial(4096) } | |
end | |
end | |
Server.new("127.0.0.1", 6666) | |
client = Client.new("127.0.0.1", 6666) | |
client.cmd("infinite echo") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment