def load_setup(self, setup): for e, s in setup.items(): endpoint = ZmqEndpoint(ZmqEndpointType.bind, e) ZmqPubConnection.highWaterMark = s['high_water_mark'] connection = ZmqPubConnection(self.factory, endpoint) for n in s['notification_type_names']: self.connection_map[n] = connection
def __init__(self, miner): # create a factory self.miner = miner self.factory = ZmqFactory() # create a connection to publish publish_endpoint = ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:" + miner.publish_port) self.publisher = ZmqPubConnection(self.factory, publish_endpoint) # create connections to subscribe self.subscribers = [] print("the ports subscribed are:") print(miner.subscribe_ports) for subscribe_port in miner.subscribe_ports: subscribe_endpoint = ZmqEndpoint( ZmqEndpointType.connect, "tcp://127.0.0.1:" + subscribe_port) subscriber = BroadcastSubscriber(self.factory, subscribe_endpoint, miner) self.subscribers.append(subscriber) # subscribe to the types of events subscriber.subscribe(PROPOSAL_TAG.encode()) subscriber.subscribe(COMMIT_TAG.encode()) subscriber.subscribe(REINFORCEMENT_TAG.encode()) subscriber.subscribe(TRANSACTION_TAG.encode()) subscriber.subscribe(MALICIOUS_PROPOSAL_AGREEMENT_TAG.encode()) subscriber.subscribe(PROPOSAL_COMMIT_TAG.encode()) subscriber.subscribe(REINFORCEMENT_INF_TAG.encode())
def __init__(self, identity): self.identity = identity # read the publish from the configuration file publish_port, self.pub_key = self.__read_conf() print("I am at the publish port:") print(publish_port) # create a factory self.factory = ZmqFactory() # create a connection to publish publish_endpoint = ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:" + publish_port) self.publisher = ZmqPubConnection(self.factory, publish_endpoint)
def main(config): zmq_requests_factory = ZmqFactory() zmq_requests_endpoint = ZmqEndpoint(ZmqEndpointType.bind, config['endpoint.command']) zmq_requests = ZmqRequests(zmq_requests_factory, zmq_requests_endpoint) zmq_broadcast_factory = ZmqFactory() zmq_broadcast_endpoint = ZmqEndpoint(ZmqEndpointType.bind, config['endpoint.broadcast']) zmq_broadcast = ZmqPubConnection(zmq_broadcast_factory, zmq_broadcast_endpoint) api_endpoint = TCP4ClientEndpoint(reactor, config['ibtws.host'], config['ibtws.port']) api_endpoint.connect(IBTWSProtocolFactory(zmq_requests, zmq_broadcast)) reactor.run()
def __init__(self, reactor, threadpool, publish_module, websocket_ipc=None): self._reactor = reactor self._threadpool = threadpool self._publish_module = publish_module # ZMQ pubsub if websocket_ipc: path = ("ipc://" + os.path.abspath(websocket_ipc)).encode("utf-8") zf = ZmqFactory() e = ZmqEndpoint(ZmqEndpointType.bind, os.path.join(path)) s = ZmqPubConnection(zf, e) global pub global pub_sock pub = s pub_sock = path # WebSockets factory = WebSocketServerFactory() factory.protocol = PubSubServerProtocol self.ws = CustomWebSocketResource(factory)
rootdir = os.path.realpath( os.path.join(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()
#!/usr/bin/env python3 import sys import time from twisted.internet import reactor from txzmq import ZmqEndpoint, ZmqEndpointType from txzmq import ZmqFactory from txzmq import ZmqSubConnection, ZmqPubConnection PUBLISH_ENDPOINT = "tcp://127.0.0.1:7777" TAG = "blinkr".encode("utf8") factory = ZmqFactory() pub_endpoint = ZmqEndpoint(ZmqEndpointType.bind, PUBLISH_ENDPOINT) pub_connection = ZmqPubConnection(factory, pub_endpoint) def publish(mode): print("publishing: %s" % mode) msg = mode.encode("utf8") pub_connection.publish(msg, tag=TAG) time.sleep(0.01) reactor.stop() reactor.callLater(1.0, publish, sys.argv[1]) reactor.run()
return SSHCommandClientEndpoint.newConnection( self.reactor, b"gerrit stream-events", self.username, self.host, port=self.port, keys=self.keys, password=self.password, agentEndpoint=self.agent, knownHosts=self.knownHosts) if __name__ == '__main__': log.startLogging(sys.stderr) zf = ZmqFactory() e = ZmqEndpoint(_CONFIG['method'], _CONFIG['endpoint']) s = ZmqPubConnection(zf, e) parameters = ConnectionParameters.fromConfig(reactor) endpoint = parameters.endpointForStream() factory = GerritJsonFactory(zf) endpoint.connect(factory) reactor.run() # zgerrit.py ends here
def connect_publisher(address): socket = ZmqPubConnection(ZmqFactory(), ZmqEndpoint("connect", address)) return socket
def init(gdata): appname = os.path.basename(gdata.app_dir) utils.update_tz(gdata.config.system.tz) syslog = logger.Logger(gdata.config, appname) dispatch.register(syslog) log.startLoggingWithObserver(syslog.emit, setStdout=0) gdata.db_engine = get_engine(gdata.config) gdata.db = scoped_session( sessionmaker(bind=gdata.db_engine, autocommit=False, autoflush=False)) # web 应用初始化 gdata.settings = dict( cookie_secret="12oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", login_url="/admin/login", template_path=os.path.join(os.path.dirname(__file__), "views"), static_path=os.path.join(os.path.dirname(__file__), "static"), xsrf_cookies=True, xheaders=True, conf=gdata.config) # 模板初始化 gdata.tp_lookup = TemplateLookup( directories=[gdata.settings['template_path']], default_filters=['decode.utf8'], input_encoding='utf-8', output_encoding='utf-8', encoding_errors='ignore', module_directory="/tmp/toughcloud") gdata.redisconf = redis_conf(gdata.config) gdata.session_manager = session.SessionManager( gdata.redisconf, gdata.settings["cookie_secret"], 7200) gdata.cache = CacheManager(gdata.redisconf, cache_name='Cache-%s' % os.getpid()) gdata.aes = utils.AESCipher(key=gdata.config.system.secret) # 数据库备份器初始化 gdata.db_backup = DBBackup(models.get_metadata(gdata.db_engine), excludes=[]) #cache event init dispatch.register(gdata.cache) # app handles init handler_dir = os.path.join(gdata.app_dir, 'handlers') load_handlers(handler_path=handler_dir, pkg_prefix="%s.handlers" % appname, excludes=[]) gdata.all_handlers = permit.all_handlers # app event init event_dir = os.path.abspath(os.path.join(gdata.app_dir, 'events')) load_events(event_dir, "%s.events" % appname) # init zmq gdata.radque = deque([], 8192) gdata.radstart = ZmqPushConnection( ZmqFactory(), ZmqEndpoint('bind', gdata.config.mqproxy.radstart_bind)) gdata.radstop = ZmqPubConnection( ZmqFactory(), ZmqEndpoint('bind', gdata.config.mqproxy.radstop_bind)) gdata.radresp = ZmqPullConnection( ZmqFactory(), ZmqEndpoint('bind', gdata.config.mqproxy.radresp_bind)) gdata.radresp.onPull = lambda m: gdata.radque.appendleft( msgpack.unpackb(m[0])) gdata.statcache = StatCounter(gdata) gdata.statcache.init() gdata.statcache.poll_calc() logger.info(gdata.radstart) logger.info(gdata.radstop) logger.info(gdata.radresp)
def add_pub(self, endpoint): if endpoint: logger.debug("开始运行 PUB 服务器,服务地址为:{}...".format(endpoint)) self.pub_ = ZmqPubConnection(ZmqFactory(), ZmqEndpoint("bind", endpoint))
def __init__(self): factory = ZmqFactory() pub_endpoint = ZmqEndpoint(ZmqEndpointType.bind, PUBLISH_ENDPOINT) self.pub_connection = ZmqPubConnection(factory, pub_endpoint) self.current_idle = "OCD"