Debian Codegraph @

gui/src/qcumber/qinterprocesschannel.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2006-2008 fullmetalcoder <[email protected]>
4 **
5 ** This file is part of the Edyuk project <http://edyuk.org>
6 **
7 ** This file may be used under the terms of the GNU General Public License
8 ** version 2 as published by the Free Software Foundation and appearing in the
9 ** file GPL.txt included in the packaging of this file.
10 **
11 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 **
14 ****************************************************************************/
15
16 #include "qinterprocesschannel.h"cxx . . $ `<file>/gui/src/qcumber/qinterprocesschannel.h`/
17
18 /*!
19 \file qinterprocesschannel.cpp
20
21 \brief Implementation of the QInterProcessChannel class.
22 */
23
24 #include "qmanagedrequest.h"cxx . . $ `<file>/gui/src/qcumber/qmanagedrequest.h`/
25 #include "qmanagedsocket.h"cxx . . $ `<file>/gui/src/qcumber/qmanagedsocket.h`/
26
27 #include <QApplication>
28 #include <QDir>
29 #include <QFile>
30 #include <QSettings>
31 #include <QTcpServer>
32 #include <QTcpSocket>
33 #include <QTimer>
34
35 #define WAIT_TIMEOUTcxx . . $ `gui/src/qcumber/qinterprocesschannel.cpp:35:9`! 5000
36
37 /*!
38 \class QInterProcessChannel
39
40 \brief A generic network-based communication channel for applications.
41
42 QInterProcessChannel offers communication facilities between several
43 instances of the same application. It initializes itself so that the first
44 created instance acts as a "server" which recieve messages from the others
45 instances. This is particularly handy when creating a single-instance
46 application which, for example, uses file association through a command line
47 interface.
48
49 \see QSingleApplication
50 */
51
52 /*!
53 \brief Constructor
54
55 Check for a valid server and create one if none found.
56 */
57 QInterProcessChannelcxx . . $ QInterProcessChannel#::QInterProcessChannelcxx . . $ QInterProcessChannel#QInterProcessChannel(749de1b6ae65bddd).(QObjectcxx . . $ QObject# *p)
58 : QThreadcxx . . $ QThread#QThread(749de1b6ae65bddd).(p), pServercxx . . $ QInterProcessChannel#pServer.(0), pServerTimercxx . . $ QInterProcessChannel#pServerTimer.(0) {
59 #ifdef Q_OS_WIN
60 rcFile = QDir::tempPath() + QDir::separator() +
61 QApplication::applicationName() + "rc";
62 #else
63 rcFilecxx . . $ QInterProcessChannel#rcFile. =cxx . . $ QString#`operator=`(6a89d0b7f4d2681c). QDircxx . . $ QDir#::tempPathcxx . . $ QDir#tempPath(4365c3963150523e).() +cxx . . $ operator+(eab1ea5b6c36e9b2). QDircxx . . $ QDir#::separatorcxx . . $ QDir#separator(44eb9225326fcbf5).() +cxx . . $ operator+(fab9b4f80031f353).
64 QApplicationcxx . . $ QApplication#::applicationNamecxx . . $ QCoreApplication#applicationName(4365c3963150523e).() +cxx . . $ operator+(3d22f08efddf8107). "-" +cxx . . $ operator+(3d22f08efddf8107). getenvcxx . . $ getenv(a8b8e32347182212).("USER") +cxx . . $ operator+(3d22f08efddf8107). "-" +cxx . . $ operator+(3d22f08efddf8107).
65 getenvcxx . . $ getenv(a8b8e32347182212).("DISPLAY") +cxx . . $ operator+(3d22f08efddf8107). "-" +cxx . . $ operator+(3d22f08efddf8107). "rc";
66 #endif
67
68 initcxx . . $ QInterProcessChannel#init(49f6e7a06ebc5aa8).();
69 }
70
71 /*!
72 \brief Destructor
73 */
74 QInterProcessChannelcxx . . $ QInterProcessChannel#::~cxx . . $ QInterProcessChannel#`~QInterProcessChannel`(49f6e7a06ebc5aa8).QInterProcessChannelcxx . . $ QInterProcessChannel#() {
75 while (isRunningcxx . . $ QThread#isRunning(50ce9a9e25b4a850).()) quitcxx . . $ QThread#quit(49f6e7a06ebc5aa8).();
76
77 closecxx . . $ QInterProcessChannel#close(49f6e7a06ebc5aa8).();
78 }
79
80 /*!
81 \return Wether the instance has a server role
82 */
83 bool QInterProcessChannelcxx . . $ QInterProcessChannel#::isServercxx . . $ QInterProcessChannel#isServer(50ce9a9e25b4a850).() const { return pServercxx . . $ QInterProcessChannel#pServer.; }
84
85 /*!
86 \return The current buffered message
87 */
88 QStringcxx . . $ QString# QInterProcessChannelcxx . . $ QInterProcessChannel#::messageBuffercxx . . $ QInterProcessChannel#messageBuffer(6ef7b6c27b00b43d).() const { return sMsgcxx . . $ QString#QString(3520e46b33348bf6).; }
89
90 /*!
91 \brief Sets a buffered message
92 */
93 void QInterProcessChannelcxx . . $ QInterProcessChannel#::setMessageBuffercxx . . $ QInterProcessChannel#setMessageBuffer(3520e46b33348bf6).(const QStringcxx . . $ QString# &s) { sMsgcxx . . $ QInterProcessChannel#sMsg. =cxx . . $ QString#`operator=`(6a89d0b7f4d2681c). s; }
94
95 /*!
96 \overload
97
98 Sends the content of the current message buffer
99 */
100 void QInterProcessChannelcxx . . $ QInterProcessChannel#::sendMessagecxx . . $ QInterProcessChannel#sendMessage(49f6e7a06ebc5aa8).() {
101 sendMessagecxx . . $ QInterProcessChannel#sendMessage(3520e46b33348bf6).(sMsgcxx . . $ QInterProcessChannel#sMsg.);
102 sMsgcxx . . $ QInterProcessChannel#sMsg..clearcxx . . $ QString#clear(49f6e7a06ebc5aa8).();
103 }
104
105 /*!
106 \overload
107 */
108 void QInterProcessChannelcxx . . $ QInterProcessChannel#::sendMessagecxx . . $ QInterProcessChannel#sendMessage(3520e46b33348bf6).(const QStringcxx . . $ QString# &s) {
109 sendMessagecxx . . $ QInterProcessChannel#sendMessage(45672f3c6961a244).(s.toUtf8cxx . . $ QString#toUtf8(ef52b713090bc85a).());
110 }
111 /*!
112 \brief Send a message to server instance
113 */
114 void QInterProcessChannelcxx . . $ QInterProcessChannel#::sendMessagecxx . . $ QInterProcessChannel#sendMessage(45672f3c6961a244).(const QByteArraycxx . . $ QByteArray# &msg) {
115 if (!pServercxx . . $ QInterProcessChannel#pServer. && msg.countcxx . . $ QByteArray#count(455f465bc33b4cdf).()) {
116 // qDebug("Sending msg : %s", msg.constData());
117
118 QTcpSocketcxx . . $ QTcpSocket# *pSocket = new QTcpSocketcxx . . $ QTcpSocket#QTcpSocket(749de1b6ae65bddd).(this);
119 pSocket->connectToHostcxx . . $ QAbstractSocket#connectToHost(f20c740007efd773).(m_addrcxx . . $ QInterProcessChannel#m_addr., m_portcxx . . $ QInterProcessChannel#m_port.);
120 pSocket->waitForConnectedcxx . . $ QAbstractSocket#waitForConnected(83c267b20bac841a).(WAIT_TIMEOUTcxx . . $ `gui/src/qcumber/qinterprocesschannel.cpp:35:9`!);
121 pSocket->writecxx . . $ QIODevice#write(4ee2750a0be83987).(msg);
122 pSocket->waitForBytesWrittencxx . . $ QAbstractSocket#waitForBytesWritten(83c267b20bac841a).(WAIT_TIMEOUTcxx . . $ `gui/src/qcumber/qinterprocesschannel.cpp:35:9`!);
123
124 } else {
125 // qWarning("Empty messages are not carried out...");
126 }
127 }
128
129 /*!
130 \brief Closes the communication channel
131
132 \see reconnect()
133 */
134 void QInterProcessChannelcxx . . $ QInterProcessChannel#::closecxx . . $ QInterProcessChannel#close(49f6e7a06ebc5aa8).() {
135 if (pServercxx . . $ QInterProcessChannel#pServer.) {
136 pServercxx . . $ QInterProcessChannel#pServer.->closecxx . . $ QTcpServer#close(49f6e7a06ebc5aa8).();
137 delete pServercxx . . $ QInterProcessChannel#pServer.;
138 pServercxx . . $ QInterProcessChannel#pServer. = 0;
139
140 QFilecxx . . $ QFile#::removecxx . . $ QFile#remove(e1defb592416720d).(rcFilecxx . . $ QInterProcessChannel#rcFile.);
141 }
142 }
143
144 /*!
145 \brief Attemps to reconnect
146
147 When the server instance is closed (or crashes...) the connectionLost()
148 signal is emitted. As an alternative to closing the client instances it is
149 possible to try a reconnection so that one of theclient will become a server.
150
151 \note When several clients are running side by side this function is very
152 likely to cause a fork and create several (independant) clients whose only
153 one will be reachable by newer clients...
154
155 \see connectionLost()
156 */
157 void QInterProcessChannelcxx . . $ QInterProcessChannel#::reconnectcxx . . $ QInterProcessChannel#reconnect(49f6e7a06ebc5aa8).() {
158 // qDebug("attempting to reconnect");
159 initcxx . . $ QInterProcessChannel#init(49f6e7a06ebc5aa8).();
160 }
161
162 /*!
163 \internal
164 */
165 void QInterProcessChannelcxx . . $ QInterProcessChannel#::runcxx . . $ QInterProcessChannel#run(49f6e7a06ebc5aa8).() {
166 /*
167 There we check that the server lives...
168 If connection fails notify it...
169 */
170
171 if (pServercxx . . $ QInterProcessChannel#pServer.) return;
172
173 // qDebug("timer setup...");
174
175 pServerTimercxx . . $ QInterProcessChannel#pServerTimer. = new QTimercxx . . $ QTimer#QTimer(749de1b6ae65bddd).;
176 pServerTimercxx . . $ QInterProcessChannel#pServerTimer.->setIntervalcxx . . $ QTimer#setInterval(d4f767463ce0a6b3).(100);
177 pServerTimercxx . . $ QInterProcessChannel#pServerTimer.->setSingleShotcxx . . $ QTimer#setSingleShot(b45ddcbc8423892c).(false);
178
179 connectcxx . . $ QObject#connect(158df1d9a01a3410).(pServerTimercxx . . $ QInterProcessChannel#pServerTimer., SIGNALcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:271:10`!(timeout()), this, SLOTcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:270:10`!(check()));
180
181 pServerTimercxx . . $ QInterProcessChannel#pServerTimer.->startcxx . . $ QTimer#start(49f6e7a06ebc5aa8).();
182
183 execcxx . . $ QThread#exec(b126dc7c1de90089).();
184 }
185
186 /*!
187 \internal
188 */
189 void QInterProcessChannelcxx . . $ QInterProcessChannel#::checkcxx . . $ QInterProcessChannel#check(49f6e7a06ebc5aa8).() {
190 // qDebug("checking connection...");
191
192 QTcpSocketcxx . . $ QTcpSocket# *pSocket = new QTcpSocketcxx . . $ QTcpSocket#QTcpSocket(749de1b6ae65bddd).(this);
193 pSocket->connectToHostcxx . . $ QAbstractSocket#connectToHost(f20c740007efd773).(m_addrcxx . . $ QInterProcessChannel#m_addr., m_portcxx . . $ QInterProcessChannel#m_port.);
194
195 if (pSocket->errorcxx . . $ QAbstractSocket#error(1a787f950c1a4c0d).() != -1) {
196 emitcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:97:10`! connectionLostcxx . . $ QInterProcessChannel#connectionLost(49f6e7a06ebc5aa8).();
197 return;
198 }
199
200 pSocket->waitForConnectedcxx . . $ QAbstractSocket#waitForConnected(83c267b20bac841a).(WAIT_TIMEOUTcxx . . $ `gui/src/qcumber/qinterprocesschannel.cpp:35:9`!);
201
202 if (pSocket->errorcxx . . $ QAbstractSocket#error(1a787f950c1a4c0d).() != -1) {
203 emitcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:97:10`! connectionLostcxx . . $ QInterProcessChannel#connectionLost(49f6e7a06ebc5aa8).();
204 return;
205 }
206 }
207
208 /*!
209 \internal
210 */
211 void QInterProcessChannelcxx . . $ QInterProcessChannel#::initcxx . . $ QInterProcessChannel#init(49f6e7a06ebc5aa8).() {
212 while (isRunningcxx . . $ QThread#isRunning(50ce9a9e25b4a850).()) quitcxx . . $ QThread#quit(49f6e7a06ebc5aa8).();
213
214 if (pServerTimercxx . . $ QInterProcessChannel#pServerTimer.) {
215 pServerTimercxx . . $ QInterProcessChannel#pServerTimer.->stopcxx . . $ QTimer#stop(49f6e7a06ebc5aa8).();
216 delete pServerTimercxx . . $ QInterProcessChannel#pServerTimer.;
217 pServerTimercxx . . $ QInterProcessChannel#pServerTimer. = 0;
218 }
219
220 bool ok = true;
221
222 m_portcxx . . $ QInterProcessChannel#m_port. = 0;
223 m_addrcxx . . $ QInterProcessChannel#m_addr. =cxx . . $ QHostAddress#`operator=`(7cfafc55173442f5). QHostAddresscxx . . $ QHostAddress#::LocalHostcxx . . $ QHostAddress#LocalHost.;
224
225 pServercxx . . $ QInterProcessChannel#pServer. = new QTcpServercxx . . $ QTcpServer#QTcpServer(749de1b6ae65bddd).(this);
226 pServercxx . . $ QInterProcessChannel#pServer.->listencxx . . $ QTcpServer#listen(6fdb1d868b5163ab).(m_addrcxx . . $ QInterProcessChannel#m_addr., m_portcxx . . $ QInterProcessChannel#m_port.);
227
228 connectcxx . . $ QObject#connect(158df1d9a01a3410).(pServercxx . . $ QInterProcessChannel#pServer., SIGNALcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:271:10`!(newConnection()), this, SLOTcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:270:10`!(connection()));
229
230 if (QFilecxx . . $ QFile#::existscxx . . $ QFile#exists(e1defb592416720d).(rcFilecxx . . $ QInterProcessChannel#rcFile.)) {
231 /*
232 found a server config file, let us assume it is from a running server
233 */
234 // qDebug("checking old server...");
235
236 QSettingscxx . . $ QSettings# confcxx . . $ QSettings#QSettings(3a5f799f337fd20b).(rcFilecxx . . $ QInterProcessChannel#rcFile., QSettingscxx . . $ QSettings#::IniFormatcxx . . $ QSettings#IniFormat.);
237
238 m_portcxx . . $ QInterProcessChannel#m_port. = conf.valuecxx . . $ QSettings#value(12a1b1749da49362).("port"cxx . . $ QString#QString(85c52e162fed56f9).).toUIntcxx . . $ QVariant#toUInt(870b63f89c7fb98).();
239 m_addrcxx . . $ QInterProcessChannel#m_addr. =cxx . . $ QHostAddress#`operator=`(4aec5e4814d7fafc). QHostAddresscxx . . $ QHostAddress#QHostAddress(3520e46b33348bf6).(conf.valuecxx . . $ QSettings#value(12a1b1749da49362).("address"cxx . . $ QString#QString(85c52e162fed56f9).).toStringcxx . . $ QVariant#toString(6ef7b6c27b00b43d).());
240
241 QTcpSocketcxx . . $ QTcpSocket# *pSocket = new QTcpSocketcxx . . $ QTcpSocket#QTcpSocket(749de1b6ae65bddd).(this);
242
243 if (!m_addrcxx . . $ QInterProcessChannel#m_addr..isNullcxx . . $ QHostAddress#isNull(50ce9a9e25b4a850).() && m_portcxx . . $ QInterProcessChannel#m_port.) {
244 pSocket->connectToHostcxx . . $ QAbstractSocket#connectToHost(f20c740007efd773).(m_addrcxx . . $ QInterProcessChannel#m_addr., m_portcxx . . $ QInterProcessChannel#m_port.);
245 ok = pSocket->waitForConnectedcxx . . $ QAbstractSocket#waitForConnected(83c267b20bac841a).(WAIT_TIMEOUTcxx . . $ `gui/src/qcumber/qinterprocesschannel.cpp:35:9`!);
246
247 if (ok) ok &= (bool)pSocket->writecxx . . $ QIODevice#write(1f59e9b51d5369a2).("--check");
248 if (ok) ok &= (bool)pSocket->waitForBytesWrittencxx . . $ QAbstractSocket#waitForBytesWritten(83c267b20bac841a).(WAIT_TIMEOUTcxx . . $ `gui/src/qcumber/qinterprocesschannel.cpp:35:9`!);
249 if (ok) ok &= (bool)pSocket->waitForReadyReadcxx . . $ QAbstractSocket#waitForReadyRead(83c267b20bac841a).(WAIT_TIMEOUTcxx . . $ `gui/src/qcumber/qinterprocesschannel.cpp:35:9`!);
250 if (ok) ok &= (pSocket->readAllcxx . . $ QIODevice#readAll(b3c286cec66e09f4).() ==cxx . . $ `operator==`(d346cf23d885fffb). "[ALIVE]");
251
252 } else {
253 ok = false;
254 }
255
256 if (!ok) {
257 QFilecxx . . $ QFile#::removecxx . . $ QFile#remove(e1defb592416720d).(rcFilecxx . . $ QInterProcessChannel#rcFile.);
258 }
259
260 pSocket->disconnectFromHostcxx . . $ QAbstractSocket#disconnectFromHost(49f6e7a06ebc5aa8).();
261 delete pSocket;
262 }
263
264 if (!QFilecxx . . $ QFile#::existscxx . . $ QFile#exists(e1defb592416720d).(rcFilecxx . . $ QInterProcessChannel#rcFile.)) {
265 // no server found... Create one
266 // qDebug("setting up new server...");
267
268 m_portcxx . . $ QInterProcessChannel#m_port. = pServercxx . . $ QInterProcessChannel#pServer.->serverPortcxx . . $ QTcpServer#serverPort(72085aa11f4b6e09).();
269 m_addrcxx . . $ QInterProcessChannel#m_addr. =cxx . . $ QHostAddress#`operator=`(4aec5e4814d7fafc). pServercxx . . $ QInterProcessChannel#pServer.->serverAddresscxx . . $ QTcpServer#serverAddress(324da51e91434ec8).();
270
271 QSettingscxx . . $ QSettings# confcxx . . $ QSettings#QSettings(3a5f799f337fd20b).(rcFilecxx . . $ QInterProcessChannel#rcFile., QSettingscxx . . $ QSettings#::IniFormatcxx . . $ QSettings#IniFormat.);
272 conf.setValuecxx . . $ QSettings#setValue(1c60a90c724e6bfb).("port"cxx . . $ QString#QString(85c52e162fed56f9)., m_portcxx . . $ QVariant#QVariant(d4f767463ce0a6b3).);
273 conf.setValuecxx . . $ QSettings#setValue(1c60a90c724e6bfb).("address"cxx . . $ QString#QString(85c52e162fed56f9)., m_addrcxx . . $ QVariant#QVariant(3520e46b33348bf6)..toStringcxx . . $ QHostAddress#toString(6ef7b6c27b00b43d).());
274
275 emitcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:97:10`! gotServerRolecxx . . $ QInterProcessChannel#gotServerRole(49f6e7a06ebc5aa8).();
276 emitcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:97:10`! serverRoleChangedcxx . . $ QInterProcessChannel#serverRoleChanged(b45ddcbc8423892c).(true);
277 } else {
278 // server found we'll hook on it later on...
279 pServercxx . . $ QInterProcessChannel#pServer.->closecxx . . $ QTcpServer#close(49f6e7a06ebc5aa8).();
280 pServercxx . . $ QInterProcessChannel#pServer. = 0;
281
282 emitcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:97:10`! serverRoleChangedcxx . . $ QInterProcessChannel#serverRoleChanged(b45ddcbc8423892c).(false);
283 }
284
285 startcxx . . $ QThread#start(bd4fe741c868f69f).();
286 }
287
288 /*!
289 \internal
290 */
291 void QInterProcessChannelcxx . . $ QInterProcessChannel#::messagecxx . . $ QInterProcessChannel#message(c179580cee373a57).(const QStringcxx . . $ QString# &msg, QManagedSocketcxx . . $ QManagedSocket# *s) {
292 if (!pServercxx . . $ QInterProcessChannel#pServer.) return;
293
294 // qDebug("message from Inter Process channel : %s", qPrintable(msg));
295
296 QStringListcxx . . $ QStringList# argv = QManagedRequestcxx . . $ QManagedRequest#::splitArgumentscxx . . $ QManagedRequest#splitArguments(c46ca66a5ac9bada).(msg);
297 const QStringcxx . . $ QString# cmd = argvcxx . . $ QString#QString(3520e46b33348bf6)..atcxx . . $ QList#at(e2681f9bb6b2f357).(0);
298 // const int argc = argv.count();
299
300 if (cmd ==cxx . . $ QString#`operator==`(7d4bdb1abc417636). "--check") {
301 s->sendcxx . . $ QManagedSocket#send(45672f3c6961a244).("[ALIVE]"cxx . . $ QByteArray#QByteArray(34d26762b42c433a).);
302 } else if (cmd ==cxx . . $ QString#`operator==`(7d4bdb1abc417636). "--request") {
303 argv.removeAtcxx . . $ QList#removeAt(d4f767463ce0a6b3).(0);
304 emitcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:97:10`! requestcxx . . $ QInterProcessChannel#request(631c0101495f0328).(argv);
305 } else {
306 emitcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:97:10`! messagecxx . . $ QInterProcessChannel#message(3520e46b33348bf6).(msg);
307 }
308 }
309
310 /*!
311 \brief internal
312 */
313 void QInterProcessChannelcxx . . $ QInterProcessChannel#::connectioncxx . . $ QInterProcessChannel#connection(49f6e7a06ebc5aa8).() {
314 if (!pServercxx . . $ QInterProcessChannel#pServer.) return;
315
316 // qDebug("incoming Inter Process connection");
317
318 while (pServercxx . . $ QInterProcessChannel#pServer.->hasPendingConnectionscxx . . $ QTcpServer#hasPendingConnections(50ce9a9e25b4a850).()) {
319 QManagedSocketcxx . . $ QManagedSocket# *s =
320 new QManagedSocketcxx . . $ QManagedSocket#QManagedSocket(7c2eb71eb177cfd8).(pServercxx . . $ QInterProcessChannel#pServer.->nextPendingConnectioncxx . . $ QTcpServer#nextPendingConnection(3b0f0a12ef6ae0dd).(), this);
321
322 connectcxx . . $ QObject#connect(158df1d9a01a3410).(s, SIGNALcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:271:10`!(message(QString, QManagedSocket *)), this,
323 SLOTcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:270:10`!(message(QString, QManagedSocket *)));
324 }
325 }