1 # QEMU Monitor Protocol Python class
3 # Copyright (C) 2009, 2010 Red Hat Inc.
6 # Luiz Capitulino <lcapitulino@redhat.com>
8 # This work is licensed under the terms of the GNU GPL, version 2. See
9 # the COPYING file in the top-level directory.
15 class QMPError(Exception):
18 class QMPConnectError(QMPError
):
21 class QMPCapabilitiesError(QMPError
):
24 class QEMUMonitorProtocol
:
25 def __init__(self
, address
, server
=False):
27 Create a QEMUMonitorProtocol class.
29 @param address: QEMU address, can be either a unix socket path (string)
30 or a tuple in the form ( address, port ) for a TCP
32 @param server: server mode listens on the socket (bool)
33 @raise socket.error on socket connection errors
34 @note No connection is established, this is done by the connect() or
38 self
.__address
= address
39 self
.__sock
= self
.__get
_sock
()
41 self
.__sock
.bind(self
.__address
)
45 if isinstance(self
.__address
, tuple):
46 family
= socket
.AF_INET
48 family
= socket
.AF_UNIX
49 return socket
.socket(family
, socket
.SOCK_STREAM
)
51 def __negotiate_capabilities(self
):
52 greeting
= self
.__json
_read
()
53 if greeting
is None or not greeting
.has_key('QMP'):
55 # Greeting seems ok, negotiate capabilities
56 resp
= self
.cmd('qmp_capabilities')
59 raise QMPCapabilitiesError
61 def __json_read(self
, only_event
=False):
63 data
= self
.__sockfile
.readline()
66 resp
= json
.loads(data
)
68 self
.__events
.append(resp
)
75 def connect(self
, negotiate
=True):
77 Connect to the QMP Monitor and perform capabilities negotiation.
79 @return QMP greeting dict
80 @raise socket.error on socket connection errors
81 @raise QMPConnectError if the greeting is not received
82 @raise QMPCapabilitiesError if fails to negotiate capabilities
84 self
.__sock
.connect(self
.__address
)
85 self
.__sockfile
= self
.__sock
.makefile()
87 return self
.__negotiate
_capabilities
()
91 Await connection from QMP Monitor and perform capabilities negotiation.
93 @return QMP greeting dict
94 @raise socket.error on socket connection errors
95 @raise QMPConnectError if the greeting is not received
96 @raise QMPCapabilitiesError if fails to negotiate capabilities
98 self
.__sock
, _
= self
.__sock
.accept()
99 return self
.__negotiate
_capabilities
()
101 def cmd_obj(self
, qmp_cmd
):
103 Send a QMP command to the QMP Monitor.
105 @param qmp_cmd: QMP command to be sent as a Python dict
106 @return QMP response as a Python dict or None if the connection has
110 self
.__sock
.sendall(json
.dumps(qmp_cmd
))
111 except socket
.error
, err
:
112 if err
[0] == errno
.EPIPE
:
114 raise socket
.error(err
)
115 return self
.__json
_read
()
117 def cmd(self
, name
, args
=None, id=None):
119 Build a QMP command and send it to the QMP Monitor.
121 @param name: command name (string)
122 @param args: command arguments (dict)
123 @param id: command id (dict, list, string or int)
125 qmp_cmd
= { 'execute': name
}
127 qmp_cmd
['arguments'] = args
130 return self
.cmd_obj(qmp_cmd
)
132 def command(self
, cmd
, **kwds
):
133 ret
= self
.cmd(cmd
, kwds
)
134 if ret
.has_key('error'):
135 raise Exception(ret
['error']['desc'])
138 def get_events(self
, wait
=False):
140 Get a list of available QMP events.
142 @param wait: block until an event is available (bool)
144 self
.__sock
.setblocking(0)
147 except socket
.error
, err
:
148 if err
[0] == errno
.EAGAIN
:
151 self
.__sock
.setblocking(1)
152 if not self
.__events
and wait
:
153 self
.__json
_read
(only_event
=True)
156 def clear_events(self
):
158 Clear current list of pending events.
164 self
.__sockfile
.close()
166 timeout
= socket
.timeout
168 def settimeout(self
, timeout
):
169 self
.__sock
.settimeout(timeout
)