tests/auto/qtcpsocket/stressTest/Test.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 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 // Qt
       
    42 #include <QByteArray>
       
    43 #include <QCoreApplication>
       
    44 #include <QDataStream>
       
    45 #include <QTimer>
       
    46 
       
    47 // Test
       
    48 #include "Test.h"
       
    49 
       
    50 #ifdef QT3_SUPPORT
       
    51 //------------------------------------------------------------------------------
       
    52 My3Socket::My3Socket(QObject *parent) 
       
    53     : Q3Socket(parent), safeShutDown(false)
       
    54 {
       
    55     connect(this, SIGNAL(readyRead()), this, SLOT(read()));
       
    56     connect(this, SIGNAL(delayedCloseFinished()), this, SLOT(closed()));
       
    57     connect(this, SIGNAL(connectionClosed()), this, SLOT(closed()));
       
    58 }
       
    59 
       
    60 //------------------------------------------------------------------------------
       
    61 void My3Socket::read()
       
    62 {
       
    63     QDataStream in(this);
       
    64 
       
    65     quint32 num, reply;
       
    66 
       
    67     while (bytesAvailable()) {
       
    68         in >> num;
       
    69         if (num == 42) {
       
    70             qDebug("SUCCESS");
       
    71             safeShutDown = true;
       
    72             QCoreApplication::instance()->quit();
       
    73             return;
       
    74         }
       
    75         reply = num + 1;
       
    76         if (reply == 42)
       
    77             ++reply;
       
    78     }
       
    79     
       
    80     // Reply with a bigger number
       
    81     sendTest(reply);
       
    82 }
       
    83 
       
    84 //------------------------------------------------------------------------------
       
    85 void My3Socket::closed()
       
    86 {
       
    87     if (!safeShutDown)
       
    88         qDebug("FAILED");
       
    89     QCoreApplication::instance()->quit();
       
    90 }
       
    91 
       
    92 //------------------------------------------------------------------------------
       
    93 void My3Socket::sendTest(quint32 num)
       
    94 {
       
    95     QByteArray  block;
       
    96     QDataStream out(&block, QIODevice::WriteOnly);
       
    97     out << num;
       
    98     writeBlock(block, block.size());
       
    99 }
       
   100 
       
   101 //------------------------------------------------------------------------------
       
   102 My3Server::My3Server(QObject *parent)
       
   103     : Q3ServerSocket(7700, 1, parent), m_socket(0)
       
   104 {
       
   105     if (ok())
       
   106         qDebug("qt3server");
       
   107 
       
   108     QTimer::singleShot(5000, this, SLOT(stopServer()));
       
   109 }
       
   110 
       
   111 //------------------------------------------------------------------------------
       
   112 void My3Server::newConnection(int socketId)
       
   113 {
       
   114     m_socket = new My3Socket(this);
       
   115     m_socket->setSocket(socketId);
       
   116 }
       
   117 
       
   118 //------------------------------------------------------------------------------
       
   119 void My3Server::stopServer()
       
   120 {
       
   121     if (m_socket) {
       
   122         qDebug("SUCCESS");
       
   123         m_socket->safeShutDown = true;
       
   124         m_socket->sendTest(42);
       
   125     } else {
       
   126         QCoreApplication::instance()->quit();
       
   127     }
       
   128 }
       
   129 #endif
       
   130 
       
   131 //------------------------------------------------------------------------------
       
   132 My4Socket::My4Socket(QObject *parent) 
       
   133     : QTcpSocket(parent), safeShutDown(false)
       
   134 {
       
   135     connect(this, SIGNAL(readyRead()), this, SLOT(read()));
       
   136     connect(this, SIGNAL(disconnected()), this, SLOT(closed()));
       
   137 }
       
   138 
       
   139 //------------------------------------------------------------------------------
       
   140 void My4Socket::read(void)
       
   141 {
       
   142     QDataStream in(this);
       
   143 
       
   144     quint32 num, reply;
       
   145 
       
   146     while (bytesAvailable()) {
       
   147         in >> num;
       
   148         if (num == 42) {
       
   149             safeShutDown = true;
       
   150             qDebug("SUCCESS");
       
   151             QCoreApplication::instance()->quit();
       
   152             return;
       
   153         }
       
   154         reply = num + 1;
       
   155         if (reply == 42)
       
   156             ++reply;
       
   157     }
       
   158     
       
   159     // Reply with a bigger number
       
   160     sendTest(reply);
       
   161 }
       
   162 
       
   163 //------------------------------------------------------------------------------
       
   164 void My4Socket::closed(void)
       
   165 {
       
   166     if (!safeShutDown)
       
   167         qDebug("FAILED");
       
   168     QCoreApplication::instance()->quit();
       
   169 }
       
   170 
       
   171 //------------------------------------------------------------------------------
       
   172 void My4Socket::sendTest(quint32 num)
       
   173 {
       
   174     QByteArray  block;
       
   175     QDataStream out(&block, QIODevice::WriteOnly);
       
   176     out << num;
       
   177 
       
   178     write(block, block.size());
       
   179 }
       
   180 
       
   181 //------------------------------------------------------------------------------
       
   182 My4Server::My4Server(QObject *parent)
       
   183     : QTcpServer(parent)
       
   184 {
       
   185     if (listen(QHostAddress::Any, 7700))
       
   186         qDebug("qt4server");
       
   187     QTimer::singleShot(5000, this, SLOT(stopServer()));
       
   188 }
       
   189 
       
   190 //------------------------------------------------------------------------------
       
   191 void My4Server::incomingConnection(int socketId)
       
   192 {
       
   193     m_socket = new My4Socket(this);
       
   194     m_socket->setSocketDescriptor(socketId);
       
   195 }
       
   196 
       
   197 //------------------------------------------------------------------------------
       
   198 void My4Server::stopServer()
       
   199 {
       
   200     if (m_socket) {
       
   201         qDebug("SUCCESS");
       
   202         m_socket->safeShutDown = true;
       
   203         m_socket->sendTest(42);
       
   204     } else {
       
   205         QCoreApplication::instance()->quit();
       
   206     }
       
   207 }
       
   208 
       
   209 //------------------------------------------------------------------------------
       
   210 Test::Test(Type type)
       
   211 {
       
   212     switch (type) {
       
   213 #ifdef QT3_SUPPORT
       
   214     case Qt3Server: {
       
   215         new My3Server(this);
       
   216         break;
       
   217     }
       
   218     case Qt3Client: {
       
   219         qDebug("qt3client");
       
   220         My3Socket *s = new My3Socket(this);
       
   221         s->connectToHost("localhost", 7700);
       
   222         s->sendTest(1);
       
   223         break;
       
   224     }
       
   225 #endif
       
   226     case Qt4Client: {
       
   227         qDebug("qt4client");
       
   228         My4Socket *s = new My4Socket(this);
       
   229         s->connectToHost("localhost", 7700);
       
   230         s->sendTest(1);
       
   231         break;
       
   232     }
       
   233     case Qt4Server: {
       
   234         new My4Server(this);
       
   235         break;
       
   236     }
       
   237     default:
       
   238         break;
       
   239     }
       
   240 }