class PluginManager(EventManager): def __init__(self, controller): super(PluginManager, self).__init__(controller) self.load_plugins() try: config = self.controller.config_file.items('router') except NoSectionError: config = {} zmq_factory = ZmqFactory() sub_endpoint = ZmqEndpoint('connect', config.get('sub-endpoint', DEFAULT_SUB_ENDPOINT)) self.zmq_pub = ZmqPubConnection(zmq_factory, sub_endpoint) def load_plugins(self): plugin_classes = list(getPlugins(IAutomatronPluginFactory)) for plugin_class in plugin_classes: try: zope.interface.verify.verifyObject(IAutomatronPluginFactory, plugin_class) except (zope.interface.verify.BrokenImplementation, zope.interface.verify.BrokenMethodImplementation) as e: log.err(e, 'Plugin %s is broken' % plugin_class.__name__) continue self.register_event_handler(plugin_class(self.controller)) def emit(self, event, *args): tag = '%s.%s' % (event.interface.getName(), event.getName()) self.zmq_pub.publish(cPickle.dumps(args), tag) def emit_internal(self, event, *args): tag = '%s.%s' % (event.interface.getName(), event.getName()) self.dispatch_event(tag, *args)
def __init__(self, zmq_factory, port, data_manager=None): self.data_manager = data_manager endpoint = ZmqEndpoint(ZmqEndpointType.bind, "tcp://*:%d" % port) ZmqPubConnection.__init__(self, zmq_factory, endpoint)
def __init__(self, controller): super(PluginManager, self).__init__(controller) self.load_plugins() try: config = self.controller.config_file.items('router') except NoSectionError: config = {} zmq_factory = ZmqFactory() sub_endpoint = ZmqEndpoint('connect', config.get('sub-endpoint', DEFAULT_SUB_ENDPOINT)) self.zmq_pub = ZmqPubConnection(zmq_factory, sub_endpoint)
def __init__(self): self.zf = ZmqFactory() self.bind_to = "tcp://127.0.0.1:5555" #These are just a namedtuples that hold the connection type AND # the target address. self.server = ZmqEndpoint('bind', self.bind_to) self.client = ZmqEndpoint('connect', self.bind_to) #The actual publisher/server socket self.server_s = ZmqPubConnection(self.zf, self.server) #A brute force way to hold client sockets and prevent them from # getting lost. self.clients = []
from txzmq import ZmqEndpoint, ZmqFactory, ZmqPubConnection, ZmqSubConnection parser = OptionParser("") parser.add_option("-m", "--method", dest="method", help="0MQ socket connection: bind|connect") parser.add_option("-e", "--endpoint", dest="endpoint", help="0MQ Endpoint") parser.add_option("-M", "--mode", dest="mode", help="Mode: publisher|subscriber") parser.set_defaults(method="connect", endpoint="epgm://eth0;239.192.1.1:10011") (options, args) = parser.parse_args() zf = ZmqFactory() e = ZmqEndpoint(options.method, options.endpoint) if options.mode == "publisher": s = ZmqPubConnection(zf, e) def publish(): data = str(time.time()) print("publishing %r" % data) s.publish(data.encode()) reactor.callLater(1, publish) publish() else: s = ZmqSubConnection(zf, e) s.subscribe(b"") def doPrint(*args): print("message received: %r" % (args, ))
parser = OptionParser("") parser.add_option("-m", "--method", dest="method", help="0MQ socket connection: bind|connect", default='bind') parser.add_option("-e", "--endpoint", dest="endpoint", help="0MQ Endpoint") parser.add_option("-M", "--mode", dest="mode", help="Mode: publisher|subscriber") parser.set_defaults(method="connect", endpoint="tcp://127.0.0.1:1024") (options, args) = parser.parse_args() zf = ZmqFactory() e = ZmqEndpoint(options.method, options.endpoint) if options.mode == "publisher": print "Publisher", options s = ZmqPubConnection(zf, e) def publish(): data = str(time.time()) print "publishing %r" % data s.publish(data) reactor.callLater(1, publish) publish() else: print "Subscriber", options s = ZmqSubConnection(zf, e) s.subscribe("") def doPrint(*args):
from txzmq import ZmqEndpoint, ZmqFactory, ZmqPubConnection, ZmqSubConnection (options, args) = base.getOptionsAndArgs() def publish(server): data = str(time.time()) print "publishing %r" % data server.publish(data) reactor.callLater(1, publish, server) class MySubscriber(ZmqSubConnection): def gotMessage(self, message, tag): print "message received: %s (%s)" % (message, tag) endpoint = ZmqEndpoint(options.method, options.endpoint) if options.mode == "publisher": server = ZmqPubConnection(endpoint) server.listen(ZmqFactory()) publish(server) elif options.mode == "subscriber": client = MySubscriber(endpoint) client.connect(ZmqFactory()) client.subscribe("") reactor.run()
class ZeHub(object): """ Actual business logic of this example: Hub opens a central server publisher socket and then on demand builds client sockets """ def __init__(self): self.zf = ZmqFactory() self.bind_to = "tcp://127.0.0.1:5555" #These are just a namedtuples that hold the connection type AND # the target address. self.server = ZmqEndpoint('bind', self.bind_to) self.client = ZmqEndpoint('connect', self.bind_to) #The actual publisher/server socket self.server_s = ZmqPubConnection(self.zf, self.server) #A brute force way to hold client sockets and prevent them from # getting lost. self.clients = [] def send(self, msg): """ Publishes a message onto the pub/sub :param msg: Expected to be a simple string message """ self.server_s.publish(msg) def on_msg(self, callBack, time_out = None): """ A messy callback handler for when a new message pops up. :param callBack: expected def callBack(stringMessage) :param time_out: TODO a timeout value in seconds """ """ This is a tad not production sane as its going to open a new ZMQ socket for every single message sent. Its fine when it's just 1-2 people chatting for a short-demo duration but a better approach might be to bind the client ZMQ socket to a HTTP session with a timeout. So say someone leaves, the HTTP session would timeout after some T duration and this socket would be cleaned up. Additionally this would prevent some amount of thrash of instantiating and destroying sockets. """ client = ZmqSubConnection(self.zf, self.client) client.subscribe("") self.clients.append(client) print len(self.clients), " waiting clients" def cleanup(): """ Our message is done, clean up! """ c_index = self.clients.index(client) self.clients.pop(c_index) client.shutdown() def on_msg(*args, **kwargs): try: callBack("".join(args[:-1])) finally: cleanup() """ Blink and you might miss it, this does the actual binding to the client socket. Initially I thought "Man this would be some much better using a deferred" EXCEPT what happens after that first deferred fires? """ client.gotMessage = on_msg
os.path.dirname(sys.argv[0]), '..', '..')) sys.path.insert(0, rootdir) os.chdir(rootdir) from examples.pubsub import base from txzmq import ZmqEndpoint, ZmqFactory, ZmqPubConnection (options, args) = base.getOptionsAndArgs() def publish(server): data = str(time.time()) print "Publishing %r ..." % data server.publish(data) print "Done." reactor.callLater(1, publish, server) def onConnect(server): print "Connected!" publish(server) endpoint = ZmqEndpoint(options.method, options.endpoint) server = ZmqPubConnection(endpoint) deferred = server.listen(ZmqFactory()) deferred.addCallback(onConnect) reactor.run()
sys.path.insert(1, os.path.realpath(os.path.pardir)) from bannerpunk.pixel import Pixel from bannerpunk.preimage import Preimage # This module publishes a script of messages to a ZMQ endpoint PUBLISH_ENDPOINT = "tcp://127.0.0.1:5557" TAG = "forward_event".encode("utf8") factory = ZmqFactory() pub_endpoint = ZmqEndpoint(ZmqEndpointType.bind, PUBLISH_ENDPOINT) pub_connection = ZmqPubConnection(factory, pub_endpoint) MAX_X = 255 MAX_Y = 255 parser = argparse.ArgumentParser(prog="mock-png.py") parser.add_argument("image_no", type=int) parser.add_argument("x_offset", type=int) parser.add_argument("y_offset", type=int) parser.add_argument("png_file", type=str) s = parser.parse_args() img = Image.open(s.png_file) width, height = img.size rgb_raw = img.convert("RGB")
def __init__(self): factory = ZmqFactory() pub_endpoint = ZmqEndpoint(ZmqEndpointType.bind, PUBLISH_ENDPOINT) self.pub_connection = ZmqPubConnection(factory, pub_endpoint) self.current_idle = "OCD"