Skip to content

Instantly share code, notes, and snippets.

@andrewvc
Created August 31, 2010 15:16
Show Gist options
  • Save andrewvc/2b1b44d8ef4fad1a84d8 to your computer and use it in GitHub Desktop.
Save andrewvc/2b1b44d8ef4fad1a84d8 to your computer and use it in GitHub Desktop.
# NOTE: This example isn't quite ready for students,
# It's not styled right, and is just here for me to
# rough out an idea for this lesson.
# Annotations and explanations will come in good time
require 'rubygems'
require 'zmqmachine'
Thread.abort_on_exception = true
ADDR = ZM::Address.new('127.0.0.1', 2200, :tcp)
class PubHandler
def initialize(ctx,name)
@ctx = ctx
@name = name
@sent_messages = 0
end
def on_attach(socket)
socket.bind(ADDR)
end
def on_writable(socket)
@ctx.deregister_writable(socket)
return true
end
end
class SubHandler
def initialize(ctx,name)
@ctx = ctx
@name = name
end
def on_attach(socket)
socket.connect(ADDR)
socket.subscribe('')
end
def on_readable(socket,messages)
message = messages.first.copy_out_string
puts "#{@name}: #{message}"
end
end
reactor = ZM::Reactor.new(:my_reactor).run do |ctx|
ctx.pub_socket(PubHandler.new(ctx, 'pub1'))
ctx.sub_socket(SubHandler.new(ctx, 'sub1'))
ctx.sub_socket(SubHandler.new(ctx, 'sub2'))
end
reactor.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment