blob: f3adcb90f040b67c9e910daace94617f4a76237b (
plain)
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
|
#include "localserver.h"
#include <QFile>
#include <QStringList>
/**
* @brief LocalServer::LocalServer
* Constructor
*/
LocalServer::LocalServer()
{
}
/**
* @brief LocalServer::~LocalServer
* Destructor
*/
LocalServer::~LocalServer()
{
server->close();
for(int i = 0; i < clients.size(); ++i)
{
clients[i]->close();
}
}
/**
* -----------------------
* QThread requred methods
* -----------------------
*/
/**
* @brief run
* Initiate the thread.
*/
void LocalServer::run()
{
server = new QLocalServer();
QObject::connect(server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
QObject::connect(this, SIGNAL(privateDataReceived(QString)), this, SLOT(slotOnData(QString)));
#ifdef Q_OS_UNIX
// Make sure the temp address file is deleted
QFile address(QString("/tmp/" LOCAL_SERVER_NAME));
if(address.exists()){
address.remove();
}
#endif
QString serverName = QString(LOCAL_SERVER_NAME);
server->listen(serverName);
while(server->isListening() == false){
server->listen(serverName);
msleep(100);
}
exec();
}
/**
* @brief LocalServer::exec
* Keeps the thread alive. Waits for incomming connections
*/
void LocalServer::exec()
{
while(server->isListening())
{
msleep(100);
server->waitForNewConnection(100);
for(int i = 0; i < clients.size(); ++i)
{
if(clients[i]->waitForReadyRead(100)){
QByteArray data = clients[i]->readAll();
emit privateDataReceived(data);
}
}
}
}
/**
* -------
* SLOTS
* -------
*/
/**
* @brief LocalServer::slotNewConnection
* Executed when a new connection is available
*/
void LocalServer::slotNewConnection()
{
clients.push_front(server->nextPendingConnection());
}
/**
* @brief LocalServer::slotOnData
* Executed when data is received
* @param data
*/
void LocalServer::slotOnData(QString data)
{
if(data.contains("CMD:", Qt::CaseInsensitive)){
onSGC(data);
} else {
emit dataReceived(data);
}
}
/**
* -------
* Helper methods
* -------
*/
void LocalServer::onCMD(QString data)
{
// Trim the leading part from the command
data.replace(0, 4, "");
QStringList commands;
commands << "showUp";
switch(commands.indexOf(data)){
case 0:
emit showUp();
}
}
|