1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#ifndef BALL_PYTHON_PYSERVER_H
#define BALL_PYTHON_PYSERVER_H
#include <BALL/COMMON/global.h>
#include <QtNetwork/QTcpServer>
#include <memory>
namespace BALL
{
class BALL_EXPORT PyServer final
{
public:
/**
* Starts a new Python server and listens for requests.
*/
PyServer();
~PyServer() = default;
/**
* Processes a single request and sends a suitable response.
*/
void processRequest();
private:
/**
* Processes a single `execute_request`, that is, runs the received Python code through PyInterpreter
* and sends the result to the requesting client.
*
* @param client
* @param request
*/
void processExecuteRequest(QTcpSocket* client, const QString& request);
/**
* Utility function to create a JSON-encoded message to a client.
*
* @param msg_type `execute_result` or `error`
* @param content actual message
* @return JSON-encoded message
*/
QByteArray createMessage(QString msg_type, QString content);
/**
* Disconnects a client connection (after all messages to the same client have been sent).
*
* @param client
*/
void disconnectClient(QTcpSocket* client);
std::unique_ptr<QTcpServer> server_ {nullptr};
};
}
#endif // BALL_PYTHON_PYSERVER_H
|