def test_format_address(self):
        m = MagicMock()
        m.getsockname.return_value = ("127.0.0.1", 52300, None, None)
        m.getpeername.return_value = ("127.0.0.1", 4800, None, None)
        ws = WebSocket(sock=m)

        log = format_addresses(ws)
        self.assertEqual(log, "[Local => 127.0.0.1:52300 | Remote => 127.0.0.1:4800]")
示例#2
0
 def handshake_ok(self):
     print " EchoClient Opening %s" % format_addresses(self)
     self.send(
         json.dumps({'header': {
             'type': 'ack-connect',
             'idws': 'request'
         }}))
     getServerOnPort(self.peer_address[1]).server.manager.add(self)
    def test_format_address(self):
        m = MagicMock()
        m.getsockname.return_value = ('127.0.0.1', 52300, None, None)
        m.getpeername.return_value = ('127.0.0.1', 4800, None, None)
        ws = WebSocket(sock=m)

        log = format_addresses(ws)
        self.assertEqual(
            log, "[Local => 127.0.0.1:52300 | Remote => 127.0.0.1:4800]")
示例#4
0
    def remove(self, websocket):
        """
        Remove the given ``websocket`` from the manager.

        This does not call its :meth:`closed() <ws4py.websocket.WebSocket.closed>`
        method as it's out-of-band by your application
        or from within the manager's run loop.
        """
        logger.info("Removing websocket %s" % format_addresses(websocket))
        with self.lock:
            fd = websocket.sock.fileno()
            self.websockets.pop(fd, None)
            self.poller.unregister(fd)
示例#5
0
    def remove(self, websocket):
        """
        Remove the given ``websocket`` from the manager.

        This does not call its :meth:`closed() <ws4py.websocket.WebSocket.closed>`
        method as it's out-of-band by your application
        or from within the manager's run loop.
        """
        logger.info("Removing websocket %s" % format_addresses(websocket))
        with self.lock:
            fd = websocket.sock.fileno()
            self.websockets.pop(fd, None)
            self.poller.unregister(fd)
示例#6
0
    def add(self, websocket):
        """
        Manage a new websocket.

        First calls its :meth:`opened() <ws4py.websocket.WebSocket.opened>`
        method and register its socket against the poller
        for reading events.
        """
        logger.info("Managing websocket %s" % format_addresses(websocket))
        websocket.opened()
        with self.lock:
            fd = websocket.sock.fileno()
            self.websockets[fd] = websocket
            self.poller.register(fd)
示例#7
0
    def add(self, websocket):
        """
        Manage a new websocket.

        First calls its :meth:`opened() <ws4py.websocket.WebSocket.opened>`
        method and register its socket against the poller
        for reading events.
        """
        logger.info("Managing websocket %s" % format_addresses(websocket))
        websocket.opened()
        with self.lock:
            fd = websocket.sock.fileno()
            self.websockets[fd] = websocket
            self.poller.register(fd)
示例#8
0
    def run(self):
        """
        Manager's mainloop executed from within a thread.

        Constantly poll for read events and, when available,
        call related websockets' `once` method to
        read and process the incoming data.

        If the :meth:`once() <ws4py.websocket.WebSocket.once>`
        method returns a `False` value, its :meth:`terminate() <ws4py.websocket.WebSocket.terminate>`
        method is also applied to properly close
        the websocket and its socket is unregistered from the poller.

        Note that websocket shouldn't take long to process
        their data or they will block the remaining
        websockets with data to be handled. As for what long means,
        it's up to your requirements.
        """
        self.running = True
        while self.running:
            with self.lock:
                polled = self.poller.poll()

            if not self.running:
                break

            for fd in polled:
                if not self.running:
                    break

                ws = self.websockets.get(fd)

                if ws and not ws.terminated:
                    try:
                        result = ws.once()
                    except:
                        result = None

                    if not result:
                        with self.lock:
                            fd = ws.sock.fileno()
                            self.websockets.pop(fd, None)
                            self.poller.unregister(fd)

                        if not ws.terminated:
                            logger.info("Terminating websocket %s" %
                                        format_addresses(ws))
                            ws.terminate()
示例#9
0
    def run(self):
        """
        Manager's mainloop executed from within a thread.

        Constantly poll for read events and, when available,
        call related websockets' `once` method to
        read and process the incoming data.

        If the :meth:`once() <ws4py.websocket.WebSocket.once>`
        method returns a `False` value, its :meth:`terminate() <ws4py.websocket.WebSocket.terminate>`
        method is also applied to properly close
        the websocket and its socket is unregistered from the poller.

        Note that websocket shouldn't take long to process
        their data or they will block the remaining
        websockets with data to be handled. As for what long means,
        it's up to your requirements.
        """
        self.running = True
        while self.running:
            with self.lock:
                polled = self.poller.poll()
            if not self.running:
                break

            for fd in polled:
                if not self.running:
                    break

                ws = self.websockets.get(fd)
                if ws and not ws.terminated:
                    # I don't know what kind of errors might spew out of here
                    # but they probably shouldn't crash the entire server.
                    try:
                        x = ws.once()
                    # Treat the error as if once() had returned None
                    except Exception as e:
                        x = None
                        logger.error("Terminating websocket %s due to exception: %s in once method" % (format_addresses(ws), repr(e)) )
                    if not x:
                        with self.lock:
                            self.websockets.pop(fd, None)
                            self.poller.unregister(fd)

                        if not ws.terminated:
                            logger.info("Terminating websocket %s" % format_addresses(ws))
                            ws.terminate()
示例#10
0
    def run(self):
        """
        Manager's mainloop executed from within a thread.
        Constantly poll for read events and, when available,
        call related websockets' `once` method to
        read and process the incoming data.
        If the :meth:`once() <ws4py.websocket.WebSocket.once>`
        method returns a `False` value, its :meth:`terminate() <ws4py.websocket.WebSocket.terminate>`
        method is also applied to properly close
        the websocket and its socket is unregistered from the poller.
        Note that websocket shouldn't take long to process
        their data or they will block the remaining
        websockets with data to be handled. As for what long means,
        it's up to your requirements.
        Need to add reconnect logic to WebSocketManager
        """
        self.running = True
        self.terminations = -1
        while self.running:
            global j
            l = len(j)
            with self.lock:
                polled = self.poller.poll()
            if not self.running:
                break

            for fd in polled:
                if not self.running:
                    break

                ws = self.websockets.get(fd)

                if ws and not ws.terminated:
                    if not ws.once():
                        with self.lock:
                            fd = ws.sock.fileno()
                            self.websockets.pop(fd, None)
                            self.poller.unregister(fd)

                        if not ws.terminated:
                            logger.info("Terminating websocket %s" % format_addresses(ws))
                            ws.terminate()
                            self.connect_terminated(ws.url)
                if time.time() > self.timeout:
                    self.running = False
                    break
示例#11
0
 def handshake_ok(self):
     logger.info("Opening %s" % format_addresses(self))
     self._mgr.add(self)
示例#12
0
文件: websocket.py 项目: gnott/h
 def track(self, websocket):
     log.debug("managing websocket %s" % format_addresses(websocket))
     return self.spawn(websocket.run)
 def link_websocket_to_server(self, websocket):
     logger.info("Managing websocket %s" % format_addresses(websocket))
     self._websockets.spawn(websocket.run)
示例#14
0
 def handshake_ok(self):
     print " EchoClient Opening %s" % format_addresses(self)
     self.send(json.dumps({'header':{'type': 'ack-connect', 'idws':'request'}}))
     getServerOnPort(self.peer_address[1]).server.manager.add(self)
 def link_websocket_to_server(self, websocket):
     logger.info("Managing websocket %s" % format_addresses(websocket))
     self._websockets.spawn(websocket.run)
示例#16
0
 def track(self, websocket):
     log.debug("managing websocket %s" % format_addresses(websocket))
     return self.spawn(websocket.run)
示例#17
0
 def track(self, websocket):
     logger.info("Managing websocket %s" % format_addresses(websocket))
     return self.spawn(websocket.run)
示例#18
0
 def handshake_ok(self):
     LOGGER.info("Opening %s" % format_addresses(self))
     MANAGER.add(self)