Projekt

Obecné

Profil

Stáhnout (4 KB) Statistiky
| Větev: | Tag: | Revize:
1
#ifndef CURVEDATASERVER_H
2
#define CURVEDATASERVER_H
3

    
4
#include <QObject>
5
#include <QTcpServer>
6
#include <QTcpSocket>
7
#include <map>
8
#include <mutex>
9
#include <thread>
10

    
11

    
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
class CurveDataServer : QObject
18
{
19
    Q_OBJECT
20

    
21
public:
22

    
23
    /**
24
     * Holds information about a client, its Qt socket, data buffer etc.
25
     */
26
    struct Client {
27
        /**
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
        static const size_t BUF_SIZE = 4096;
33

    
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

    
37
        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

    
42
        char buf[BUF_SIZE];                 ///< Message buffer
43
        size_t bufUsed = 0;                 ///< Number of bytes used up in the message buffer
44

    
45
        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

    
49
        Client(QTcpSocket* socket);
50
    };
51

    
52
    explicit CurveDataServer();
53
    ~CurveDataServer();
54

    
55
    /** Sends the current actuator position. */
56
    void sendActuatorPosition(float x, float y, float z);
57

    
58
    /** Sends the actual direction the actuator is moving in. */
59
    void sendActualDirection(float x, float y, float z);
60

    
61
    /** Sends the direction the actuator *should* be moving in, in order to draw the curve. */
62
    void sendTargetDirection(float x, float y, float z);
63

    
64
    /** Stores and sends the specified curve. */
65
    void sendNewCurve(QList<QVector4D> &points);
66

    
67
    /** Stores and sends the specified curve. */
68
    void sendNewCurve(QList<QVector3D> &points);
69

    
70
    /** Sends the currently stored curve. This is used for newly connected clients. */
71
    void sendCurve(Client &client);
72

    
73
    /** Sends ping messages to the clients, checks whether any clients have timed out, and disconnects any that have. */
74
    void processPings();
75

    
76
public slots:
77
    void onNewConnection();
78
    void onSocketStateChanged(QAbstractSocket::SocketState socketState);
79
    void onReadyRead();
80

    
81
private:
82
    /** Gets the client that is connected via the specified socket. */
83
    QList<Client>::iterator clientOf(QTcpSocket *socket);
84

    
85
    void handleMessage(Client& client, uint16_t messageId, void* content, size_t contentSize);
86
    void handleProtocolMagic(Client& client, char* content);
87
    void handleVersion(Client& client, uint8_t content);
88
    void handlePong(Client& client, uint64_t* content);
89
    void handleEOT(Client& client, char* content, size_t contentSize);
90

    
91
    void removeSocket(QTcpSocket *socket);
92
    void terminateConnection(Client& client, std::string&& reason);
93
    void sendPing(Client& client);
94

    
95
    void sendPreamble(QTcpSocket *socket);
96
    void sendProtocolMagic(QTcpSocket *socket);
97
    void sendVersion(QTcpSocket *socket);
98
    void sendEot(QTcpSocket *socket, std::string&& message);
99

    
100
    void sendMessageToAllConnected(QByteArray &message);
101

    
102
    QTcpServer _server;
103

    
104
    std::recursive_mutex _socketsLock;
105
    QList<Client> _sockets;
106

    
107
    std::recursive_mutex _currentCurveLock;
108
    QList<QVector3D> _currentCurve;
109
};
110

    
111
#endif // CURVEDATASERVER_H
(3-3/19)