sysstatemgmt/systemstatemgr/ssm/src/ssmswptransitionscheduler.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <ssm/ssmswppolicy.h>
       
    17 
       
    18 #include "ssmdebug.h"
       
    19 #include "ssmswptransitionengine.h"
       
    20 #include "ssmswptransitionscheduler.h"
       
    21 #include "ssmswpindividualtransitionscheduler.h"
       
    22 
       
    23 //
       
    24 //Construction/Destruction
       
    25 //
       
    26 
       
    27 
       
    28 /**
       
    29 Create a scheduler
       
    30 */
       
    31 CSsmSwpTransitionScheduler* CSsmSwpTransitionScheduler::NewL()
       
    32 	{
       
    33 	CSsmSwpTransitionScheduler* self = NewLC();
       
    34 	CleanupStack::Pop(self);
       
    35 	return self;
       
    36 	}
       
    37 
       
    38 CSsmSwpTransitionScheduler* CSsmSwpTransitionScheduler::NewLC()
       
    39 	{
       
    40 	CSsmSwpTransitionScheduler* self = new (ELeave) CSsmSwpTransitionScheduler();
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL();
       
    43 	return self;
       
    44 	}
       
    45 
       
    46 void CSsmSwpTransitionScheduler::ConstructL()
       
    47 	{
       
    48 	CTimer::ConstructL();
       
    49 	CActiveScheduler::Add(this);
       
    50 	}
       
    51 
       
    52 LOCAL_C TInt OrderingFunction(const CSsmSwpIndividualTransitionScheduler& aLhs, const CSsmSwpIndividualTransitionScheduler& aRhs)
       
    53 	{
       
    54 	return aLhs.SwpKey() - aRhs.SwpKey();
       
    55 	}
       
    56 
       
    57 LOCAL_C TInt SearchingFunction(const TUint* aUint, const CSsmSwpIndividualTransitionScheduler& aObject)
       
    58 	{
       
    59 	return *aUint - aObject.SwpKey();
       
    60 	}
       
    61 
       
    62 CSsmSwpTransitionScheduler::CSsmSwpTransitionScheduler()
       
    63 : CTimer(CActive::EPriorityStandard),
       
    64   iLinearOrder(&OrderingFunction)
       
    65 	{
       
    66 	
       
    67 	}
       
    68 
       
    69 /**
       
    70 Deactivate
       
    71 Destroy queue
       
    72 */
       
    73 CSsmSwpTransitionScheduler::~CSsmSwpTransitionScheduler()
       
    74 	{
       
    75 	CTimer::Cancel();
       
    76 	iIndividualTransitionSchedulers.ResetAndDestroy();
       
    77 	}
       
    78 
       
    79 /**
       
    80 Take ownership of the transition engine and prepare it to run
       
    81 @param aEngine - the engine to run
       
    82 */
       
    83 void CSsmSwpTransitionScheduler::SubmitL(CSsmSwpTransitionEngine* aEngine)
       
    84 	{
       
    85 	if(!aEngine)
       
    86 		{
       
    87 		SSMLOGLEAVE(KErrArgument); //lint !e527 Suppress Unreachable. Lint is just confused by macro, warning goes away when code in macro gets expanded
       
    88 		}
       
    89 	CSsmSwpIndividualTransitionScheduler* scheduler = FindScheduler(aEngine);
       
    90 	if(scheduler == NULL)
       
    91 		{
       
    92 		// No scheduler already for this SwP, need to create one
       
    93 		scheduler = CSsmSwpIndividualTransitionScheduler::NewLC(aEngine->TransitionValue().Key(), aEngine->CleSessionProxy(), *this);
       
    94 		iIndividualTransitionSchedulers.InsertInOrderL(scheduler, iLinearOrder);
       
    95 		CleanupStack::Pop(scheduler);
       
    96 		// Submit the request
       
    97 		scheduler->SubmitL(aEngine);
       
    98 		}
       
    99 	else
       
   100 		{
       
   101 		// Already have a scheduler for this session so submit it
       
   102 		scheduler->SubmitL(aEngine);
       
   103 		}
       
   104 	}
       
   105 
       
   106 void CSsmSwpTransitionScheduler::Cancel(CSession2* aSession)
       
   107 	{
       
   108 	// Iterate through the array informing all schedulers of the cancel
       
   109 	TInt count = iIndividualTransitionSchedulers.Count();
       
   110 	for(TInt i = 0; i < count; ++i)
       
   111 		{
       
   112 		iIndividualTransitionSchedulers[i]->Cancel(aSession);
       
   113 		}
       
   114 	}
       
   115 
       
   116 CSsmSwpIndividualTransitionScheduler* CSsmSwpTransitionScheduler::FindScheduler(CSsmSwpTransitionEngine* aEngine)
       
   117 	{
       
   118 	TInt offset = iIndividualTransitionSchedulers.FindInOrder(aEngine->TransitionValue().Key(), &SearchingFunction);
       
   119 	if(offset != KErrNotFound)
       
   120 		{
       
   121 		return iIndividualTransitionSchedulers[offset];
       
   122 		}
       
   123 	return NULL;
       
   124 	}
       
   125 
       
   126 /**
       
   127 Notify the monitor (if set) that one of the transition events has occured
       
   128 */
       
   129 void CSsmSwpTransitionScheduler::NotifyTransitionQueued()
       
   130 	{
       
   131 	if(iTransactionMonitor != NULL)
       
   132 		{
       
   133 		iTransactionMonitor->SwpTransitionQueued();
       
   134 		}
       
   135 	}
       
   136 
       
   137 void CSsmSwpTransitionScheduler::NotifyTransitionStarted()
       
   138 	{
       
   139 	if(iTransactionMonitor != NULL)
       
   140 		{
       
   141 		iTransactionMonitor->SwpTransitionStarted();
       
   142 		}
       
   143 	}
       
   144 
       
   145 // Time to wait after completion before starting lazy cleanup
       
   146 const TInt KLazyCleanupTimeOut = 5000000;
       
   147 
       
   148 void CSsmSwpTransitionScheduler::NotifyTransitionCompleted()
       
   149 	{
       
   150 	DEBUGPRINT1(_L("ERequestSwpChange Completed"));
       
   151 	if(iTransactionMonitor != NULL)
       
   152 		{
       
   153 		iTransactionMonitor->SwpTransitionCompleted(++iNumberOfCompletedTransitions);
       
   154 		}
       
   155 	if(!IsActive())
       
   156 		{
       
   157 		After(KLazyCleanupTimeOut);
       
   158 		}
       
   159 	}
       
   160 
       
   161 void CSsmSwpTransitionScheduler::RunL()
       
   162 	{
       
   163 	// ignore any error in iStatus as clean-up is a safe operations to perform anytime
       
   164 	
       
   165 	// iterate through schedulers, removing them if they're not doing anything
       
   166 	TInt top = iIndividualTransitionSchedulers.Count() - 1;
       
   167 	for(TInt i = top; i >= 0; --i)
       
   168 		{
       
   169 		if (iIndividualTransitionSchedulers[i]->IsIdle())
       
   170 			{
       
   171 			delete iIndividualTransitionSchedulers[i];
       
   172 			iIndividualTransitionSchedulers[i] = NULL;
       
   173 			iIndividualTransitionSchedulers.Remove(i);
       
   174 			}
       
   175 		}
       
   176 	// If there are now none left, free the memory
       
   177 	if(iIndividualTransitionSchedulers.Count() == 0)
       
   178 		{
       
   179 		iIndividualTransitionSchedulers.GranularCompress();
       
   180 		}
       
   181 	}
       
   182 
       
   183 TInt CSsmSwpTransitionScheduler::RunError(TInt __DEBUG_ONLY(aError))
       
   184 	{
       
   185 	DEBUGPRINT2A("CSsmSwpTransitionScheduler::RunError() run with error %d, ignoring", aError);
       
   186 	// Ignore any errors with the timer as it is only used for clean up 
       
   187 	return KErrNone;
       
   188 	}
       
   189 
       
   190 void CSsmSwpTransitionScheduler::NotifyTransitionFailed()
       
   191 	{
       
   192 	DEBUGPRINT1(_L("Error: ERequestSwpChange Failed"));
       
   193 	if(iTransactionMonitor != NULL)
       
   194 		{
       
   195 		iTransactionMonitor->SwpTransitionFailed(++iNumberOfFailedTransitions);
       
   196 		}
       
   197 	}
       
   198 
       
   199 /**
       
   200  * Used for testing purposes
       
   201  * Delete all the individual Transition Schedulers stored in the array
       
   202  * and free the memory
       
   203  */
       
   204 #ifdef _DEBUG
       
   205 void CSsmSwpTransitionScheduler::CleanupIndividualSchedulerArray()
       
   206 	{
       
   207 	TInt top = iIndividualTransitionSchedulers.Count() - 1;
       
   208 	for(TInt i = top; i >= 0; --i)
       
   209 		{
       
   210 		if (iIndividualTransitionSchedulers[i]->IsIdle())
       
   211 			{
       
   212 			delete iIndividualTransitionSchedulers[i];
       
   213 			iIndividualTransitionSchedulers[i] = NULL;
       
   214 			iIndividualTransitionSchedulers.Remove(i);
       
   215 			}
       
   216 		}
       
   217 	// If there are now none left, free the memory
       
   218 	if(iIndividualTransitionSchedulers.Count() == 0)
       
   219 		{
       
   220 		iIndividualTransitionSchedulers.GranularCompress();
       
   221 		}
       
   222 	}
       
   223 #endif