tests/auto/qnetworksession/lackey/main.cpp
changeset 30 5dc02b23752f
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QCoreApplication>
       
    43 #include <QStringList>
       
    44 #include <QLocalSocket>
       
    45 
       
    46 #include <QtNetwork/qnetworkconfiguration.h>
       
    47 #include <QtNetwork/qnetworkconfigmanager.h>
       
    48 #include <QtNetwork/qnetworksession.h>
       
    49 
       
    50 #include <QEventLoop>
       
    51 #include <QTimer>
       
    52 #include <QDebug>
       
    53 
       
    54 QT_USE_NAMESPACE
       
    55 
       
    56 
       
    57 #define NO_DISCOVERED_CONFIGURATIONS_ERROR 1
       
    58 #define SESSION_OPEN_ERROR 2
       
    59 
       
    60 
       
    61 int main(int argc, char** argv)
       
    62 {
       
    63     QCoreApplication app(argc, argv);
       
    64 
       
    65     // Update configurations so that everything is up to date for this process too.
       
    66     // Event loop is used to wait for awhile.
       
    67     QNetworkConfigurationManager manager;
       
    68     manager.updateConfigurations();
       
    69     QEventLoop iIgnoreEventLoop;
       
    70     QTimer::singleShot(3000, &iIgnoreEventLoop, SLOT(quit()));
       
    71     iIgnoreEventLoop.exec();
       
    72 
       
    73     QList<QNetworkConfiguration> discovered =
       
    74         manager.allConfigurations(QNetworkConfiguration::Discovered);
       
    75 
       
    76         foreach(QNetworkConfiguration config, discovered) {
       
    77             qDebug() << "Lackey: Name of the config enumerated: " << config.name();
       
    78             qDebug() << "Lackey: State of the config enumerated: " << config.state();
       
    79         }
       
    80 
       
    81     if (discovered.isEmpty()) {
       
    82         qDebug("Lackey: no discovered configurations, returning empty error.");
       
    83         return NO_DISCOVERED_CONFIGURATIONS_ERROR;
       
    84     }
       
    85 
       
    86     // Cannot read/write to processes on WinCE or Symbian.
       
    87     // Easiest alternative is to use sockets for IPC.
       
    88     QLocalSocket oopSocket;
       
    89 
       
    90     oopSocket.connectToServer("tst_qnetworksession");
       
    91     oopSocket.waitForConnected(-1);
       
    92 
       
    93     qDebug() << "Lackey started";
       
    94 
       
    95     QNetworkSession *session = 0;
       
    96     do {
       
    97         if (session) {
       
    98             delete session;
       
    99             session = 0;
       
   100         }
       
   101 
       
   102         qDebug() << "Discovered configurations:" << discovered.count();
       
   103 
       
   104         if (discovered.isEmpty()) {
       
   105             qDebug() << "No more discovered configurations";
       
   106             break;
       
   107         }
       
   108 
       
   109         qDebug() << "Taking first configuration";
       
   110 
       
   111         QNetworkConfiguration config = discovered.takeFirst();
       
   112 
       
   113         if ((config.state() & QNetworkConfiguration::Active) == QNetworkConfiguration::Active) {
       
   114             qDebug() << config.name() << "is active, therefore skipping it (looking for configs in 'discovered' state).";
       
   115             continue;
       
   116         }
       
   117 
       
   118         qDebug() << "Creating session for" << config.name() << config.identifier();
       
   119 
       
   120         session = new QNetworkSession(config);
       
   121 
       
   122         QString output = QString("Starting session for %1\n").arg(config.identifier());
       
   123         oopSocket.write(output.toAscii());
       
   124         oopSocket.waitForBytesWritten();
       
   125         session->open();
       
   126         session->waitForOpened();
       
   127     } while (!(session && session->isOpen()));
       
   128 
       
   129     qDebug() << "lackey: loop done";
       
   130 
       
   131     if (!session) {
       
   132         qDebug() << "Could not start session";
       
   133 
       
   134         oopSocket.disconnectFromServer();
       
   135         oopSocket.waitForDisconnected(-1);
       
   136 
       
   137         return SESSION_OPEN_ERROR;
       
   138     }
       
   139 
       
   140     QString output = QString("Started session for %1\n").arg(session->configuration().identifier());
       
   141     oopSocket.write(output.toAscii());
       
   142     oopSocket.waitForBytesWritten();
       
   143 
       
   144     oopSocket.waitForReadyRead();
       
   145     oopSocket.readLine();
       
   146 
       
   147     session->stop();
       
   148 
       
   149     delete session;
       
   150 
       
   151     oopSocket.disconnectFromServer();
       
   152     oopSocket.waitForDisconnected(-1);
       
   153 
       
   154     return 0;
       
   155 }