RefactoringRope/JsonClient.py

branch
server_client_variant
changeset 169
fb8a4182f12e
parent 168
53d76b4fc1ac
child 173
2cdb7c48b719
--- a/RefactoringRope/JsonClient.py	Sun Sep 17 17:03:16 2017 +0200
+++ b/RefactoringRope/JsonClient.py	Sun Sep 17 17:53:20 2017 +0200
@@ -58,6 +58,10 @@
         """
         Private method to receive a JSON encode command and data from the
         server.
+        
+        @return tuple containing the received command and a dictionary
+            containing the associated data
+        @rtype tuple of (str, dict)
         """
         line = self.__connection.recv(1024 * 1024, socket.MSG_PEEK)
         # 1MB buffer
@@ -78,15 +82,14 @@
                     "ExceptionValue": str(err),
                     "ProtocolData": line.strip(),
                 })
-                return
+                return None, None
             
             method = commandDict["method"]
             params = commandDict["params"]
             
-            if method == "Exit":
-                self.__exitClient = True
-            else:
-                self.handleCall(method, params)
+            return method, params
+        
+        return None, None
     
     def handleCall(self, method, params):
         """
@@ -116,7 +119,12 @@
                     continue
                 
                 if self.__connection in rrdy:
-                    self.__receiveJson()
+                    method, params = self.__receiveJson()
+                    if method is not None:
+                        if method == "Exit":
+                            self.__exitClient = True
+                        else:
+                            self.handleCall(method, params)
                 
                 if self.__exitClient:
                     break
@@ -138,18 +146,40 @@
         self.__connection.shutdown(socket.SHUT_RDWR)
         self.__connection.close()
     
-    def poll(self):
+    def poll(self, wait=False):
         """
         Public method to check and receive one message (if available).
+        
+        @param wait flag indicating to wait until something has been received
+        @type bool
+        @return tuple containing the received command and a dictionary
+            containing the associated data
+        @rtype tuple of (str, dict)
         """
         try:
-            rrdy, wrdy, xrdy = select.select([self.__connection], [], [], 0)
+            if wait:
+                rrdy, wrdy, xrdy = select.select(
+                    [self.__connection], [], [])
+            else:
+                rrdy, wrdy, xrdy = select.select(
+                    [self.__connection], [], [], 0)
+            
             if self.__connection in rrdy:
-                self.__receiveJson()
+                method, params = self.__receiveJson()
+                if method is not None:
+                    if method == "Exit":
+                        self.__exitClient = True
+                    else:
+                        if wait:
+                            # wait means to return the data to the caller
+                            return method, params
+                        else:
+                            # no wait means to pass on to the handler method
+                            self.handleCall(method, params)
         
         except (select.error, KeyboardInterrupt, socket.error):
             # just ignore these
-            return
+            pass
         
         except Exception:
             exctype, excval, exctb = sys.exc_info()
@@ -163,3 +193,5 @@
                 "ExceptionValue": str(excval),
                 "Traceback": tbinfo,
             })
+        
+        return None, None

eric ide

mercurial