loadgen/src/loadgen_phonecall.cpp
branchRCL_3
changeset 22 fad26422216a
parent 0 d6fe6244b863
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "loadgen_phonecall.h"
       
    21 #include "loadgen_model.h"
       
    22 #include "loadgen.hrh"
       
    23 #include <loadgen.rsg>
       
    24 
       
    25 #include <e32math.h>
       
    26 
       
    27 _LIT(KThreadName, "PhoneCall %d");
       
    28 
       
    29 const TInt KDefaultStart = 50;
       
    30 const TInt KDefaultPeriod = 5000000;
       
    31     
       
    32 // ===================================== MEMBER FUNCTIONS =====================================
       
    33 
       
    34 CPhoneCall* CPhoneCall::NewL(TPhoneCallAttributes& aAttributes, TInt aReferenceNumber)
       
    35     {
       
    36     CPhoneCall* self = new(ELeave) CPhoneCall(aAttributes, aReferenceNumber);
       
    37     CleanupStack::PushL(self);
       
    38     self->ConstructL();
       
    39     CleanupStack::Pop(self);
       
    40     return self;    
       
    41     }
       
    42 
       
    43 // --------------------------------------------------------------------------------------------
       
    44 
       
    45 CPhoneCall::~CPhoneCall()
       
    46     {
       
    47     Close();
       
    48     }
       
    49 
       
    50 // --------------------------------------------------------------------------------------------
       
    51 
       
    52 CPhoneCall::CPhoneCall(TPhoneCallAttributes& aAttributes, TInt aReferenceNumber) : iAttributes(aAttributes)
       
    53     {
       
    54     iAttributes.iId = aReferenceNumber;
       
    55     }
       
    56 
       
    57 // --------------------------------------------------------------------------------------------
       
    58 
       
    59 void CPhoneCall::ConstructL()
       
    60     {
       
    61     CLoadBase::ConstructL();
       
    62     
       
    63     iType = ELoadGenCmdNewLoadPhoneCall;
       
    64     
       
    65     TBuf<64> threadName;
       
    66     threadName.Format(KThreadName, iAttributes.iId);
       
    67     
       
    68     // create a thread
       
    69     User::LeaveIfError(iThread.Create(threadName, ThreadFunction, KDefaultStackSize*2, KMinHeapSize, 1024*KMinHeapSize, (TAny*) &iAttributes ));
       
    70     
       
    71     // set priority of the thread
       
    72     SetPriority();
       
    73     }
       
    74 
       
    75 // --------------------------------------------------------------------------------------------
       
    76 
       
    77 TInt CPhoneCall::ThreadFunction(TAny* aThreadArg)
       
    78     {
       
    79     CTrapCleanup* pC = CTrapCleanup::New();
       
    80     CActiveScheduler* pS = new CActiveScheduler;
       
    81     CActiveScheduler::Install(pS);
       
    82 
       
    83     // start generating load, pass pointer to arguments
       
    84     GenerateLoad(*((TPhoneCallAttributes*) aThreadArg));
       
    85 
       
    86     delete pS;
       
    87     delete pC;
       
    88     
       
    89     return KErrNone;
       
    90     }
       
    91 
       
    92 // --------------------------------------------------------------------------------------------
       
    93 
       
    94 void CPhoneCall::GenerateLoad(TPhoneCallAttributes& aAttributes)
       
    95     {
       
    96     CPhoneCallManager* phoneCallManager = NULL;
       
    97     TRAPD(err, phoneCallManager = CPhoneCallManager::NewL(aAttributes));
       
    98     if (err == KErrNone) CActiveScheduler::Start();
       
    99     delete phoneCallManager;
       
   100     }
       
   101 
       
   102 // --------------------------------------------------------------------------------------------
       
   103 
       
   104 void CPhoneCall::Resume()
       
   105     {
       
   106     CLoadBase::Resume();
       
   107     
       
   108     iThread.Resume();
       
   109     }
       
   110 
       
   111 // --------------------------------------------------------------------------------------------
       
   112 
       
   113 void CPhoneCall::Suspend()
       
   114     {
       
   115     CLoadBase::Suspend();
       
   116     
       
   117     iThread.Suspend();
       
   118     }
       
   119 
       
   120 // --------------------------------------------------------------------------------------------
       
   121 
       
   122 void CPhoneCall::SetPriority()
       
   123     {
       
   124     CLoadBase::SetPriority();
       
   125     
       
   126     iThread.SetPriority(CLoadGenModel::SettingItemToThreadPriority(iAttributes.iPriority));
       
   127     }
       
   128     
       
   129 // --------------------------------------------------------------------------------------------
       
   130 
       
   131 void CPhoneCall::Close()
       
   132     {
       
   133     CLoadBase::Close();
       
   134 
       
   135     if (iThread.ExitReason() == 0) // check if the thread is still alive
       
   136         {
       
   137         // signal the thread that it needs to close
       
   138         iThread.RequestComplete(iAttributes.iDeathStatus, KErrCancel);
       
   139 
       
   140         // wait the thread to die
       
   141         TRequestStatus waiter;
       
   142         iThread.Logon(waiter);
       
   143         User::WaitForRequest(waiter);
       
   144         iThread.Close();
       
   145         }
       
   146     }
       
   147     
       
   148 // --------------------------------------------------------------------------------------------
       
   149 
       
   150 TPtrC CPhoneCall::Description()
       
   151     {
       
   152     TBuf<256> buf;
       
   153     TBuf<16> prioBuf;
       
   154     CLoadGenModel::SettingItemToThreadDescription(iAttributes.iPriority, prioBuf);
       
   155     
       
   156     _LIT(KPhoneCallEntry, "[%d] PhoneCall prio=%S dest=%S length=%dms idle=%dms random=%d%%");
       
   157     buf.Format(KPhoneCallEntry, iAttributes.iId, &prioBuf, &iAttributes.iDestination, iAttributes.iLength, iAttributes.iIdle, iAttributes.iRandomVariance);
       
   158    
       
   159     return TPtrC(buf);
       
   160     }               
       
   161 
       
   162 // --------------------------------------------------------------------------------------------
       
   163 // --------------------------------------------------------------------------------------------
       
   164 
       
   165 CPhoneCallManager* CPhoneCallManager::NewL(TPhoneCallAttributes& aAttributes)
       
   166     {
       
   167     CPhoneCallManager* self = new(ELeave) CPhoneCallManager(aAttributes);
       
   168     CleanupStack::PushL(self);
       
   169     self->ConstructL();
       
   170     CleanupStack::Pop();
       
   171     return self;
       
   172     }
       
   173 
       
   174 // --------------------------------------------------------------------------------------------
       
   175 
       
   176 CPhoneCallManager::CPhoneCallManager(TPhoneCallAttributes& aAttributes) :
       
   177     CActive(EPriorityStandard), iAttributes(aAttributes), iState(EStateIdle)
       
   178     {
       
   179     }
       
   180 
       
   181 // --------------------------------------------------------------------------------------------
       
   182     
       
   183 CPhoneCallManager::~CPhoneCallManager()
       
   184     {
       
   185     Cancel();
       
   186     
       
   187     if (iPeriodicTimer)
       
   188         {
       
   189         iPeriodicTimer->Cancel();
       
   190         delete iPeriodicTimer;
       
   191         }
       
   192         
       
   193     delete iDialer;
       
   194     }
       
   195 
       
   196 // --------------------------------------------------------------------------------------------
       
   197  
       
   198 void CPhoneCallManager::ConstructL()
       
   199     {
       
   200     CActiveScheduler::Add(this);
       
   201     
       
   202     // set the status as pending
       
   203     iStatus = KRequestPending;
       
   204     SetActive();
       
   205     
       
   206     // set the death status pointer point to the request status of this ao
       
   207     iAttributes.iDeathStatus = &iStatus;
       
   208 
       
   209     // init dialer ao
       
   210     iDialer = CDialer::NewL(*this);
       
   211     
       
   212     // start timer    
       
   213     iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard);
       
   214     iPeriodicTimer->Start(KDefaultStart, KDefaultPeriod, TCallBack(PeriodicTimerCallBack, this));
       
   215     }
       
   216 
       
   217 // --------------------------------------------------------------------------------------------
       
   218  
       
   219 void CPhoneCallManager::RunL()
       
   220     {
       
   221     // request status has completed by the main thread meaning that we need to stop now
       
   222     CActiveScheduler::Stop();
       
   223     }
       
   224 
       
   225 // --------------------------------------------------------------------------------------------
       
   226  
       
   227 void CPhoneCallManager::DoCancel()
       
   228     {
       
   229     }
       
   230     
       
   231 // --------------------------------------------------------------------------------------------
       
   232 
       
   233 TInt CPhoneCallManager::PeriodicTimerCallBack(TAny* aAny)
       
   234     {
       
   235     CPhoneCallManager* self = static_cast<CPhoneCallManager*>( aAny );
       
   236 
       
   237     self->iPeriodicTimer->Cancel();
       
   238     self->HandleCalls();
       
   239 
       
   240     return KErrNone;
       
   241     }
       
   242 
       
   243 // --------------------------------------------------------------------------------------------
       
   244 
       
   245 void CPhoneCallManager::HandleCalls()
       
   246     {
       
   247     if (iState == EStateIdle)
       
   248         {
       
   249         // make a new call
       
   250         iState = EStateCall;
       
   251         iDialer->Dial(iAttributes.iDestination);
       
   252         }
       
   253     
       
   254     else if (iState == EStateCall)
       
   255         {
       
   256         // hangup existing call
       
   257         iState = EStateIdle;
       
   258         iDialer->Hangup();
       
   259         }
       
   260     }
       
   261 
       
   262 // --------------------------------------------------------------------------------------------
       
   263 
       
   264 void CPhoneCallManager::HandleStatus(TInt /*aErr*/)
       
   265     {
       
   266     // call timer after wait
       
   267     if (iState == EStateCall)
       
   268         {
       
   269         iPeriodicTimer->Start(CLoadGenModel::MilliSecondsToMicroSeconds(iAttributes.iLength, iAttributes.iRandomVariance), KDefaultPeriod, TCallBack(PeriodicTimerCallBack, this));
       
   270         }
       
   271 
       
   272     else if (iState == EStateIdle)
       
   273         {
       
   274         iPeriodicTimer->Start(CLoadGenModel::MilliSecondsToMicroSeconds(iAttributes.iIdle, iAttributes.iRandomVariance), KDefaultPeriod, TCallBack(PeriodicTimerCallBack, this));
       
   275         }        
       
   276     }
       
   277 
       
   278 // --------------------------------------------------------------------------------------------
       
   279 // --------------------------------------------------------------------------------------------
       
   280 
       
   281 CDialer* CDialer::NewL(CPhoneCallManager& aManager)
       
   282     {
       
   283     CDialer* self = new(ELeave) CDialer(aManager);
       
   284     CleanupStack::PushL(self);
       
   285     self->ConstructL();
       
   286     CleanupStack::Pop();
       
   287     return self;
       
   288     }
       
   289 
       
   290 // --------------------------------------------------------------------------------------------
       
   291 
       
   292 CDialer::CDialer(CPhoneCallManager& aManager) :
       
   293     CActive(EPriorityStandard), iManager(aManager), iCallParamsPckg(iCallParams)
       
   294     {
       
   295     }
       
   296 
       
   297 // --------------------------------------------------------------------------------------------
       
   298     
       
   299 CDialer::~CDialer()
       
   300     {
       
   301     Cancel();
       
   302     delete iTelephony;
       
   303     }
       
   304 
       
   305 // --------------------------------------------------------------------------------------------
       
   306  
       
   307 void CDialer::ConstructL()
       
   308     {
       
   309     CActiveScheduler::Add(this);
       
   310     
       
   311     // init telephony object
       
   312     iTelephony = CTelephony::NewL();
       
   313     }
       
   314 
       
   315 // --------------------------------------------------------------------------------------------
       
   316  
       
   317 void CDialer::RunL()
       
   318     {
       
   319     // notify the observer that status has changed
       
   320     iManager.HandleStatus(iStatus.Int());
       
   321     }
       
   322     
       
   323 // --------------------------------------------------------------------------------------------
       
   324  
       
   325 void CDialer::DoCancel()
       
   326     {
       
   327     iTelephony->CancelAsync(CTelephony::EDialNewCallCancel);
       
   328     iTelephony->CancelAsync(CTelephony::EHangupCancel);
       
   329     }
       
   330 
       
   331 // --------------------------------------------------------------------------------------------
       
   332     
       
   333 void CDialer::Dial(const TDesC& aDestination)
       
   334     {
       
   335     CTelephony::TTelNumber telNumber(aDestination);
       
   336     iCallParams.iIdRestrict = CTelephony::ESendMyId;
       
   337     iTelephony->DialNewCall(iStatus, iCallParamsPckg, telNumber, iCallId);
       
   338     SetActive();
       
   339     }
       
   340 
       
   341 // --------------------------------------------------------------------------------------------
       
   342 
       
   343 void CDialer::Hangup()
       
   344     {
       
   345     iTelephony->Hangup(iStatus, iCallId);
       
   346     SetActive();
       
   347     }
       
   348 
       
   349 // --------------------------------------------------------------------------------------------
       
   350 
       
   351     
       
   352 
       
   353 // End of File