-
-
Save erikh/748cd566a52c17a5a4a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sm_zmq' | |
client = SM::Control::Client.new | |
trap(:INT) { client.shutdown } | |
loop do | |
puts client.status | |
sleep 0.1 | |
end | |
client.shutdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sm_zmq' | |
server = SM::Control::Server.new | |
trap(:INT) { server.shutdown } | |
loop do | |
server.delegate | |
sleep 0.1 | |
end | |
server.shutdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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