qthighway/tests/testapps/testservice/testservice.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 #include <QDebug>
       
    23 #include <xqservicerequest.h>
       
    24 #include <QTimer>
       
    25 #include <QPixmap>
       
    26 #include "testservice.h"
       
    27 #include "mytimer.h"
       
    28 
       
    29 TestService::TestService(QObject* parent ) 
       
    30  : XQServiceProvider("com.nokia.services.testservice.TestService", parent)
       
    31 {
       
    32     publishAll();
       
    33     timer1 = new QTimer(this);
       
    34     timer2 = new QTimer(this);
       
    35     mRequestIndex1 = -1;
       
    36     mRequestIndex2 = -1;
       
    37     timerKey = 0;    
       
    38 }
       
    39 
       
    40 TestService::~TestService()
       
    41 {
       
    42     delete timer1;
       
    43     delete timer2;
       
    44 }
       
    45 
       
    46 int TestService::syncNoParams()
       
    47 {
       
    48     qDebug() << "[QTH] [TestService] syncNoParams";
       
    49 
       
    50     for( int i = 0; i<20000; i++ )
       
    51         {
       
    52         int ii = i;
       
    53         }
       
    54     
       
    55     qDebug() << "[QTH] [TestService] syncNoParams end";
       
    56     return 1;
       
    57 }
       
    58 
       
    59 int TestService::syncWithParams(QString param1, int param2)
       
    60 {
       
    61     qDebug() << "[QTH] [TestService] syncWithParam ";
       
    62     qDebug() << "[QTH] [TestService] param1: " << param1;
       
    63     qDebug() << "[QTH] [TestService] param2: " << param2;
       
    64     
       
    65     for( int i = 0; i<20000; i++ )
       
    66         {
       
    67         int ii = i;
       
    68         }
       
    69     
       
    70     qDebug() << "[QTH] [TestService] syncWithParam end";
       
    71     return 1;
       
    72 }
       
    73 
       
    74 int TestService::asyncNoParams()
       
    75 {
       
    76     qDebug() << "[QTH] [TestService] asyncNoParams START";
       
    77     QString returnString("my_request");
       
    78 
       
    79     mRequestIndex1 = setCurrentRequestAsync();
       
    80     
       
    81     bool res = connect(timer1, SIGNAL(timeout()), this, SLOT(DoneAsyncNoParams()));
       
    82     qDebug() << "[QTH] [TestService] \t connect timer1->timeout() to this->DoneAsyncNoParams() = " << res ;
       
    83     timer1->start(10000);
       
    84     qDebug() << "[QTH] [TestService] asyncNoParams END";
       
    85     return 1;
       
    86 }
       
    87 
       
    88 void TestService::DoneAsyncNoParams()
       
    89     {
       
    90     qDebug() << "[QTH] [TestService] DoneasyncNoParams START";
       
    91     
       
    92     if (mRequestIndex1 == -1) {
       
    93         qDebug() << "[QTH] [TestService] \t mRequestIndex1 == -1 !!!!";
       
    94         return;
       
    95     }
       
    96     
       
    97     qDebug() << "[QTH] [TestService] \t disconnect";
       
    98     disconnect(timer1, SIGNAL(timeout()), this, SLOT(DoneAsyncNoParams()));
       
    99     qDebug() << "[QTH] [TestService] \t completeRequest(1, QVariant(1)) start";
       
   100     completeRequest(mRequestIndex1,  QVariant(1));
       
   101     qDebug() << "[QTH] [TestService] \t completeRequest(" << mRequestIndex1 << ", QVariant(1)) end";
       
   102     qDebug() << "[QTH] [TestService] DoneasyncNoParams END";
       
   103     }
       
   104 
       
   105 
       
   106 int TestService::asyncWithParams(QString param1, int param2)
       
   107 {
       
   108     timerKey++;
       
   109     qDebug() << "[QTH] [TestService] asyncWithParams START";
       
   110     qDebug() << "[QTH] [TestService] \t param1: " << param1;
       
   111     qDebug() << "[QTH] [TestService] \t param2: " << param2;
       
   112 
       
   113     qDebug() << "[QTH] [TestService] \t setCurrentRequestAsync";
       
   114     mRequestIndex2 = setCurrentRequestAsync();
       
   115     qDebug() << "[QTH] [TestService] \t currentRequestIndex = " << mRequestIndex2;
       
   116     
       
   117 
       
   118     MyTimer* mt = new MyTimer();
       
   119 
       
   120     mt->setTimerKey(timerKey);
       
   121     mt->setRequestId(mRequestIndex2);
       
   122     qDebug() << "[QTH] [TestService] \t insert a new timer having key = " << timerKey;
       
   123     timers.insert(timerKey, mt);
       
   124     qDebug() << "[QTH] [TestService] \t size of timers map = " << timers.size();
       
   125     
       
   126     qDebug() << "[QTH] [TestService] \t connect(timer2, SIGNAL(timeout()), this, SLOT(DoneAsyncWithParams()))";
       
   127     bool res = connect(mt, SIGNAL(timeout(int)), this, SLOT(DoneAsyncWithParams(int)));
       
   128     qDebug() << "[QTH] [TestService] \t connect result = " << res;
       
   129     
       
   130     mt->start(10000);
       
   131     
       
   132     QString returnString;
       
   133     returnString.append(param1);
       
   134     returnString.append(" ");
       
   135     returnString.append(param2);
       
   136     qDebug() << "[QTH] [TestService] \t returnString = " << returnString;
       
   137     qDebug() << "[QTH] [TestService] asyncWithParams END";
       
   138     return 1;
       
   139 }
       
   140 
       
   141 
       
   142 void TestService::DoneAsyncWithParams(int timerId)
       
   143 {
       
   144     qDebug() << "[QTH] [TestService] DoneAsyncWithParams START";
       
   145     qDebug() << "[QTH] [TestService] \t timerId = " << timerId;
       
   146     
       
   147     if (!timers.contains(timerId)) {
       
   148         qDebug() << "[QTH] [TestService] \t timers map does not contain timer having key = " << timerId << " !!!! ";
       
   149         qDebug() << "[QTH] [TestService] \t return !!!";
       
   150         qDebug() << "[QTH] [TestService] DoneAsyncWithParams END (1)";
       
   151         return;
       
   152     } else {
       
   153         MyTimer* mt = timers[timerId];
       
   154         disconnect(mt, SIGNAL(timeout(int)), this, SLOT(DoneAsyncWithParams(int)));
       
   155         int requestId = mt->getRequestId();
       
   156         qDebug() << "[QTH] [TestService] \t requestId = " << requestId;
       
   157         timers.remove(timerId);
       
   158         delete mt;
       
   159         mt = NULL;
       
   160         
       
   161         if (requestId == -1) {
       
   162             qDebug() << "[QTH] [TestService] \t requestId == -1 !!!!";
       
   163             return;
       
   164         }        
       
   165         qDebug() << "[QTH] [TestService] \t completeRequest -> requestId = " << requestId << ", retValue = QVariant(\"helloworld!\")";
       
   166         completeRequest(requestId,  QVariant("helloworld!"));        
       
   167         qDebug() << "[QTH] [TestService] DoneAsyncWithParams END (2)";
       
   168     }
       
   169 }
       
   170 
       
   171 // not used
       
   172 void TestService::longRunningRequest()
       
   173 {
       
   174     qDebug() << "[QTH] [TestService] longRunningRequest START";
       
   175     qDebug() << "[QTH] [TestService] \t QTimer::singleShot(30000, this, SLOT(completeLongRunningRequest()))";
       
   176     QTimer::singleShot(30000, this, SLOT(completeLongRunningRequest()));
       
   177     qDebug() << "[QTH] [TestService] longRunningRequest END";
       
   178 }
       
   179 
       
   180 void TestService::completeLongRunningRequest()
       
   181 {
       
   182     qDebug() << "[QTH] [TestService] completeLongRunningRequest END";
       
   183     completeRequest(mRequestIndex3 = setCurrentRequestAsync(), QVariant(true));
       
   184     qDebug() << "[QTH] [TestService] completeLongRunningRequest END";
       
   185 }