Skip to content

Instantly share code, notes, and snippets.

@jonasschneider
Created January 28, 2012 20:49
Show Gist options
  • Save jonasschneider/1695708 to your computer and use it in GitHub Desktop.
Save jonasschneider/1695708 to your computer and use it in GitHub Desktop.
# author: Oleg Sidorov <4pcbr> i4pcbr@gmail.com
# this code is licenced under the MIT/X11 licence.
require 'rubygems'
require 'bundler'
Bundler.require
context = ZMQ::Context.new
socket = context.socket(ZMQ::ROUTER)
socket.bind('tcp://*:5559')
loop do
socket.recv_strings(msg = [])
puts "got #{msg.inspect}"
envelopes = msg.slice!(0, msg.index('')+1)
response = envelopes + ['Ohai :)']
socket.send_strings(response)
end
class EMTestPullHandler
attr_reader :received
def on_readable(socket, parts)
p parts.map{|p| p.copy_out_string }
end
end
#config['handler'] = XREQHandler.new(@router)
config['ctx'] = EM::ZeroMQ::Context.new(1)
config['handler'] = EMTestPullHandler.new
config['zmq'] ||= config['ctx'].connect(ZMQ::DEALER, 'tcp://localhost:5559', config['handler'], :identity => config['identity'])
require 'bundler'
Bundler.setup
# $: << '/git/spdy/lib'
# $: << '/git/goliath/lib'
# $: << '/git/em-zeromq/lib'
#
require './lib/headers'
require 'em-zeromq'
require 'goliath'
class Trivial < Goliath::API
def call(env)
env.config['zmq'] ||= env.config['ctx'].connect(ZMQ::DEALER, 'tcp://localhost:5559', config['handler'], :identity => config['identity'])
super
end
def response(env)
env.logger.info "Finished connection-request"
parts = [
"\x00\x00\x00\x04",
"",
"\x00\x06\x00\nuser-agent\x00dcurl/7.21.6 (i486-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.22 libssh2/1.2.6\x00\x04host\x00\x0Elocalhost:9000\x00\x06accept\x00\x03*/*\x00\aversion\x00\x031.1\x00\x06method\x00\x03GET\x00\x03url\x00\x01/",
""
]
puts 'sending'
sent = env.config['zmq'].send_msg(*parts)
env.stream_close
[nil, nil, nil]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment