qtmobility/tests/auto/qnetworksession/lackey/main.cpp
branchRCL_3
changeset 10 cd2778e5acfe
equal deleted inserted replaced
9:5d007b20cfd0 10:cd2778e5acfe
       
     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 Qt Mobility Components.
       
     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 #include "../../../../src/bearer/qnetworkconfigmanager.h"
       
    46 #include "../../../../src/bearer/qnetworkconfiguration.h"
       
    47 #include "../../../../src/bearer/qnetworksession.h"
       
    48 
       
    49 #include <QEventLoop>
       
    50 #include <QTimer>
       
    51 #include <QDebug>
       
    52 
       
    53 QTM_USE_NAMESPACE
       
    54 
       
    55 
       
    56 #define NO_DISCOVERED_CONFIGURATIONS_ERROR 1
       
    57 #define SESSION_OPEN_ERROR 2
       
    58 
       
    59 
       
    60 int main(int argc, char** argv)
       
    61 {
       
    62     QCoreApplication app(argc, argv);
       
    63 
       
    64     // Update configurations so that everything is up to date for this process too.
       
    65     // Event loop is used to wait for awhile.
       
    66     QNetworkConfigurationManager manager;
       
    67     manager.updateConfigurations();
       
    68     QEventLoop iIgnoreEventLoop;
       
    69     QTimer::singleShot(3000, &iIgnoreEventLoop, SLOT(quit()));
       
    70     iIgnoreEventLoop.exec();
       
    71 
       
    72     QList<QNetworkConfiguration> discovered =
       
    73         manager.allConfigurations(QNetworkConfiguration::Discovered);
       
    74 
       
    75         foreach(QNetworkConfiguration config, discovered) {
       
    76             qDebug() << "Lackey: Name of the config enumerated: " << config.name();
       
    77             qDebug() << "Lackey: State of the config enumerated: " << config.state();
       
    78         }
       
    79 
       
    80     if (discovered.isEmpty()) {
       
    81         qDebug("Lackey: no discovered configurations, returning empty error.");
       
    82         return NO_DISCOVERED_CONFIGURATIONS_ERROR;
       
    83     }
       
    84 
       
    85     // Cannot read/write to processes on WinCE or Symbian.
       
    86     // Easiest alternative is to use sockets for IPC.
       
    87     QLocalSocket oopSocket;
       
    88 
       
    89     oopSocket.connectToServer("tst_qnetworksession");
       
    90     oopSocket.waitForConnected(-1);
       
    91 
       
    92     qDebug() << "Lackey started";
       
    93 
       
    94     QNetworkSession *session = 0;
       
    95     do {
       
    96         if (session) {
       
    97             delete session;
       
    98             session = 0;
       
    99         }
       
   100 
       
   101         qDebug() << "Discovered configurations:" << discovered.count();
       
   102 
       
   103         if (discovered.isEmpty()) {
       
   104             qDebug() << "No more discovered configurations";
       
   105             break;
       
   106         }
       
   107 
       
   108         qDebug() << "Taking first configuration";
       
   109 
       
   110         QNetworkConfiguration config = discovered.takeFirst();
       
   111 
       
   112         if ((config.state() & QNetworkConfiguration::Active) == QNetworkConfiguration::Active) {
       
   113             qDebug() << config.name() << " is active, therefore skipping it (looking for configs in 'discovered' state).";
       
   114             continue;
       
   115         }
       
   116 
       
   117         qDebug() << "Creating session for" << config.name() << config.identifier();
       
   118 
       
   119         session = new QNetworkSession(config);
       
   120 
       
   121         QString output = QString("Starting session for %1\n").arg(config.identifier());
       
   122         oopSocket.write(output.toAscii());
       
   123         oopSocket.waitForBytesWritten();
       
   124         session->open();
       
   125         session->waitForOpened();
       
   126     } while (!(session && session->isOpen()));
       
   127 
       
   128     qDebug() << "lackey: loop done";
       
   129 
       
   130     if (!session) {
       
   131         qDebug() << "Could not start session";
       
   132 
       
   133         oopSocket.disconnectFromServer();
       
   134         oopSocket.waitForDisconnected(-1);
       
   135 
       
   136         return SESSION_OPEN_ERROR;
       
   137     }
       
   138 
       
   139     QString output = QString("Started session for %1\n").arg(session->configuration().identifier());
       
   140     oopSocket.write(output.toAscii());
       
   141     oopSocket.waitForBytesWritten();
       
   142 
       
   143     oopSocket.waitForReadyRead();
       
   144     oopSocket.readLine();
       
   145 
       
   146     session->stop();
       
   147 
       
   148     delete session;
       
   149 
       
   150     oopSocket.disconnectFromServer();
       
   151     oopSocket.waitForDisconnected(-1);
       
   152 
       
   153     return 0;
       
   154 }