QI-QMP v0.2.2
Qt-based Interface for QEMU Machine Protocol
qmpi.h
1#ifndef QMPI_H
2#define QMPI_H
3
4// Shared Lib Support
5#include "qi-qmp/qmpi_export.h"
6
7// Standard Library Includes
8#include <any>
9#include <queue>
10
11// Qt Includes
12#include <QObject>
13#include <QTcpSocket>
14#include <QJsonDocument>
15#include <QJsonObject>
16#include <QJsonArray>
17#include <QTimer>
18
19class QI_QMP_QMPI_EXPORT Qmpi : public QObject
20{
21 Q_OBJECT
22//-Class Enums--------------------------------------------------------------------------------------------------------------
23public:
24 enum State { Disconnected, Connecting, AwaitingWelcome, Negotiating, Idle, Closing, SendingCommand, AwaitingMessage, ReadingMessage };
25 enum CommunicationError { WriteFailed, ReadFailed, TransactionTimeout, UnexpectedReceive, UnexpectedResponse };
26
27//-Class Structs------------------------------------------------------------------------------------------------------------
28private:
29 struct ExecutionTask
30 {
31 QString command;
32 QJsonObject args;
33 std::any context;
34 };
35
36//-Instance Variables--------------------------------------------------------------------------------------------------------
37private:
38 // Network
39 std::variant<QHostAddress, QString> mHostId; static_assert(std::variant_size_v<decltype(mHostId)> == 2);
40 quint16 mPort;
41 QTcpSocket mSocket;
42
43 // Workables
44 State mState;
45 std::queue<ExecutionTask> mExecutionQueue;
46
47 // Timeout
48 QTimer mTransactionTimer;
49
50//-Constructor------------------------------------------------------------------------------------------------------------
51public:
52 explicit Qmpi(QObject* parent = nullptr);
53 explicit Qmpi(const QHostAddress& address, quint16 port, QObject* parent = nullptr);
54 explicit Qmpi(const QString& hostname, quint16 port, QObject* parent = nullptr);
55
56//-Destructor-------------------------------------------------------------
57public:
58 ~Qmpi();
59
60//-Instance Functions------------------------------------------------------------------------------------------------------
61private:
62 // Management
63 void changeState(State newState);
64 bool startTransactionTimer();
65 bool stopTransactionTimer();
66 void reset();
67 void raiseCommunicationError(CommunicationError error);
68 void finish();
69
70 // Setup
71 void negotiateCapabilities();
72
73 // Commands
74 bool sendCommand(QString command, QJsonObject args = QJsonObject());
75 void propagate();
76
77 // Message Processing
78 void processServerMessage(const QJsonObject& jMsg);
79 bool processGreetingMessage(const QJsonObject& jGreeting);
80 bool processSuccessMessage(const QJsonObject& jSuccess);
81 bool processErrorMessage(const QJsonObject& jError);
82 bool processEventMessage(const QJsonObject& jEvent);
83
84public:
85 // Info
86 State state() const;
87
88 // Properties
89 QHostAddress address() const;
90 QString hostname() const;
91 quint16 port() const;
92 int transactionTimeout() const;
93
94 void setAddress(const QHostAddress address);
95 void setHostname(const QString hostname);
96 void setPort(quint16 port);
97 void setTransactionTimeout(int timeout = 30000);
98
99 // Connection
100 void connectToHost();
101 void disconnectFromHost();
102 void abort();
103 bool isConnectionActive() const;
104 bool isConnected() const;
105
106 // Commands
107 void execute(QString command, QJsonObject args = QJsonObject(), std::any context = std::any());
108
109//-Signals & Slots------------------------------------------------------------------------------------------------------------
110private slots:
111 void handleSocketStateChange(QAbstractSocket::SocketState socketState);
112 void handleReceivedData();
113 void handleTransactionTimeout();
114
115signals:
116 void connected(QJsonObject version, QJsonArray capabilities);
118 void disconnected(); // Only emitted if a connection to the server was made in the first place
119 void finished(); // Emitted when the interface is fully closed, regardless of how it got there
120 void responseReceived(QJsonValue value, std::any context);
121 void eventReceived(QString name, QJsonObject data, QDateTime timestamp);
122 void connectionErrorOccurred(QAbstractSocket::SocketError error); // Will be disconnected after
123 void communicationErrorOccurred(Qmpi::CommunicationError error); // Will disconnect after
124 void errorResponseReceived(QString errorClass, QString description, std::any context); // Will not disconnect after
127};
128
129#endif // QMPI_H
The Qmpi class is an interface through which to communicate with a QEMU instance via the QEMU Machine...
Definition: qmpi.h:20
void stateChanged(Qmpi::State state)
State
Definition: qmpi.h:24
@ AwaitingMessage
Definition: qmpi.h:24
CommunicationError
Definition: qmpi.h:25
void eventReceived(QString name, QJsonObject data, QDateTime timestamp)
void connected(QJsonObject version, QJsonArray capabilities)
void commandQueueExhausted()
void communicationErrorOccurred(Qmpi::CommunicationError error)
void errorResponseReceived(QString errorClass, QString description, std::any context)
void disconnected()
void connectionErrorOccurred(QAbstractSocket::SocketError error)
void readyForCommands()
void finished()
void responseReceived(QJsonValue value, std::any context)
QTextStream & reset(QTextStream &stream)