summaryrefslogtreecommitdiff
path: root/qt4/examples/stream-tubes/tube-initiator.cpp
blob: dd5aa4da5559d23b5a8ff47247594a370f8643bd (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
/*
 * This file is part of TelepathyQt4
 *
 * Copyright (C) 2010-2011 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright (C) 2011 Nokia Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "tube-initiator.h"

#include <TelepathyQt4/Account>
#include <TelepathyQt4/AccountFactory>
#include <TelepathyQt4/ClientRegistrar>
#include <TelepathyQt4/ContactFactory>
#include <TelepathyQt4/Connection>
#include <TelepathyQt4/ConnectionFactory>
#include <TelepathyQt4/Constants>
#include <TelepathyQt4/Contact>
#include <TelepathyQt4/ContactCapabilities>
#include <TelepathyQt4/ContactFactory>
#include <TelepathyQt4/ContactManager>
#include <TelepathyQt4/Debug>
#include <TelepathyQt4/OutgoingStreamTubeChannel>
#include <TelepathyQt4/PendingChannelRequest>
#include <TelepathyQt4/PendingContacts>
#include <TelepathyQt4/PendingOperation>
#include <TelepathyQt4/PendingReady>
#include <TelepathyQt4/Presence>
#include <TelepathyQt4/StreamTubeChannel>
#include <TelepathyQt4/StreamTubeServer>

#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>

TubeInitiator::TubeInitiator(const QString &accountName, const QString &receiver, QObject *parent)
    : QObject(parent),
      mReceiver(receiver),
      mTubeRequested(false)
{
    mServer = new QTcpServer(this);
    connect(mServer, SIGNAL(newConnection()), this, SLOT(onTcpServerNewConnection()));
    mServer->listen();

    AccountFactoryPtr accountFactory = AccountFactory::create(
            QDBusConnection::sessionBus(), Account::FeatureCore);
    // We only care about CONNECTED connections, so let's specify that in a Connection Factory
    ConnectionFactoryPtr connectionFactory = ConnectionFactory::create(
            QDBusConnection::sessionBus(), Connection::FeatureCore | Connection::FeatureConnected);
    ChannelFactoryPtr channelFactory = ChannelFactory::create(QDBusConnection::sessionBus());
    ContactFactoryPtr contactFactory = ContactFactory::create(Contact::FeatureAlias);

    mTubeServer = StreamTubeServer::create(
            QStringList() << QLatin1String("tp-qt4-stube-example"), /* one peer-to-peer service */
            QStringList(), /* no Room tube services */
            QString(), /* autogenerated Client name */
            true, /* do monitor connections */
            accountFactory, connectionFactory, channelFactory, contactFactory);

    connect(mTubeServer.data(),
            SIGNAL(newTcpConnection(QHostAddress,quint16,Tp::AccountPtr,Tp::ContactPtr,Tp::OutgoingStreamTubeChannelPtr)),
            SLOT(onTubeNewConnection(QHostAddress,quint16,Tp::AccountPtr,Tp::ContactPtr)));
    connect(mTubeServer.data(),
            SIGNAL(tcpConnectionClosed(QHostAddress,quint16,Tp::AccountPtr,Tp::ContactPtr,QString,QString,Tp::OutgoingStreamTubeChannelPtr)),
            SLOT(onTubeConnectionClosed(QHostAddress,quint16,Tp::AccountPtr,Tp::ContactPtr,QString,QString)));

    mTubeServer->exportTcpSocket(mServer);
    Q_ASSERT(mTubeServer->isRegistered());

    connect(accountFactory->proxy(
                TP_QT4_ACCOUNT_MANAGER_BUS_NAME,
                TP_QT4_ACCOUNT_OBJECT_PATH_BASE + QLatin1Char('/') + accountName,
                connectionFactory,
                mTubeServer->registrar()->channelFactory(),
                mTubeServer->registrar()->contactFactory()),
            SIGNAL(finished(Tp::PendingOperation *)),
            SLOT(onAccountReady(Tp::PendingOperation *)));
}

TubeInitiator::~TubeInitiator()
{
}

void TubeInitiator::onAccountReady(Tp::PendingOperation *op)
{
    if (op->isError()) {
        qWarning() << "Account cannot become ready - " <<
            op->errorName() << '-' << op->errorMessage();
        QCoreApplication::exit(1);
        return;
    }

    PendingReady *ready = qobject_cast<PendingReady *>(op);
    Q_ASSERT(ready != NULL);

    mAccount = AccountPtr::qObjectCast(ready->proxy());

    qDebug() << "Account ready";
    connect(mAccount.data(),
            SIGNAL(connectionChanged(Tp::ConnectionPtr)),
            SLOT(onAccountConnectionChanged(Tp::ConnectionPtr)));

    if (mAccount->connection().isNull()) {
        qDebug() << "The account given has no Connection. Please set it online to continue.";
    } else {
        onAccountConnectionChanged(mAccount->connection());
    }
}

void TubeInitiator::onAccountConnectionChanged(const ConnectionPtr &conn)
{
    if (!conn) {
        return;
    }

    // Connection::FeatureConnected being in the Connection Factory means that we shouldn't get
    // pre-Connected Connections
    Q_ASSERT(conn->isValid());
    Q_ASSERT(conn->status() == ConnectionStatusConnected);

    qDebug() << "Got a Connected Connection!";
    mConn = conn;

    qDebug() << "Creating contact object for receiver" << mReceiver;
    connect(mConn->contactManager()->contactsForIdentifiers(QStringList() << mReceiver,
                Features(Contact::FeatureCapabilities)),
            SIGNAL(finished(Tp::PendingOperation *)),
            SLOT(onContactRetrieved(Tp::PendingOperation *)));
}

void TubeInitiator::onContactRetrieved(PendingOperation *op)
{
    if (op->isError()) {
        qWarning() << "Unable to create contact object for receiver" <<
            mReceiver << "-" << op->errorName() << ": " << op->errorMessage();
        return;
    }

    PendingContacts *pc = qobject_cast<PendingContacts *>(op);
    Q_ASSERT(pc->contacts().size() == 1);
    mContact = pc->contacts().first();

    qDebug() << "Checking contact capabilities...";
    connect(mContact.data(),
            SIGNAL(capabilitiesChanged(Tp::ContactCapabilities)),
            SLOT(onContactCapabilitiesChanged()));

    if (mContact->capabilities().streamTubes(QLatin1String("tp-qt4-stube-example"))) {
        onContactCapabilitiesChanged();
    } else {
        qDebug() << "The remote contact needs to be online and have the receiver application running to continue";
    }
}

void TubeInitiator::onContactCapabilitiesChanged()
{
    if (mTubeRequested) {
        return;
    }

    if (mContact->capabilities().streamTubes(QLatin1String("tp-qt4-stube-example"))) {
        qDebug() << "The remote contact is capable of receiving tubes with service tp-qt4-stube-example now";

        mTubeRequested = true;
        connect(mAccount->createStreamTube(
                    mContact->id(),
                    QLatin1String("tp-qt4-stube-example")),
                SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(onTubeRequestFinished(Tp::PendingOperation*)));
    }
}

void TubeInitiator::onTubeRequestFinished(PendingOperation *op)
{
    if (op->isError()) {
        qWarning() << "Unable to request stream tube channel -" <<
            op->errorName() << ": " << op->errorMessage();
        return;
    }

    qDebug() << "Stream tube channel request finished successfully!";
}

void TubeInitiator::onTubeNewConnection(
        const QHostAddress &srcAddr,
        quint16 srcPort,
        const Tp::AccountPtr &account,
        const Tp::ContactPtr &contact)
{
    qDebug() << "Source port" << srcPort << "is" << contact->alias();
    mConnAliases.insert(qMakePair(srcAddr, srcPort), contact->alias());
}

void TubeInitiator::onTubeConnectionClosed(
        const QHostAddress &srcAddr,
        quint16 srcPort,
        const Tp::AccountPtr &account,
        const Tp::ContactPtr &contact,
        const QString &error,
        const QString &message)
{
    qDebug() << "Connection from source port" << srcPort << "closed with" << error << ':' << message;
    mConnAliases.remove(qMakePair(srcAddr, srcPort));
}

void TubeInitiator::onTcpServerNewConnection()
{
    qDebug() << "Pending connection found";
    QTcpSocket *socket = mServer->nextPendingConnection();
    connect(socket, SIGNAL(readyRead()), this, SLOT(onDataFromSocket()));
}

void TubeInitiator::onDataFromSocket()
{
    QAbstractSocket *source = qobject_cast<QAbstractSocket *>(sender());
    QString data = QLatin1String(source->readLine());
    data.remove(QLatin1Char('\n'));

    // Look up the alias of the remote sending us data based on the tube connection tracking. Of
    // course, this is slightly overengineered; given that this example uses one-to-one tubes (not
    // group/MUC tubes), all remote connections are always from the same contact we opened the tube
    // to in the first place.
    QPair<QHostAddress, quint16> peerAddr(source->peerAddress(), source->peerPort());
    if (mConnAliases.contains(peerAddr)) {
        qDebug() << "New data from contact" << mConnAliases.value(peerAddr) << ':' << data;
    } else {
        // We haven't found out the identity for this connection yet. This may happen, because the
        // TCP server listen socket and the D-Bus connection are on separate sockets and have no
        // mutual ordering guarantees.
        //
        // A GUI application would likely use a placeholder item and lazily replace it with a
        // representation of the contact when newTcpConnection for this source address has been
        // received. A non-interactive daemon or alike might, in turn, queue incoming data until the
        // contact is known, or just carry on if the contact's details aren't needed (yet).
        qDebug() << "New data from source port" << peerAddr.second << ':' << data;
    }

    if (data == QLatin1String("Hi there!!")) {
        source->write(QByteArray("Hey back mate.\n"));
    } else {
        source->write(QByteArray("Sorry, I have no time for you right now.\n"));
    }
}

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);

    if (argc != 3) {
        qDebug() << "usage:" << argv[0] << "<account name, as in mc-tool list> <receiver contact ID>";
        return 1;
    }

    Tp::registerTypes();

    new TubeInitiator(QLatin1String(argv[1]), QLatin1String(argv[2]), &app);

    return app.exec();
}

#include "_gen/tube-initiator.moc.hpp"