Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

simple ZMQ worker to respond to routers requests

  • Loading branch information...
commit f2427e258324b3b6ae8ad9d1c7c99eac5e6898f0 1 parent 1e23bee
@igrigorik authored
Showing with 74 additions and 0 deletions.
  1. +13 −0 hello.rb
  2. +61 −0 lib/worker.rb
View
13 hello.rb
@@ -0,0 +1,13 @@
+require 'lib/worker'
+
+class HelloWorld < Worker
+ def response(head, body)
+ p [:HELLO_WORLD, head, body]
+
+ [200, {'X-ZMQ' => @worker_identity}, "Hello from #{@worker_identity}"]
+ end
+end
+
+puts "Starting worker: #{ARGV[0]}"
+w = HelloWorld.new({:route => 'tcp://127.0.0.1:8000', :identity => ARGV[0]})
+w.run
View
61 lib/worker.rb
@@ -0,0 +1,61 @@
+$: << '/git/spdy/lib'
+
+require 'ffi-rzmq'
+require 'spdy'
+
+class Worker
+ def initialize(opts = {})
+ @worker_identity = opts[:identity]
+
+ ctx = ZMQ::Context.new(1)
+ @conn = ctx.socket(ZMQ::XREP)
+ @conn.setsockopt(ZMQ::IDENTITY, opts[:identity])
+ @conn.connect(opts[:route])
+
+ @stream_id = nil
+ @headers = {}
+ @body = ''
+
+ @p = SPDY::Parser.new
+ @p.on_headers_complete do |stream, astream, priority, head|
+ @stream_id = stream
+ @headers = head
+ end
+
+ @p.on_body do |stream, body|
+ @body << body
+ end
+
+ @p.on_message_complete do |stream|
+ status, head, body = response(@headers, @body)
+
+ synreply = SPDY::Protocol::Control::SynReply.new
+ headers = {'status' => status.to_s, 'version' => 'HTTP/1.1'}.merge(head)
+ synreply.create(:stream_id => @stream_id, :headers => headers)
+
+ @conn.send_string(@identity, ZMQ::SNDMORE)
+ @conn.send_string(synreply.to_binary_s)
+
+ # Send body & close connection
+ resp = SPDY::Protocol::Data::Frame.new
+ resp.create(:stream_id => @stream_id, :flags => 1, :data => body)
+
+ @conn.send_string(@identity, ZMQ::SNDMORE)
+ @conn.send_string(resp.to_binary_s)
+ end
+ end
+
+ def run
+ loop do
+ @identity = @conn.recv_string()
+ delimiter = @conn.recv_string()
+
+ head = @conn.recv_string()
+ body = @conn.recv_string()
+
+ @p << head
+ @p << body
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.