| 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" |
| 17 | |
| 18 | /*! |
| 19 | \file qinterprocesschannel.cpp |
| 20 | |
| 21 | \brief Implementation of the QInterProcessChannel class. |
| 22 | */ |
| 23 | |
| 24 | #include "qmanagedrequest.h" |
| 25 | #include "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_TIMEOUT 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 | QInterProcessChannel::QInterProcessChannel(QObject *p) |
| 58 | : QThread(p), pServer(0), pServerTimer(0) { |
| 59 | #ifdef Q_OS_WIN |
| 60 | rcFile = QDir::tempPath() + QDir::separator() + |
| 61 | QApplication::applicationName() + "rc"; |
| 62 | #else |
| 63 | rcFile = QDir::tempPath() + QDir::separator() + |
| 64 | QApplication::applicationName() + "-" + getenv("USER") + "-" + |
| 65 | getenv("DISPLAY") + "-" + "rc"; |
| 66 | #endif |
| 67 | |
| 68 | init(); |
| 69 | } |
| 70 | |
| 71 | /*! |
| 72 | \brief Destructor |
| 73 | */ |
| 74 | QInterProcessChannel::~QInterProcessChannel() { |
| 75 | while (isRunning()) quit(); |
| 76 | |
| 77 | close(); |
| 78 | } |
| 79 | |
| 80 | /*! |
| 81 | \return Wether the instance has a server role |
| 82 | */ |
| 83 | bool QInterProcessChannel::isServer() const { return pServer; } |
| 84 | |
| 85 | /*! |
| 86 | \return The current buffered message |
| 87 | */ |
| 88 | QString QInterProcessChannel::messageBuffer() const { return sMsg; } |
| 89 | |
| 90 | /*! |
| 91 | \brief Sets a buffered message |
| 92 | */ |
| 93 | void QInterProcessChannel::setMessageBuffer(const QString &s) { sMsg = s; } |
| 94 | |
| 95 | /*! |
| 96 | \overload |
| 97 | |
| 98 | Sends the content of the current message buffer |
| 99 | */ |
| 100 | void QInterProcessChannel::sendMessage() { |
| 101 | sendMessage(sMsg); |
| 102 | sMsg.clear(); |
| 103 | } |
| 104 | |
| 105 | /*! |
| 106 | \overload |
| 107 | */ |
| 108 | void QInterProcessChannel::sendMessage(const QString &s) { |
| 109 | sendMessage(s.toUtf8()); |
| 110 | } |
| 111 | /*! |
| 112 | \brief Send a message to server instance |
| 113 | */ |
| 114 | void QInterProcessChannel::sendMessage(const QByteArray &msg) { |
| 115 | if (!pServer && msg.count()) { |
| 116 | // qDebug("Sending msg : %s", msg.constData()); |
| 117 | |
| 118 | QTcpSocket *pSocket = new QTcpSocket(this); |
| 119 | pSocket->connectToHost(m_addr, m_port); |
| 120 | pSocket->waitForConnected(WAIT_TIMEOUT); |
| 121 | pSocket->write(msg); |
| 122 | pSocket->waitForBytesWritten(WAIT_TIMEOUT); |
| 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 QInterProcessChannel::close() { |
| 135 | if (pServer) { |
| 136 | pServer->close(); |
| 137 | delete pServer; |
| 138 | pServer = 0; |
| 139 | |
| 140 | QFile::remove(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 QInterProcessChannel::reconnect() { |
| 158 | // qDebug("attempting to reconnect"); |
| 159 | init(); |
| 160 | } |
| 161 | |
| 162 | /*! |
| 163 | \internal |
| 164 | */ |
| 165 | void QInterProcessChannel::run() { |
| 166 | /* |
| 167 | There we check that the server lives... |
| 168 | If connection fails notify it... |
| 169 | */ |
| 170 | |
| 171 | if (pServer) return; |
| 172 | |
| 173 | // qDebug("timer setup..."); |
| 174 | |
| 175 | pServerTimer = new QTimer; |
| 176 | pServerTimer->setInterval(100); |
| 177 | pServerTimer->setSingleShot(false); |
| 178 | |
| 179 | connect(pServerTimer, SIGNAL(timeout()), this, SLOT(check())); |
| 180 | |
| 181 | pServerTimer->start(); |
| 182 | |
| 183 | exec(); |
| 184 | } |
| 185 | |
| 186 | /*! |
| 187 | \internal |
| 188 | */ |
| 189 | void QInterProcessChannel::check() { |
| 190 | // qDebug("checking connection..."); |
| 191 | |
| 192 | QTcpSocket *pSocket = new QTcpSocket(this); |
| 193 | pSocket->connectToHost(m_addr, m_port); |
| 194 | |
| 195 | if (pSocket->error() != -1) { |
| 196 | emit connectionLost(); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | pSocket->waitForConnected(WAIT_TIMEOUT); |
| 201 | |
| 202 | if (pSocket->error() != -1) { |
| 203 | emit connectionLost(); |
| 204 | return; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /*! |
| 209 | \internal |
| 210 | */ |
| 211 | void QInterProcessChannel::init() { |
| 212 | while (isRunning()) quit(); |
| 213 | |
| 214 | if (pServerTimer) { |
| 215 | pServerTimer->stop(); |
| 216 | delete pServerTimer; |
| 217 | pServerTimer = 0; |
| 218 | } |
| 219 | |
| 220 | bool ok = true; |
| 221 | |
| 222 | m_port = 0; |
| 223 | m_addr = QHostAddress::LocalHost; |
| 224 | |
| 225 | pServer = new QTcpServer(this); |
| 226 | pServer->listen(m_addr, m_port); |
| 227 | |
| 228 | connect(pServer, SIGNAL(newConnection()), this, SLOT(connection())); |
| 229 | |
| 230 | if (QFile::exists(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 | QSettings conf(rcFile, QSettings::IniFormat); |
| 237 | |
| 238 | m_port = conf.value("port").toUInt(); |
| 239 | m_addr = QHostAddress(conf.value("address").toString()); |
| 240 | |
| 241 | QTcpSocket *pSocket = new QTcpSocket(this); |
| 242 | |
| 243 | if (!m_addr.isNull() && m_port) { |
| 244 | pSocket->connectToHost(m_addr, m_port); |
| 245 | ok = pSocket->waitForConnected(WAIT_TIMEOUT); |
| 246 | |
| 247 | if (ok) ok &= (bool)pSocket->write("--check"); |
| 248 | if (ok) ok &= (bool)pSocket->waitForBytesWritten(WAIT_TIMEOUT); |
| 249 | if (ok) ok &= (bool)pSocket->waitForReadyRead(WAIT_TIMEOUT); |
| 250 | if (ok) ok &= (pSocket->readAll() == "[ALIVE]"); |
| 251 | |
| 252 | } else { |
| 253 | ok = false; |
| 254 | } |
| 255 | |
| 256 | if (!ok) { |
| 257 | QFile::remove(rcFile); |
| 258 | } |
| 259 | |
| 260 | pSocket->disconnectFromHost(); |
| 261 | delete pSocket; |
| 262 | } |
| 263 | |
| 264 | if (!QFile::exists(rcFile)) { |
| 265 | // no server found... Create one |
| 266 | // qDebug("setting up new server..."); |
| 267 | |
| 268 | m_port = pServer->serverPort(); |
| 269 | m_addr = pServer->serverAddress(); |
| 270 | |
| 271 | QSettings conf(rcFile, QSettings::IniFormat); |
| 272 | conf.setValue("port", m_port); |
| 273 | conf.setValue("address", m_addr.toString()); |
| 274 | |
| 275 | emit gotServerRole(); |
| 276 | emit serverRoleChanged(true); |
| 277 | } else { |
| 278 | // server found we'll hook on it later on... |
| 279 | pServer->close(); |
| 280 | pServer = 0; |
| 281 | |
| 282 | emit serverRoleChanged(false); |
| 283 | } |
| 284 | |
| 285 | start(); |
| 286 | } |
| 287 | |
| 288 | /*! |
| 289 | \internal |
| 290 | */ |
| 291 | void QInterProcessChannel::message(const QString &msg, QManagedSocket *s) { |
| 292 | if (!pServer) return; |
| 293 | |
| 294 | // qDebug("message from Inter Process channel : %s", qPrintable(msg)); |
| 295 | |
| 296 | QStringList argv = QManagedRequest::splitArguments(msg); |
| 297 | const QString cmd = argv.at(0); |
| 298 | // const int argc = argv.count(); |
| 299 | |
| 300 | if (cmd == "--check") { |
| 301 | s->send("[ALIVE]"); |
| 302 | } else if (cmd == "--request") { |
| 303 | argv.removeAt(0); |
| 304 | emit request(argv); |
| 305 | } else { |
| 306 | emit message(msg); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /*! |
| 311 | \brief internal |
| 312 | */ |
| 313 | void QInterProcessChannel::connection() { |
| 314 | if (!pServer) return; |
| 315 | |
| 316 | // qDebug("incoming Inter Process connection"); |
| 317 | |
| 318 | while (pServer->hasPendingConnections()) { |
| 319 | QManagedSocket *s = |
| 320 | new QManagedSocket(pServer->nextPendingConnection(), this); |
| 321 | |
| 322 | connect(s, SIGNAL(message(QString, QManagedSocket *)), this, |
| 323 | SLOT(message(QString, QManagedSocket *))); |
| 324 | } |
| 325 | } |