python/qmp/legacy: make QEMUMonitorProtocol accept a socket

Teach QEMUMonitorProtocol to accept an exisiting socket.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Message-id: 20230111080101.969151-3-marcandre.lureau@redhat.com
Signed-off-by: John Snow <jsnow@redhat.com>
This commit is contained in:
Marc-André Lureau 2023-01-11 12:01:00 +04:00 committed by John Snow
parent a3cfea92e2
commit 603a3bad4b

View File

@ -22,6 +22,7 @@ old interface.
#
import asyncio
import socket
from types import TracebackType
from typing import (
Any,
@ -69,22 +70,32 @@ class QEMUMonitorProtocol:
:param address: QEMU address, can be either a unix socket path (string)
or a tuple in the form ( address, port ) for a TCP
connection
connection or None
:param sock: a socket or None
:param server: Act as the socket server. (See 'accept')
:param nickname: Optional nickname used for logging.
"""
def __init__(self, address: SocketAddrT,
def __init__(self,
address: Optional[SocketAddrT] = None,
sock: Optional[socket.socket] = None,
server: bool = False,
nickname: Optional[str] = None):
assert address or sock
self._qmp = QMPClient(nickname)
self._aloop = asyncio.get_event_loop()
self._address = address
self._sock = sock
self._timeout: Optional[float] = None
if server:
self._sync(self._qmp.start_server(self._address))
if sock:
assert self._sock is not None
self._sync(self._qmp.open_with_socket(self._sock))
else:
assert self._address is not None
self._sync(self._qmp.start_server(self._address))
_T = TypeVar('_T')
@ -139,6 +150,7 @@ class QEMUMonitorProtocol:
:return: QMP greeting dict, or None if negotiate is false
:raise ConnectError: on connection errors
"""
assert self._address is not None
self._qmp.await_greeting = negotiate
self._qmp.negotiate = negotiate