-
Notifications
You must be signed in to change notification settings - Fork 10
/
saveserver.cpp
232 lines (212 loc) · 7.65 KB
/
saveserver.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "saveserver.h"
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QNetworkInterface>
saveServer::saveServer(frameWorker *fw, QObject *parent ) :
QTcpServer(parent)
{
this->reference = fw;
port = 65000; // we'll hardcode the port number for now
clientConnection = new QTcpSocket();
clientConnection->setObjectName("lv:saveconn");
ipAddress = QHostAddress("127.0.0.1");
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readCommand()));
connect(this, SIGNAL(newConnection()), this, SLOT(new_conn_slot()) );
connect(clientConnection, SIGNAL(disconnected()), this, SLOT(handleDisconnect()));
signalConnected = true;
listen(ipAddress, port); // This will automatically connect on the IP Address of the machine
}
void saveServer::reconnectSignal()
{
if(!signalConnected)
{
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readCommand()));
signalConnected = true;
}
}
void saveServer::disconnectSignal()
{
if(signalConnected)
{
disconnect(clientConnection, SIGNAL(readyRead()), this, SLOT(readCommand()));
signalConnected = false;
}
}
void saveServer::handleDisconnect()
{
genStatusMessage("Remote host disconnected.");
}
void saveServer::incomingConnection(qintptr socketDescriptor)
{
if(!clientConnection->setSocketDescriptor(socketDescriptor)) {
genErrorMessage("Client connection refused by host.");
return;
}
}
void saveServer::connected_to_client()
{
genStatusMessage("Called connected()");
}
void saveServer::new_conn_slot()
{
// This indicates a client has connected.
genStatusMessage("New remote connection is active.");
}
bool saveServer::checkValues(uint16_t framesToSaveCount,
QString filename, uint16_t naverages)
{
bool ok = true;
if(framesToSaveCount > 60000)
ok = false;
if((filename.length() < 4) or (filename.length() > 4096))
ok = false;
if( (naverages > 2000) or (naverages > framesToSaveCount))
ok = false;
return ok;
}
void saveServer::readCommand()
{
readMutex.lock();
QDataStream in(clientConnection);
in.setVersion(QDataStream::Qt_4_0);
blockSize = 0;
bool readData = true;
while(readData)
{
if (clientConnection->bytesAvailable() < (qint64) sizeof(quint16)) {
//clientConnection->disconnectFromHost();
//genStatusMessage("No further data available over tcp/ip.");
readMutex.unlock();
readData = false;
return;
}
in >> blockSize;
//qInfo() << "Block size: " << blockSize;
if (clientConnection->bytesAvailable() < blockSize) {
std::stringstream msg;
msg << "Only " << clientConnection->bytesAvailable() << " bytes to read compared with block of " << blockSize << " bytes." << std::endl;
genErrorMessage(QString::fromStdString(msg.str()));
//clientConnection->disconnectFromHost();
readMutex.unlock();
return;
}
in >> commandType;
// genStatusMessage("Command received."); // DEBUG message, disable on release build
switch (commandType) {
case CMD_START_SAVING:
genStatusMessage("SAVE command received.");
if(reference->to.save_framenum == 0)
{
in >> framesToSave;
in >> fname;
in >> navgs;
if(checkValues(framesToSave, fname, navgs))
{
reference->navgs = navgs;
reference->startSavingRawData(framesToSave, fname, navgs);
}
} else {
genErrorMessage("Received SAVE command while already saving.");
}
break;
case CMD_START_FLIGHT_SAVING:
{
genStatusMessage("START_FLIGHT_SAVING command received.");
// No arguments expected.
// emit signal to MainWindow which will trigger the automatic recording of data.
emit startSavingFlightData();
}
break;
case CMD_STATUS:
{
genStatusMessage("Sending STATUS information back.");
QByteArray block;
QDataStream out( &block, QIODevice::WriteOnly );
out.setVersion(QDataStream::Qt_4_0);
out << (uint16_t)0;
out << (uint16_t)reference->to.save_framenum; // send the number of frames left to save, and
out << (uint16_t)reference->delta; // send the frames per second
out << (uint16_t)reference->navgs; // number of averages, new code, bogus
out.device()->seek(0);
out << (uint16_t)(block.size() - sizeof(quint16));
//printHex(&block);
clientConnection->write(block);
break;
}
case CMD_STOP_SAVING:
{
genStatusMessage("Received STOP_SAVING command");
emit stopSavingData();
break;
}
case CMD_STATUS_EXTENDED:
{
genStatusMessage("Sending STATUS_EXTENDED information back.");
QByteArray block;
QDataStream out( &block, QIODevice::WriteOnly );
out.setVersion(QDataStream::Qt_4_0);
out << (uint16_t)0; // will be changed to the size of the message later.
out << (uint16_t)CMD_STATUS_EXTENDED; // new addition
// NOTE: When the total number of frames to be saved is undefined (start/stop recording mode),
// the value returned is bogus and should not be used.
out << (uint16_t)reference->to.save_framenum; // send the number of frames left to save, and
out << (uint16_t)reference->delta; // send the frames per second (as a uint)
out << (uint16_t)reference->navgs; // number of averages, new code, bogus
if(fname.isEmpty())
out << QString("");
else
out << fname;
out.device()->seek(0);
out << (uint16_t)(block.size() - sizeof(quint16));
//printHex(&block);
clientConnection->write(block);
break;
}
case CMD_START_DARKSUB:
{
genStatusMessage("Client requested CMD_START_DARKSUB, starting dark collection.");
emit startTakingDarks();
break;
}
case CMD_STOP_DARKSUB:
{
genStatusMessage("Client requested CMD_STOP_DARKSUB, stopping dark collection.");
emit stopTakingDarks();
break;
}
default:
genErrorMessage("Unknown command received: " + QString("0x%1").arg(commandType, 2, 16, QChar('0')));
genErrorMessage("Disconnecting remote host now.");
clientConnection->disconnectFromHost();
break;
}
}
readMutex.unlock();
}
void saveServer::genErrorMessage(QString errorMessage)
{
QString msg = "[saveServer]: ERROR: ";
msg.append(errorMessage);
std::cout << msg.toStdString() << std::endl;
emit sigMessage(msg);
}
void saveServer::genStatusMessage(QString statusMessage)
{
QString msg = "[saveServer]: Status: ";
msg.append(statusMessage);
std::cout << msg.toStdString() << std::endl;
emit sigMessage(msg);
}
void saveServer::printHex(QByteArray *b)
{
qDebug() << "Begin byte array debug: ";
QString i;
QString h;
for(int p=0; p < b->length(); p++)
{
i.append(QString("[%1]").arg(p,2,10,QChar('0')));
h.append(QString("[%1]").arg((unsigned char)b->at(p), 2, 16, QChar('0')));
}
qDebug() << i;
qDebug() << h;
qDebug() << "End byte array debug.";
}