tsrc/testtools/stubsrv/src/stubsrv.cpp
branchRCL_3
changeset 92 dde4619868dc
parent 86 703a2b94c06c
child 95 55a3258355ea
equal deleted inserted replaced
86:703a2b94c06c 92:dde4619868dc
     1 /*
       
     2 * Copyright (c) 2010 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 #include "stubsrv.h"
       
    19 #include "stubsrvsession.h"
       
    20 #include "stubsrvapiexecutor.h"
       
    21 #include "apibehavior.h"
       
    22 #include "stubsrvsecuritypolicy.h"
       
    23 
       
    24 CStubSrv::CStubSrv(TInt aPriority)
       
    25   	:CPolicyServer(aPriority, KStubSrvPolicy)
       
    26     {
       
    27     }
       
    28 
       
    29 CStubSrv* CStubSrv::NewLC()
       
    30     {
       
    31     CStubSrv* self = new (ELeave) CStubSrv(EPriorityNormal);
       
    32     CleanupStack::PushL(self);
       
    33     self->StartL(KStubSrvName);
       
    34     self->ConstructL();
       
    35     return self;
       
    36     }
       
    37 
       
    38 CStubSrv::~CStubSrv()
       
    39     {
       
    40     iBehQueue.ResetAndDestroy();
       
    41     iBehQueue.Close();
       
    42     iSessions.Close();
       
    43     delete iTimer;
       
    44     }
       
    45 
       
    46 const TInt KShutdownDelay = 1000000;
       
    47 
       
    48 void CStubSrv::StartShutdownTimerIfNoSessions()
       
    49 	{
       
    50 	if (iSessions.Count() == 0 && !iTimer->IsActive())
       
    51 		{
       
    52 		iTimer->Start(KShutdownDelay, 0, TCallBack(CStubSrv::TimerFired, this));
       
    53 		}
       
    54 	}
       
    55 
       
    56 TInt CStubSrv::ClientOpened(CStubSrvSess& aSession)
       
    57 	{
       
    58 	return iSessions.Append(&aSession);
       
    59 	}
       
    60 
       
    61 void CStubSrv::ClientClosed(CStubSrvSess& aSession)
       
    62 	{
       
    63 	const TUint sessCount = iSessions.Count();
       
    64 	for ( TUint ii = 0 ; ii < sessCount ; ++ii )
       
    65 		{
       
    66 		if ( iSessions[ii] == &aSession )
       
    67 			{
       
    68 			iSessions.Remove(ii);
       
    69 			break;
       
    70 			}
       
    71 		}
       
    72 	StartShutdownTimerIfNoSessions();
       
    73 	}
       
    74 
       
    75 void CStubSrv::EnqueueL(const RMessage2& aMessage)
       
    76     {
       
    77     TApiBehaviorPckgBuf buf;
       
    78     aMessage.ReadL(0, buf);
       
    79     TInt duration = aMessage.Int1();
       
    80     
       
    81     TInt sesss = iSessions.Count();
       
    82     for (TInt i = 0; i < sesss; i++)
       
    83         {
       
    84         TInt err = iSessions[i]->ConsumeApiBehavior(buf(), duration);
       
    85         if (err == KErrNone)
       
    86             {
       
    87             return;
       
    88             }
       
    89         }
       
    90     TApiQueueItem* item = new (ELeave) TApiQueueItem();
       
    91     item->iBeh = buf();
       
    92     item->iExeDuration = duration;
       
    93     item->iTransactionId = 0;  
       
    94     CleanupStack::PushL(item);
       
    95     iBehQueue.AppendL(item);
       
    96     CleanupStack::Pop(item);
       
    97     }
       
    98 
       
    99 void CStubSrv::DeleteQueues(const RMessage2& aMessage)
       
   100     {
       
   101     TUint lib = aMessage.Int0();
       
   102     TUint api = aMessage.Int1();
       
   103     TInt count = iBehQueue.Count();
       
   104     for (TInt i = count - 1; i >= 0; i--)
       
   105         {
       
   106         if (iBehQueue[i]->iBeh.MatchId(lib, api))
       
   107             {
       
   108             delete iBehQueue[i];
       
   109             iBehQueue.Remove(i);
       
   110             }
       
   111         }
       
   112     }
       
   113 
       
   114 TInt CStubSrv::Dequeue(TApiBehavior& aBeh, TInt& aExeDuration, TInt aTransactionId)
       
   115 	{
       
   116     TInt count = iBehQueue.Count();
       
   117     for (TInt i = 0; i < count; i++)
       
   118         {
       
   119         if (iBehQueue[i]->iBeh.MatchId(aBeh.iLib, aBeh.iApi) &&
       
   120             aTransactionId == iBehQueue[i]->iTransactionId)
       
   121             {
       
   122             aBeh = iBehQueue[i]->iBeh;
       
   123             aExeDuration = iBehQueue[i]->iExeDuration;
       
   124             delete iBehQueue[i];
       
   125             iBehQueue.Remove(i);
       
   126             return KErrNone;
       
   127             }
       
   128         }
       
   129     return KErrNotFound;	
       
   130 	}
       
   131 
       
   132 void CStubSrv::GetApiCompleteCodeL(const RMessage2& aMessage)
       
   133     {
       
   134     TUint lib = aMessage.Int0();
       
   135     TUint api = aMessage.Int1();
       
   136     TInt count = iBehQueue.Count();
       
   137     for (TInt i = 0; i < count; i++)
       
   138         {
       
   139         if (iBehQueue[i]->iBeh.MatchId(lib, api))
       
   140             {
       
   141             iBehQueue[i]->iTransactionId = iTransctionUnique++;
       
   142             TPckgBuf<TInt> buf(iBehQueue[i]->iTransactionId);
       
   143             aMessage.WriteL(2, buf);
       
   144             aMessage.Complete(iBehQueue[i]->iBeh.iCompleteCode);
       
   145             return;
       
   146             }
       
   147         }
       
   148     aMessage.Complete(0);
       
   149     }
       
   150 
       
   151 void CStubSrv::ConstructL()
       
   152     {
       
   153     iTimer = CPeriodic::NewL(CActive::EPriorityStandard);
       
   154     }
       
   155 
       
   156 CSession2* CStubSrv::NewSessionL(const TVersion& aVersion, const RMessage2& /*aMessage*/) const
       
   157     {
       
   158 	TVersion v(KStubSrvMajorVersionNumber, KStubSrvMinorVersionNumber, KStubSrvBuildNumber);
       
   159 	if ( !User::QueryVersionSupported(v, aVersion) )
       
   160 		User::Leave(KErrNotSupported);
       
   161     CStubSrvSess* session = CStubSrvSess::NewL(const_cast<CStubSrv&>(*this));
       
   162     const_cast<CStubSrv*>(this)->CancelShutdownTimer();
       
   163     return session;
       
   164     }
       
   165 
       
   166 void CStubSrv::CancelShutdownTimer()
       
   167     {
       
   168     iTimer->Cancel();
       
   169     }
       
   170 
       
   171 TInt CStubSrv::TimerFired(TAny* /*aThis*/)
       
   172 	{
       
   173 	CActiveScheduler::Stop();
       
   174 	return KErrNone;
       
   175 	}
       
   176     
       
   177 static void RunServerL()
       
   178 	{
       
   179     static_cast<void>(User::LeaveIfError(User::RenameThread(KStubSrvName)));	
       
   180 	CActiveScheduler* scheduler = new(ELeave) CActiveScheduler;
       
   181 	CleanupStack::PushL(scheduler);
       
   182 	CActiveScheduler::Install(scheduler);
       
   183     CStubSrv::NewLC();
       
   184 	RProcess::Rendezvous(KErrNone);
       
   185 	CActiveScheduler::Start();
       
   186 	CleanupStack::PopAndDestroy(2, scheduler);
       
   187 	}
       
   188 
       
   189 GLDEF_C TInt E32Main()
       
   190     {
       
   191 	TInt ret = KErrNoMemory;
       
   192 	__UHEAP_MARK;
       
   193 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   194 	if (cleanup)
       
   195 		{
       
   196 		TRAP(ret, RunServerL());
       
   197 		delete cleanup;
       
   198 		}
       
   199 	__UHEAP_MARKEND;
       
   200 	return ret;    
       
   201     }