networkcontrol/commsuserpromptmgr/state/src/netupsstatemachine.cpp
changeset 0 af10295192d8
child 37 052078dda061
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     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 // This file provides the implementation of the NetUps Statemachine.
       
    15 // @internalAll
       
    16 // @prototype
       
    17 // 
       
    18 //
       
    19 
       
    20 #include "e32def.h"			//defines __ASSERT_DEBUG
       
    21 #include "e32cmn.h"
       
    22 
       
    23 #include "netupsassert.h"
       
    24 
       
    25 #include "netupsstatemachine.h"
       
    26 #include "netupsstate.h"
       
    27 #include "netupsstatedef.h"
       
    28 
       
    29 #include "netupsserviceid.h"
       
    30 
       
    31 #include "netupsimpl.h"
       
    32 
       
    33 #include <comms-infras/commsdebugutility.h> 		// defines the comms debug logging utility
       
    34 
       
    35 namespace NetUps
       
    36 {
       
    37 __FLOG_STMT(_LIT8(KNetUpsSubsys, 	"esock");)   
       
    38 __FLOG_STMT(_LIT8(KNetUpsComponent, "NetUps");) /*esockloader*/
       
    39 
       
    40 CUpsStateMachine* CUpsStateMachine::NewL(CNetUpsImpl& aNetUpsImpl, TInt32 aServiceId)
       
    41 	{
       
    42 	__FLOG_STATIC1(KNetUpsSubsys, KNetUpsComponent, _L("CUpsStateMachine::NewL(), aServiceId = %d"), aServiceId);		
       
    43 
       
    44 	CUpsStateMachine* self = NULL;
       
    45 	if (aServiceId != EIpServiceId)
       
    46 		{
       
    47 		__FLOG_STATIC0(KNetUpsSubsys, KNetUpsComponent, _L("CUpsStateMachine::NewL() Service id not supported"));
       
    48 		User::Leave(KErrNotSupported);
       
    49 		}
       
    50 	else
       
    51 		{
       
    52 		self = new (ELeave) CUpsStateMachine(aNetUpsImpl, aServiceId);
       
    53 
       
    54 		CleanupStack::PushL(self);
       
    55 		self->ConstructL();
       
    56 		CleanupStack::Pop(self);
       
    57 		}
       
    58 		
       
    59 	return self;
       
    60 	}
       
    61 
       
    62 CUpsStateMachine::CUpsStateMachine(CNetUpsImpl& aNetUpsImpl, TInt32 aServiceId) : iNetUpsImpl(aNetUpsImpl), iServiceId(aServiceId) 
       
    63 	{
       
    64 	__FLOG_2(_L("CUpsStateMachine %08x:\t CUpsStateMachine()"), this, aServiceId);			
       
    65 	}
       
    66 
       
    67 void CUpsStateMachine::ConstructL()
       
    68 	{
       
    69 	__FLOG_2(_L("CUpsStateMachine %08x:\t ConstructL()"), this, iNetUpsImpl.LifeTimeMode());		
       
    70 
       
    71 	// Note the order in which the states are instantiated must match the 
       
    72 	// order in which they are defined in the enumeration TNetUpsState - or a panic will occur.
       
    73 
       
    74 	iState.Append(CState::NewL(ENull, *this));	
       
    75 	switch(iNetUpsImpl.LifeTimeMode())
       
    76 		{
       
    77 		case CNetUpsImpl::EProcessLifeTimeMode:
       
    78 			{
       
    79 			iState.Append(CState::NewL(EProcLife_NonSession, *this));			
       
    80 			iState.Append(CState::NewL(EProcLife_Transit_SessionYes, *this)); // a transient state is entered when the UPS Server responds with either SessionYes or SessionNo and there are 1 or more UPS requests outstanding to other subsessions which are associated with the same process.			
       
    81 			iState.Append(CState::NewL(EProcLife_SessionYes, *this));
       
    82 			iState.Append(CState::NewL(EProcLife_Transit_SessionNo, *this));
       
    83 			iState.Append(CState::NewL(EProcLife_SessionNo, *this));
       
    84 			break;
       
    85 			}
       
    86 		case CNetUpsImpl::ENetworkLifeTimeMode:
       
    87 			{
       
    88 			iState.Append(CState::NewL(ENetLife_NonSession, *this));			
       
    89 			iState.Append(CState::NewL(ENetLife_SessionNo_Transit_WithoutConnections, *this));			
       
    90 			iState.Append(CState::NewL(ENetLife_SessionNo_WithOutConnections, *this));
       
    91 			iState.Append(CState::NewL(ENetLife_SessionNo_Transit_WithConnections, *this));
       
    92 			iState.Append(CState::NewL(ENetLife_SessionNo_WithConnections, *this));
       
    93 			iState.Append(CState::NewL(ENetLife_Transit_SessionYes, *this));
       
    94 			iState.Append(CState::NewL(ENetLife_SessionYes, *this));
       
    95 			break;				
       
    96 			}
       
    97 		default:
       
    98 			{
       
    99 			User::Leave(KErrNotSupported);			
       
   100 			}
       
   101 		}
       
   102 
       
   103 	__FLOG_OPEN(KNetUpsSubsys, KNetUpsComponent);
       
   104 	__FLOG_1(_L("CUpsStateMachine %08x:\t ConstructL()"), this);				
       
   105 	}
       
   106 
       
   107 TUint32 CUpsStateMachine::GetIndex(TNetUpsState aNetUpsState)
       
   108 	{		
       
   109 	__FLOG_2(_L("CUpsStateMachine %08x:\t GetIndex() aNetUpsState = %d"), this, aNetUpsState );		
       
   110 
       
   111 	TUint32 index = 0;
       
   112 	if (aNetUpsState != ENull)
       
   113 		{
       
   114 		switch(iNetUpsImpl.LifeTimeMode())
       
   115 			{
       
   116 			case CNetUpsImpl::EProcessLifeTimeMode:
       
   117 				index = aNetUpsState  - EProcLife_NonSession + 1;
       
   118 				//__FLOG_3(_L("CUpsStateMachine %08x:\t GetIndex() aNetUpsState = %d, index = %d, mode = iNetUpsImpl.LifeTimeMode()"), this, index, iNetUpsImpl.LifeTimeMode() );		
       
   119 				break;
       
   120 			case CNetUpsImpl::ENetworkLifeTimeMode:
       
   121 				index = aNetUpsState -  ENetLife_NonSession + 1;
       
   122 				//__FLOG_3(_L("CUpsStateMachine %08x:\t GetIndex() aNetUpsState = %d, index = %d, mode = iNetUpsImpl.LifeTimeMode()"), this, index, iNetUpsImpl.LifeTimeMode() );		
       
   123 
       
   124 				break;
       
   125 			default:				
       
   126 				__FLOG_2(_L("CUpsStateMachine %08x:\t GetIndex() aNetUpsState = %d, mode = iNetUpsImpl.LifeTimeMode()"), this, iNetUpsImpl.LifeTimeMode() );		
       
   127 				User::Panic(KNetUpsPanic, KPanicSessionTypeOutOfRange);
       
   128 				break;
       
   129 			}
       
   130 		}
       
   131 	return index;	
       
   132 	}
       
   133 
       
   134 
       
   135 CUpsStateMachine::~CUpsStateMachine()
       
   136 	{
       
   137 	__FLOG_1(_L("CUpsStateMachine %08x:\t ~CUpsStateMachine()"), this);		
       
   138 
       
   139 	for (TInt i = iState.Count() - 1; i >= 0; i--)
       
   140 			{
       
   141 			delete iState.operator[](i);
       
   142 			__FLOG_2(_L("CUpsStateMachine %08x:\t ~CUpsStateMachine(), iState[i] = %08x"), this, iState[i]);		
       
   143 			iState.Remove(i);
       
   144 			}
       
   145 	iState.Reset();
       
   146 	iState.Close();		
       
   147 
       
   148 	__FLOG_CLOSE;	
       
   149 	}
       
   150 
       
   151 TUint32 CUpsStateMachine::GetIndex(CProcessEntry& aProcessEntry)
       
   152 {
       
   153 	TNetUpsState netUpsState = CUpsStateMachine::GetState(aProcessEntry);
       
   154 	TInt32 index = static_cast<TInt32>(GetIndex(netUpsState));
       
   155 
       
   156 	__FLOG_3(_L("CUpsStateMachine %08x:\t GetIndex(), index = %d, netUpsState = %d"),
       
   157 			 this, index, netUpsState); 
       
   158 		
       
   159 	__ASSERT_DEBUG((index < iState.Count()), User::Panic(KNetUpsPanic, KPanicStateMachineIndexOutOfRange));
       
   160 	__ASSERT_DEBUG((iState.operator[](index)->State() == netUpsState), User::Panic(KNetUpsPanic, KPanicInvalidStateMachineIndex));
       
   161 
       
   162 	return index;
       
   163 }
       
   164 
       
   165 void CUpsStateMachine::ProcessPolicyCheckRequestL(TThreadKey& aThreadKey, const TPolicyCheckRequestData& aPolicyCheckRequestData, TRequestId aRequestId)
       
   166 	{
       
   167 	__FLOG_8(_L("CUpsStateMachine %08x:\t ProcessPolicyCheckRequestL(), processId = %d, threadId = %d, serviceId  = %d, platSecCheckResult  = %d, commsId  = %d, originator  = %08x, request id = %d"),
       
   168 				 this, aPolicyCheckRequestData.iProcessId.Id(),
       
   169 				 aPolicyCheckRequestData.iThreadId.Id(),		aPolicyCheckRequestData.iServiceId,
       
   170 				 aPolicyCheckRequestData.iPlatSecCheckResult, 	&aPolicyCheckRequestData.iCommsId.Node(),
       
   171 				 &(aPolicyCheckRequestData.iPolicyCheckRequestOriginator), aRequestId); 
       
   172 
       
   173 	TUint32 index = GetIndex(aThreadKey.iProcessEntry);
       
   174 	(iState.operator[](index))->ProcessPolicyCheckRequestL(aThreadKey, aPolicyCheckRequestData, aRequestId);
       
   175 	}
       
   176 
       
   177 void CUpsStateMachine::ProcessPolicyCheckRequestL(TProcessKey& aProcessKey, const TPolicyCheckRequestData& aPolicyCheckRequestData, TRequestId aRequestId)
       
   178 	{
       
   179 	__FLOG_8(_L("CUpsStateMachine %08x:\t ProcessPolicyCheckRequestL(), processId = %d, threadId = %d, serviceId  = %d, platSecCheckResult  = %d, commsId  = %08x, originator  = %08x, request id = %d"),
       
   180 				 this, aPolicyCheckRequestData.iProcessId.Id(),
       
   181 				 aPolicyCheckRequestData.iThreadId.Id(),		aPolicyCheckRequestData.iServiceId,
       
   182 				 aPolicyCheckRequestData.iPlatSecCheckResult, 	&aPolicyCheckRequestData.iCommsId.Node(),
       
   183 				 &(aPolicyCheckRequestData.iPolicyCheckRequestOriginator), aRequestId); 
       
   184 
       
   185 	// Suspect that this method is not required, when threadEntry is not known, we are in session lifetime.
       
   186 	(void) aProcessKey;
       
   187 	(void) aPolicyCheckRequestData;
       
   188 	(void) aRequestId;
       
   189 	User::Panic(KNetUpsPanic, KPanicMethodNotSupported);
       
   190 	}
       
   191 
       
   192 void CUpsStateMachine::IncrementConnectionCountL(TCommsIdKey& aCommsIdKey)
       
   193 	{
       
   194 	__FLOG_4(_L("CUpsStateMachine %08x:\t IncrementConnectionCountL(), processId = %d, threadId = %d, commsId = %d"),
       
   195 				 this, aCommsIdKey.iProcessEntry.ProcessId().Id(),
       
   196 				 aCommsIdKey.iThreadEntry.ThreadId().Id(),
       
   197 				 &aCommsIdKey.iCommsId.Node() ); 
       
   198 
       
   199 	(void) aCommsIdKey;
       
   200 	
       
   201 	// Suspect that this method is not required, connection counts are incremented without the request going via the state machine.
       
   202 
       
   203 	/*
       
   204 	TUint32 index = GetIndex(aCommsIdKey.iProcessEntry);
       
   205 	(iState.operator[](index))->IncrementConnectionCountL(aCommsIdKey);
       
   206 	*/
       
   207 	User::Panic(KNetUpsPanic, KPanicMethodNotSupported);
       
   208 
       
   209 	}
       
   210 
       
   211 void CUpsStateMachine::DecrementConnectionCountL(TCommsIdKey& aCommsIdKey)
       
   212 	{
       
   213 	__FLOG_4(_L("CUpsStateMachine %08x:\t DecrementConnectionCountL(), processId = %d, threadId = %d, commsId = %d"),
       
   214 				 this, aCommsIdKey.iProcessEntry.ProcessId().Id(),
       
   215 				 aCommsIdKey.iThreadEntry.ThreadId().Id(),
       
   216 				 &aCommsIdKey.iCommsId.Node() ); 
       
   217 
       
   218 	TUint32 index = GetIndex(aCommsIdKey.iProcessEntry);
       
   219 	(iState.operator[](index))->DecrementConnectionCountL(aCommsIdKey);
       
   220 	}
       
   221 		
       
   222 void CUpsStateMachine::HandleUPSRequestCompletionL(TCommsIdKey& aCommsIdKey, MPolicyCheckRequestOriginator& aPolicyCheckRequestOriginator , TNetUpsDecision aNetUpsDecision)
       
   223 	{
       
   224 	__FLOG_5(_L("CUpsStateMachine %08x:\t HandleUPSRequestCompletionL(), processId = %d, threadId = %d, commsId = %d, netUpsDecision = %d"),
       
   225 				 this, aCommsIdKey.iProcessEntry.ProcessId().Id(),
       
   226 				 aCommsIdKey.iThreadEntry.ThreadId().Id(),
       
   227 				 &aCommsIdKey.iCommsId.Node(),
       
   228 				 aNetUpsDecision ); 
       
   229 
       
   230 	TUint32 index = GetIndex(aCommsIdKey.iProcessEntry);
       
   231 	(iState.operator[](index))->HandleUPSRequestCompletionL(aCommsIdKey, aPolicyCheckRequestOriginator, aNetUpsDecision);
       
   232 	}
       
   233 
       
   234 void CUpsStateMachine::HandleUPSErrorOnCompletionL(TCommsIdKey& aCommsIdKey, MPolicyCheckRequestOriginator& aPolicyCheckRequestOriginator, TInt aError)
       
   235 	{
       
   236 	__FLOG_5(_L("CUpsStateMachine %08x:\t HandleUPSErrorOnCompletionL(), processId = %d, threadId = %d, commsId = %d, aError = %d"),
       
   237 				 this, aCommsIdKey.iProcessEntry.ProcessId().Id(),
       
   238 				 aCommsIdKey.iThreadEntry.ThreadId().Id(),
       
   239 				 &aCommsIdKey.iCommsId.Node(),
       
   240 				 aError ); 
       
   241 
       
   242 	TUint32 index = GetIndex(aCommsIdKey.iProcessEntry);
       
   243 	(iState.operator[](index))->HandleUPSErrorOnCompletionL(aCommsIdKey, aPolicyCheckRequestOriginator, aError);
       
   244 	}
       
   245 
       
   246 void CUpsStateMachine::HandleProcessTermination(TProcessKey& aProcessKey)
       
   247 	{
       
   248 	__FLOG_2(_L("CUpsStateMachine %08x:\t HandleProcessTermination(), processId %d"),
       
   249 				 this, aProcessKey.iProcessEntry.ProcessId().Id()); 
       
   250 
       
   251 	TUint32 index = GetIndex(aProcessKey.iProcessEntry);
       
   252 	(iState.operator[](index))->HandleProcessTermination(aProcessKey);
       
   253 	}
       
   254 
       
   255 void CUpsStateMachine::HandleThreadTermination(TThreadKey& aThreadKey)
       
   256 	{
       
   257 	__FLOG_3(_L("CUpsStateMachine\t HandleThreadTermination() processId = %d, threadId = %d"),
       
   258 				 this, aThreadKey.iProcessEntry.ProcessId().Id(), aThreadKey.iThreadEntry.ThreadId().Id() ); 
       
   259 
       
   260 	TUint32 index = GetIndex(aThreadKey.iProcessEntry);
       
   261 	(iState.operator[](index))->HandleThreadTermination(aThreadKey);
       
   262 	}
       
   263 		
       
   264 TInt32	CUpsStateMachine::ServiceId() 
       
   265 	{
       
   266 	return iServiceId;
       
   267 	}
       
   268 
       
   269 TNetUpsState CUpsStateMachine::GetState(CProcessEntry& aProcessEntry)
       
   270 	{
       
   271 	__FLOG_STATIC2(KNetUpsSubsys, KNetUpsComponent, _L("CUpsStateMachine\t GetState() processId = %d, netUpsState = %d"),
       
   272 				 	aProcessEntry.ProcessId().Id(), aProcessEntry.NetUpsState()); 
       
   273 
       
   274 	return aProcessEntry.NetUpsState();
       
   275 	}
       
   276 
       
   277 CNetUpsImpl& CUpsStateMachine::NetUpsImpl()
       
   278 	{
       
   279 	return iNetUpsImpl;	
       
   280 	}
       
   281 
       
   282 } // end of namespace
       
   283