realtimenetprots/sipfw/ProfileAgent/Server/Src/sipalrmigrationcontroller.cpp
changeset 0 307788aac0a8
child 4 dd3853b8dc3f
child 15 8248b03a2669
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2008-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 // Name          : sipalrmigrationcontroller.cpp
       
    15 // Part of       : SIP / SIP Profile Server
       
    16 // Version       : SIP/6.0 
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #include "sipalrmigrationcontroller.h"
       
    22 #include "sipalrmonitor.h"
       
    23 #include "sipalrmigrationobserver.h"
       
    24 #include "sipalrmigrationobserverinfo.h"
       
    25 #include "SipProfileLog.h"
       
    26 
       
    27 
       
    28 // -----------------------------------------------------------------------------
       
    29 // CSipAlrMigrationController::NewLC
       
    30 // -----------------------------------------------------------------------------
       
    31 //
       
    32 CSipAlrMigrationController*
       
    33 CSipAlrMigrationController::NewLC(CSipAlrMonitor& aAlrMonitor, TUint32 aSnapId)
       
    34 	{
       
    35 	CSipAlrMigrationController* self =
       
    36 		new (ELeave) CSipAlrMigrationController(aAlrMonitor, aSnapId);
       
    37     CleanupStack::PushL(self);
       
    38     return self;
       
    39 	}
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // CSipAlrMigrationController::CSipAlrMigrationController
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CSipAlrMigrationController::CSipAlrMigrationController(
       
    46 	CSipAlrMonitor& aAlrMonitor,
       
    47 	TUint32 aSnapId) :
       
    48 	iAlrMonitor(aAlrMonitor),
       
    49     iSnapId(aSnapId)
       
    50 #ifdef CPPUNIT_TEST
       
    51     // Set the array granularity to 1, so it allocates memory in each append
       
    52     , iObservers(1)
       
    53 #endif
       
    54 	{
       
    55 	}
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CSipAlrMigrationController::~CSipAlrMigrationController
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 CSipAlrMigrationController::~CSipAlrMigrationController()
       
    62 	{
       
    63 	PROFILE_DEBUG1("~CSipAlrMigrCtrl")
       
    64 
       
    65 	iAlrMonitor.FreeResources(*this);
       
    66 	iObservers.Close();
       
    67 	}
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CSipAlrMigrationController::AlrEvent
       
    71 // If migration is disallowed, the original IAP id is offered again.
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 void CSipAlrMigrationController::AlrEvent(MSipAlrObserver::TEvent aEvent,
       
    75 										  TUint32 aSnapId,
       
    76 										  TUint32 aIapId)
       
    77 	{
       
    78 	PROFILE_DEBUG3("CSipAlrMigrationController::AlrEvent", (TInt)(aEvent))
       
    79 
       
    80 	switch (aEvent)
       
    81 		{
       
    82 		case MSipAlrObserver::EIapAvailable:
       
    83 			if (aIapId == iAllowedIapId)
       
    84 				{
       
    85 				// Pass the failed IAP to observer, not the original IAP.
       
    86 				SendMigrationResult(iOfferedIapId, EFalse);
       
    87 				}
       
    88 			else
       
    89 				{
       
    90 				IapAvailable(aSnapId, aIapId);
       
    91 				}
       
    92 			break;
       
    93 
       
    94 		case MSipAlrObserver::EIapActive:
       
    95 			iAllowedIapId = aIapId;
       
    96 			SendMigrationResult(aIapId, ETrue);
       
    97 		    break;
       
    98 
       
    99 		case MSipAlrObserver::ENoNewIapAvailable:
       
   100 			NoNewIapAvailable();
       
   101 		    break;
       
   102 
       
   103 		case MSipAlrObserver::ERefreshError:
       
   104 			// Start from the end, as RefreshIAPsFailed() causes entries to
       
   105 			// remove themselves from the array.
       
   106 			for (TInt i = iObservers.Count() - 1; i >= 0; --i)
       
   107 				{
       
   108 				iObservers[i].iObserver.RefreshIAPsFailed();
       
   109 				}
       
   110 			break;
       
   111 
       
   112 		case MSipAlrObserver::EOfferedIapRejected:
       
   113 			OfferedIapRejected();
       
   114 		    break;
       
   115 
       
   116 
       
   117         default:
       
   118         	User::Panic(_L("ALRMigrationCtrl::AlrEvent"), KErrArgument);
       
   119         }
       
   120 	}
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // CSipAlrMigrationController::SnapId
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 TUint32 CSipAlrMigrationController::SnapId() const
       
   127 	{
       
   128 	return iSnapId;
       
   129 	}
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CSipAlrMigrationController::IsUsed
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 TBool CSipAlrMigrationController::IsUsed() const
       
   136 	{
       
   137 	return iObservers.Count() > 0;
       
   138 	}
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // CSipAlrMigrationController::AttachProfileL
       
   142 // Initiate monitoring only after first observer exists. Otherwise IAP
       
   143 // available event could not be forwarded to observer.
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 TUint32
       
   147 CSipAlrMigrationController::AttachProfileL(MSipAlrMigrationObserver& aObserver)
       
   148     {
       
   149 	TSipAlrMigrationObserverInfo observerInfo(aObserver);
       
   150 	iObservers.AppendL(observerInfo);
       
   151 
       
   152     const TInt KFirstObserverExists = 1;
       
   153     if (iObservers.Count() == KFirstObserverExists)
       
   154     	{
       
   155     	PROFILE_DEBUG3("CSipAlrMigrCtrl::AttachProfileL snapId", iSnapId)
       
   156     	iAlrMonitor.MonitorSnapL(iSnapId, *this);
       
   157     	return KNoIap;
       
   158     	}
       
   159 
       
   160     return iAllowedIapId;
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // CSipAlrMigrationController::DetachProfile
       
   165 // This is typically called from destructors, so don't leave.
       
   166 // -----------------------------------------------------------------------------
       
   167 //
       
   168 void
       
   169 CSipAlrMigrationController::DetachProfile(MSipAlrMigrationObserver& aObserver)
       
   170 	{
       
   171 	TInt index = FindIndex(aObserver);
       
   172 	if (index != KErrNotFound)
       
   173 		{
       
   174 		iObservers.Remove(index);
       
   175 		}
       
   176 
       
   177 	if (iObservers.Count() == 0)
       
   178 		{
       
   179 		PROFILE_DEBUG1("CSipAlrMigrCtrl::DetachProfile free resources")
       
   180 
       
   181 		iAlrMonitor.FreeResources(*this);
       
   182 		ResetFlags();
       
   183 		}
       
   184 	}
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CSipAlrMigrationController::IapAvailable
       
   188 // Set all observers "waiting", before using IapAvailable. Even if the first
       
   189 // observer syncronously uses SetMigrationPermission, ObserversWaitingPermission
       
   190 // returns correct value.
       
   191 // If an IAP comes available before each observer has (dis)allowed a previous
       
   192 // IAP, pass the new IAP to observers and forget the older IAP.
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 void
       
   196 CSipAlrMigrationController::IapAvailable(TUint32 aSnapId, TUint32 aIapId)
       
   197 	{
       
   198 	PROFILE_DEBUG4("CSipAlrMigrCtrl::IapAvailable snap,iap", aSnapId, aIapId)
       
   199 
       
   200 	iOfferedIapId = aIapId;
       
   201 	iMigrationDisallowed = EFalse;
       
   202 	iRefreshIssued = EFalse;
       
   203 
       
   204 	for (TInt i = 0; i < iObservers.Count(); ++i)
       
   205 		{
       
   206 		iObservers[i].iIsWaitingForPermission = ETrue;
       
   207 		iObservers[i].iIsWaitingForAcceptance = ETrue;
       
   208 		}
       
   209 
       
   210 	for (TInt i = 0; i < iObservers.Count(); ++i)
       
   211 		{
       
   212 		iObservers[i].iObserver.IapAvailable(aSnapId, aIapId);
       
   213 		}
       
   214 	}
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // CSipAlrMigrationController::SetMigrationPermission
       
   218 // Handle only the latest IAP. If IAP==EMostRecentIap don't care if the observer
       
   219 // has already used SetMigrationPermission. Otherwise do nothing if observer is
       
   220 // not waiting, as it has already automatically called SetMigrationPermission.
       
   221 // -----------------------------------------------------------------------------
       
   222 //
       
   223 void CSipAlrMigrationController::SetMigrationPermission(
       
   224 	MSipAlrMigrationObserver& aObserver,
       
   225 	TBool aAllow,
       
   226 	TUint32 aIapId)
       
   227 	{
       
   228 	PROFILE_DEBUG5("CSipAlrMigrCtrl::SetMigrPermission iap,allow,offeredIap",
       
   229 				   aIapId,
       
   230 				   aAllow,
       
   231 				   iOfferedIapId)
       
   232 
       
   233 	TInt index = FindIndex(aObserver);
       
   234 	// If no pending observers, result has been forwarded. Don't do it twice.
       
   235 	if (ObserversWaitingPermission() &&
       
   236 	    index != KErrNotFound &&
       
   237 	    ((aIapId == EMostRecentIap) ||
       
   238 		 (aIapId == iOfferedIapId &&
       
   239 		  iObservers[index].iIsWaitingForPermission)))
       
   240 		{
       
   241 		iObservers[index].iIsWaitingForPermission = EFalse;
       
   242 		if (!aAllow)
       
   243 			{
       
   244 			iMigrationDisallowed = ETrue;
       
   245 			}
       
   246 
       
   247 		if (!ObserversWaitingPermission())
       
   248 			{
       
   249 			PROFILE_DEBUG3("CSipAlrMigrCtrl::SetMigrPermission, allow=",
       
   250 						   !iMigrationDisallowed)
       
   251 			TInt err(KErrNone);
       
   252 			if (iMigrationDisallowed)
       
   253 				{
       
   254 				err = iAlrMonitor.DisallowMigration(iSnapId);
       
   255 				}
       
   256 			else
       
   257 				{
       
   258 				err = iAlrMonitor.AllowMigration(iSnapId);
       
   259 				}
       
   260 			if (err != KErrNone)
       
   261 				{
       
   262 	        	for (TInt i = 0; i < iObservers.Count(); ++i)
       
   263 					{
       
   264 					iObservers[i].iObserver.ErrorOccurred(err);
       
   265 					}
       
   266 				}
       
   267 			}
       
   268 		}
       
   269 	}
       
   270 
       
   271 // -----------------------------------------------------------------------------
       
   272 // CSipAlrMigrationController::RefreshIapAvailabilityL
       
   273 // -----------------------------------------------------------------------------
       
   274 //
       
   275 void CSipAlrMigrationController::RefreshIapAvailabilityL(TUint32 aSnapId)
       
   276 	{
       
   277 	if (!iRefreshIssued && aSnapId == iSnapId)
       
   278 		{
       
   279 		iAlrMonitor.RefreshIapAvailabilityL(aSnapId);
       
   280 		iRefreshIssued = ETrue;
       
   281 		}
       
   282 	}
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CSipAlrMigrationController::SetIapAcceptance
       
   286 // Pass result to ALR monitor if any profile succeeds with new IAP or all failed
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 TInt CSipAlrMigrationController::SetIapAcceptance(
       
   290 	MSipAlrMigrationObserver& aObserver,
       
   291 	TBool aIapWorks)
       
   292 	{
       
   293 	PROFILE_DEBUG4("CSipAlrMigrCtrl::SetIapAcceptance, iap,works=",
       
   294 				   iAllowedIapId,
       
   295 				   aIapWorks)
       
   296 
       
   297 	TInt index = FindIndex(aObserver);
       
   298 	if (index != KErrNotFound && iObservers[index].iIsWaitingForAcceptance)
       
   299 		{
       
   300 		iObservers[index].iIsWaitingForAcceptance = EFalse;
       
   301 		if (aIapWorks)
       
   302 			{
       
   303 			// IAP works, no need to wait other observers
       
   304 			for (TInt i = 0; i < iObservers.Count(); ++i)
       
   305 				{
       
   306 				iObservers[i].iIsWaitingForAcceptance = EFalse;
       
   307 				}
       
   308 			PROFILE_DEBUG1("CSipAlrMigrCtrl::SetIapAcceptance accepted")
       
   309 			return iAlrMonitor.NewIapAccepted(iSnapId);
       
   310 			}
       
   311 
       
   312 		if (!ObserversWaitingAcceptance())
       
   313 			{
       
   314 			// Nobody got IAP to work
       
   315 			PROFILE_DEBUG1("CSipAlrMigrCtrl::SetIapAcceptance rejected")
       
   316 			return iAlrMonitor.NewIapRejected(iSnapId);
       
   317 			}
       
   318 		}
       
   319 	return KErrNone;
       
   320 	}
       
   321 
       
   322 // -----------------------------------------------------------------------------
       
   323 // CSipAlrMigrationController::SendMigrationResult
       
   324 // -----------------------------------------------------------------------------
       
   325 //
       
   326 void
       
   327 CSipAlrMigrationController::SendMigrationResult(TUint32 aIapId, TBool aAllowed)
       
   328 	{
       
   329 	PROFILE_DEBUG5("CSipAlrMigrCtrl::SendMigrationResult iap,allow,obsCount",
       
   330 				   aIapId,
       
   331 				   aAllowed,
       
   332 				   iObservers.Count())	
       
   333 
       
   334 	for (TInt i = 0; i < iObservers.Count(); ++i)
       
   335 		{
       
   336 		MSipAlrMigrationObserver& obs = iObservers[i].iObserver;
       
   337 		TRAPD(err, if (aAllowed)
       
   338 			{
       
   339 			obs.MigrationIsAllowedL(aIapId);
       
   340 			}
       
   341 		else
       
   342 			{
       
   343 			obs.MigrationIsDisallowedL(aIapId);
       
   344 			});
       
   345 
       
   346 		if (err != KErrNone)
       
   347 			{
       
   348 			PROFILE_DEBUG4("MigrationIs(Dis)AllowedL leaves err,index", err, i)
       
   349 			obs.ErrorOccurred(err);
       
   350 			}
       
   351 		}
       
   352 	}
       
   353 
       
   354 // -----------------------------------------------------------------------------
       
   355 // CSipAlrMigrationController::FindIndex
       
   356 // -----------------------------------------------------------------------------
       
   357 //
       
   358 TInt
       
   359 CSipAlrMigrationController::FindIndex(MSipAlrMigrationObserver& aObserver) const
       
   360 	{
       
   361 	TSipAlrMigrationObserverInfo observerInfo(aObserver);
       
   362 	TIdentityRelation<TSipAlrMigrationObserverInfo>
       
   363 		compare(TSipAlrMigrationObserverInfo::CompareObservers);
       
   364 	return iObservers.Find(observerInfo, compare);
       
   365 	}
       
   366 
       
   367 // -----------------------------------------------------------------------------
       
   368 // CSipAlrMigrationController::ObserversWaitingPermission
       
   369 // -----------------------------------------------------------------------------
       
   370 //
       
   371 TBool CSipAlrMigrationController::ObserversWaitingPermission() const
       
   372 	{
       
   373 	for (TInt i = 0; i < iObservers.Count(); ++i)
       
   374 		{
       
   375 		if (iObservers[i].iIsWaitingForPermission)
       
   376 			{
       
   377 			return ETrue;
       
   378 			}
       
   379 		}
       
   380 
       
   381 	return EFalse;
       
   382 	}
       
   383 
       
   384 // -----------------------------------------------------------------------------
       
   385 // CSipAlrMigrationController::ObserversWaitingAcceptance
       
   386 // -----------------------------------------------------------------------------
       
   387 //
       
   388 TBool CSipAlrMigrationController::ObserversWaitingAcceptance() const
       
   389 	{
       
   390 	for (TInt i = 0; i < iObservers.Count(); ++i)
       
   391 		{
       
   392 		if (iObservers[i].iIsWaitingForAcceptance)
       
   393 			{
       
   394 			return ETrue;
       
   395 			}
       
   396 		}
       
   397 
       
   398 	return EFalse;
       
   399 	}
       
   400 
       
   401 // -----------------------------------------------------------------------------
       
   402 // CSipAlrMigrationController::NoNewIapAvailable
       
   403 // -----------------------------------------------------------------------------
       
   404 //
       
   405 void CSipAlrMigrationController::NoNewIapAvailable() const
       
   406 	{
       
   407 	for (TInt i = 0; i < iObservers.Count(); ++i)
       
   408 		{
       
   409 		iObservers[i].iObserver.NoNewIapAvailable();
       
   410 		}		
       
   411 	}
       
   412 
       
   413 // -----------------------------------------------------------------------------
       
   414 // CSipAlrMigrationController::ResetFlags
       
   415 // -----------------------------------------------------------------------------
       
   416 //
       
   417 void CSipAlrMigrationController::ResetFlags()
       
   418 	{
       
   419 	iRefreshIssued = EFalse;
       
   420     iAllowedIapId = KNoIap;
       
   421     iOfferedIapId = KNoIap;
       
   422     iMigrationDisallowed = EFalse;
       
   423 	}
       
   424 
       
   425 // -----------------------------------------------------------------------------
       
   426 // CSipAlrMigrationController::OfferedIapRejected
       
   427 // -----------------------------------------------------------------------------
       
   428 //
       
   429 void CSipAlrMigrationController::OfferedIapRejected() const
       
   430 	{
       
   431 	// Start from the end, as RefreshIAPsFailed() causes entries to
       
   432 	// remove themselves from the array.
       
   433 	for (TInt i = iObservers.Count() - 1; i >= 0; --i)
       
   434 		{
       
   435 		iObservers[i].iObserver.OfferedIapRejected();
       
   436 		}
       
   437 	}