test/simple_server.rb in ruby_skynet-0.2.0 vs test/simple_server.rb in ruby_skynet-0.3.0
- old
+ new
@@ -1,14 +1,14 @@
require 'rubygems'
require 'socket'
require 'bson'
require 'semantic_logger'
+require 'celluloid/io'
# This a simple stand-alone server that does not use the Skynet code so that
# the Skynet code can be tested
-
# Read the bson document, returning nil if the IO is closed
# before receiving any data or a complete BSON document
def read_bson_document(io)
bytebuf = BSON::ByteBuffer.new
# Read 4 byte size of following BSON document
@@ -24,39 +24,29 @@
end
# Simple single threaded server for testing purposes using a local socket
# Sends and receives BSON Messages
class SimpleServer
- attr_reader :thread
- def initialize(port = 2000)
- start(port)
- end
+ include Celluloid::IO
- def start(port)
- @server = TCPServer.open(port)
+ def initialize(port)
+ # Since we included Celluloid::IO, we're actually making a
+ # Celluloid::IO::TCPServer here
+ @server = TCPServer.new('127.0.0.1', port)
@logger = SemanticLogger::Logger.new(self.class)
+ run!
+ end
- @thread = Thread.new do
- loop do
- @logger.debug "Waiting for a client to connect"
-
- # Wait for a client to connect
- on_request(@server.accept)
- end
+ def run
+ loop do
+ @logger.debug "Waiting for a client to connect"
+ handle_connection!(@server.accept)
end
end
- def stop
- if @thread
- @thread.kill
- @thread.join
- @thread = nil
- end
- begin
- @server.close if @server
- rescue IOError
- end
+ def finalize
+ @server.close if @server
end
# Called for each message received from the client
# Returns a Hash that is sent back to the caller
def on_message(method, params)
@@ -76,20 +66,19 @@
{ 'result' => "Unknown method: #{method}" }
end
end
# Called for each client connection
- # In a real server each request would be handled in a separate thread
- def on_request(client)
+ def handle_connection(client)
@logger.debug "Client connected, waiting for data from client"
# Process handshake
handshake = {
'registered' => true,
'clientid' => '123'
}
- client.print(BSON.serialize(handshake))
+ client.write(BSON.serialize(handshake))
read_bson_document(client)
while(header = read_bson_document(client)) do
@logger.debug "\n******************"
@logger.debug "Received Request"
@@ -100,35 +89,33 @@
break unless request
if reply = on_message(request['method'], BSON.deserialize(request['in']))
@logger.debug "Sending Header"
# For this test we just send back the received header
- client.print(BSON.serialize(header))
+ client.write(BSON.serialize(header))
@logger.debug "Sending Reply"
@logger.trace 'Reply', reply
- client.print(BSON.serialize({'out' => BSON.serialize(reply).to_s}))
+ client.write(BSON.serialize({'out' => BSON.serialize(reply).to_s}))
else
@logger.debug "Closing client since no reply is being sent back"
@server.close
client.close
@logger.debug "Server closed"
- #@thread.kill
- @logger.debug "thread killed"
- start(2000)
+ run!
@logger.debug "Server Restarted"
break
end
end
# Disconnect from the client
client.close
@logger.debug "Disconnected from the client"
end
-
end
if $0 == __FILE__
SemanticLogger::Logger.default_level = :trace
SemanticLogger::Logger.appenders << SemanticLogger::Appender::File.new(STDOUT)
+ Celluloid.logger = SemanticLogger::Logger.new('Celluloid')
server = SimpleServer.new(2000)
server.thread.join
end
\ No newline at end of file