From deb2e075cd3d94c7a884c7d00456bdb4373eae0d Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Tue, 28 Apr 2020 11:21:49 +0200 Subject: Add OgQMP recv method recv method is useful for receiving information that was not previously requested (such as "events"). This patch also implements automatic handshake on OgQMP by sending an "qmp_capabilities" request. --- src/virtual/ogOperations.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/virtual') diff --git a/src/virtual/ogOperations.py b/src/virtual/ogOperations.py index ff9c937..c152d62 100644 --- a/src/virtual/ogOperations.py +++ b/src/virtual/ogOperations.py @@ -50,6 +50,10 @@ class OgQMP: if err.errno == errno.ECONNREFUSED: raise Exception('cannot connect to qemu') + out = self.talk(str({"execute": "qmp_capabilities"})) + if 'QMP' not in out: + raise Exception('cannot handshake qemu') + def talk(self, data): try: self.sock.send(bytes(data, 'utf-8')) @@ -61,6 +65,21 @@ class OgQMP: if self.sock in readable: try: out = self.sock.recv(4096).decode('utf-8') + out = json.loads(out) + except socket.error as err: + raise Exception('cannot talk to qemu') + else: + raise Exception('timeout when talking to qemu') + return out + + def recv(self, timeout=QMP_TIMEOUT): + readset = [self.sock] + readable, _, _ = select.select(readset, [], [], timeout) + + if self.sock in readable: + try: + out = self.sock.recv(4096).decode('utf-8') + out = json.loads(out) except socket.error as err: raise Exception('cannot talk to qemu') else: @@ -68,7 +87,10 @@ class OgQMP: return out def disconnect(self): - self.sock.close() + try: + self.sock.close() + except: + pass class OgVirtualOperations: def __init__(self): -- cgit v1.2.3-18-g5258