Projekt

Obecné

Profil

Stáhnout (4 KB) Statistiky
| Větev: | Tag: | Revize:
1 681d1a97 Jakub Hejman
#ifndef CURVEDATASERVER_H
2
#define CURVEDATASERVER_H
3
4
#include <QObject>
5
#include <QTcpServer>
6
#include <QTcpSocket>
7 0cda8e8d Oto Šťáva
#include <map>
8
#include <mutex>
9 6d9543aa Jakub Hejman
#include <thread>
10 f06d5ae1 Jakub Hejman
11 313ff1dc Oto Šťáva
12
/**
13
 * Manages communication with clients who can connect to the server in order to receive real-time data about the current
14
 * exercise. The served data may be used e.g. for external visualization. A reference implementation of such a client is
15
 * the deltarobot-vr Unity application.
16
 */
17 681d1a97 Jakub Hejman
class CurveDataServer : QObject
18
{
19
    Q_OBJECT
20
21
public:
22 313ff1dc Oto Šťáva
23
    /**
24
     * Holds information about a client, its Qt socket, data buffer etc.
25
     */
26 0cda8e8d Oto Šťáva
    struct Client {
27 285c6fe5 Oto Šťáva
        /**
28
         * Size of the client buffer.
29
         *
30
         * @note Current implementation truncates any variable-length messages longer than what would fit in the buffer.
31
         */
32 6d9543aa Jakub Hejman
        static const size_t BUF_SIZE = 4096;
33 285c6fe5 Oto Šťáva
34
        static const size_t MESSAGE_ID_SIZE = sizeof(uint16_t);      ///< Size of the protocol message identifier.
35
        static const size_t MESSAGE_LENGTH_SIZE = sizeof(uint32_t);  ///< Size of the variable-length message's content length.
36 0cda8e8d Oto Šťáva
37 313ff1dc Oto Šťáva
        QTcpSocket* socket;                 ///< Communication socket
38
        std::map<uint64_t, uint64_t> ping;  ///< Key: ping ID;  Value: ping timestamp (msec);
39
        uint64_t latency;                   ///< Current latency
40
        uint64_t lastPong;                  ///< Timestamp of the last pong
41 0cda8e8d Oto Šťáva
42 313ff1dc Oto Šťáva
        char buf[BUF_SIZE];                 ///< Message buffer
43
        size_t bufUsed = 0;                 ///< Number of bytes used up in the message buffer
44 0cda8e8d Oto Šťáva
45 313ff1dc Oto Šťáva
        bool magic = false;                 ///< Whether the client has sent the protocol magic
46
        bool version = false;               ///< Whether the client has sent the protocol version
47
        bool initialized = false;           ///< Whether the client has successfully initialized its communication (by sending its magic and version)
48 6d9543aa Jakub Hejman
49 0cda8e8d Oto Šťáva
        Client(QTcpSocket* socket);
50
    };
51
52
    explicit CurveDataServer();
53
    ~CurveDataServer();
54
55 313ff1dc Oto Šťáva
    /** Sends the current actuator position. */
56 e6053a29 Oto Šťáva
    void sendActuatorPosition(float x, float y, float z);
57 313ff1dc Oto Šťáva
58
    /** Sends the actual direction the actuator is moving in. */
59 f06d5ae1 Jakub Hejman
    void sendActualDirection(float x, float y, float z);
60 313ff1dc Oto Šťáva
61
    /** Sends the direction the actuator *should* be moving in, in order to draw the curve. */
62 f06d5ae1 Jakub Hejman
    void sendTargetDirection(float x, float y, float z);
63 313ff1dc Oto Šťáva
64
    /** Stores and sends the specified curve. */
65 705930ed Jakub Hejman
    void sendNewCurve(QList<QVector4D> &points);
66 313ff1dc Oto Šťáva
67
    /** Stores and sends the specified curve. */
68 705930ed Jakub Hejman
    void sendNewCurve(QList<QVector3D> &points);
69 313ff1dc Oto Šťáva
70
    /** Sends the currently stored curve. This is used for newly connected clients. */
71 6d9543aa Jakub Hejman
    void sendCurve(Client &client);
72
73 313ff1dc Oto Šťáva
    /** Sends ping messages to the clients, checks whether any clients have timed out, and disconnects any that have. */
74
    void processPings();
75 681d1a97 Jakub Hejman
76
public slots:
77
    void onNewConnection();
78
    void onSocketStateChanged(QAbstractSocket::SocketState socketState);
79
    void onReadyRead();
80
81
private:
82 313ff1dc Oto Šťáva
    /** Gets the client that is connected via the specified socket. */
83 0cda8e8d Oto Šťáva
    QList<Client>::iterator clientOf(QTcpSocket *socket);
84
85 285c6fe5 Oto Šťáva
    void handleMessage(Client& client, uint16_t messageId, void* content, size_t contentSize);
86 6d9543aa Jakub Hejman
    void handleProtocolMagic(Client& client, char* content);
87
    void handleVersion(Client& client, uint8_t content);
88
    void handlePong(Client& client, uint64_t* content);
89 285c6fe5 Oto Šťáva
    void handleEOT(Client& client, char* content, size_t contentSize);
90 6d9543aa Jakub Hejman
91
    void removeSocket(QTcpSocket *socket);
92
    void terminateConnection(Client& client, std::string&& reason);
93
    void sendPing(Client& client);
94 0cda8e8d Oto Šťáva
95 e60ade62 Jakub Hejman
    void sendPreamble(QTcpSocket *socket);
96
    void sendProtocolMagic(QTcpSocket *socket);
97
    void sendVersion(QTcpSocket *socket);
98 0cda8e8d Oto Šťáva
    void sendEot(QTcpSocket *socket, std::string&& message);
99 681d1a97 Jakub Hejman
100 6d9543aa Jakub Hejman
    void sendMessageToAllConnected(QByteArray &message);
101
102 681d1a97 Jakub Hejman
    QTcpServer _server;
103 0cda8e8d Oto Šťáva
104
    std::recursive_mutex _socketsLock;
105
    QList<Client> _sockets;
106 6d9543aa Jakub Hejman
107
    std::recursive_mutex _currentCurveLock;
108
    QList<QVector3D> _currentCurve;
109 681d1a97 Jakub Hejman
};
110
111
#endif // CURVEDATASERVER_H