Skip to content

Instantly share code, notes, and snippets.

@tinomen
Created January 26, 2011 01:43
Show Gist options
  • Save tinomen/796063 to your computer and use it in GitHub Desktop.
Save tinomen/796063 to your computer and use it in GitHub Desktop.
demo for urug on msgpack and 0mq
require 'Date'
class Client
attr_accessor :from, :msg
attr_reader :at, :diff
def initialize
@at = Time.now
end
def set_time(dump)
@at = Time._load(dump)
end
def to_msgpack
{:from => @from, :msg => @msg, :at => @at._dump}
end
def self.from_msgpack(msg)
f = Client.new
f.from = msg['from']
f.msg = msg['msg']
f.set_time(msg['at'])
dif = Time.now - f.at
puts "#{sprintf("%0.2f usec", dif*100)} - #{f.from} - #{f.msg}"
end
end
require 'Date'
require 'rubygems'
require 'msgpack'
require 'msgpack/rpc'
require './client'
@client = MessagePack::RPC::Client.new("127.0.0.1", 9090)
c = Client.new
c.from = "Tester"
msg = "weeeeeeeeeeeeeee "
10.times do |i|
c.msg = "#{msg} #{i.to_s}"
@client.call(:log, c.to_msgpack)
end
require 'Date'
require 'rubygems'
require 'msgpack'
require 'msgpack/rpc'
require './client'
class MyHandler
def log(msg)
c = Client.from_msgpack(msg)
end
end
svr = MessagePack::RPC::Server.new
svr.listen('0.0.0.0', 9090, MyHandler.new)
puts "ready to party"
svr.run
require 'Date'
require 'rubygems'
require 'msgpack'
require 'zmq'
require './client'
z = ZMQ::Context.new
s = z.socket(ZMQ::DOWNSTREAM)
s.bind("tcp://127.0.0.1:9010")
c = Client.new
c.from = "Tester"
msg = "weeeeeeeeeeeeeee "
10.times do |i|
c.msg = "weeeeeeeeeeeeeee #{i.to_s}"
s.send(MessagePack.pack(c.to_msgpack))
end
s.send({:exit => true}.to_msgpack)
require 'Date'
require 'rubygems'
require 'msgpack'
require 'zmq'
require './client'
z = ZMQ::Context.new
s = z.socket(ZMQ::UPSTREAM)
s.connect "tcp://0.0.0.0:9010"
exit = false
while !exit
msg = MessagePack.unpack(s.recv)
if msg['exit']
exit = true
else
c = Client.from_msgpack(msg)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment