Created
April 10, 2014 10:07
-
-
Save mengzhuo/10364287 to your computer and use it in GitHub Desktop.
forwarder_device.py
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
import zmq | |
def main(): | |
try: | |
context = zmq.Context(1) | |
# Socket facing clients | |
frontend = context.socket(zmq.SUB) | |
frontend.bind("tcp://*:5559") | |
frontend.setsockopt(zmq.SUBSCRIBE, "") | |
# Socket facing services | |
backend = context.socket(zmq.PUB) | |
backend.bind("tcp://*:5560") | |
zmq.device(zmq.FORWARDER, frontend, backend) | |
except Exception, e: | |
print e | |
print "bringing down zmq device" | |
finally: | |
pass | |
frontend.close() | |
backend.close() | |
context.term() | |
if __name__ == "__main__": | |
main() |
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
import zmq | |
import random | |
import sys | |
import time | |
port = "5559" | |
context = zmq.Context() | |
socket = context.socket(zmq.PUB) | |
socket.connect("tcp://localhost:%s" % port) | |
publisher_id = random.randrange(0,9999) | |
while True: | |
topic = random.randrange(1,10) | |
messagedata = "server#%s" % publisher_id | |
print "%s %s" % (topic, messagedata) | |
socket.send("%d %s" % (topic, messagedata)) | |
time.sleep(1) |
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
import sys | |
import zmq | |
port = "5560" | |
# Socket to talk to server | |
context = zmq.Context() | |
socket = context.socket(zmq.SUB) | |
print "Collecting updates from server..." | |
socket.connect ("tcp://localhost:%s" % port) | |
topicfilter = "9" | |
socket.setsockopt(zmq.SUBSCRIBE, topicfilter) | |
for update_nbr in range(10): | |
string = socket.recv() | |
topic, messagedata = string.split() | |
print topic, messagedata |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment