Skip to content

Instantly share code, notes, and snippets.

@erikh
Created January 21, 2012 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikh/748cd566a52c17a5a4a1 to your computer and use it in GitHub Desktop.
Save erikh/748cd566a52c17a5a4a1 to your computer and use it in GitHub Desktop.
require 'sm_zmq'
client = SM::Control::Client.new
trap(:INT) { client.shutdown }
loop do
puts client.status
sleep 0.1
end
client.shutdown
require 'sm_zmq'
server = SM::Control::Server.new
trap(:INT) { server.shutdown }
loop do
server.delegate
sleep 0.1
end
server.shutdown
require 'zmq'
module SM
module Control
class Client
def initialize(address="ipc://sm-control")
@socket = ZMQ::Context.new.socket(ZMQ::REQ)
@address = address
connect
end
def connect
@socket.connect(@address)
end
def msgpair(msg)
while (arr = ZMQ.select(nil, [@socket], [@socket], 0.1)).nil?
end
@socket.send(msg)
while (arr = ZMQ.select([@socket], nil, [@socket], 0.1)).nil?
end
return @socket.recv
end
def status
msgpair("STATUS")
end
def shutdown
@socket.close
end
end
class Server
def initialize(address="ipc://sm-control")
@socket = ZMQ::Context.new.socket(ZMQ::REP)
@address = address
bind
end
def bind
@socket.bind(@address)
end
def next_message
@socket.recv(ZMQ::NOBLOCK)
rescue IOError
bind
retry
end
def delegate
msg = next_message
p msg
case msg
when "STATUS"
@socket.send(SM::Status.new.status)
end
end
def shutdown
@socket.close
end
end
end
class Status
def status
rand.to_s
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment