示例#1
0
    def __init__(self, port=5556):
        self.port = port
        self.ctx = zmq.Context()
        self.kvmap = {}
        self.loop = IOLoop.instance()

        # Set up our clone server sockets
        self.snapshot = self.ctx.socket(zmq.ROUTER)
        self.publisher = self.ctx.socket(zmq.PUB)
        self.collector = self.ctx.socket(zmq.PULL)
        self.snapshot.bind("tcp://*:%d" % self.port)
        self.publisher.bind("tcp://*:%d" % (self.port + 1))
        self.collector.bind("tcp://*:%d" % (self.port + 2))

        # Wrap sockets in ZMQStreams for IOLoop handlers
        self.snapshot = ZMQStream(self.snapshot)
        self.publisher = ZMQStream(self.publisher)
        self.collector = ZMQStream(self.collector)

        # Register our handlers with reactor
        self.snapshot.on_recv(self.handle_snapshot)
        self.collector.on_recv(self.handle_collect)
        self.flush_callback = PeriodicCallback(self.flush_ttl, 1000)

        # basic log formatting:
        logging.basicConfig(format="%(asctime)s %(message)s",
                            datefmt="%Y-%m-%d %H:%M:%S",
                            level=logging.INFO)
示例#2
0
    def __init__(self, runtime, port=5556):
        from rill.runtime.handlers.runtime import RuntimeHandler

        self.runtime = runtime

        self.port = port
        # Context wrapper
        self.ctx = zmq.Context()
        # IOLoop reactor
        self.loop = IOLoop.instance()

        if not isinstance(self.loop, IOLoop):
            raise TypeError("Expected %s, got %s" % (IOLoop, type(self.loop)))

        # Set up our client server sockets

        # Publish updates to clients
        self.publisher = self.ctx.socket(zmq.PUB)
        # Collect updates and snapshot requests from clients, and send back
        # errors
        self.collector = self.ctx.socket(zmq.ROUTER)

        self.collector.bind("tcp://*:%d" % (self.port))
        self.publisher.bind("tcp://*:%d" % (self.port + 1))

        # Wrap sockets in ZMQStreams for IOLoop handlers
        self.publisher = zmqstream.ZMQStream(
            self.publisher)  # only necessary for heartbeat
        self.collector = zmqstream.ZMQStream(self.collector)

        # Register handlers with reactor
        self.collector.on_recv(self.handle_message)

        self.dispatcher = MessageDispatcher(self.publisher, self.collector)
        self.handler = RuntimeHandler(self.dispatcher, runtime)
示例#3
0
文件: main.py 项目: liushapku/zguide
def handle_zmqsub_stream():
    # print()
    ws = request.environ.get('wsgi.websocket')
    # raw socket: ws.stream.handler.socket._sock : type stdlib socket
    rawsock = ws.stream.handler.socket._sock
    if ws is None:
        flask.abort(404)
    context = zmq.Context.instance()
    subscriber = context.socket(zmq.SUB)
    subscriber.connect("tcp://localhost:5563")
    subscriber.setsockopt(zmq.SUBSCRIBE, b"B")
    ioloop = IOLoop()
    stream = ZMQStream(subscriber, ioloop)

    def on_recv(msg):
        address, contents = msg
        message = "{} ".format(
            time.asctime()) + (b"[%s] %s" % (address, contents)).decode()
        print('===zmqsub:', message)
        ws.send(message)

    stream.on_recv(on_recv)

    def ws_receive():
        ws.receive()
        ioloop.stop()

    try:
        globalpool.spawn(ws_receive)
        ioloop.start()

    except geventwebsocket.WebSocketError as ex:
        print('exception ', type(ex), ex)
    except Exception as ex:
        print('Exception ', ex)
    finally:
        print('zmqsub closed')
        subscriber.close()
        ws.close()
    return ''
 def __init__(self, socket, io_loop=None):
     io_loop = io_loop or IOLoop.instance()
     super(ZMQStream, self).__init__(socket, io_loop=io_loop)
示例#5
0
 def __init__(self, socket, io_loop=None):
     io_loop = io_loop or IOLoop.instance()
     super(ZMQStream, self).__init__(socket, io_loop=io_loop)
示例#6
0
from __future__ import absolute_import, print_function

import logging

import zmq.green as zmq
from zmq.green.eventloop.ioloop import IOLoop
import zmq.green.eventloop.zmqstream as zmqstream

from rill.runtime.plumbing import Message, MessageDispatcher, dump

IOLoop.configure(IOLoop)


class RuntimeServer(object):
    """
    Server managing the runtime state
    """
    def __init__(self, runtime, port=5556):
        from rill.runtime.handlers.runtime import RuntimeHandler

        self.runtime = runtime

        self.port = port
        # Context wrapper
        self.ctx = zmq.Context()
        # IOLoop reactor
        self.loop = IOLoop.instance()

        if not isinstance(self.loop, IOLoop):
            raise TypeError("Expected %s, got %s" % (IOLoop, type(self.loop)))