Msrp/MsrpServer/src/CMSRPConnectionManager.cpp
branchMSRP_FrameWork
changeset 25 505ad3f0ce5c
child 58 cdb720e67852
equal deleted inserted replaced
22:f1578314b8da 25:505ad3f0ce5c
       
     1 /*
       
     2 * Copyright (c) 2009-2010 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 * Initial Contributors:
       
     9 * Nokia Corporation - initial contribution.
       
    10 * Contributors:
       
    11 *
       
    12 * Description:
       
    13 * MSRP Implementation
       
    14 *
       
    15 */
       
    16 
       
    17 // CLASS HEADER
       
    18 #include "CMSRPConnectionManager.h"
       
    19 #include "MSRPCommon.h"
       
    20 #include "CMSRPConnection.h"
       
    21 #include "CMSRPConnListener.h"
       
    22 #include <CommDbConnPref.h>
       
    23 #if defined(__SERIES60_)
       
    24 #include <extendedconnpref.h>
       
    25 #endif
       
    26 
       
    27 // INTERNAL INCLUDES
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // Constructor
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 CMSRPConnectionManager::CMSRPConnectionManager( const TUint32 aIapID, MMSRPConnectionManagerObserver& aServer ) 
       
    34                                                 : iIapID( aIapID ), iObserver( aServer )
       
    35     {
       
    36     MSRPLOG( "CMSRPConnectionManager::CMSRPConnectionManager enter" )
       
    37     MSRPLOG( "CMSRPConnectionManager::CMSRPConnectionManager exit" )
       
    38     }
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CMSRPConnectionManager* CMSRPConnectionManager::NewL
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 /*static*/CMSRPConnectionManager* CMSRPConnectionManager::NewL( const TUint32 aIapID, MMSRPConnectionManagerObserver& aServer )
       
    45     {
       
    46     MSRPLOG( "CMSRPConnectionManager::NewL enter" )
       
    47 
       
    48 	// Perform construction.
       
    49     CMSRPConnectionManager* self = new ( ELeave ) CMSRPConnectionManager( aIapID, aServer );
       
    50     CleanupStack::PushL( self );
       
    51     self->ConstructL();
       
    52     CleanupStack::Pop( self );
       
    53 
       
    54     MSRPLOG( "CMSRPConnectionManager::NewL exit" )
       
    55     return self;
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CMSRPConnectionManager::ConstructL
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 void CMSRPConnectionManager::ConstructL( )
       
    63 	{
       
    64     MSRPLOG( "CMSRPConnectionManager::ConstructL enter" )    
       
    65 
       
    66 	TInt status = iSocketServer.Connect();	
       
    67 	if (status != KErrNone)
       
    68 	    {
       
    69 	    //iObserver->HandleError(ESocketServerStartFailed, status , *this );//need to pass connmngr as server handles many connmngrs
       
    70 	    //User::Leave(status);
       
    71 	    User::Leave(MMSRPConnectionManagerObserver::ESocketServerStartFailed); //leave code instead of observer call, as in constructl itself
       
    72 	    }
       
    73 	
       
    74     status = StartInterface();    
       
    75     if (status != KErrNone)
       
    76         {
       
    77         User::Leave(MMSRPConnectionManagerObserver::EInterfaceStartFailed);
       
    78         }
       
    79     
       
    80     MSRPLOG( "CMSRPConnectionManager::ConstructL exit" )
       
    81 	}
       
    82 
       
    83 
       
    84 CMSRPConnectionManager::~CMSRPConnectionManager()
       
    85 	{
       
    86     MSRPLOG( "CMSRPConnectionManager::~CMSRPConnectionManager enter" )
       
    87     
       
    88 	delete iConnListener;
       
    89 	iConnectionArray.ResetAndDestroy();
       
    90 	iConnectionArray.Close();
       
    91 	iHostResolver.Close();
       
    92 	iConnection.Close();
       
    93 	iSocketServer.Close();
       
    94 	
       
    95 	MSRPLOG( "CMSRPConnectionManager::~CMSRPConnectionManager exit" )
       
    96 	}
       
    97 
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // CMSRPConnectionManager::ConnectL
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 MMSRPConnection& CMSRPConnectionManager::getConnectionL( TDesC8& aHost, TUint aPort )
       
   104 	{
       
   105     MSRPLOG( "CMSRPConnectionManager::getConnectionL enter" )
       
   106 
       
   107     TInetAddr hostAddr;
       
   108     TRAPD(err, ResolveIPAddressL( aHost, hostAddr ));
       
   109     if(err != KErrNone)
       
   110         {
       
   111         //iObserver->HandleError( MMSRPConnectionManagerObserver::EAddressResolveError, err , *this );
       
   112         User::Leave(MMSRPConnectionManagerObserver::EAddressResolveError);
       
   113         }
       
   114     hostAddr.SetPort(aPort);
       
   115     
       
   116 	MMSRPConnection* connection = CheckConnection( hostAddr, FALSE );
       
   117 	
       
   118 	if ( !connection )
       
   119 		{
       
   120         connection = CMSRPConnection::NewL( hostAddr, *this );
       
   121         CleanupDeletePushL(connection);//as M class
       
   122         //CleanupStack::PushL( connection );
       
   123         iConnectionArray.AppendL( connection );
       
   124         CleanupStack::Pop( );		        
       
   125 		}
       
   126 			
       
   127     MSRPLOG( "CMSRPConnectionManager::getConnectionL exit" )
       
   128     
       
   129 	return *connection;	
       
   130 	}
       
   131 
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CMSRPConnectionManager::StartListener
       
   135 //connection calls connmngr listen,  which in turn calls listener listen
       
   136 //if state not listening
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 void CMSRPConnectionManager::ListenL(MMSRPConnection* /*aConnection*/)
       
   140     {
       
   141     MSRPLOG( "CMSRPConnectionManager::ListenL enter" )
       
   142     if(!iConnListener)
       
   143         {
       
   144         iConnListener = CMSRPConnListener::NewL(*this);
       
   145         }
       
   146     
       
   147     TRAPD(err,iConnListener->ListenL());
       
   148 
       
   149     if(err != KErrNone)
       
   150         {
       
   151         //delete iConnListener;
       
   152         //iConnListener = NULL;        
       
   153         //iObserver->HandleError( MMSRPConnectionManagerObserver::EListenerStartError, err , *this );//communicate to server to trigger cleanup
       
   154         
       
   155         /*subsession can propagate to server (based on error), which handles error*/
       
   156         User::Leave(MMSRPConnectionManagerObserver::EListenerStartError); //listenL leaves to subsession
       
   157         }
       
   158     
       
   159     MSRPLOG( "CMSRPConnectionManager::ListenL exit" )
       
   160         
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // CMSRPConnectionManager::ListenCancel
       
   165 //connection listencancel calls mngr cancel if one registered subsession observer or state notlistening
       
   166 // -----------------------------------------------------------------------------
       
   167 //
       
   168 void CMSRPConnectionManager::ListenCancel(MMSRPConnection* aConnection)
       
   169     {
       
   170     MSRPLOG( "CMSRPConnectionManager::ListenCancel enter" )
       
   171     if(iConnListener)
       
   172         {                
       
   173         iConnListener->ListenCancel();       
       
   174         }
       
   175     Remove(aConnection);
       
   176     
       
   177     MSRPLOG( "CMSRPConnectionManager::ListenCancel exit" )    
       
   178     }
       
   179 
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CMSRPConnectionManager::ListenerState
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 void CMSRPConnectionManager::ListenerStateL( TInt aNewState, RSocket* aDataSocket, TInt aStatus )
       
   186     {
       
   187     MSRPLOG( "CMSRPConnectionManager::ListenerStateL enter" )
       
   188     if (aNewState == MMSRPListener::EListenAccepted)
       
   189         {     
       
   190         TInetAddr remoteAddr;        
       
   191         aDataSocket->RemoteName(remoteAddr);
       
   192         
       
   193         MMSRPConnection* connection = CheckConnection( remoteAddr, TRUE );
       
   194         
       
   195         if ( !connection )
       
   196             {
       
   197             iConnListener->ListenCompletedL(FALSE);            
       
   198             }
       
   199         else
       
   200             {
       
   201             iConnListener->ListenCompletedL(TRUE);            
       
   202             TRAPD(err, connection->ConnectionEstablishedL(MMSRPConnection::EConnected, aDataSocket, aStatus ));
       
   203             if(err!=KErrNone)
       
   204                 //non leaving on error, introduce separate fn later
       
   205                 connection->ConnectionEstablishedL(MMSRPConnection::EError, aDataSocket, err );
       
   206             }                
       
   207         }
       
   208     else if (aNewState == MMSRPListener::ETimedOut || aNewState == MMSRPListener::ETerminate)
       
   209         {
       
   210         delete iConnListener;
       
   211         iConnListener = NULL;
       
   212         //all listening connections, auto deleted on listen timeout in conn
       
   213         }
       
   214     else if (aNewState == MMSRPListener::EListenPortError)
       
   215         {
       
   216         delete iConnListener;
       
   217         iConnListener = NULL;
       
   218         User::Leave(MMSRPConnectionManagerObserver::EListenPortFailed);
       
   219         //leave here, propagate upto msrpscheduler, in scheduler error intimate server to handle the error 
       
   220         //instead could intimate observer (server) directly
       
   221         //iObserver->HandleError( MMSRPConnectionManagerObserver::EListenPortFailed, aStatus , *this );
       
   222         }
       
   223     MSRPLOG( "CMSRPConnectionManager::ListenerStateL exit" )
       
   224     }
       
   225 
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CMSRPConnectionManager::CheckConnection
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 MMSRPConnection* CMSRPConnectionManager::CheckConnection( TInetAddr& aHostAddress, TBool aListenOnly )
       
   232 	{
       
   233 	MSRPLOG( "CMSRPConnectionManager::CheckConnection enter" )
       
   234 	for( TInt i = 0; i < iConnectionArray.Count(); i++ )
       
   235 		{
       
   236 		if ( iConnectionArray[ i ]->CheckConnection( aHostAddress, aListenOnly ) )
       
   237 			{
       
   238 			return iConnectionArray[ i ];
       
   239 			}
       
   240 		}
       
   241 	MSRPLOG( "CMSRPConnectionManager::CheckConnection exit" )
       
   242 	return NULL;	
       
   243 	}
       
   244 
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // CMSRPConnectionManager::StartInterfaceL
       
   248 // Activates PDP ctx
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 TInt CMSRPConnectionManager::StartInterface()
       
   252     {
       
   253     
       
   254     MSRPLOG( "CMSRPConnectionManager::StartInterface enter" )
       
   255     
       
   256     TInt status = KErrNone;
       
   257     //if( !iConnection.SubSessionHandle() )
       
   258         {
       
   259         status = iConnection.Open( iSocketServer );
       
   260         }
       
   261 
       
   262 #if defined(__SERIES60_)
       
   263     if (status == KErrNone)
       
   264     {
       
   265         TConnPrefList  prefList;
       
   266         TExtendedConnPref extPrefs;
       
   267         
       
   268         extPrefs.SetIapId( iIapID );
       
   269         TRAP(status, prefList.AppendL(&extPrefs));
       
   270         if (status == KErrNone)
       
   271         {
       
   272         status = iConnection.Start( prefList );
       
   273         }
       
   274     }
       
   275 #else
       
   276     if (status == KErrNone)
       
   277         {
       
   278         TCommDbConnPref prefs;
       
   279         prefs.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
   280         prefs.SetDirection( ECommDbConnectionDirectionUnknown );
       
   281         prefs.SetIapId( iIapID );
       
   282     
       
   283         status = iConnection.Start( prefs );
       
   284         }    
       
   285 #endif        
       
   286                
       
   287     MSRPLOG( "CMSRPConnectionManager::StartInterface exit" )
       
   288     return status;
       
   289          
       
   290     }
       
   291 
       
   292 // -----------------------------------------------------------------------------
       
   293 // CMSRPConnectionManager::ResolveIPAddressL
       
   294 // -----------------------------------------------------------------------------
       
   295 //
       
   296 void CMSRPConnectionManager::ResolveIPAddressL(
       
   297     const TDesC8& aHost,
       
   298     TInetAddr& aAddress )
       
   299     {
       
   300     MSRPLOG( "CMSRPConnectionManager::ResolveIPAddressL enter" )
       
   301     //RHostResolver hostResolver;
       
   302     TNameEntry entry;
       
   303     TBuf< KMaxLengthOfUrl > hostAddr;
       
   304     
       
   305     if( !iHostResolver.SubSessionHandle() )
       
   306         {
       
   307         User::LeaveIfError( iHostResolver.Open( iSocketServer, KAfInet,
       
   308                                           KProtocolInetTcp, iConnection ));
       
   309         }
       
   310 
       
   311     //CleanupClosePushL( hostResolver );
       
   312     
       
   313     hostAddr.Copy( aHost );
       
   314     User::LeaveIfError( iHostResolver.GetByName( hostAddr, entry ) );
       
   315     if ( !TInetAddr::Cast( entry().iAddr ).IsWildAddr() )
       
   316          {
       
   317          aAddress = TInetAddr::Cast( entry().iAddr );
       
   318          }
       
   319     //CleanupStack::PopAndDestroy( );
       
   320     MSRPLOG( "CMSRPConnectionManager::ResolveIPAddressL exit" )
       
   321     }
       
   322 
       
   323 // -----------------------------------------------------------------------------
       
   324 // CMSRPIncomingListener::ResolveLocalIPAddressL
       
   325 // Resolves the local IP address.
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 void CMSRPConnectionManager::ResolveLocalIPAddressL(TInetAddr& aLocalAddr)
       
   329     {
       
   330     MSRPLOG( "CMSRPConnectionManager::ResolveLocalIPAddressL enter" )
       
   331     if (!iLocalAddr.IsUnspecified())
       
   332         {
       
   333         aLocalAddr = iLocalAddr;
       
   334         return;
       
   335         }
       
   336     /* Open an RSocket and find its address */
       
   337     RSocket socket;
       
   338     User::LeaveIfError(socket.Open(iSocketServer, KAfInet, KSockDatagram, KProtocolInetUdp,iConnection));
       
   339     
       
   340     CleanupClosePushL(socket);
       
   341 
       
   342     TSoInetInterfaceInfo networkInfo;
       
   343     TPckg<TSoInetInterfaceInfo> opt(networkInfo);
       
   344     User::LeaveIfError(socket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
       
   345     TSoInetIfQuery query;
       
   346     TPckg<TSoInetIfQuery> queryBuf(query);
       
   347     TInt res = KErrNone;
       
   348     do 
       
   349     {
       
   350         res = socket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, opt);
       
   351         if (!res && opt().iState == EIfUp)
       
   352         {
       
   353             query.iName = opt().iName;
       
   354             res = socket.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, queryBuf);
       
   355             if (!res && query.iZone[1] == iIapID)
       
   356             {
       
   357                 networkInfo = opt();
       
   358                 break;
       
   359             }
       
   360         }
       
   361     }
       
   362     while (res == KErrNone);
       
   363     User::LeaveIfError(res);
       
   364     CleanupStack::PopAndDestroy(1);
       
   365     aLocalAddr = networkInfo.iAddress;
       
   366     iLocalAddr = aLocalAddr;
       
   367     MSRPLOG( "CMSRPConnectionManager::ResolveLocalIPAddressL exit" )
       
   368     }
       
   369 
       
   370 void CMSRPConnectionManager::Remove(MMSRPConnection* aConn)
       
   371     {
       
   372     MSRPLOG( "CMSRPConnectionManager::Remove connection enter" )
       
   373     TInt index = iConnectionArray.Find(aConn);
       
   374     
       
   375     delete iConnectionArray[index];
       
   376     iConnectionArray.Remove(index);       
       
   377     MSRPLOG( "CMSRPConnectionManager::Remove connection exit" )
       
   378     }
       
   379 
       
   380 
       
   381 RSocketServ& CMSRPConnectionManager::SocketServer() 
       
   382     {
       
   383     MSRPLOG( "CMSRPConnectionManager::SocketServer fetch enter" )
       
   384     MSRPLOG( "CMSRPConnectionManager::SocketServer fetch exit" )
       
   385     return iSocketServer;
       
   386    
       
   387     }
       
   388        
       
   389 RConnection& CMSRPConnectionManager::SocketServerConn()
       
   390     {
       
   391     MSRPLOG( "CMSRPConnectionManager::SocketServerConn fetch enter" )
       
   392     MSRPLOG( "CMSRPConnectionManager::SocketServerConn fetch exit" )
       
   393     return iConnection;    
       
   394     }
       
   395        
       
   396 /*
       
   397 TBool CMSRPConnectionManager::MatchIapId(TUint32 aIapID) 
       
   398     {
       
   399     return (aIapID == iIapID);
       
   400     }
       
   401 */
       
   402 
       
   403 
       
   404 // End of File