qtmobility/tests/auto/qnetworksession/lackey/main.cpp
changeset 0 cfcbf08528c4
child 3 87be51aa5b5b
equal deleted inserted replaced
-1:000000000000 0:cfcbf08528c4
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 <qnetworkconfigmanager.h>
       
    46 #include <qnetworkconfiguration.h>
       
    47 #include <qnetworksession.h>
       
    48 
       
    49 #include <QDebug>
       
    50 
       
    51 QTM_USE_NAMESPACE
       
    52 
       
    53 
       
    54 #define NO_DISCOVERED_CONFIGURATIONS_ERROR 1
       
    55 #define SESSION_OPEN_ERROR 2
       
    56 
       
    57 
       
    58 int main(int argc, char** argv)
       
    59 {
       
    60     QCoreApplication app(argc, argv);
       
    61 
       
    62     QNetworkConfigurationManager manager;
       
    63     QList<QNetworkConfiguration> discovered =
       
    64         manager.allConfigurations(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("Lackey: no discovered configurations, returning empty error.");
       
    73         return NO_DISCOVERED_CONFIGURATIONS_ERROR;
       
    74     }
       
    75 
       
    76     // Cannot read/write to processes on WinCE or Symbian.
       
    77     // Easiest alternative is to use sockets for IPC.
       
    78 
       
    79     QLocalSocket oopSocket;
       
    80 
       
    81     oopSocket.connectToServer("tst_qnetworksession");
       
    82     oopSocket.waitForConnected(-1);
       
    83 
       
    84     qDebug() << "Lackey started";
       
    85 
       
    86     QNetworkSession *session = 0;
       
    87     do {
       
    88         if (session) {
       
    89             delete session;
       
    90             session = 0;
       
    91         }
       
    92 
       
    93         qDebug() << "Discovered configurations:" << discovered.count();
       
    94 
       
    95         if (discovered.isEmpty()) {
       
    96             qDebug() << "No more discovered configurations";
       
    97             break;
       
    98         }
       
    99 
       
   100         qDebug() << "Taking first configuration";
       
   101 
       
   102         QNetworkConfiguration config = discovered.takeFirst();
       
   103 
       
   104         if ((config.state() & QNetworkConfiguration::Active) == QNetworkConfiguration::Active) {
       
   105             qDebug() << config.name() << " is active, therefore skipping it (looking for configs in 'discovered' state).";
       
   106             continue;
       
   107         }
       
   108 
       
   109         qDebug() << "Creating session for" << config.name() << config.identifier();
       
   110 
       
   111         session = new QNetworkSession(config);
       
   112 
       
   113         QString output = QString("Starting session for %1\n").arg(config.identifier());
       
   114         oopSocket.write(output.toAscii());
       
   115         oopSocket.waitForBytesWritten();
       
   116         session->open();
       
   117         session->waitForOpened();
       
   118     } while (!(session && session->isOpen()));
       
   119 
       
   120     qDebug() << "lackey: loop done";
       
   121 
       
   122     if (!session) {
       
   123         qDebug() << "Could not start session";
       
   124 
       
   125         oopSocket.disconnectFromServer();
       
   126         oopSocket.waitForDisconnected(-1);
       
   127 
       
   128         return SESSION_OPEN_ERROR;
       
   129     }
       
   130 
       
   131     QString output = QString("Started session for %1\n").arg(session->configuration().identifier());
       
   132     oopSocket.write(output.toAscii());
       
   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(-1);
       
   144 
       
   145     return 0;
       
   146 }