videoutils_plat/videoconnutility_api/tsrc/VCXTestCommon/src/TestUtilConnectionWaiter.cpp
branchRCL_3
changeset 23 13a33d82ad98
parent 0 822a42b6c3f1
equal deleted inserted replaced
22:826cea16efd9 23:13a33d82ad98
       
     1 /*
       
     2 * Copyright (c) 2008 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 the License "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 
       
    21 #include "TestUtilConnectionWaiter.h"
       
    22 #include "VCXTestLog.h"
       
    23 #include "MTestUtilConnectionObserver.h"
       
    24 
       
    25 const TInt KMillion = 1000000;
       
    26 
       
    27 // ========================== MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // CTestUtilConnectionWaiter::NewL()
       
    31 //
       
    32 // Constructs CTestUtilConnectionWaiter object
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 EXPORT_C CTestUtilConnectionWaiter* CTestUtilConnectionWaiter::NewL(MTestUtilConnectionObserver* aObserver)
       
    36     {
       
    37     VCXLOGLO1(">>>CTestUtilConnectionWaiter::NewL");
       
    38     CTestUtilConnectionWaiter* self = NewLC(aObserver);
       
    39     CleanupStack::Pop(self);
       
    40     VCXLOGLO1("<<<CTestUtilConnectionWaiter::NewL");
       
    41     return self;
       
    42     }
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // CTestUtilConnectionWaiter::NewLC()
       
    46 //
       
    47 // Constructs CTestUtilConnectionWaiter object
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 EXPORT_C CTestUtilConnectionWaiter* CTestUtilConnectionWaiter::NewLC(MTestUtilConnectionObserver* aObserver)
       
    51     {
       
    52     VCXLOGLO1(">>>CTestUtilConnectionWaiter::NewLC");
       
    53     CTestUtilConnectionWaiter* self = new (ELeave) CTestUtilConnectionWaiter(aObserver);
       
    54     CleanupStack::PushL(self);
       
    55     self->ConstructL();
       
    56     VCXLOGLO1("<<<CTestUtilConnectionWaiter::NewLC");
       
    57     return self;
       
    58     }
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // CTestUtilConnectionWaiter::CTestUtilConnectionWaiter()
       
    62 //
       
    63 // Constructor
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 EXPORT_C CTestUtilConnectionWaiter::CTestUtilConnectionWaiter(MTestUtilConnectionObserver* aObserver)
       
    67 : CActive(CActive::EPriorityHigh), iObserver(aObserver)
       
    68     {
       
    69     VCXLOGLO1(">>>CTestUtilConnectionWaiter::CTestUtilConnectionWaiter");
       
    70 
       
    71     VCXLOGLO1("<<<CTestUtilConnectionWaiter::CTestUtilConnectionWaiter");
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // CTestUtilConnectionWaiter::~CTestUtilConnectionWaiter()
       
    76 //
       
    77 // Destructor
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 EXPORT_C CTestUtilConnectionWaiter::~CTestUtilConnectionWaiter()
       
    81     {
       
    82     VCXLOGLO1(">>>CTestUtilConnectionWaiter::~CTestUtilConnectionWaiter");
       
    83 
       
    84    	if( IsAdded() )
       
    85         {
       
    86         Deque(); // calls also Cancel()
       
    87         }
       
    88 
       
    89    	iServ.Close();
       
    90 
       
    91    	iTimer.Close();
       
    92 
       
    93     VCXLOGLO1("<<<CTestUtilConnectionWaiter::~CTestUtilConnectionWaiter");
       
    94     }
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // CTestUtilConnectionWaiter::ConstructL()
       
    98 //
       
    99 // Second phase constructor
       
   100 // ---------------------------------------------------------------------------
       
   101 //
       
   102 void CTestUtilConnectionWaiter::ConstructL()
       
   103     {
       
   104     VCXLOGLO1(">>>CTestUtilConnectionWaiter::ConstructL");
       
   105 
       
   106    	// get connection for the iap
       
   107 
       
   108    	TInt err = iServ.Connect();
       
   109    	if(err != KErrNone)
       
   110    		{
       
   111    		VCXLOGLO2("Could not connect to socket server, %d.", err);
       
   112    		User::Leave(err);
       
   113    		}
       
   114 
       
   115    	User::LeaveIfError(iTimer.CreateLocal());
       
   116 
       
   117 	CActiveScheduler::Add(this);
       
   118 
       
   119 
       
   120    	VCXLOGLO1("<<<CTestUtilConnectionWaiter::ConstructL");
       
   121     }
       
   122 
       
   123 // ---------------------------------------------------------------------------
       
   124 // CTestUtilConnectionWaiter::WaitForConnection()
       
   125 //
       
   126 // Start waiting the IAP to be active
       
   127 // ---------------------------------------------------------------------------
       
   128 //
       
   129 EXPORT_C void CTestUtilConnectionWaiter::WaitForConnection(TUint32 aIapId)
       
   130 	{
       
   131 	VCXLOGLO1(">>>CTestUtilConnectionWaiter::WaitForConnection");
       
   132 
       
   133 	if (!IsActive())
       
   134 		{
       
   135 		iWaitState = EWaitingConnection;
       
   136 
       
   137 		iIapId = aIapId;
       
   138 		iTimer.After(iStatus, KMillion);
       
   139 		SetActive();
       
   140 		iCheckCount = 0;
       
   141 
       
   142 		VCXLOGLO2(">>>CTestUtilConnectionWaiter:: Waiting until connection is created for Iap: %d", aIapId);
       
   143         }
       
   144 	else
       
   145 		{
       
   146 		VCXLOGLO1(">>>CTestUtilConnectionWaiter:: error - already ACTIVE");
       
   147 		}
       
   148 
       
   149 	VCXLOGLO1("<<<CTestUtilConnectionWaiter::WaitForConnection");
       
   150 	}
       
   151 
       
   152 
       
   153 // ---------------------------------------------------------------------------
       
   154 // CTestUtilConnectionWaiter::WaitUntilConnectionIsClosed()
       
   155 //
       
   156 // Start waiting the IAP to be active
       
   157 // ---------------------------------------------------------------------------
       
   158 //
       
   159 EXPORT_C void CTestUtilConnectionWaiter::WaitUntilConnectionIsClosed(TUint32 aIapId)
       
   160 	{
       
   161 	VCXLOGLO1(">>>CTestUtilConnectionWaiter::WaitUntilConnectionIsClosed");
       
   162 
       
   163 	if (!IsActive())
       
   164 		{
       
   165 		iWaitState = EWaitingConnectionClosed;
       
   166 
       
   167 		iIapId = aIapId;
       
   168 		iTimer.After(iStatus, KMillion);
       
   169 		SetActive();
       
   170 		iCheckCount = 0;
       
   171 
       
   172 		VCXLOGLO2(">>>CTestUtilConnectionWaiter:: Waiting until connection closed for Iap: %d", aIapId);
       
   173         }
       
   174 	else
       
   175 		{
       
   176 		VCXLOGLO1(">>>CTestUtilConnectionWaiter:: error - already ACTIVE");
       
   177 		}
       
   178 
       
   179 	VCXLOGLO1("<<<CTestUtilConnectionWaiter::WaitUntilConnectionIsClosed");
       
   180 	}
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // CTestUtilConnectionWaiter::RunL()
       
   184 //
       
   185 // Handle request completion events
       
   186 // ---------------------------------------------------------------------------
       
   187 //
       
   188 void CTestUtilConnectionWaiter::RunL()
       
   189 	{
       
   190  	VCXLOGLO1(">>>CTestUtilConnectionWaiter::RunL");
       
   191 
       
   192 //    TInt statusCode = iStatus.Int();
       
   193 
       
   194 	if( iCheckCount++ == 30 )
       
   195 		{
       
   196 		VCXLOGLO1("CTestUtilConnectionWaiter:: Connection request timed out.");
       
   197 		iObserver->ConnectionTimeout();
       
   198 		iCheckCount = 0;
       
   199 		Cancel();
       
   200 		}
       
   201     else
       
   202         {
       
   203     	switch(iWaitState)
       
   204     		{
       
   205     		case EWaitingConnection:
       
   206     			{
       
   207     			if(IsConnectionActive(iIapId))
       
   208     				{
       
   209     				iObserver->ConnectionCreated();
       
   210     				iWaitState = ENoWaitState;
       
   211     				VCXLOGLO1("Connection is active!");
       
   212     				}
       
   213     		    else
       
   214     		    	{
       
   215     		    	SetActive();
       
   216     				iTimer.After(iStatus, KMillion);
       
   217     		    	}
       
   218     			}
       
   219     			break;
       
   220 
       
   221     		case EWaitingConnectionClosed:
       
   222     			{
       
   223     			if(!IsConnectionActive(iIapId))
       
   224     				{
       
   225     				iObserver->ConnectionClosed();
       
   226     				iWaitState = ENoWaitState;
       
   227     				VCXLOGLO1("Connection is closed!");
       
   228     				}
       
   229     			else
       
   230     				{
       
   231     		    	SetActive();
       
   232     				iTimer.After(iStatus, KMillion);
       
   233     				}
       
   234     			}
       
   235     			break;
       
   236 
       
   237     		default:
       
   238     			break;
       
   239     		}
       
   240     	}
       
   241 
       
   242     VCXLOGLO1("<<<CTestUtilConnectionWaiter::RunL");
       
   243 
       
   244 	}
       
   245 
       
   246 // ---------------------------------------------------------------------------
       
   247 // CTestUtilConnectionWaiter::DoCancel()
       
   248 //
       
   249 // Cancels ongoing requests
       
   250 // ---------------------------------------------------------------------------
       
   251 //
       
   252 void CTestUtilConnectionWaiter::DoCancel()
       
   253 	{
       
   254 	VCXLOGLO1(">>>CTestUtilConnectionWaiter::DoCancel");
       
   255 	iTimer.Cancel();
       
   256 	VCXLOGLO1("<<<CTestUtilConnectionWaiter::DoCancel");
       
   257     }
       
   258 
       
   259 // ---------------------------------------------------------------------------
       
   260 // CTestUtilConnectionWaiter::IsConnectionActive(TUint32 aIapId)
       
   261 //
       
   262 // Checks if IAP specified in iWaitConnectionForIapId is active
       
   263 // ---------------------------------------------------------------------------
       
   264 EXPORT_C TBool CTestUtilConnectionWaiter::IsConnectionActive(TUint32 aIapId)
       
   265 	{
       
   266 	VCXLOGLO1(">>>CTestUtilConnectionWaiter::IsConnectionActive");
       
   267 
       
   268     RConnection connection;
       
   269 
       
   270 	TUint count;
       
   271 
       
   272  	TInt err = connection.Open(iServ, KAfInet);
       
   273 
       
   274  	if(err != KErrNone)
       
   275  		{
       
   276  		iServ.Close();
       
   277    		VCXLOGLO2("CTestUtilConnectionWaiter::Could not connect to open connection: %d.", err);
       
   278    		User::Leave(err);
       
   279  		}
       
   280 
       
   281     err = connection.EnumerateConnections(count);
       
   282  	if(err != KErrNone)
       
   283  		{
       
   284  		connection.Close();
       
   285  		iServ.Close();
       
   286    		VCXLOGLO2("CTestUtilConnectionWaiter::Could not enum connections, %d.", err);
       
   287    		User::Leave(err);
       
   288  		}
       
   289 
       
   290     for(; count; --count)
       
   291         {
       
   292         TPckgBuf<TConnectionInfo> connInfo;
       
   293 
       
   294         if(connection.GetConnectionInfo(count, connInfo) == KErrNone)
       
   295             {
       
   296             VCXLOGLO2("CTestUtilConnectionWaiter:: Connection IAP id: %d", connInfo().iIapId);
       
   297 
       
   298             if(aIapId == connInfo().iIapId)
       
   299             	{
       
   300             	VCXLOGLO1("CTestUtilConnectionWaiter:: IAP is connected.");
       
   301             	VCXLOGLO1("<<<CTestUtilConnectionWaiter::IsConnectionActive");
       
   302             	connection.Close();
       
   303 				return ETrue;
       
   304             	}
       
   305             }
       
   306         }
       
   307 
       
   308  	connection.Close();
       
   309 
       
   310 	VCXLOGLO1("<<<CTestUtilConnectionWaiter::IsConnectionActive");
       
   311     return EFalse;
       
   312 	}
       
   313 
       
   314 
       
   315 // End of file