Permalink
Browse files

Ruby translation of mtserver example

  • Loading branch information...
zetaben committed Oct 2, 2011
1 parent e935076 commit 320bf378b1ae3f21f4961c87e3ed2cd1652c60e0
Showing with 42 additions and 0 deletions.
  1. +42 −0 examples/Ruby/mtserver.rb
View
@@ -0,0 +1,42 @@
+#
+# Multithreaded Hello World server
+#
+
+require 'rubygems'
+require 'ffi-rzmq'
+
+def worker_routine(context)
+ # Socket to talk to dispatcher
+ receiver = context.socket(ZMQ::REP)
+ receiver.connect("inproc://workers")
+
+ loop do
+ string = receiver.recv_string
+ puts "Recieved request: [#{string}]"
+ # Do some 'work'
+ sleep(1)
+ # Send reply back to client
+ receiver.send_string("world")
+ end
+end
+
+
+context = ZMQ::Context.new
+
+puts "Starting Hello World server..."
+
+# socket to listen for clients
+clients = context.socket(ZMQ::ROUTER)
+clients.bind("tcp://*:5555")
+
+# socket to talk to workers
+workers = context.socket(ZMQ::DEALER)
+workers.bind("inproc://workers")
+
+# Launch pool of worker threads
+5.times do
+ Thread.new{worker_routine(context)}
+end
+
+# Connect work threads to client threads via a queue
+ZMQ::Device.new(ZMQ::QUEUE,clients,workers)

0 comments on commit 320bf37

Please sign in to comment.