cellularsrvapitest/datatransferhaitest/esock/src/T_RHostResolverData.cpp
changeset 0 3553901f7fa8
child 24 6638e7f4bd8f
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     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 "T_RHostResolverData.h"
       
    20 
       
    21 
       
    22 /*@{*/
       
    23 //LIT's for reading from ini file
       
    24 _LIT(KRSocketServ,				"RSocketServ");
       
    25 _LIT(KRConnection,				"RConnection");
       
    26 _LIT(KAddress,      			"Address");
       
    27 /*@}*/
       
    28 
       
    29 /*@{*/
       
    30 //LIT's for Commands
       
    31 _LIT(KCmdDestructor,			"~");
       
    32 _LIT(KCmdOpenHostResolver,		"Open");
       
    33 _LIT(KCmdGetByName,				"GetByName");
       
    34 _LIT(KCmdCloseHostResolver,		"Close");
       
    35 /*@}*/
       
    36 
       
    37 CT_RHostResolverData* CT_RHostResolverData::NewLC()
       
    38 	{
       
    39 	CT_RHostResolverData* self = new (ELeave) CT_RHostResolverData();
       
    40 	CleanupStack::PushL(self);
       
    41 	self->ConstructL();	
       
    42 	return self;
       
    43 	}
       
    44 
       
    45 CT_RHostResolverData* CT_RHostResolverData::NewL()
       
    46 	{
       
    47 	CT_RHostResolverData* self = CT_RHostResolverData::NewLC();
       
    48 	CleanupStack::Pop(self);
       
    49 	return self;
       
    50 	}
       
    51 
       
    52 CT_RHostResolverData::~CT_RHostResolverData()
       
    53 	{
       
    54 	if(iActiveCallback)
       
    55 		{
       
    56 		delete iActiveCallback;
       
    57 		iActiveCallback = NULL;
       
    58 		}
       
    59 	
       
    60 	DestroyData();
       
    61 	}
       
    62 
       
    63 CT_RHostResolverData::CT_RHostResolverData()
       
    64 :	iHostResolver(NULL),
       
    65 	iActiveCallback(NULL)
       
    66 	{
       
    67 	}
       
    68 
       
    69 void CT_RHostResolverData::ConstructL()
       
    70 	{
       
    71 	iActiveCallback = CActiveCallback::NewL(*this);
       
    72 	iHostResolver = new (ELeave)RHostResolver();
       
    73 	}
       
    74 
       
    75 
       
    76 void CT_RHostResolverData::RunL(CActive* aActive, TInt aIndex)
       
    77 	{
       
    78 	INFO_PRINTF1(_L("*START* CT_RHostResolverData::RunL"));
       
    79 	DecOutstanding(); // One of the async calls has completed 
       
    80     
       
    81     if( aActive == iActiveCallback )
       
    82     	{
       
    83     	INFO_PRINTF1(_L("Asynchronous task has completed. RunL  called"));
       
    84     	}     
       
    85     else
       
    86     	{ 
       
    87     	ERR_PRINTF1(_L("Stray RunL signal"));
       
    88     	TInt err = aActive->iStatus.Int(); 
       
    89     	if( err != KErrNone ) 
       
    90     		{ 
       
    91     		ERR_PRINTF2(_L("RunL Error %d"), err); 
       
    92     		SetAsyncError( aIndex, err ); 
       
    93     		}
       
    94     	}
       
    95     INFO_PRINTF1(_L("*END* CT_RHostResolverData::RunL"));
       
    96     }
       
    97 
       
    98 /**
       
    99  * Return a pointer to the object that the data wraps
       
   100  *
       
   101  * @return	pointer to the object that the data wraps
       
   102  */
       
   103 TAny* CT_RHostResolverData::GetObject()
       
   104 	{
       
   105 	return iHostResolver;
       
   106 	}
       
   107 
       
   108 /**
       
   109  * Process a command read from the ini file
       
   110  *
       
   111  * @param aCommand			The command to process
       
   112  * @param aSection			The section in the ini containing data for the command
       
   113  * @param aAsyncErrorIndex	Command index for async calls to return errors to
       
   114  *
       
   115  * @return					ETrue if the command is processed
       
   116  *
       
   117  * @leave					System wide error
       
   118  */
       
   119 TBool CT_RHostResolverData::DoCommandL(const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt aAsyncErrorIndex)
       
   120 	{
       
   121 	TBool ret = ETrue;
       
   122 	if( aCommand == KCmdOpenHostResolver() )
       
   123 		{
       
   124 		DoCmdOpen(aSection);
       
   125 		}
       
   126 	else if( aCommand == KCmdGetByName() )
       
   127 		{
       
   128 		DoCmdGetByName(aSection,aAsyncErrorIndex);
       
   129 		}
       
   130 	else if( aCommand == KCmdCloseHostResolver())
       
   131 		{
       
   132 		DoCmdClose();
       
   133 		}
       
   134 	else if( aCommand == KCmdDestructor())
       
   135 		{
       
   136 		DestroyData();
       
   137 		}
       
   138 	else
       
   139 		{
       
   140 		ERR_PRINTF1(_L("Unknown command"));
       
   141 		ret=EFalse;
       
   142 		}
       
   143 	
       
   144 	return ret;
       
   145 	}
       
   146 
       
   147 
       
   148 
       
   149 /**
       
   150  * Opens RHostResolver
       
   151  * @param aSection -The section in config file to look for the RSocketServ and RConnection.
       
   152  * Sets TEF error if not successful.
       
   153  */
       
   154 void CT_RHostResolverData::DoCmdOpen( const TTEFFunction& aSection)
       
   155     {
       
   156     INFO_PRINTF1(_L("*START* CT_RHostResolverData::DoCmdOpen"));
       
   157    
       
   158     // Start host resolver for DNS queries.
       
   159 	INFO_PRINTF1(_L("Starting host resolver"));
       
   160 	
       
   161 	TPtrC socketServerName;
       
   162 	TPtrC connectionName;
       
   163 	TBool dataOk = ETrue;
       
   164 	TInt error = KErrNone;
       
   165 	
       
   166 	if( !GetStringFromConfig(aSection, KRSocketServ(), socketServerName) )
       
   167 		{
       
   168 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KRSocketServ);
       
   169 		SetBlockResult(EFail);
       
   170 		dataOk = EFalse;
       
   171 		}
       
   172 	
       
   173 	if( !GetStringFromConfig(aSection, KRConnection(), connectionName) )
       
   174 		{
       
   175 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KRConnection);
       
   176 		SetBlockResult(EFail);
       
   177 		dataOk = EFalse;
       
   178 		}
       
   179 	
       
   180 	if( dataOk )
       
   181 		{
       
   182 		INFO_PRINTF1(_L("Create a RSocketServ for iHostResolver"));
       
   183 		RSocketServ* socketServ = static_cast<RSocketServ*>(GetDataObjectL(socketServerName));
       
   184 		
       
   185 		INFO_PRINTF1(_L("Create a RConnection for iHostResolver"));
       
   186 		RConnection* connection = static_cast<RConnection*>(GetDataObjectL(connectionName));
       
   187 		
       
   188 		error = iHostResolver->Open(*socketServ, KAfInet, KProtocolInetTcp, *connection);
       
   189 		}
       
   190 	
       
   191 	if ( error != KErrNone )
       
   192         {
       
   193         ERR_PRINTF2(_L("HostResolver could not be opened. Host resolver start failed [%d]"), error);
       
   194         SetError(error);
       
   195         }
       
   196 
       
   197     INFO_PRINTF1(_L("*END* CT_RHostResolverData::DoCmdOpen"));
       
   198     }
       
   199 
       
   200 /**
       
   201  * Resolves a machine name to a TSockAddress asynchronously and saves it in iNameEntry
       
   202  * @param aSection			The section in the ini containing data for the command
       
   203  * @param aAsyncErrorIndex	Command index for async calls to return errors to
       
   204  */
       
   205 void CT_RHostResolverData::DoCmdGetByName(const TTEFFunction& aSection, const TInt aAsyncErrorIndex)
       
   206 	{
       
   207 	INFO_PRINTF1(_L("*START* CT_RHostResolverData::DoCmdGetByName"));
       
   208 	
       
   209 	TPtrC address;
       
   210 	TBool dataOk = ETrue;
       
   211 	
       
   212     if( !GetStringFromConfig( aSection, KAddress, address ) )
       
   213     	{
       
   214     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KAddress);
       
   215     	SetBlockResult(EFail);
       
   216     	dataOk = EFalse;
       
   217     	}	
       
   218 
       
   219     if( dataOk )
       
   220     	{
       
   221     	// Resolve Internet address of our host.
       
   222     	iHostResolver->GetByName(address, iNameEntry, iActiveCallback->iStatus);
       
   223     	iActiveCallback->Activate(aAsyncErrorIndex);
       
   224     	IncOutstanding();
       
   225     	}
       
   226     
       
   227     INFO_PRINTF1(_L("*END* CT_RHostResolverData::DoCmdGetByName"));
       
   228 	}
       
   229 
       
   230 /**
       
   231  * Closes RHostResolver
       
   232  */
       
   233 void CT_RHostResolverData::DoCmdClose()
       
   234 	{
       
   235 	INFO_PRINTF1(_L("*START* CT_RHostResolverData::DoCmdClose"));
       
   236     // DNS host resolver can be closed
       
   237     iHostResolver->Close();
       
   238     INFO_PRINTF1(_L("*END* CT_RHostResolverData::DoCmdClose"));
       
   239 	}
       
   240 
       
   241 /**
       
   242  * Destroys Wrapper
       
   243  */
       
   244 void CT_RHostResolverData::DoCmdDestructor()
       
   245 	{
       
   246 	INFO_PRINTF1(_L("*START* CT_RHostResolverData::DoCmdDestructor"));
       
   247 	DestroyData();
       
   248 	INFO_PRINTF1(_L("*END* CT_RHostResolverData::DoCmdDestructor"));
       
   249 	}
       
   250 
       
   251 /**
       
   252  * Liberates allocated memory and closes RHostResolver
       
   253  */
       
   254 void CT_RHostResolverData::DestroyData()
       
   255 	{
       
   256 	INFO_PRINTF1(_L("*START* CT_RHostResolverData::DestroyData"));
       
   257 	if(iHostResolver)
       
   258 		{
       
   259 		iHostResolver->Close();
       
   260 		delete iHostResolver;
       
   261 		iHostResolver = NULL;
       
   262 		}
       
   263 	INFO_PRINTF1(_L("*END* CT_RHostResolverData::DestroyData"));
       
   264 	}
       
   265 
       
   266 /**
       
   267  * Getter method that returns iNameEntry (the result of name resolution).
       
   268  * @iNameEntry - Current name resolution result 
       
   269  */
       
   270 TNameEntry CT_RHostResolverData::GetNameEntry()
       
   271 	{
       
   272 	return iNameEntry;
       
   273 	}
       
   274