Commits

Anonymous committed c2d98c4 Draft

- use PluginManager to keep track of currently used WebSockets
- disabled outbound method for now; should go via internal queue

Comments (0)

Files changed (1)

 from geventwebsocket.exceptions import WebSocketError
 import gevent
 from araldo.message import Message
+from araldo.plugins import PluginManager
 
 STATIC_DIR = "."
 WS_TIMEOUT = 10  # WebSocket timeout (s)
 
 
-
 class TooLong(Exception):
     """ Indicates a gevent timeout
 
     """
     def __init__(self, queue):
         self._queue = queue
-        self._sockets = set()
         self._logger = logging.getLogger("araldo")
+        self._plugin_manager = PluginManager.get()
+        outbound = self._plugin_manager.plugin_instances()["araldo.endpoints.outbound"]
+        if "websocket" not in outbound:
+            outbound["websocket"] = {}
+        self._websockets = outbound["websocket"]
 
     def _communicate_with_websocket(self, web_socket):
         """ Receive incoming message from websocket.
             self._logger.debug(
                 "message length: %d",
                 len(message))
-            #for socket in self._sockets:
-            #    socket.send(message)
             self._queue.put(
                 ("web_socket",
                 Message(
             gevent.sleep(0)
         else:
             self._logger.debug("null message (client disconnect?)")
-            if web_socket in self._sockets:
-                self._sockets.remove(web_socket)
+            if id(web_socket) in self._websockets:
+                self._logger.debug("Removing websocket")
+                del self._websockets[id(web_socket)]
             return
 
     def _handle_websocket(self, environ):
         """
         self._logger.debug("Received WebSocket call: %s" % environ)
         web_socket = environ["wsgi.websocket"]
-        self._sockets.add(web_socket)
+        self._logger.debug("Adding websocket")
+        self._logger.debug("### %s" % dir(web_socket))
+        self._websockets[id(web_socket)] = web_socket
         while True:
             try:
                 self._communicate_with_websocket(
             except gevent.Timeout, error:
                 self._logger.debug(str(error))
             gevent.sleep(0)
-            if len(self._sockets) == 0:
+            if len(self._websockets) == 0:
                 self._logger.debug(
                     "No more sockets; terminating websocket handler")
                 break
 
-    def _handle_outbound(self, environ, start_response):
-        """ Handle HTTP call for outbound message
-
-            :param environ: environment passed by WSGIServer
-            :param start_response: WSGI response callback
-        """
-        sockets_ = self._sockets.copy()  # copy for potential removal
-        self._logger.debug("Outbound call to %d websockets" % len(sockets_))
-        for socket in sockets_:
-            try:
-                socket.send(environ["QUERY_STRING"])
-            except WebSocketError, error:
-                self._logger.debug(
-                    "Client %s has gone: %s",
-                    socket,
-                    str(error))
-                socket.close()
-                self._sockets.remove(socket)
-        start_response('200 OK', [('Content-Type', 'text/html')])
-        return ["outbound response\n"]
+##    def _handle_outbound(self, environ, start_response):
+##        """ Handle HTTP call for outbound message
+##
+##            :param environ: environment passed by WSGIServer
+##            :param start_response: WSGI response callback
+##        """
+##        sockets_ = self._websockets.copy()  # copy for potential removal
+##        self._logger.debug("Outbound call to %d websockets" % len(sockets_))
+##        for socket in sockets_:
+##            try:
+##                socket.send(environ["QUERY_STRING"])
+##            except WebSocketError, error:
+##                self._logger.debug(
+##                    "Client %s has gone: %s",
+##                    socket,
+##                    str(error))
+##                socket.close()
+##                del self_._websockets[id(socket)]
+##        start_response('200 OK', [('Content-Type', 'text/html')])
+##        return ["outbound response\n"]
 
     def __call__(self, environ, start_response):
         """ main WSGI method