secret
Created

  • Download Gist
jruby_fail.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#!/usr/bin/env ruby
 
$:.push File.expand_path('../../lib', __FILE__)
require 'celluloid/io'
 
class Server
include Celluloid::IO
 
def initialize(host, port)
puts "*** Starting server on #{host}:#{port}"
 
@server = TCPServer.new(host, port)
run!
end
 
def finalize
puts "Server closing"
@server.close if @server
end
 
def run
loop { handle_connection! @server.accept }
end
 
def handle_connection(socket)
_, port, host = socket.peeraddr
puts "*** Received connection from #{host}:#{port}"
loop { puts socket.readpartial(4096) }
rescue EOFError
puts "*** #{host}:#{port} disconnected"
socket.close
end
end
 
class Client
include Celluloid::IO
 
def initialize(host, port)
puts "*** Connecting to echo server on #{host}:#{port}"
 
@socket = TCPSocket.new(host, port)
run!
end
 
def shutdown
@socket.close if @socket
end
 
def run
loop { puts @socket.readpartial 4096 }
rescue EOFError => e
puts "Client socket closed due to #{e.message}!"
terminate
end
 
end
 
server = Server.new("127.0.0.1", 1234)
client = Client.new("127.0.0.1", 1234)
client.shutdown

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.