def setup(self, cfg): BasePlugin.setup(self, cfg) self.factory = MZMQFactory(plugin=self) self.factory.plugin = self self.pub_endpoint_uri = cfg.get("pub_endpoint", "tcp://*:9901") self.pub_endpoint = ZmqEndpoint("bind", self.pub_endpoint_uri) self.sub_endpoint_uri = cfg.get("sub_endpoint", "tcp://*:9901") self.sub_endpoint = ZmqEndpoint("connect", self.sub_endpoint_uri) self.pub = ZmqPubConnection(self.factory, self.pub_endpoint) self.sub = ZmqSubConnection(self.factory, self.sub_endpoint) self.sub.subscribe("") self.sub.gotMessage = self.on_message self.subscribe(self.process_event)
class ZmqPlugin(BasePlugin): """ Zero Messaging Queue (ZMQ/0MQ) plugin. """ default_config = { "enabled": False, "pub_endpoint": "tcp://*:9901", "sub_endpoint": "tcp://*:9901" } def setup(self, cfg): BasePlugin.setup(self, cfg) self.factory = MZMQFactory(plugin=self) self.factory.plugin = self self.pub_endpoint_uri = cfg.get("pub_endpoint", "tcp://*:9901") self.pub_endpoint = ZmqEndpoint("bind", self.pub_endpoint_uri) self.sub_endpoint_uri = cfg.get("sub_endpoint", "tcp://*:9901") self.sub_endpoint = ZmqEndpoint("connect", self.sub_endpoint_uri) self.pub = ZmqPubConnection(self.factory, self.pub_endpoint) self.sub = ZmqSubConnection(self.factory, self.sub_endpoint) self.sub.subscribe("") self.sub.gotMessage = self.on_message self.subscribe(self.process_event) def process_event(self, tags, detail): detail_json = str(json.dumps(detail)) self.pub.publish(detail_json, str(" ".join(tags))) def on_message(self, detail, tags): self.logger.debug("ZMQ: [%s] %s" % (tags, detail))
#!/usr/bin/env python import json import pprint from twisted.internet import reactor, defer from txZMQ import ZmqEndpoint, ZmqFactory, ZmqPubConnection, ZmqSubConnection zf = ZmqFactory() e = ZmqEndpoint("connect", "tcp://higgs:9901") s = ZmqSubConnection(zf, e) s.subscribe("") # ("scheduler.scheduler001.new_interval") def on_message(*args): # json_str = args[0] pprint.pprint(args) # json_obj = json.loads(json_str) # pprint.pprint(json_obj) s.gotMessage = on_message reactor.run()
#!/usr/bin/env python import json import pprint import sys from twisted.internet import reactor, defer from txZMQ import ZmqEndpoint, ZmqFactory, ZmqPubConnection, ZmqSubConnection zf = ZmqFactory() pe = ZmqEndpoint("bind", "tcp://*:9901") se = ZmqEndpoint("connect", "tcp://10.0.2.40:9901") pc = ZmqPubConnection(zf, pe) sc = ZmqSubConnection(zf, se) sc.subscribe("") # ("scheduler.scheduler001.new_interval") pc.publish("F*****G WORK") sys.exit(0) def matches(tags, query): if query is None: query = list() tags = set(tags.split(" ")) query = set(query) return query.issubset(tags) def on_message(detail, tags):