summaryrefslogtreecommitdiffstats
path: root/src/libraries/qmfclient/support/qcopserver.cpp
blob: e22ce2f4ba2db6ad77763308e909b86e824fd3b5 (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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Messaging Framework.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qcopserver_p.h"
#include "qcopchannel_p.h"
#include "qcopchannel_p_p.h"
#include "qcopchannelmonitor_p.h"
#include <QtCore/qfile.h>
#include <QtCore/qdir.h>

QCopServerPrivate::QCopServerPrivate()
    : QCopLocalServer()
{
    bool ok;
#ifndef QT_NO_QCOP_LOCAL_SOCKET
    QString path = QCopThreadData::socketPath();
    ok = listen(path);
#ifdef Q_OS_UNIX
    if (!ok) {
        // There may be another qcop server running, or the path
        // was simply left in the filesystem after a server crash.
        // QLocalServer does not clean up such paths.  We try to
        // connect to the existing qcop server, and if that fails
        // we remove the path and try again.
        QLocalSocket *socket = new QLocalSocket();
        socket->connectToServer(path);
        if (!socket->waitForConnected()) {
            delete socket;
            QFile::remove(QDir::tempPath() + QChar('/') + path);
            ok = listen(path);
        } else {
            delete socket;
        }
    }
#endif
#else
    ok = listen(QHostAddress::LocalHost, QCopThreadData::listenPort());
    QString path = QString::number(QCopThreadData::listenPort());
#endif
    if (!ok)
        qWarning() << "Could not listen for qcop connections on"
                   << path << "; another qcop server may already be running.";
}

QCopServerPrivate::~QCopServerPrivate()
{
    qDeleteAll(applications);
}

#ifndef QT_NO_QCOP_LOCAL_SOCKET
void QCopServerPrivate::incomingConnection(quintptr socketDescriptor)
#else
void QCopServerPrivate::incomingConnection(int socketDescriptor)
#endif
{
    QCopLocalSocket * sock = new QCopLocalSocket;
    sock->setSocketDescriptor(socketDescriptor);

    QCopClient *client;
    client = new QCopClient(sock, sock);
    sock->setParent(client);
    client->setParent(this);
}

/* ! - documentation comments in this file are disabled:
    \class QCopServer
    \inpublicgroup QtBaseModule
    \ingroup qws
    \brief The QCopServer class provides the server-side implementation of QCopChannel.

    QCopServer is used internally by Qt Extended to implement the server-side
    counterpart to QCopChannel.

    The first QCopServer instance that is created will initialize the
    server socket, start listening for connections, and set instance().

    The QCop server will be shut down when the first QCopServer instance
    that was created by the application is destroyed.

    Only one process should create an instance of QCopServer; the process
    that has been selected to act as the QCop server.  All other processes
    should use QCopChannel to connect in client mode.

    \sa QCopChannel
*/

/* !
    Construct the QCop server and attach it to \a parent.
*/
QCopServer::QCopServer(QObject *parent)
    : QObject(parent)
{
    QCopThreadData *td = QCopThreadData::instance();
    if (!td->server) {
        d = new QCopServerPrivate();
        td->server = this;

        // Create the in-memory loopback client connection.
        if (!td->conn) {
            QCopLoopbackDevice *end1 = new QCopLoopbackDevice();
            end1->open(QIODevice::ReadWrite);
            QCopLoopbackDevice *end2 = new QCopLoopbackDevice(end1);
            end2->open(QIODevice::ReadWrite);
            QCopClient *client1 = new QCopClient(end1, true);
            QCopClient *client2 = new QCopClient(end2, false);
            end1->setParent(client1);
            end2->setParent(client2);
            client1->setParent(this);
            client2->setParent(this);
            td->conn = client2;
        }

        // Now perform the rest of the server initialization.
        d->init();
    } else {
        qWarning() << "Multiple QCopServer instances should not be created";
        d = 0;
    }
}

/* !
    Destruct the QCop server.
*/
QCopServer::~QCopServer()
{
    if (d) {
        QCopThreadData *td = QCopThreadData::instance();
        delete d;
        td->server = 0;
        td->conn = 0;
    }
}

class QCopServerSavedMessage
{
public:
    QString message;
    QByteArray data;
};

class QCopServerAppInfo
{
public:
    bool pidChannelAvailable;
    qint64 pid;
    QString pidChannel;
    QList<QCopServerSavedMessage> queue;
    QCopChannelMonitor *monitor;

    ~QCopServerAppInfo()
    {
        delete monitor;
    }
};

/* !
    Requests that an application called \a name should be activated
    because a QCop message arrived on \c{QPE/Application/<name>} and
    that application is not currently running.

    Returns the process identifier of the application if it has
    been started, or -1 if the application could not be started.
    The default implementation returns -1.

    Messages will be queued up and forwarded to the application-specific
    channel \c{QPE/Pid/<pid>} once that channel becomes available
    in the system.  If the application could not be started, then any
    queued messages will be discarded.

    \sa applicationExited()
*/
qint64 QCopServer::activateApplication(const QString& name)
{
    Q_UNUSED(name);
    return -1;
}

/* !
    Notifies the QCop server that an application with process ID \a pid
    that was previously started in response to a call to
    activateApplication() has exited or crashed.

    The next time a QCop message arrives on \c{QPE/Application/<name>},
    activateApplication() will be called to start the application again.

    \sa activateApplication()
*/
void QCopServer::applicationExited(qint64 pid)
{
    Q_UNUSED(pid);
    // TODO
}

    void startupComplete();
void QCopClient::handleAck(const QString& ch)
{
    QCopThreadData *td = QCopThreadData::instance();
    QMap<QString, QCopServerAppInfo *>::ConstIterator it;
    it = td->server->d->pidChannels.find(ch);
    if (it != td->server->d->pidChannels.constEnd()) {
        QCopServerAppInfo *info = it.value();
        if (!info->queue.isEmpty())
            info->queue.removeFirst();
    }
}

void QCopServerPrivate::init()
{
    QCopChannel *appChannel =
        new QCopChannel(QLatin1String("QPE/Application/*"), this);
    connect(appChannel, SIGNAL(connected()), QCopThreadData::instance()->server, SIGNAL(ready()));
    connect(appChannel, SIGNAL(forwarded(QString,QByteArray,QString)),
            this, SLOT(forwarded(QString,QByteArray,QString)));
}

// Handle messages that were forwarded on QPE/Application/* channels.
void QCopServerPrivate::forwarded
        (const QString& msg, const QByteArray &data, const QString& channel)
{
    QCopThreadData *td = QCopThreadData::instance();
    QCopServerAppInfo *info;

    // Do we already know about this application?
    QString appName = channel.mid(16);
    QMap<QString, QCopServerAppInfo *>::ConstIterator it;
    it = applications.find(appName);
    if (it != applications.constEnd()) {
        info = it.value();
    } else {
        // We haven't seen this application before, so try to start it.
        qint64 pid = td->server->activateApplication(appName);
        if (pid == -1)
            return;
        info = new QCopServerAppInfo();
        info->pidChannelAvailable = false;
        info->pid = pid;
        info->pidChannel = QLatin1String("QPE/Pid/") + QString::number(pid);
        info->monitor = new QCopChannelMonitor(info->pidChannel);
        connect(info->monitor, SIGNAL(registered()), this, SLOT(registered()));
        connect(info->monitor, SIGNAL(unregistered()), this, SLOT(unregistered()));
        applications.insert(appName, info);
        pidChannels.insert(info->pidChannel, info);
    }

    // Add the message to the application's saved message queue.
    QCopServerSavedMessage saved;
    saved.message = msg;
    saved.data = data;
    info->queue.append(saved);

    // If the application is already running, then pass it on.
    if (info->pidChannelAvailable) {
        // XXX - not right, should use answer()
        td->clientConnection()->send
            (info->pidChannel, msg, data, QCopCmd_SendRequestAck);
    }
}

void QCopServerPrivate::registered()
{
    QCopChannelMonitor *monitor = qobject_cast<QCopChannelMonitor *>(sender());
    if (monitor) {
        QMap<QString, QCopServerAppInfo *>::ConstIterator it;
        it = pidChannels.find(monitor->channel());
        if (it != pidChannels.constEnd()) {
            QCopServerAppInfo *info = it.value();
            if (!info->pidChannelAvailable)
                applicationRegistered(info);
        }
    }
}

void QCopServerPrivate::unregistered()
{
    QCopChannelMonitor *monitor = qobject_cast<QCopChannelMonitor *>(sender());
    if (monitor) {
        QMap<QString, QCopServerAppInfo *>::ConstIterator it;
        it = pidChannels.find(monitor->channel());
        if (it != pidChannels.constEnd()) {
            QCopServerAppInfo *info = it.value();
            if (info->pidChannelAvailable)
                applicationUnregistered(info);
        }
    }
}

void QCopServerPrivate::applicationRegistered(QCopServerAppInfo *info)
{
    Q_UNUSED(info);
    // TODO
}

void QCopServerPrivate::applicationUnregistered(QCopServerAppInfo *info)
{
    Q_UNUSED(info);
    // TODO
}