The following example fails unless line 8 is commented out:
#!/usr/bin/env ruby
#
# Run this as: bundle exec examples/echo_server.rb
require 'bundler/setup'
require 'celluloid/io'
Celluloid.task_class = Celluloid::TaskThread
class Server
include Celluloid::IO
finalizer :finalize
def initialize
async.run
end
def run
@handler = UDPHandler.new("0.0.0.0", 8000)
loop do
puts @handler.inspect
sleep 10
end
end
end
class UDPHandler
include Celluloid::IO
finalizer :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 = UDPSocket.new
@server.bind(host, port)
async.run
end
def finalize
@server.close if @server
end
def run
loop { handle_connection }
end
def handle_connection
data, (_, port, host) = @server.recvfrom(1024)
puts "*** Received connection from #{host}:#{port}"
@server.send(data, 0, host, port)
rescue => error
puts "*** #{error.inspect}"
end
end
supervisor = Server.supervise
trap("INT") { supervisor.terminate; exit }
sleep
You can test the server by running:
dig @localhost -p 8000 dev.mydomain.org A
It works fine once, then the 2nd time, it gets stuck inside recvfrom. Is this correct behaviour?
The following example fails unless line 8 is commented out:
You can test the server by running:
It works fine once, then the 2nd time, it gets stuck inside
recvfrom
. Is this correct behaviour?