[NEW PATCH] BZ#727871: guestIF: rewrite the message reading code. (via gerrit-bot)

Gal Hammer ghammer at redhat.com
Sun Aug 7 07:41:03 UTC 2011


New patch submitted by Gal Hammer (ghammer at redhat.com)

You can review this change at: http://gerrit.usersys.redhat.com/785

commit b0ba145c7c0370ae87d0763754f8dd1de2c0f5d3
Author: Gal Hammer <ghammer at redhat.com>
Date:   Wed Aug 3 17:02:09 2011 +0300

    BZ#727871: guestIF: rewrite the message reading code.
    
    The previous code handle only one message at a time. This mean that in case of
    multiple messages vdsm handled only the first message while the following
    messages was not handle until more data was send from the guest.
    
    This patch change this behavior to scan the entire buffer for messages and
    handle all of them.
    
    Change-Id: Id3c2ef7f08cadd686329a280c06f7db438f667db

diff --git a/vdsm/guestIF.py b/vdsm/guestIF.py
index 1e2195d..a648dec 100644
--- a/vdsm/guestIF.py
+++ b/vdsm/guestIF.py
@@ -254,47 +254,47 @@ class GuestAgent (threading.Thread):
             self.log.error(traceback.format_exc())
 
     READSIZE = 2**16
-    def _readMessage(self, accum=''):
-        done = False
-        msglen = -headerLengthBytes
-        while not done and not self._stopped:
-            while not self._stopped:
-                try:
-                    s = self._sock.recv(self.READSIZE)
+    def _readBuffer(self):
+        while not self._stopped:
+            try:
+                s = self._sock.recv(self.READSIZE)
+                if s:
+                    self._buffer += s
+                    self._agentTimestamp = time.time()
                     break
-                except socket.timeout:
-                    # TODO move these specific bits out of here
-                    self.guestInfo['memUsage'] = 0
-                    if self.guestStatus not in ("Powered down",
-                                                "RebootInProgress"):
-                        self.log.log(logging.TRACE, "Guest connection timed out")
-                        self.guestStatus = None
-            accum += s
-            if s == '':
-                done = True
-            if len(accum) >= headerLengthBytes:
-                msglen = self._parseHeader(accum[:headerLengthBytes])
-                if len(accum) >= msglen + headerLengthBytes:
-                    done = True
-        if len(accum) >= msglen + headerLengthBytes:
-            return accum[headerLengthBytes:headerLengthBytes + msglen], \
-                   accum[headerLengthBytes + msglen:]
-        return accum, ''
+                time.sleep(1)
+            except socket.timeout:
+                # TODO move these specific bits out of here
+                self.guestInfo['memUsage'] = 0
+                if self.guestStatus not in ("Powered down", "RebootInProgress"):
+                    self.log.log(logging.TRACE, "Guest connection timed out")
+                    self.guestStatus = None
+
+    def _readMessage(self):
+        msg = None
+        if len(self._buffer) >= headerLengthBytes:
+            msglen = self._parseHeader(self._buffer[:headerLengthBytes])
+            if len(self._buffer) >= headerLengthBytes + msglen:
+                msg = self._buffer[headerLengthBytes:headerLengthBytes + msglen]
+                self._buffer = self._buffer[headerLengthBytes + msglen:]
+        return msg
+
+    def _parseMessages(self):
+        s = self._readMessage()
+        while not self._stopped and s:
+            self._parseBody(s)
+            s = self._readMessage()
 
-    def run (self):
+    def run(self):
         self._stopped = False
         try:
             if not self._connect():
                 return
             self.sendHcCmdToDesktop('refresh')
-            leftover = ''
+            self._buffer = ''
             while not self._stopped:
-                s, leftover = self._readMessage(leftover)
-                if s:
-                    self._agentTimestamp = time.time()
-                    self._parseBody(s)
-                else:
-                    time.sleep(1)
+                self._readBuffer()
+                self._parseMessages()
         except:
             if not self._stopped:
                 self.log.error("Unexpected exception: " + traceback.format_exc())




More information about the vdsm-patches mailing list