Skip to content
Browse files

Server commits seppuku

  • Loading branch information...
1 parent 8c3ee12 commit 6eecc1f8b480eacce7aa89698f0cbbda01f968ab @fractaloop fractaloop committed Dec 6, 2012
Showing with 12 additions and 6 deletions.
  1. +12 −6 bin/wu-server
View
18 bin/wu-server
@@ -17,15 +17,16 @@ load wu_file
$processor = File.basename(wu_file, '.rb').to_sym
# EventMachine server
-
+$clients = 0
class WukongServer < EM::Protocols::LineAndTextProtocol
def post_init
puts "[server] Client connected"
-
+ $clients += 1
+
dataflow_class = Wukong.dataflow(:server){ send($processor) }
flow_builder = Wukong.registry.retrieve(:server)
flow = flow_builder.build(processor: $processor)
-
+
@buffer = []
@processor = flow.stages.values.first
@processor.emitter = ->(value){ @buffer << value }
@@ -34,21 +35,26 @@ class WukongServer < EM::Protocols::LineAndTextProtocol
def receive_data(data)
@buffer = []
input = MultiJson.load data
-
+
op = proc { @processor.process(input) }
callback = proc { send_data MultiJson.dump(@buffer) + "\n" }
EM.defer(op, callback)
-
+
rescue MultiJson::DecodeError => ex
STDERR.puts "[server] Dropped: Malformed request"
end
def unbind
puts "[server] Client disconnected."
+ $clients -= 1
+ if $clients <= 0
+ puts "I die with honor."
+ exit
+ end
end
end
EM::run {
EM::start_server(Settings[:host], Settings[:port], WukongServer)
puts "Listening on #{Settings[:host]}:#{Settings[:port]}"
-}
+}

0 comments on commit 6eecc1f

Please sign in to comment.
Something went wrong with that request. Please try again.