-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
134 lines (109 loc) · 4.7 KB
/
Copy pathserver.cpp
File metadata and controls
134 lines (109 loc) · 4.7 KB
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
#include "server.h"
#include "mysqlconn.h"
/** Constructor **/
Server::Server(QObject *parent) : QTcpServer(parent){
connection = std::make_shared<MySqlConn>();
connection->openConn("probe_requests_db", "root", "password", "localhost");
}
/** Sets server port **/
void Server::setPort(int port){
this->port = port;
}
/** This method set a new connection, listening on port passed as parameter **/
void Server::setConnection(){
if (!listen(QHostAddress::Any, port)) {
qDebug("Unable to start server\n");
close();
return;
}
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i) {
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
break;
}
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
qDebug("The server is running\n");
}
/** This method sets how many boards are expected by server during syncro. mode **/
void Server::setNumberOfHosts(int n){
this->scheduledBoards = n;
}
/** It returns number of scheduled boards so far **/
int Server::getNumberOfHosts(int){
return this->scheduledBoards;
}
void Server::newThreadRecord(){
qDebug("New Thread\n");
}
/** Returns the system time **/
unsigned long Server::getSystemTime(){
double now = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
unsigned long _now = htonl((unsigned long) now);
return _now;
}
/** Invoked when a new connection is available **/
void Server::incomingConnection(qintptr socketDescriptor){
/* The following code is executed each time a new board gets connected to server */
qDebug("New Connection!");
emit newConnect();
//std::shared_ptr<MySqlConn> conn = std::make_shared<MySqlConn>();
ServerThread *thread = new ServerThread(socketDescriptor, this, connection);
//ServerThread *thread = new ServerThread(socketDescriptor, this, conn);
//connect(thread, &ServerThread::finished, thread, &ServerThread::deleteLater);
//connect(thread, &ServerThread::finished, this, &Server::threadFinished);
newThreadRecord();
thread->start();
}
/** This method is invoked when a sub-thread ends. If the number of threads becomes zero
* means that all boards have finished to exchange data with server.
**/
void Server::threadFinished(){
// qDebug("Thread finished\n");
// qDebug() << "All thread have finished" << endl;
// MySqlConn conn;
// if(!conn.openConn("probe_requests_db", "root", "password", "localhost"))
// qDebug() << "Error on creating connection" << endl;
// else
// qDebug() << "OK connection" << endl;
// for(int i = 0; i < packetsArray->size(); i++){
// //qDebug() << packetsArray->at(i).c_str() << endl;
// std::vector<std::string> v = split(packetsArray->at(i), ',');
// std::string pHash = v.at(v.size() - 1); // hash received by board
// //qDebug() << "Hash" << pHash.c_str() << endl;
// std::size_t found = packetsArray->at(i).find(pHash);
// if (found != std::string::npos){
// //qDebug() << "Hash checking OK: they're the same string" << endl;
// // INSERIRE QUI LA CHIAMATA A FUNZIONE PER INSERIRE IL PACCHETTO NEL DB
// conn.insertData(QString(packetsArray->at(i).c_str()));
// }
// else
// qDebug() << "Hash checking FAILED: they're not the same string" << endl;
// }
// // Alla fine svuoto il vector per ricevere i prossimi pacchetti
// packetsArray->clear();
// conn.selectAll();
// FARE QUI I VARI FILTRAGGI, SCARTANDO I PACCHETTI CHE NON SERVONO
// Una volta finito lanciare un segnale alla GUI, che aprirà il DB e disegnerà
// i puntini sul grafico
// emit paintDevicesSignal();
// In questo punto devo aggiornare i parametri della cattura che sta per iniziare.
// Devo aggiornare il numero di board attese per la prossima cattura, azzerare il numero
// di board che hanno completato il processo
//this->syncronizedBoards = number_of_hosts; // riporto il numero di board al valore atteso iniziale
//boardsProcessDone = 0;
//sleep(5);
// sleep(3);
// std::lock_guard<std::mutex> lg(*(cv_mutex));
// (*spuriusFlag) = true; // flag delle notifiche spurie disattivato
// qDebug() << "notifyAll now!" << endl;
// (*cv).notify_all();
// (*spuriusFlag) = false; // flag delle notifiche spurie riattivato
}