qthighway/tests/auto/xqserviceipc/server/serviceipcservertest.cpp
changeset 18 1b485afba084
parent 16 19b186e43276
child 28 19321a443c34
equal deleted inserted replaced
16:19b186e43276 18:1b485afba084
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
       
    19 *
       
    20 */
       
    21 
       
    22 /*
       
    23 * Module Test cases for Service Framework IPC 
       
    24 */
       
    25 
       
    26 #include <QtCore>
       
    27 #include <QCoreApplication>
       
    28 #include <xqserviceipcobserver.h>
       
    29 #include <xqserviceipcrequest.h>
       
    30 #include <xqserviceipcserver.h>
       
    31 #include "commondefs.h"
       
    32 
       
    33 #ifdef __SYMBIAN32__
       
    34 #include <e32base.h>
       
    35 #endif // __SYMBIAN32__
       
    36 
       
    37 using namespace QtService;
       
    38 
       
    39 // CONSTANTS
       
    40 class QIPCServerTest : public QObject, public MServiceIPCObserver
       
    41 {
       
    42     Q_OBJECT
       
    43     public:
       
    44         QIPCServerTest::QIPCServerTest() 
       
    45             {
       
    46             TServiceIPCBackends backend;
       
    47 #ifdef __SYMBIAN32__
       
    48             backend = ESymbianServer;
       
    49 #else 
       
    50             backend = ELocalSocket;
       
    51 #endif //__SYMBIAN32__
       
    52             iServer = new ServiceFwIPCServer( backend, this, this );
       
    53             iServer->listen( TEST_SERVER_NAME );
       
    54             }
       
    55 
       
    56         QIPCServerTest::~QIPCServerTest()
       
    57             {
       
    58             iServer->disconnect();
       
    59             }
       
    60 
       
    61     private:
       
    62         // ===============================================================
       
    63         // From MServiceIPCObserver                  
       
    64         // ===============================================================
       
    65         bool handleRequest( ServiceIPCRequest* aRequest )
       
    66             {
       
    67             if( aRequest->getOperation() == REQUEST_1 )
       
    68                 {
       
    69                 // Simply echo the data back to the client
       
    70                 aRequest->write( aRequest->getData() );
       
    71                 aRequest->completeRequest();
       
    72                 }
       
    73             else if( aRequest->getOperation() == REQUEST_2 )
       
    74                 {
       
    75                 int length = aRequest->getData().length();
       
    76                 QString reply;
       
    77                 reply.setNum( length );
       
    78                 aRequest->write( reply.toAscii() );
       
    79                 aRequest->completeRequest();
       
    80                 }
       
    81             else if( aRequest->getOperation() == REQUEST_3 ||
       
    82                      aRequest->getOperation() == REQUEST_4 )
       
    83                 {
       
    84                 // Callback in 100ms
       
    85                 QTimer::singleShot( 100, this, SLOT( doAsyncCallback() ) );
       
    86                 iAsyncRequest = aRequest;
       
    87                 }
       
    88             else if( aRequest->getOperation() == REQUEST_5 )
       
    89                 {
       
    90                 int length = aRequest->getData().toLong();
       
    91                 
       
    92                 QByteArray data;
       
    93                 for( int i=0; i<length; ++i )
       
    94                     {
       
    95                     data.append( "a" );
       
    96                     }
       
    97                 aRequest->write( data );
       
    98                 aRequest->completeRequest();
       
    99                 }
       
   100             else if( aRequest->getOperation() == KILL_SERVER )
       
   101                 {
       
   102                 aRequest->write( "bye" );
       
   103                 aRequest->completeRequest();
       
   104                 QCoreApplication::quit();
       
   105                 }
       
   106             return true;
       
   107             }
       
   108 
       
   109         // ===============================================================
       
   110         // From MServiceIPCObserver         
       
   111         // ===============================================================
       
   112         void handleCancelRequest( ServiceIPCRequest* /*aRequest*/ )
       
   113             {
       
   114             }
       
   115         
       
   116         void handleDeleteRequest( ServiceIPCRequest* /*aRequest*/ )
       
   117         {
       
   118         }
       
   119     
       
   120     public slots:
       
   121 
       
   122         // ===============================================================
       
   123         // From MServiceIPCObserver         
       
   124         // ===============================================================
       
   125         void doAsyncCallback()
       
   126             {
       
   127             if( iAsyncRequest ) 
       
   128                 {
       
   129                 if( iAsyncRequest->getOperation() == REQUEST_3 )
       
   130                     {
       
   131                     // Simply echo the data back to the client
       
   132                     iAsyncRequest->write( iAsyncRequest->getData() );
       
   133                     iAsyncRequest->completeRequest();
       
   134                     iAsyncRequest = NULL;
       
   135                     }
       
   136                 else if ( iAsyncRequest->getOperation() == REQUEST_4 )
       
   137                     {
       
   138                     int length = iAsyncRequest->getData().length();
       
   139                     QString reply;
       
   140                     reply.setNum( length );
       
   141                     iAsyncRequest->write( reply.toAscii() );
       
   142                     iAsyncRequest->completeRequest();
       
   143                     iAsyncRequest = NULL;
       
   144                     }
       
   145                 }
       
   146             }
       
   147     private:
       
   148         ServiceFwIPCServer* iServer;
       
   149         ServiceIPCRequest*  iAsyncRequest;  // not owned
       
   150 };
       
   151 
       
   152 #ifdef __SYMBIAN32__
       
   153 LOCAL_C void StartServerL()
       
   154     {
       
   155     // Some test code 
       
   156     User::LeaveIfError(User::RenameThread(_L("testipcserver")));
       
   157     CActiveScheduler* scheduler = new(ELeave)CActiveScheduler;
       
   158     CleanupStack::PushL(scheduler);    
       
   159     CActiveScheduler::Install(scheduler);
       
   160     QIPCServerTest* server = new QIPCServerTest();
       
   161     RProcess::Rendezvous(KErrNone);
       
   162     
       
   163     RProcess proc;
       
   164     TInt err = proc.Create(_L("serviceipctest.exe"),_L(""));
       
   165     proc.Resume();
       
   166     CActiveScheduler::Start();
       
   167     CActiveScheduler::Install(NULL);
       
   168     CleanupStack::PopAndDestroy(scheduler);
       
   169     }
       
   170 #endif // __SYMBIAN32__
       
   171 
       
   172 int main(int argc, char *argv[])
       
   173 {
       
   174     // Simple test application
       
   175 #if 1 
       
   176     QCoreApplication a(argc, argv);
       
   177     QIPCServerTest* server = new QIPCServerTest();
       
   178         
       
   179     int rtn = a.exec();
       
   180     delete server;
       
   181 #else // __SYMBIAN32__
       
   182     // Symbian currently does not support >1 QT processes on windows, so we run a traditional server
       
   183     CTrapCleanup* cleanup=CTrapCleanup::New();
       
   184     TInt rtn(KErrNoMemory);
       
   185     if (cleanup)
       
   186         {
       
   187         TRAP(rtn,StartServerL());
       
   188         }
       
   189         
       
   190     delete cleanup;
       
   191 #endif // __SYMBIAN32__
       
   192 
       
   193     return rtn;
       
   194 }
       
   195 
       
   196 #include "serviceipcservertest.moc"