Utilities/BackgroundClient.py

changeset 4221
c9fdc07753a7
parent 4218
f542ad1f76c5
child 4563
881340f4bd0c
child 4632
ca310db386ed
diff -r 4df8f9fc7ea9 -r c9fdc07753a7 Utilities/BackgroundClient.py
--- a/Utilities/BackgroundClient.py	Fri Apr 17 18:57:38 2015 +0200
+++ b/Utilities/BackgroundClient.py	Sat Apr 18 19:14:15 2015 +0200
@@ -86,7 +86,7 @@
         Private methode to receive the given length of bytes.
         
         @param length bytes to receive (int)
-        @return received bytes or None if connection closed (str)
+        @return received bytes or None if connection closed (bytes)
         """
         data = b''
         while len(data) < length:
@@ -96,20 +96,54 @@
             data += newData
         return data
     
+    def __peek(self, length):
+        """
+        Private methode to peek the given length of bytes.
+        
+        @param length bytes to receive (int)
+        @return received bytes (bytes)
+        """
+        data = b''
+        self.connection.setblocking(False)
+        try:
+            data = self.connection.recv(length, socket.MSG_PEEK)
+        except socket.error:
+            pass
+        self.connection.setblocking(True)
+        return data
+    
+    def __cancelled(self):
+        """
+        Private method to check for a job cancellation.
+        
+        @return flag indicating a cancellation (boolean)
+        """
+        msg = self.__peek(struct.calcsize(b'!II') + 6)
+        if msg[-6:] == b"CANCEL":
+            # get rid of the message data
+            self.__peek(struct.calcsize(b'!II') + 6)
+            return True
+        else:
+            return False
+    
     def run(self):
         """
         Public method implementing the main loop of the client.
         """
         try:
             while True:
-                header = self.__receive(8)
+                header = self.__receive(struct.calcsize(b'!II'))
                 # Leave main loop if connection was closed.
                 if not header:
                     break
                 
                 length, datahash = struct.unpack(b'!II', header)
+                messageType = self.__receive(6)
                 packedData = self.__receive(length)
                 
+                if messageType != b"JOB   ":
+                    continue
+                
                 assert adler32(packedData) & 0xffffffff == datahash, \
                     'Hashes not equal'
                 if sys.version_info[0] == 3:
@@ -121,7 +155,7 @@
                 elif fx.startswith("batch_"):
                     callback = self.batchServices.get(fx)
                     if callback:
-                        callback(data, self.__send, fx)
+                        callback(data, self.__send, fx, self.__cancelled)
                         ret = "__DONE__"
                     else:
                         ret = 'Unknown batch service.'

eric ide

mercurial