testconns/statapi/device/source/statapi/src/activeconnection.cpp
changeset 4 b8d1455fddc0
equal deleted inserted replaced
2:73b88125830c 4:b8d1455fddc0
       
     1 /*
       
     2 * Copyright (c) 2005-2009 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 
       
    19 #include "activeconnection.h"
       
    20 
       
    21 CActiveConnection* CActiveConnection::NewL(const TDesC& ipAddress, TInt port)
       
    22 {
       
    23 	CActiveConnection* self=new(ELeave)CActiveConnection();
       
    24 	CleanupStack::PushL(self);
       
    25 	self->ConstructL(ipAddress,port);
       
    26 	CleanupStack::Pop();
       
    27 	return self;	
       
    28 }
       
    29 
       
    30 CActiveConnection::CActiveConnection(void) : CActive(0)
       
    31 {
       
    32 	;
       
    33 }
       
    34 
       
    35 void CActiveConnection::ConstructL(const TDesC& ipAddress, TInt port)
       
    36 {
       
    37 	iDstAddr.SetPort(port);		
       
    38     iDstAddr.Input(ipAddress);
       
    39     
       
    40 	CActiveScheduler::Add(this);
       
    41 }
       
    42 
       
    43 CActiveConnection::~CActiveConnection()
       
    44 {
       
    45 	Cancel();
       
    46 }
       
    47 
       
    48 //Checks for user input to console.
       
    49 void CActiveConnection::RunL()
       
    50 {
       
    51 	
       
    52 	if(iStatus==KErrInUse)
       
    53 	{
       
    54 		Connect();
       
    55 		SetActive();
       
    56 	}
       
    57 
       
    58 }
       
    59 
       
    60 void CActiveConnection::DoCancel()
       
    61 {
       
    62 }
       
    63 
       
    64 void CActiveConnection::Connect(TActiveConnectionMode aConnMode, TInt aConnIndex)
       
    65 {
       
    66 	//Wait 2 seconds before attempting to connect to NTRAS
       
    67 	//It solves 8, randomly failing to connect to NTRAS
       
    68 
       
    69 	User::After(KDelay);
       
    70 	
       
    71 #if defined(__WINS__)
       
    72 
       
    73     // WinTap IAP
       
    74 	TUint32 lIAP = 0;
       
    75 	
       
    76 	_LIT16(KWIN_TAP, "WinTAP");
       
    77 	
       
    78 	/****************** CommsDat Soloution ************************
       
    79 		CMDBSession* iDb = CMDBSession::NewL(CMDBSession::LatestVersion());
       
    80 		CCDIAPRecord* ptrIAPRecord = static_cast<CCDIAPRecord *> (CCDRecordBase::RecordFactoryL(KCDTIdIAPRecord));
       
    81 		
       
    82 		TPtrC recordname(KWIN_TAP);
       
    83 		ptrIAPRecord->iRecordName.SetMaxLengthL(recordname.Length());
       
    84 		ptrIAPRecord->iRecordName = recordname;
       
    85 
       
    86 		if(ptrIAPRecord->FindL(*iDb))
       
    87 	    {
       
    88 		    // Found a matching record
       
    89 	//	    ptrIAPRecord->iService.LoadL(*iDb);
       
    90 		    
       
    91 		    delete ptrIAPRecord;  
       
    92 	    }
       
    93 	***************************************************************/
       
    94 
       
    95 
       
    96 	if (aConnMode == EModeIAP && aConnIndex == 0)
       
    97 	{
       
    98 		// Open CommDB IAP table
       
    99 		CCommsDatabase* lCommsDb = CCommsDatabase::NewL();
       
   100     	CleanupStack::PushL(lCommsDb);
       
   101 		CCommsDbTableView* lIapTable = lCommsDb->OpenTableLC(TPtrC(IAP));
       
   102 		
       
   103 		TBuf<52> lIapName;
       
   104 	
       
   105 		// Point to the first entry
       
   106 		User::LeaveIfError(lIapTable->GotoFirstRecord());
       
   107 		lIapTable->ReadTextL(TPtrC(COMMDB_NAME), lIapName);
       
   108 
       
   109 		if (lIapName.Find(KWIN_TAP) != KErrNotFound) 
       
   110 		{
       
   111 			lIapTable->ReadUintL(TPtrC(COMMDB_ID), lIAP);
       
   112 		}
       
   113 		else
       
   114 		{
       
   115 			// Point to the rest of the entries
       
   116 			TInt err = KErrNone;
       
   117 			while (err = lIapTable->GotoNextRecord(), err == KErrNone)
       
   118 			{
       
   119 				lIapTable->ReadTextL(TPtrC(COMMDB_NAME), lIapName);
       
   120 				if (lIapName.Find(KWIN_TAP) != KErrNotFound) 
       
   121 				{
       
   122 					lIapTable->ReadUintL(TPtrC(COMMDB_ID), lIAP);
       
   123 				}
       
   124 	
       
   125 			}
       
   126 		}
       
   127 		
       
   128 		CleanupStack::PopAndDestroy(lIapTable);
       
   129     	CleanupStack::PopAndDestroy(lCommsDb);
       
   130 
       
   131 		aConnIndex = lIAP;
       
   132 	}
       
   133 
       
   134 #endif
       
   135 
       
   136 	// Create Connection with IAP
       
   137 	TInt lErr = iSocketServer.Connect();
       
   138 	if (lErr != KErrNone) 
       
   139 		return;
       
   140 	
       
   141 	RConnection lConn;
       
   142     lErr = lConn.Open(iSocketServer);
       
   143     
       
   144     if (lErr == KErrNone && aConnIndex > 0)
       
   145     {
       
   146     	if (aConnMode == EModeIAP)
       
   147     	{
       
   148 	    	// Set IAP Preferences
       
   149 			TCommDbConnPref lPrefs;
       
   150 			lPrefs.SetIapId(aConnIndex);
       
   151 			lPrefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
       
   152 			lErr = lConn.Start(lPrefs);
       
   153     	}
       
   154 #ifdef SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
   155     	else if (aConnMode == EModeSnap)
       
   156     	{
       
   157     		TConnSnapPref lPrefs(aConnIndex);
       
   158     		lErr = lConn.Start(lPrefs);
       
   159     	}
       
   160 #endif // SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
   161     	else
       
   162     	{
       
   163     		// use default mode
       
   164     		lErr = KErrNotSupported;
       
   165     	}
       
   166 
       
   167 		if (lErr == KErrNone) 
       
   168 		{
       
   169 			RHostResolver lHostResolver;
       
   170 			lErr = lHostResolver.Open(iSocketServer, KAfInet, KProtocolInetTcp, lConn);
       
   171 			
       
   172 			if (lErr == KErrNone) 
       
   173 			{
       
   174 				lErr = iSocket.Open(iSocketServer, KAfInet, KSockStream, KProtocolInetTcp, lConn);
       
   175 			}
       
   176 		}
       
   177     }
       
   178     
       
   179     if (aConnIndex == 0 || lErr != KErrNone)
       
   180     {
       
   181     	if(iSocket.Open(iSocketServer, KAfInet, KSockStream, KProtocolInetTcp) != KErrNone) {
       
   182     		return;
       
   183     	}
       
   184     }
       
   185    
       
   186     // Connect
       
   187 	iSocket.Connect(iDstAddr, iStatus);
       
   188 }
       
   189 
       
   190 //Initial request
       
   191 void CActiveConnection::Start(TActiveConnectionMode aConnMode, TInt aConnIndex)
       
   192 {    
       
   193 	Connect(aConnMode, aConnIndex);
       
   194 	SetActive();
       
   195 }
       
   196 
       
   197 void CActiveConnection::CloseSocket()
       
   198 {    
       
   199 	iSocket.CancelConnect();
       
   200 	iSocket.Close();
       
   201 }