QI-QMP v0.2.2
Qt-based Interface for QEMU Machine Protocol
|
The Qmpi class is an interface through which to communicate with a QEMU instance via the QEMU Machine Protocol. More...
#include <qi-qmp/qmpi.h>
Public Types | |
enum | CommunicationError { WriteFailed , ReadFailed , TransactionTimeout , UnexpectedReceive , UnexpectedResponse } |
enum | State { Disconnected , Connecting , AwaitingWelcome , Negotiating , Idle , Closing , SendingCommand , AwaitingMessage , ReadingMessage } |
Signals | |
void | commandQueueExhausted () |
void | communicationErrorOccurred (Qmpi::CommunicationError error) |
void | connected (QJsonObject version, QJsonArray capabilities) |
void | connectionErrorOccurred (QAbstractSocket::SocketError error) |
void | disconnected () |
void | errorResponseReceived (QString errorClass, QString description, std::any context) |
void | eventReceived (QString name, QJsonObject data, QDateTime timestamp) |
void | finished () |
void | readyForCommands () |
void | responseReceived (QJsonValue value, std::any context) |
void | stateChanged (Qmpi::State state) |
Public Member Functions | |
Qmpi (const QHostAddress &address, quint16 port, QObject *parent=nullptr) | |
Qmpi (const QString &hostname, quint16 port, QObject *parent=nullptr) | |
Qmpi (QObject *parent=nullptr) | |
~Qmpi () | |
void | abort () |
QHostAddress | address () const |
void | connectToHost () |
void | disconnectFromHost () |
void | execute (QString command, QJsonObject args=QJsonObject(), std::any context=std::any()) |
QString | hostname () const |
bool | isConnected () const |
bool | isConnectionActive () const |
quint16 | port () const |
void | setAddress (const QHostAddress address) |
void | setHostname (const QString hostname) |
void | setPort (quint16 port) |
void | setTransactionTimeout (int timeout=30000) |
State | state () const |
int | transactionTimeout () const |
Public Member Functions inherited from QObject | |
QObject (QObject *parent) | |
bool | blockSignals (bool block) |
const QObjectList & | children () const const |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const |
void | deleteLater () |
void | destroyed (QObject *obj) |
bool | disconnect (const char *signal, const QObject *receiver, const char *method) const const |
bool | disconnect (const QObject *receiver, const char *method) const const |
void | dumpObjectInfo () const const |
void | dumpObjectTree () const const |
QList< QByteArray > | dynamicPropertyNames () const const |
virtual bool | event (QEvent *e) |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
T | findChild (const QString &name, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QString &name, Qt::FindChildOptions options) const const |
QList< T > | findChildren (Qt::FindChildOptions options) const const |
bool | inherits (const char *className) const const |
void | installEventFilter (QObject *filterObj) |
bool | isQuickItemType () const const |
bool | isWidgetType () const const |
bool | isWindowType () const const |
void | killTimer (int id) |
virtual const QMetaObject * | metaObject () const const |
void | moveToThread (QThread *targetThread) |
QString | objectName () const const |
void | objectNameChanged (const QString &objectName) |
QObject * | parent () const const |
QVariant | property (const char *name) const const |
Q_CLASSINFO (Name, Value) | |
Q_DISABLE_COPY (Class) | |
Q_DISABLE_COPY_MOVE (Class) | |
Q_EMIT Q_EMIT | |
Q_ENUM (...) | |
Q_ENUM_NS (...) | |
Q_ENUMS (...) | |
Q_FLAG (...) | |
Q_FLAG_NS (...) | |
Q_FLAGS (...) | |
Q_GADGET Q_GADGET | |
Q_GADGET_EXPORT (EXPORT_MACRO) | |
Q_INTERFACES (...) | |
Q_INVOKABLE Q_INVOKABLE | |
Q_MOC_INCLUDE Q_MOC_INCLUDE | |
Q_NAMESPACE Q_NAMESPACE | |
Q_NAMESPACE_EXPORT (EXPORT_MACRO) | |
Q_OBJECT Q_OBJECT | |
Q_PROPERTY (...) | |
Q_REVISION Q_REVISION | |
Q_SET_OBJECT_NAME (Object) | |
Q_SIGNAL Q_SIGNAL | |
Q_SIGNALS Q_SIGNALS | |
Q_SLOT Q_SLOT | |
Q_SLOTS Q_SLOTS | |
T | qobject_cast (const QObject *object) |
T | qobject_cast (QObject *object) |
QT_NO_NARROWING_CONVERSIONS_IN_CONNECT QT_NO_NARROWING_CONVERSIONS_IN_CONNECT | |
void | removeEventFilter (QObject *obj) |
void | setObjectName (const QString &name) |
void | setObjectName (QAnyStringView name) |
void | setParent (QObject *parent) |
bool | setProperty (const char *name, const QVariant &value) |
bool | signalsBlocked () const const |
int | startTimer (int interval, Qt::TimerType timerType) |
int | startTimer (std::chrono::milliseconds time, Qt::TimerType timerType) |
QThread * | thread () const const |
Additional Inherited Members | |
Static Public Member Functions inherited from QObject | |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, Functor functor) |
bool | disconnect (const QMetaObject::Connection &connection) |
bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method) |
bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method) |
bool | disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method) |
QString | tr (const char *sourceText, const char *disambiguation, int n) |
Public Attributes inherited from QObject | |
typedef | QObjectList |
Protected Member Functions inherited from QObject | |
virtual void | childEvent (QChildEvent *event) |
virtual void | connectNotify (const QMetaMethod &signal) |
virtual void | customEvent (QEvent *event) |
virtual void | disconnectNotify (const QMetaMethod &signal) |
bool | isSignalConnected (const QMetaMethod &signal) const const |
int | receivers (const char *signal) const const |
QObject * | sender () const const |
int | senderSignalIndex () const const |
virtual void | timerEvent (QTimerEvent *event) |
Properties inherited from QObject | |
objectName | |
The interface works asynchronously, providing updates via signals and requires an event loop to function.
All commands issued through the interface may optionally contain a context parameter in the form of std::any, which will be provided along with that command's corresponding response when responseReceived() or error errorResponseReceived() is emitted.
This enum describes the various communication errors that can occur.
enum Qmpi::State |
This enum describes the different states in which the interface can be.
|
explicit |
Constructs a QMP interface with parent parent.
The connection address is set to QHostAddress::LocalHost and the port is set to 4444.
|
explicit |
Constructs a QMP interface configured to connect to a QEMU instance at address address on port port. port is specified in native byte order.
The parent of the object is set to parent.
Constructs a QMP interface configured to connect to a QEMU instance at hostname hostname on port port. hostname may be an IP address in string form (e.g., "192.168.1.1", or "7e72:692b:9797:8310:8e68:3a98:3a21:473c"), or it may be a host name (e.g., "example.com"). The interface will do a lookup only if required. port is specified in native byte order.
The parent of the object is set to parent.
Qmpi::~Qmpi | ( | ) |
Destroys the interface, closing the underlying connection immediately if necessary.
void Qmpi::abort | ( | ) |
Immediately closes the interface's connection, discarding any pending data.
QHostAddress Qmpi::address | ( | ) | const |
Returns the IP address the interface is configured to connect to if it was set as a QHostAddress; otherwise, returns QHostAddress::Null.
|
signal |
This signal is emitted when the interface's command queue becomes empty and the response to the last command in the queue has been received, just before it enters State::Idle.
|
signal |
This signal is emitted when IO with the server fails, the known QMP protocol is violated during communication, or the server otherwise behaves unexpectedly. error contains the type of communication error.
|
signal |
This signal is emitted once the interface has finished connecting to the QEMU instance and performing capabilities negotiation.
version contains the server's version information, while capabilities describes the server's QMP capabilities.
|
signal |
This signal is emitted when the underlying socket experiences a connection error, with error containing the type of error.
void Qmpi::connectToHost | ( | ) |
Attempts to make a connection to the QEMU instance via the interfaces configured address and port.
The interface's state immediately changes to Connecting, followed by AwaitingWelcome if the underlying socket successfully establishes a connection. Once the interface has received the server's welcome message, connected() is emitted and Qmpi enters the Negotiating state. At this point capabilities negotiation is handled automatically since only the base protocol capabilities are currently supported, upon which readyForCommands() is emitted and the interface enters the Idle state.
At any point, the interface can emit connectionErrorOccurred() or communicationErrorOccurred() to signal an error occurred.
|
signal |
This signal is emitted when the interface has fully disconnected from the QEMU instance.
void Qmpi::disconnectFromHost | ( | ) |
Initiates closure of the interface's connection and enters the Closing state. If there is pending data waiting to be written, Qmpi waits until all data has been written before the connection is closed. Eventually, it will enter the Disconnected state and emit the disconnected() signal.
|
signal |
This signal is emitted when the an error response is received from the server after executing a command.
errorClass contains the type of error and description contains a human readable summary of the error, while context contains the context object provided when the command was executed, if set.
|
signal |
This signal is emitted when an asynchronous event is posted by the server.
The signal parameters provide the name of the event, the event's data and its timestamp.
void Qmpi::execute | ( | QString | command, |
QJsonObject | args = QJsonObject() , |
||
std::any | context = std::any() |
||
) |
Executes the provided command on the connected QEMU instance.
command | The name of the command to execute. |
args | The command's arguments, if any. |
context | An optional object that will be provided with server's response to the command. |
The command is placed into an internal queue to be sent when it reaches the front. Only one command is sent at a time, with the interface waiting for a response before sending the next.
The server responds to commands in the same order they are received.
The context parameter is useful for identifying which command a response is directed towards or associating the response to a command with specific data or actions (i.e. a callback function).
|
signal |
This signal is emitted when the interface returns to the Disconnected state for any reason, regardless of whether or not a complete connection to the server was ever achieved.
QString Qmpi::hostname | ( | ) | const |
Returns the hostname the interface is configured to connect to if it was set as a hostname; otherwise, returns a null string.
bool Qmpi::isConnected | ( | ) | const |
Returns true
if the interface has fully connected to the QEMU instance, meaning the underlying socket successfully established a connection and the server's greeting was received.
bool Qmpi::isConnectionActive | ( | ) | const |
Returns true
if the interface's connection is active is any way. That is, the interface is in any state other than Disconnected; otherwise, returns false
.
quint16 Qmpi::port | ( | ) | const |
|
signal |
This signal is emitted once capabilities negotiation has been completed and the server is ready to receive commands.
|
signal |
This signal is emitted when a success (i.e. return
) response is received from the server after executing a command.
value contains the return value of the command, which will be an empty QJsonObject if the command does not return data, while context contains the context object provided when the command was executed, if set.
void Qmpi::setAddress | ( | const QHostAddress | address | ) |
Sets the address of the interface to address.
This function does nothing if the connection is currently active
void Qmpi::setHostname | ( | const QString | hostname | ) |
Sets the hostname of the interface to hostname.
This function does nothing if the connection is currently active
void Qmpi::setPort | ( | quint16 | port | ) |
Sets the port, specified in native byte order, of the interface to port.
This function does nothing if the connection is currently active
void Qmpi::setTransactionTimeout | ( | int | timeout = 30000 | ) |
Sets the transaction timeout of the interface to timeout.
The transaction timeout is how long the connected QEMU instance has to reply with a complete message after sending a command before the connection is automatically closed.
The received message does not have to be a response to that particular command; any received message will reset the timeout, while sending additional commands will not.
If a transaction timeout occurs communicationErrorOccurred() will be emitted with the value TransactionTimeout. If all sent commands have been processed and the interface is not currently expecting any responses the timeout will not be in effect.
Setting this value to -1
will disable the timeout.
Qmpi::State Qmpi::state | ( | ) | const |
Returns the state of the interface.
|
signal |
This signal is emitted when the interface's state changes, with state containing the new state.
int Qmpi::transactionTimeout | ( | ) | const |
Returns the transaction timeout of the interface.
The default is 30,000 milliseconds.