/
kipc.cpp
211 lines (178 loc) · 5.37 KB
/
kipc.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
#include "kipc.h"
#include "kapp.h"
#include "klog.h"
#include "kdocument.h"
#include <QFile>
#include <QDir>
#include <QDesktopServices>
#include <QLocalSocket>
#define KLUDGET_IPC_NAME "kludgetsipc"
#define MAX_IPC_INDEX 100
KIPCServer::KIPCServer(QObject *parent) : QObject(parent)
{
_server = new QLocalServer(this);
listen();
}
KIPCServer::~KIPCServer()
{}
void KIPCServer::listen()
{
for(int i = 0; i<MAX_IPC_INDEX; i++) {
QString ipcName = QString("%1%2").arg(KLUDGET_IPC_NAME).arg(i);
if (_server->listen(ipcName)) {
connect(_server, SIGNAL(newConnection()),
this, SLOT(onProcessNewConnection()));
KLog::log(QString("ipc server listening %1").arg(ipcName));
break;
}
}
}
void KIPCServer::onProcessNewConnection()
{
while (_server->hasPendingConnections()) {
KIPCClient *client = new KIPCClient(this, _server->nextPendingConnection());
client->sendMessage("ping");
}
}
bool KIPCServer::sendMessage(QString message, QString id, QString instance)
{
QList<KIPCClient*> clients = findChildren<KIPCClient*>();
bool hasSent = false;
for (int i = 0; i<clients.length(); i++) {
// KLog::log(QString("sending %1").arg(clients[i]->isConnected()));
hasSent = clients[i]->sendMessage(message, id, instance) || hasSent;
}
for (int i = 0; i<clients.length(); i++) {
if (!clients[i]->isConnected()) {
delete clients[i];
clients = findChildren<KIPCClient*>();
i = 0;
}
}
return hasSent;
}
QStringList KIPCServer::getInstances(QString id)
{
QStringList instances;
QList<KIPCClient*> clients = findChildren<KIPCClient*>();
for (int i = 0; i<clients.length(); i++) {
if (id != clients[i]->_id)
continue;
if (clients[i]->isConnected())
instances.push_back(clients[i]->_instance);
}
return instances;
}
KIPCClient::KIPCClient(QObject *parent, QLocalSocket *socket) : QObject(parent), _socket(socket), _ipcIndex(0)
{
if (!_socket) {
_socket = new QLocalSocket(this);
connect(&_updateTimer, SIGNAL(timeout()), this, SLOT(onUpdateConnection()));
}
connect(_socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
}
KIPCClient::~KIPCClient()
{}
bool KIPCClient::isConnected()
{
return (_socket->state() == QLocalSocket::ConnectedState);
}
void KIPCClient::onUpdateConnection()
{
if (_socket->state() == QLocalSocket::UnconnectedState) {
QString ipcName = QString("%1%2").arg(KLUDGET_IPC_NAME).arg(_ipcIndex);
_socket->connectToServer(ipcName);
if (_socket->waitForConnected(500))
KLog::log("ipc client connected");
else {
KLog::log("ipc client unable to connect");
_ipcIndex++;
if (_ipcIndex > MAX_IPC_INDEX)
_ipcIndex = 0;
}
}
}
void KIPCClient::onReadyRead()
{
KDocument doc;
doc.setContent(QString(_socket->readAll()));
QString id = doc.getValue("msg/id", "");
QString instance = doc.getValue("msg/instance", "");
QString msg = doc.getValue("msg", "");
KLog::log(QString("ipc read: %1").arg(msg));
if (msg == "pong") {
_id = id;
_instance = instance;
KLog::log(QString("pong id: %1").arg(id));
KLog::log(QString("pong instance: %1").arg(instance));
}
emit messageReceived(msg, id, instance);
}
bool KIPCClient::sendMessage(QString message, QString id, QString instance)
{
// KLog::log("sendMessage");
if (id.length() && _id.length()) {
if (id != _id) {
// KLog::log(QString("sendMessage %1 %2").arg(id).arg(_id));
return false;
}
}
QString msg = QString("<msg id=\"%1\" instance=\"%2\">%3</msg>").
arg(id).
arg(instance).
arg(message);
KLog::log(QString("ipc write: %1").arg(msg));
bool sent = _socket->write(msg.toUtf8());
_socket->flush();
return sent;
}
void KIPCClient::connectToServer()
{
_updateTimer.start(2000);
onUpdateConnection();
}
int KIPC::getProcessId(const QString &kludgetId)
{
int pid = 0;
QString pidfile = getPIDFile(kludgetId);
if (QFile::exists(pidfile))
{
QFile file;
file.setFileName(pidfile);
if (file.open(QIODevice::ReadOnly))
{
QString text = file.readAll();
file.close();
pid = text.toInt();
}
}
// qDebug(">>%s %d", qPrintable(kludgetId), pid);
if (!checkProcess(pid))
return 0;
return pid;
}
bool KIPC::setProcessId(const QString &kludgetId, int pid)
{
QString pidfile = getPIDFile(kludgetId);
QFile file;
file.setFileName(pidfile);
if (file.open(QIODevice::WriteOnly))
{
file.write(QString::number(pid).toUtf8());
file.write("\r\n", 2);
file.close();
return true;
}
return false;
}
QString KIPC::getPIDFile(const QString &kludgetId)
{
return KApp::temporaryDirPath() + "/" + kludgetId + ".kludget.pid";
}
void KIPC::destroyPIDFile(const QString &kludgetId)
{
QString pidfile = getPIDFile(kludgetId);
if (QFile::exists(pidfile))
QFile::remove
(pidfile);
}