1
|
#include "curvedataserver.h"
|
2
|
|
3
|
#include <QDebug>
|
4
|
#include <QHostAddress>
|
5
|
#include <QAbstractSocket>
|
6
|
|
7
|
CurveDataServer::CurveDataServer() :
|
8
|
_server(this),
|
9
|
_sockets()
|
10
|
{
|
11
|
_server.listen(QHostAddress::Any, 4242);
|
12
|
connect(&_server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
|
13
|
}
|
14
|
|
15
|
CurveDataServer::~CurveDataServer()
|
16
|
{
|
17
|
// asi vlastne neni co
|
18
|
}
|
19
|
|
20
|
void CurveDataServer::onNewConnection()
|
21
|
{
|
22
|
QTcpSocket *clientSocket = _server.nextPendingConnection();
|
23
|
connect(clientSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
|
24
|
connect(clientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onSocketStateChanged(QAbstractSocket::SocketState)));
|
25
|
|
26
|
_sockets.push_back(clientSocket);
|
27
|
|
28
|
sendPreamble(clientSocket);
|
29
|
}
|
30
|
|
31
|
void CurveDataServer::onSocketStateChanged(QAbstractSocket::SocketState socketState)
|
32
|
{
|
33
|
if (socketState == QAbstractSocket::UnconnectedState)
|
34
|
{
|
35
|
QTcpSocket* sender = static_cast<QTcpSocket*>(QObject::sender());
|
36
|
_sockets.removeOne(sender);
|
37
|
}
|
38
|
}
|
39
|
|
40
|
void CurveDataServer::onReadyRead()
|
41
|
{
|
42
|
// tady actually nakonec switch zpracovani ruznych zprav dle identifikatoru
|
43
|
QTcpSocket* sender = static_cast<QTcpSocket*>(QObject::sender());
|
44
|
QByteArray data = sender->readAll();
|
45
|
for (QTcpSocket* socket : _sockets) {
|
46
|
if (socket != sender)
|
47
|
socket->write(QByteArray::fromStdString(sender->peerAddress().toString().toStdString() + ": " + data.toStdString()));
|
48
|
}
|
49
|
}
|
50
|
|
51
|
QByteArray *CurveDataServer::createMessageId(int sizeId, int typeId)
|
52
|
{
|
53
|
QByteArray *id = new QByteArray();
|
54
|
|
55
|
id->append((sizeId & 0x0F) << 4 + ((typeId & 0x0F00) >> 12));
|
56
|
id->append(typeId & 0xFF);
|
57
|
|
58
|
return id;
|
59
|
}
|
60
|
/*
|
61
|
QByteArray *CurveDataServer::appendFloatToMessage(QByteArray *message, float f)
|
62
|
{
|
63
|
if (!message)
|
64
|
return message;
|
65
|
|
66
|
unsigned char *c = reinterpret_cast<unsigned char *>(&f);
|
67
|
|
68
|
for (int i = 0; i < 4; i++)
|
69
|
{
|
70
|
message->append(c[3 - i]);
|
71
|
}
|
72
|
|
73
|
return message;
|
74
|
}
|
75
|
*/
|
76
|
QByteArray *CurveDataServer::putFloatIntoMessage(QByteArray *message, int index, float f)
|
77
|
{
|
78
|
if (!message || message->size() < index + 4)
|
79
|
return message;
|
80
|
|
81
|
unsigned char *c = reinterpret_cast<unsigned char *>(&f);
|
82
|
|
83
|
for (int i = 0; i < 4; i++)
|
84
|
{
|
85
|
(*message)[index + i] = c[i];
|
86
|
}
|
87
|
|
88
|
return message;
|
89
|
}
|
90
|
|
91
|
|
92
|
void CurveDataServer::sendApplicatorPoint(float x, float y, float z)
|
93
|
{
|
94
|
QByteArray message(18, 0);
|
95
|
message[0] = 0x03;
|
96
|
message[1] = 0x40;
|
97
|
|
98
|
putFloatIntoMessage(&message, 2, x);
|
99
|
putFloatIntoMessage(&message, 6, y);
|
100
|
putFloatIntoMessage(&message, 10, z);
|
101
|
putFloatIntoMessage(&message, 14, 0.0);
|
102
|
|
103
|
for (QTcpSocket* socket : _sockets) {
|
104
|
socket->write(message);
|
105
|
}
|
106
|
//qDebug() << message.toHex();
|
107
|
//qDebug() << x << " " << y << " " << z;
|
108
|
}
|
109
|
|
110
|
|
111
|
void CurveDataServer::sendPreamble(QTcpSocket *socket)
|
112
|
{
|
113
|
sendProtocolMagic(socket);
|
114
|
sendVersion(socket);
|
115
|
}
|
116
|
|
117
|
void CurveDataServer::sendProtocolMagic(QTcpSocket *socket)
|
118
|
{
|
119
|
char *messageContent = "DeltaRVr";
|
120
|
QByteArray message(10, 0);
|
121
|
message[0] = 0x01;
|
122
|
message[1] = 0x30;
|
123
|
|
124
|
for (int i = 0; i < 8; i++)
|
125
|
{
|
126
|
message[i + 2] = messageContent[i];
|
127
|
}
|
128
|
|
129
|
socket->write(message);
|
130
|
|
131
|
qDebug() << "Sent Protocol Magic to" << socket->peerAddress();
|
132
|
//qDebug() << message.toHex();
|
133
|
}
|
134
|
|
135
|
void CurveDataServer::sendVersion(QTcpSocket *socket)
|
136
|
{
|
137
|
QByteArray message(6, 0);
|
138
|
message[0] = 0x02;
|
139
|
message[1] = 0x20;
|
140
|
|
141
|
message[2] = 0x01;
|
142
|
|
143
|
socket->write(message);
|
144
|
|
145
|
qDebug() << "Sent Protocol Version to" << socket->peerAddress();
|
146
|
//qDebug() << message.toHex();
|
147
|
}
|