summaryrefslogtreecommitdiffstats
path: root/src/virtual
diff options
context:
space:
mode:
authorRoberto Hueso Gómez <rhueso@soleta.eu>2020-04-28 11:21:49 +0200
committerRoberto Hueso Gómez <rhueso@soleta.eu>2020-04-28 11:21:49 +0200
commitdeb2e075cd3d94c7a884c7d00456bdb4373eae0d (patch)
tree7b8ee5bdcdc39c70423695eb8bead21534feb610 /src/virtual
parent5444e453ee6a0afeb6007c49b140e1252973c1d5 (diff)
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.
Diffstat (limited to 'src/virtual')
-rw-r--r--src/virtual/ogOperations.py24
1 files changed, 23 insertions, 1 deletions
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):