1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QCoreApplication>
30#include <QStringList>
31#include <QLocalSocket>
32
33#ifndef QT_NO_BEARERMANAGEMENT
34#include <QtNetwork/qnetworkconfiguration.h>
35#include <QtNetwork/qnetworkconfigmanager.h>
36#include <QtNetwork/qnetworksession.h>
37#endif
38
39#include <QEventLoop>
40#include <QTimer>
41#include <QDebug>
42
43QT_USE_NAMESPACE
44
45
46#define NO_DISCOVERED_CONFIGURATIONS_ERROR 1
47#define SESSION_OPEN_ERROR 2
48
49
50int main(int argc, char** argv)
51{
52 QCoreApplication app(argc, argv);
53
54#ifndef QT_NO_BEARERMANAGEMENT
55 // Update configurations so that everything is up to date for this process too.
56 // Event loop is used to wait for awhile.
57 QNetworkConfigurationManager manager;
58 manager.updateConfigurations();
59 QEventLoop iIgnoreEventLoop;
60 QTimer::singleShot(msec: 3000, receiver: &iIgnoreEventLoop, SLOT(quit()));
61 iIgnoreEventLoop.exec();
62
63 QList<QNetworkConfiguration> discovered =
64 manager.allConfigurations(flags: QNetworkConfiguration::Discovered);
65
66 foreach(QNetworkConfiguration config, discovered) {
67 qDebug() << "Lackey: Name of the config enumerated: " << config.name();
68 qDebug() << "Lackey: State of the config enumerated: " << config.state();
69 }
70
71 if (discovered.isEmpty()) {
72 qDebug(msg: "Lackey: no discovered configurations, returning empty error.");
73 return NO_DISCOVERED_CONFIGURATIONS_ERROR;
74 }
75
76 // Cannot read/write to processes on WinCE.
77 // Easiest alternative is to use sockets for IPC.
78 QLocalSocket oopSocket;
79
80 oopSocket.connectToServer(name: "tst_qnetworksession");
81 oopSocket.waitForConnected(msecs: -1);
82
83 qDebug() << "Lackey started";
84
85 QNetworkSession *session = 0;
86 do {
87 if (session) {
88 delete session;
89 session = 0;
90 }
91
92 qDebug() << "Discovered configurations:" << discovered.count();
93
94 if (discovered.isEmpty()) {
95 qDebug() << "No more discovered configurations";
96 break;
97 }
98
99 qDebug() << "Taking first configuration";
100
101 QNetworkConfiguration config = discovered.takeFirst();
102
103 if ((config.state() & QNetworkConfiguration::Active) == QNetworkConfiguration::Active) {
104 qDebug() << config.name() << "is active, therefore skipping it (looking for configs in 'discovered' state).";
105 continue;
106 }
107
108 qDebug() << "Creating session for" << config.name() << config.identifier();
109
110 session = new QNetworkSession(config);
111
112 QString output = QString("Starting session for %1\n").arg(a: config.identifier());
113 oopSocket.write(data: output.toLatin1());
114 oopSocket.waitForBytesWritten();
115 session->open();
116 session->waitForOpened();
117 } while (!(session && session->isOpen()));
118
119 qDebug() << "lackey: loop done";
120
121 if (!session) {
122 qDebug() << "Could not start session";
123
124 oopSocket.disconnectFromServer();
125 oopSocket.waitForDisconnected(msecs: -1);
126
127 return SESSION_OPEN_ERROR;
128 }
129
130 QByteArray output = "Started session for "
131 + session->configuration().identifier().toLatin1() + '\n';
132 oopSocket.write(data: output);
133 oopSocket.waitForBytesWritten();
134
135 oopSocket.waitForReadyRead();
136 oopSocket.readLine();
137
138 session->stop();
139
140 delete session;
141
142 oopSocket.disconnectFromServer();
143 oopSocket.waitForDisconnected(msecs: -1);
144#endif
145
146 return 0;
147}
148

source code of qtbase/tests/auto/network/bearer/qnetworksession/lackey/main.cpp