Esempio n. 1
0
 def setup_zmq(self):
     zmq_factory = ZmqFactory()
     print("subscribing on: %s" % self.endpoint)
     sub_endpoint = ZmqEndpoint(ZmqEndpointType.connect, self.endpoint)
     sub_connection = ZmqSubConnection(zmq_factory, sub_endpoint)
     sub_connection.gotMessage = self.zmq_message
     sub_connection.subscribe(INVOICE_PAYMENT_TAG)
Esempio n. 2
0
    def onConnect(self, request):
        global pub_sock
        if pub_sock is not None:
            zf = ZmqFactory()
            e = ZmqEndpoint(ZmqEndpointType.connect, pub_sock)
            s = ZmqSubConnection(zf, e)
            s.gotMessage = lambda payload, topic: self.gotMessage(
                payload, topic)
            self._zmq = s
            self._guards = {}
            self._filters = []

            # By default, subscribe messages from the current path and below
            default_filter = get_physical_path_from_vhm_path(
                self._environ["PATH_INFO"])
            logger.debug("SUBSCRIBE " + default_filter)
            self._zmq.subscribe(default_filter.encode('utf-8'))
            self._filters.append(default_filter)

        # Cleanup inherited environ
        self._environ["HTTP_CONNECTION"] = "keep-alive"
        for env in [
                "HTTP_UPGRADE",
                "HTTP_SEC_WEBSOCKET_EXTENSIONS",
                "HTTP_SEC_WEBSOCKET_KEY",
                "HTTP_SEC_WEBSOCKET_VERSION",
                "wsgi.errors",
                "wsgi.input",
        ]:
            if env in self._environ:
                del self._environ[env]
Esempio n. 3
0
    def setup_zmq(self):
        zmq_factory = ZmqFactory()
        print("subscribing on: %s" % self.endpoint)
        sub_endpoint = ZmqEndpoint(ZmqEndpointType.connect, self.endpoint)
        sub_connection = ZmqSubConnection(zmq_factory, sub_endpoint)
        sub_connection.gotMessage = self.zmq_message
        sub_connection.subscribe(FORWARD_EVENT_TAG)
        sub_connection.subscribe(HTLC_ACCEPTED_TAG)

        print("subscribing on: %s" % self.mock_endpoint)
        sub_mock_endpoint = ZmqEndpoint(ZmqEndpointType.connect,
                                        self.mock_endpoint)
        sub_mock_connection = ZmqSubConnection(zmq_factory, sub_mock_endpoint)
        sub_mock_connection.gotMessage = self.zmq_message
        sub_mock_connection.subscribe(FORWARD_EVENT_TAG)
        sub_mock_connection.subscribe(HTLC_ACCEPTED_TAG)
Esempio n. 4
0
    def __init__(self):
        self.queue = Queue()

        self.zmq_factory = ZmqFactory()
        endpoint = ZmqEndpoint(ZmqEndpointType.connect, ZMQ_ENDPOINT)
        connection = ZmqSubConnection(self.zmq_factory, endpoint)
        connection.gotMessage = self.set_mode
        connection.subscribe(ZMQ_TAG)
Esempio n. 5
0
 def _load_setup(self, setup):
     for e, notification_type_names in setup.items():
         endpoint = ZmqEndpoint(ZmqEndpointType.connect, e)
         connection = ZmqSubConnection(self.factory, endpoint)
         for n in notification_type_names:
             tag = n.encode("utf8")
             connection.gotMessage = self._log_message
             connection.subscribe(tag)
Esempio n. 6
0
 def startService(self):
     self._factory = ZmqFactory()
     endpoints = [
         ZmqEndpoint(ZmqEndpointType.connect, endpoint)
         for endpoint in self.endpoints
     ]
     log.msg("Configuring ZeroMQ subscription socket",
             logLevel=logging.DEBUG)
     for endpoint in endpoints:
         log.msg("Connecting to the {endpoint} ZeroMQ endpoint".format(
             endpoint=endpoint))
         s = ZmqSubConnection(self._factory, endpoint)
         s.subscribe(b"")
         s.gotMessage = self.on_message
     log.msg("ZeroMQ consumer is ready")
Esempio n. 7
0
    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
Esempio n. 8
0
def main():
    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.set_defaults(method="connect", endpoint="tcp://localhost:5555")

    (options, args) = parser.parse_args()

    zf = ZmqFactory()
    e = ZmqEndpoint(options.method, options.endpoint)

    s = ZmqSubConnection(zf, e)

    s.subscribe("")

    def doPrint(*args):
        print "message received: %r" % (args, )

    s.gotMessage = doPrint

    reactor.run()
Esempio n. 9
0
def main():
    global _CONFIG

    with open('config.yml') as f:
        _CONFIG = yaml.load(f.read())

    log.startLogging(sys.stderr)

    ircf = IrcFactory()
    reactor.connectTCP(_CONFIG['host'], _CONFIG['port'], ircf)

    zf = ZmqFactory()
    e = ZmqEndpoint(_CONFIG['method'], _CONFIG['endpoint'])

    s = ZmqSubConnection(zf, e)
    s.subscribe("")

    def do_forward(*args):
        if _IRC_PROTOCOL:
            _IRC_PROTOCOL.forward(json.loads(args[0]))

    s.gotMessage = do_forward

    reactor.run()
Esempio n. 10
0
def main():
    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.set_defaults(method="connect", endpoint="tcp://localhost:5555")

    (options, args) = parser.parse_args()

    zf = ZmqFactory()
    e = ZmqEndpoint(options.method, options.endpoint)

    s = ZmqSubConnection(zf, e)

    s.subscribe("")

    def doPrint(*args):
        print "message received: %r" % (args, )

    s.gotMessage = doPrint

    reactor.run()
Esempio n. 11
0
def sockjs_server(**options):
    configure = options.get('configure', False)
    port = options.get('port', 8888)

    sockjs_options = {
        'websocket': True,
        'cookie_needed': False,
        'heartbeat': 25,
        'timeout': 5,
        'streaming_limit': 128 * 1024,
        'encoding': 'cp1252',  # Latin1
        'sockjs_url': 'https://d1fxtkz8shb9d2.cloudfront.net/sockjs-0.3.js'
    }

    zf = ZmqFactory()
    endpoint = '%s://localhost:%d' % (settings.FORWARDER_PUB_TRANSPORT,
                                      settings.FORWARDER_PUB_PORT)
    e = ZmqEndpoint("connect", endpoint)
    subscription = ZmqSubConnection(zf, e)
    # Subscripción a todos los mensajes
    subscription.subscribe("")

    class SockJSProtocol(protocol.Protocol):

        DIRECT_FORWARD_MESSAGE_TYPES = ('echo', )

        instances = []

        def __init__(self, *largs, **kwargs):
            print "SockJS"
            #protocol.Protocol.__init__(*largs, **kwargs)
            SockJSProtocol.instances.append(self)

        def dataReceived(self, data_string):
            try:
                data = json.loads(data_string)
                msgtype = data.get('type', None)
                if msgtype in self.DIRECT_FORWARD_MESSAGE_TYPES:
                    self.send_json(data, timestamp=repr(datetime.now()))
            except ValueError as e:
                print e
                self.send_json({'data': data_string, 'type': 'unknown'})

        def send_json(self, data=None, **opts):
            '''Envia una respuesta en JSON por el websocket'''
            if data:
                if isinstance(data, dict):
                    data_safe = copy(data)
                    data_safe.update(opts)
                elif isinstance(data, basestring):
                    try:
                        data_safe = json.loads(data)
                    except ValueError:
                        raise ValueError("Can't convert %s to json" % data)
            else:
                data_safe = opts
            self.transport.write(json.dumps(data_safe))

        def connectionLost(self, reason):
            print "Cerrando Socket"

            self.instances.remove(self)
            protocol.Protocol.connectionLost(self, reason)

        @classmethod
        def broadcast(cls, data, *largs, **kwargs):
            print "Received from forwarder %s" % data
            for conn in cls.instances:
                try:
                    conn.send_json(data)
                except Exception as e:
                    print e

    subscription.gotMessage = SockJSProtocol.broadcast

    factory = protocol.ServerFactory()
    factory.protocol = SockJSProtocol
    reactor.listenTCP(port, SockJSFactory(factory, sockjs_options))
    print "SocketServer running on %d" % port
    if not configure:
        try:
            reactor.run()
        except error.ReactorNotRunning:
            print "Closing bridge"
Esempio n. 12
0
def sockjs_server(**options):
    configure = options.get('configure', False)
    port = options.get('port', 8888)

    sockjs_options = {
        'websocket': True,
        'cookie_needed': False,
        'heartbeat': 25,
        'timeout': 5,
        'streaming_limit': 128 * 1024,
        'encoding': 'cp1252',  # Latin1
        'sockjs_url': 'https://d1fxtkz8shb9d2.cloudfront.net/sockjs-0.3.js'
    }

    zf = ZmqFactory()
    endpoint = '%s://localhost:%d' % (settings.FORWARDER_PUB_TRANSPORT,
                                      settings.FORWARDER_PUB_PORT)
    e = ZmqEndpoint("connect", endpoint)
    subscription = ZmqSubConnection(zf, e)
    # Subscripción a todos los mensajes
    subscription.subscribe("")

    class SockJSProtocol(protocol.Protocol):

        DIRECT_FORWARD_MESSAGE_TYPES = ('echo', )

        instances = []

        def __init__(self, *largs, **kwargs):
            print "SockJS"
            #protocol.Protocol.__init__(*largs, **kwargs)
            SockJSProtocol.instances.append(self)

        def dataReceived(self, data_string):
            try:
                data = json.loads(data_string)
                msgtype = data.get('type', None)
                if msgtype in self.DIRECT_FORWARD_MESSAGE_TYPES:
                    self.send_json(data, timestamp=repr(datetime.now()))
            except ValueError as e:
                print e
                self.send_json({'data': data_string, 'type': 'unknown'})

        def send_json(self, data=None, **opts):
            '''Envia una respuesta en JSON por el websocket'''
            if data:
                if isinstance(data, dict):
                    data_safe = copy(data)
                    data_safe.update(opts)
                elif isinstance(data, basestring):
                    try:
                        data_safe = json.loads(data)
                    except ValueError:
                        raise ValueError("Can't convert %s to json" % data)
            else:
                data_safe = opts
            self.transport.write(json.dumps(data_safe))

        def connectionLost(self, reason):
            print "Cerrando Socket"

            self.instances.remove(self)
            protocol.Protocol.connectionLost(self, reason)

        @classmethod
        def broadcast(cls, data, *largs, **kwargs):
            print "Received from forwarder %s" % data
            for conn in cls.instances:
                try:
                    conn.send_json(data)
                except Exception as e:
                    print e

    subscription.gotMessage = SockJSProtocol.broadcast

    factory = protocol.ServerFactory()
    factory.protocol = SockJSProtocol
    reactor.listenTCP(port, SockJSFactory(factory, sockjs_options))
    print "SocketServer running on %d" % port
    if not configure:
        try:
            reactor.run()
        except error.ReactorNotRunning:
            print "Closing bridge"
Esempio n. 13
0
class ProxyFactory(Factory):
    protocol = ProxyProtocol

def main():
    o = ServiceOptions()
    try:
        o.parseOptions()
    except usage.UsageError, msg:
        print "%s: %s" % (sys.argv[0], msg)
        print "%s: use --help for usage details" % (sys.argv[0],)
        raise SystemExit, 1

    zf = ZmqFactory()
    sub = ZmqSubConnection(zf, ZmqEndpoint("connect", 
                                  "tcp://%s" % o.opts['subsocket']))
    sub.subscribe("")

    ws = listen("tcp:%s" % o.opts['websocket'], WebSocketFactory(ProxyFactory()))

    def forwardToWs(msg):
        for c in connections:
            c.transport.write(msg)

    sub.gotMessage = forwardToWs
    reactor.run()

if __name__ == '__main__':
    main()