internetradio2.0/streamsourcesrc/irsocketopener.cpp
changeset 14 896e9dbc5f19
parent 12 608f67c22514
child 15 065198191975
equal deleted inserted replaced
12:608f67c22514 14:896e9dbc5f19
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  Socket opener implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32property.h>
       
    20 #include <HttpDownloadMgrCommon.h>
       
    21 #include <in_sock.h>
       
    22 #include <uriutils.h>
       
    23 
       
    24 #include "irpubsubkeys.h"
       
    25 #include "irdebug.h"
       
    26 #include "irnetworkcontroller.h"
       
    27 #include "irsocketopener.h"
       
    28 #include "irstationconnection.h"
       
    29 #include "irstreamsourceerrors.h"
       
    30 #include "irstreamsourceliterals.h"
       
    31 
       
    32 const TInt KIRSOTimeOutValue = 10000000;
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // CIRSocketOpener::NewL
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CIRSocketOpener* CIRSocketOpener::NewL( RSocket& aSocket, CIRStationConnection& aOwner )
       
    39 	{
       
    40 	CIRSocketOpener* self = new ( ELeave ) CIRSocketOpener( aSocket, aOwner );
       
    41 	CleanupStack::PushL( self );
       
    42 	self->ConstructL();
       
    43 	CleanupStack::Pop( self );
       
    44 	return self;
       
    45 	}
       
    46 	
       
    47 // ---------------------------------------------------------------------------
       
    48 // CIRSocketOpener::CIRSocketOpener
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 CIRSocketOpener::CIRSocketOpener( RSocket& aSocket, CIRStationConnection& aOwner )
       
    52 	:CActive( EPriorityStandard ), iPort( KDefaultPort ), iSocket( aSocket), iOwner( aOwner )
       
    53 	{
       
    54 	}
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // CIRSocketOpener::ConstructL
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 void CIRSocketOpener::ConstructL()
       
    61 	{
       
    62 	IRLOG_DEBUG( "CIRSocketOpener::ConstructL" );	
       
    63 	CActiveScheduler::Add( this );
       
    64 	iState = EIRIdle;
       
    65 	iSocketTimer = CIRSocketTimeOutTimer::NewL( CActive::EPriorityHigh,*this );
       
    66 
       
    67 	CIRNetworkController* networkControllerHandle = CIRNetworkController::OpenL();
       
    68 	CleanupClosePushL( *networkControllerHandle );
       
    69 
       
    70     TProtocolName protocolName( KTcpProtocol );
       
    71 	TProtocolDesc protocolInfo;
       
    72 	
       
    73 	networkControllerHandle->GetIRSocketServer().FindProtocol( protocolName, protocolInfo );
       
    74 	
       
    75 	iResolver.Close();
       
    76 
       
    77 	User::LeaveIfError( iResolver.Open( networkControllerHandle->GetIRSocketServer(),
       
    78 	                                   protocolInfo.iAddrFamily, protocolInfo.iProtocol,
       
    79 	                                   networkControllerHandle->GetIRConnection() ) );
       
    80 
       
    81 	IRLOG_DEBUG( "CIRSocketOpener::ConstructL - Opened resolver." );
       
    82 	User::LeaveIfError( iSocket.Open( networkControllerHandle->GetIRSocketServer(),
       
    83 				 protocolInfo.iAddrFamily,protocolInfo.iSockType,protocolInfo.iProtocol,
       
    84 	                                  networkControllerHandle->GetIRConnection() ) );
       
    85     IRLOG_DEBUG( "CIRSocketOpener::ConstructL - Opened socket." );
       
    86 	                                   
       
    87     CleanupStack::PopAndDestroy();
       
    88 	IRLOG_DEBUG( "CIRSocketOpener::ConstructL - Exiting." );	    
       
    89 	}
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 // CIRSocketOpener::CIRSocketOpener
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 CIRSocketOpener::~CIRSocketOpener()
       
    96 	{
       
    97 	Cancel();
       
    98 //	iResolver.Close()	
       
    99     iHost.Close();	
       
   100 	delete iSocketTimer;	
       
   101 	}
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // CIRSocketOpener::ResolveAddress()
       
   105 // ---------------------------------------------------------------------------
       
   106 //
       
   107 void CIRSocketOpener::ResolveAddress()
       
   108 	{
       
   109 	IRLOG_INFO2( "CIRSocketOpener::ResolveAddress - %S", &iHost );
       
   110     Cancel();
       
   111 	iState = EIRResolving;
       
   112 
       
   113 	iSocketTimer->Cancel();
       
   114 	iSocketTimer->After( KIRSOTimeOutValue );
       
   115 	
       
   116 	iResolver.GetByName( iHost, iResolvedHostInfo, iStatus );
       
   117 	SetActive();
       
   118 
       
   119 	IRLOG_DEBUG( "CIRSocketOpener::ResolveAddress - Exiting." );                
       
   120 	}
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // CIRSocketOpener::ConnectToAddress()
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 void CIRSocketOpener::ConnectToAddress()
       
   127 	{
       
   128 	IRLOG_INFO( "CIRSocketOpener::ConnectToAddress" );
       
   129     Cancel();
       
   130 	iState = EIRConnecting;
       
   131 
       
   132 	iSocketTimer->Cancel();
       
   133 	iSocketTimer->After( KIRSOTimeOutValue );
       
   134 	
       
   135 	iSocket.Connect( iSockAddr, iStatus );
       
   136 	SetActive();
       
   137 
       
   138 	IRLOG_DEBUG( "CIRSocketOpener::ConnectToAddress - Exiting." );    
       
   139     }   
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // CIRSocketOpener::ExtractUriComponents
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 TBool CIRSocketOpener::ExtractUriComponentsL(const TDesC& aUrl)
       
   146 	{
       
   147 	IRLOG_DEBUG( "CIRSocketOpener::ExtractUriComponentsL" );    
       
   148 
       
   149     TBool retMe = EFalse;
       
   150 
       
   151 	if( !UriUtils::HasInvalidChars( aUrl ) )
       
   152 		{
       
   153     	// Initialize the Uri Parser
       
   154     	CUri8* uri = UriUtils::CreateUriL( aUrl );
       
   155     	CleanupStack::PushL( uri );
       
   156     	
       
   157     	const TUriC8& accessorUri = uri->Uri(); 
       
   158 
       
   159         iHost.Close();
       
   160     
       
   161         const TDesC8& host = accessorUri.Extract( EUriHost ); 
       
   162         if ( host.Length() > 0 )
       
   163             {
       
   164             if ( iHost.Create( host.Length() ) == KErrNone )
       
   165                 {
       
   166                 iHost.Copy( host );
       
   167             
       
   168 	            TLex8 uriLex( accessorUri.Extract( EUriPort ) );
       
   169 	            if ( uriLex.Val( iPort ) != KErrNone )
       
   170 	                {
       
   171     	            iPort = KDefaultPort;
       
   172 	                }
       
   173                 retMe = ETrue;	            
       
   174                 }
       
   175             }
       
   176         CleanupStack::PopAndDestroy( uri );
       
   177 		}
       
   178 	IRLOG_DEBUG2( "CIRSocketOpener::ExtractUriComponentsL - Returning %d.", retMe );        
       
   179 	return retMe;
       
   180 	}
       
   181 
       
   182 
       
   183 // ---------------------------------------------------------------------------
       
   184 // CIRSocketOpener::RunL
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 void CIRSocketOpener::RunL()
       
   188 	{
       
   189 	IRLOG_INFO3( "CIRSocketOpener::RunL - iStatus = %d, iState = %d", iStatus.Int(), iState );
       
   190 
       
   191 	if( iStatus == KErrNone )
       
   192 		{
       
   193 		switch( iState )
       
   194 			{
       
   195 			case EIRResolving:
       
   196 				iSocketTimer->Cancel();
       
   197 				iSockAddr = ( iResolvedHostInfo() ).iAddr;
       
   198 				iSockAddr.SetPort( iPort );
       
   199 				ConnectToAddress();
       
   200 				break;
       
   201 			case EIRConnecting:
       
   202     			iSocketTimer->Cancel();
       
   203 				iState = EIRReady;
       
   204 				iOwner.ConnectionReady();
       
   205 				break;
       
   206 			default:
       
   207                 __ASSERT_DEBUG( EFalse, User::Invariant() );
       
   208     			break;
       
   209 			}
       
   210 		}
       
   211 	else 
       
   212 		{
       
   213 		switch( iState )
       
   214 			{
       
   215 			case EIRResolving:
       
   216 				IRLOG_ERROR( "CIRConnectionHandler::RunL - EIRResolving" );
       
   217 				iSocketTimer->Cancel();
       
   218 				iOwner.ConnectionError( KIRStreamSourceLookupError );
       
   219 				break;
       
   220 			case EIRConnecting: 
       
   221 				IRLOG_ERROR( "CIRConnectionHandler::RunL - EConnecting"); 
       
   222 				iSocketTimer->Cancel();
       
   223 				iOwner.ConnectionError( KIRStreamSourceConnectionError );
       
   224 				break;
       
   225 			default:
       
   226 				IRLOG_FATAL2( "CIRConnectionHandler::RunL - Error in unexpected state (%d)", iStatus.Int() );
       
   227                 __ASSERT_DEBUG( EFalse, User::Invariant() );				
       
   228 				break;
       
   229 			}
       
   230 		}
       
   231 	IRLOG_DEBUG( "CIRSocketOpener::RunL - Exiting." );
       
   232 	}
       
   233 
       
   234 // ---------------------------------------------------------------------------
       
   235 // CIRSocketOpener::Connect
       
   236 // ---------------------------------------------------------------------------
       
   237 //
       
   238 void CIRSocketOpener::ConnectL( const TDesC& aUrl )
       
   239 	{
       
   240     IRLOG_INFO2( "CIRSocketOpener::ConnectL - %S", &aUrl );
       
   241 	if( ExtractUriComponentsL( aUrl ) )
       
   242 		{
       
   243 		ResolveAddress();		  	
       
   244 		}
       
   245 	else
       
   246 		{
       
   247 		IRLOG_ERROR( "CIRConnectionHandler::ConnectL - Invalid URL" );
       
   248 		iOwner.ConnectionError( KIRStreamSourceInvalidUrl );		  		
       
   249 		}  
       
   250 	IRLOG_DEBUG( "CIRSocketOpener::ConnectL - Exiting." );		
       
   251 	}
       
   252 
       
   253 // ---------------------------------------------------------------------------
       
   254 // CIRSocketOpener::TimerExpired
       
   255 // ---------------------------------------------------------------------------
       
   256 //
       
   257 void CIRSocketOpener::TimerExpired()
       
   258 	{
       
   259 	switch( iState )
       
   260 		{
       
   261 		case EIRResolving: 
       
   262 			Cancel();
       
   263 			IRLOG_ERROR( "CIRSocketOpener::TimerExpired - Timed out while looking up server." );
       
   264 			iOwner.ConnectionError( KIRStreamSourceTimeOut );
       
   265 			break;
       
   266 		
       
   267 		case EIRConnecting: 
       
   268 		    Cancel();
       
   269 			IRLOG_ERROR( "CIRSocketOpener::TimerExpired - Connecting to server timed out." );   	 		    
       
   270 			iOwner.ConnectionError( KIRStreamSourceTimeOut );
       
   271 			break;
       
   272 		default:
       
   273             __ASSERT_DEBUG( EFalse, User::Invariant() );				
       
   274 			break;
       
   275 		} 
       
   276 	}
       
   277 
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 // CIRSocketOpener::DoCancel
       
   281 // ---------------------------------------------------------------------------
       
   282 //
       
   283 void CIRSocketOpener::DoCancel()
       
   284 	{
       
   285 	IRLOG_DEBUG( "CIRSocketOpener::DoCancel." );	
       
   286 	iResolver.Cancel();
       
   287 	iSocket.CancelAll();
       
   288 	
       
   289 	iSocketTimer->Cancel();
       
   290 	IRLOG_DEBUG( "CIRSocketOpener::DoCancel - Exiting." );		
       
   291 	}
       
   292 
       
   293 
       
   294