[PATCH 2/3] QMPMonitor: Introduce the send() method

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This method directly sends data to the QMP monitor and returns its
response without any kind of special treatment or sanity checking.

The send_cmd() method is also introduced. It's a simple wrapper
which sends a QMP command to the monitor.

Both methods are going to be used by the QMP test-suite.

TODO: Refactor _get_command_output() and/or cmd() to use the new
      methods.

Signed-off-by: Luiz Capitulino <lcapitulino@xxxxxxxxxx>
---
 client/tests/kvm/kvm_monitor.py |   49 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 49 insertions(+), 0 deletions(-)

diff --git a/client/tests/kvm/kvm_monitor.py b/client/tests/kvm/kvm_monitor.py
index c23835c..9a66388 100644
--- a/client/tests/kvm/kvm_monitor.py
+++ b/client/tests/kvm/kvm_monitor.py
@@ -604,6 +604,55 @@ class QMPMonitor(Monitor):
         return self._greeting
 
 
+    def send(self, data, timeout=20):
+        """
+        Send data to the QMP monitor and return its response.
+
+        @param data: Data to send
+        @param timeout: Time duration to wait for response
+        @return: The response received
+        @raise MonitorLockError: Raised if the lock cannot be acquired
+        @raise MonitorSendError: Raised if the command cannot be sent
+        @raise MonitorProtocolError: Raised if no response is received
+        """
+        if not self._acquire_lock(20):
+            raise MonitorLockError("Could not acquire exclusive lock to send "
+                                   "QMP command '%s'" % cmd)
+        try:
+            self._read_objects()
+            self._socket.sendall(data)
+            end_time = time.time() + timeout
+            while time.time() < end_time:
+                for obj in self._read_objects():
+                    if isinstance(obj, dict):
+                        if "return" in obj or "error" in obj:
+                            return obj
+                time.sleep(0.1)
+            else:
+                 raise MonitorProtocolError("Received no response (data: %s)"
+                                            % str(data))
+        except socket.error:
+            raise MonitorSendError("Could not send data '%s'" % str(data))
+        finally:
+            self._lock.release()
+
+
+    def send_cmd(self, command, timeout=20):
+        """
+        Send a QMP command. This is a simple wrapper to send() method that does
+        JSON formatting.
+
+        @param command: QMP command to send
+        @param timeout: Time duration to wait for response
+        @return: The response received
+        @raise MonitorLockError: Raised if the lock cannot be acquired
+        @raise MonitorSendError: Raised if the command cannot be sent
+        @raise MonitorProtocolError: Raised if no response is received
+
+        """
+        return self.send(json.dumps(command) + "\n", timeout)
+
+
     # Command wrappers
     # Note: all of the following functions raise exceptions in a similar manner
     # to cmd() and _get_command_output().
-- 
1.7.3.1.104.gc752e

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux