upnpavcontrolpoint/avcpengine/src/upnpavcpmanager.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
parent 39 6369bfd1b60d
child 41 b4d83ea1d6e2
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
     1 /** @file
       
     2 * Copyright (c) 2005-2006 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:  CUpnpAVCPManager
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include "upnpavcpmanager.h"
       
    22 #include "upnpavcpenginehelper.h"
       
    23 #include "upnppathresolver.h"
       
    24 #include "upnpresponsehandler.h"
       
    25 #include "upnpmdebug.h"
       
    26 #include "upnpavcpenginecommon.h"
       
    27 
       
    28 
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CUpnpAVCPManager::NewL
       
    32 // Creates an instance of the implementation.
       
    33 // -----------------------------------------------------------------------------
       
    34 CUpnpAVCPManager* CUpnpAVCPManager::NewL()
       
    35 	{ 
       
    36 	CUpnpAVCPManager* self = new (ELeave) CUpnpAVCPManager(NULL);
       
    37 	CleanupStack::PushL( self );
       
    38 	self->ConstructL();
       
    39 	CleanupStack::Pop( self );
       
    40 	return self;
       
    41 	}
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CUpnpAVCPManager::CUpnpAVCPManager
       
    45 // First phase construction.
       
    46 // -----------------------------------------------------------------------------
       
    47 CUpnpAVCPManager::CUpnpAVCPManager(MUpnpAVControlPointObserver* aObserver):CUpnpAVControlPoint((MUpnpAVControlPointObserver&)aObserver)
       
    48 	{
       
    49     
       
    50 	}
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CUpnpAVCPManager::ConstructL
       
    54 // Second phase construction.
       
    55 // -----------------------------------------------------------------------------
       
    56 void CUpnpAVCPManager::ConstructL()
       
    57 	{	    
       
    58     
       
    59     _LIT8( KMediaServer, "MediaServer" );
       
    60 	//CUpnpControlPoint::ConstructL( KMediaServer, value );	
       
    61 	CUpnpControlPoint::ConstructL( KMediaServer );	
       
    62 	InitializeCdsActionFactoryL();
       
    63         
       
    64 	// maybe we should look for something different??
       
    65 	TPtrC8 devicePtr;
       
    66     devicePtr.Set( UpnpSSDP::KSearchTargetAll );
       
    67     //SSDP search for mediaserver UPnP devices
       
    68 	DEBUGSTRING(("Search for devices"));    
       
    69 	SearchL( devicePtr );
       
    70 	}
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // Destructor.
       
    74 // -----------------------------------------------------------------------------
       
    75 CUpnpAVCPManager::~CUpnpAVCPManager()
       
    76 	{
       
    77 	iPendingHandlers.Reset();
       
    78 	iHttpPendingHandlers.Reset();
       
    79 	iPathResolvers.ResetAndDestroy();    
       
    80 	}
       
    81 	
       
    82 // -----------------------------------------------------------------------------
       
    83 // CUpnpAVCPManager::NewCommandId
       
    84 // return new command id
       
    85 // -----------------------------------------------------------------------------	
       
    86 TInt CUpnpAVCPManager::NewCommandId() 
       
    87 	{
       
    88 	return ++iLastCommandId;
       
    89 	}	
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CUpnpAVCPManager::PathResolver
       
    93 // return resolver or creates a new one
       
    94 // -----------------------------------------------------------------------------	
       
    95 CUpnpPathResolver& CUpnpAVCPManager::PathResolverL(const TDesC8& aUUID, CUpnpAVCPEngineSession* aSession) 
       
    96     {
       
    97 	    
       
    98     CUpnpPathResolver* resolver = CUpnpPathResolver::NewL(*this,aUUID);
       
    99     CleanupStack::PushL(resolver);
       
   100 
       
   101     TIdentityRelation<CUpnpPathResolver> matcher( CUpnpPathResolver::Match );
       
   102 	TInt idx = iPathResolvers.Find( resolver, matcher ); 
       
   103 	
       
   104 		
       
   105 	if (idx != KErrNotFound)   
       
   106     	{
       
   107 	    CleanupStack::PopAndDestroy(resolver);	
       
   108 	    resolver = iPathResolvers[idx];
       
   109     	}
       
   110 	else 
       
   111     	{	    
       
   112 	    //subscribe
       
   113 	    CUpnpDevice* device = const_cast<CUpnpDevice*>(Device(aUUID));
       
   114 	    if (device) 
       
   115 	    	{
       
   116 	        CUpnpService* service  = Service(device, UpnpAVCPEngine::KContentDirectory);
       
   117 			DEBUGSTRING8(("Subscribe for events"));	        
       
   118 	        SubscribeL( service );
       
   119 	    	} 
       
   120 	    else 
       
   121 	    	{
       
   122 	    	User::Leave(KErrNotFound);
       
   123 	    	}
       
   124 	    CleanupStack::Pop(resolver);	
       
   125 	    iPathResolvers.AppendL(resolver);	    
       
   126     	}  
       
   127     	
       
   128     resolver->RegisterSession(aSession);	
       
   129     return *resolver;	
       
   130     }
       
   131     
       
   132 // -----------------------------------------------------------------------------
       
   133 // CUpnpAVCPManager::CleanupSession
       
   134 // cleanup after closed client session
       
   135 // -----------------------------------------------------------------------------		
       
   136 void CUpnpAVCPManager::CleanupSessionL( CUpnpAVCPEngineSession* aSession) 
       
   137 	{
       
   138 	DEBUGSTRING8(("CleanupSessionL"));	        	
       
   139     for (TInt i = 0; i < iPathResolvers.Count(); i++ ) 
       
   140     {
       
   141         if (iPathResolvers[i]->UnregisterSession(aSession) == 0 ) 
       
   142         {
       
   143             //unsubscribe
       
   144             CUpnpDevice* device = const_cast<CUpnpDevice*>(Device(iPathResolvers[i]->UUID()));
       
   145 	        if (device) 
       
   146 	        {
       
   147 	            CUpnpService* service  = Service(device, UpnpAVCPEngine::KContentDirectory);
       
   148 	            DEBUGSTRING8(("UnsubscribeL for events"));
       
   149 	            UnsubscribeL( service );
       
   150 	        }
       
   151 	    
       
   152             //remove
       
   153             delete iPathResolvers[i];
       
   154             iPathResolvers.Remove(i);
       
   155             i--;
       
   156         }
       
   157     }    
       
   158 	}
       
   159 	
       
   160 // -----------------------------------------------------------------------------
       
   161 // CUpnpAVCPManager::RegisterForAction
       
   162 // register for get action responses
       
   163 // -----------------------------------------------------------------------------			
       
   164 void CUpnpAVCPManager::RegisterForAction(MUpnpResponseHandler& aHandler)	 
       
   165   {
       
   166       iPendingHandlers.AppendL(&aHandler);
       
   167   }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CUpnpAVCPManager::HandlerBySessionId
       
   171 // return index of handler for specific session
       
   172 // -----------------------------------------------------------------------------	
       
   173 TInt CUpnpAVCPManager::HandlerBySessionId(TInt aSessionId) 
       
   174     {
       
   175     TInt i = KErrNotFound;
       
   176 
       
   177     for ( i=0; i < iPendingHandlers.Count() && iPendingHandlers[i]->SessionId() != aSessionId; i++ )
       
   178     {}
       
   179 
       
   180     if ( i != iPendingHandlers.Count() )
       
   181         {
       
   182         return i;
       
   183         }
       
   184     return KErrNotFound;   
       
   185     }
       
   186 // -----------------------------------------------------------------------------
       
   187 // CUpnpAVCPManager::HandlerBySessionId
       
   188 // return index of handler for specific session
       
   189 // -----------------------------------------------------------------------------	
       
   190 void CUpnpAVCPManager::RegisterForHttp(MUpnpHttpResponseHandler& aHandler)	 
       
   191     {
       
   192     iHttpPendingHandlers.AppendL(&aHandler);
       
   193     }
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CUpnpAVCPManager::HandlerBySessionId
       
   197 // return index of handler for specific session
       
   198 // -----------------------------------------------------------------------------	    
       
   199 void CUpnpAVCPManager::UnregisterForHttp(MUpnpHttpResponseHandler& aHandler)	 
       
   200     {
       
   201     TInt index = iHttpPendingHandlers.Find(&aHandler);
       
   202     if (index != KErrNotFound) 
       
   203         {
       
   204         iHttpPendingHandlers.Remove(index);    
       
   205         }        
       
   206     }    
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // CUpnpAVCPManager::HandlerBySessionId
       
   210 // return index of handler for specific session
       
   211 // -----------------------------------------------------------------------------	
       
   212 TInt CUpnpAVCPManager::HttpHandlerBySessionId(TInt aSessionId) 
       
   213     {
       
   214     TInt i = KErrNotFound;
       
   215 
       
   216     for ( i=0; i < iHttpPendingHandlers.Count() && iHttpPendingHandlers[i]->SessionId() != aSessionId; i++ )
       
   217     {}
       
   218 
       
   219     if ( i != iHttpPendingHandlers.Count() )
       
   220         {
       
   221             return i;
       
   222         }
       
   223     return KErrNotFound;   
       
   224     }
       
   225 
       
   226 // -----------------------------------------------------------------------------
       
   227 // CUpnpAVCPManager::HandlerBySessionId
       
   228 // return index of handler for specific session
       
   229 // -----------------------------------------------------------------------------	
       
   230 void CUpnpAVCPManager::ActionResponseReceivedL(CUpnpAction* aAction) 
       
   231     {
       
   232     TInt sessionId = aAction->SessionId();	
       
   233     TInt index = HandlerBySessionId(sessionId);
       
   234     if (index != KErrNotFound) 
       
   235         {
       
   236         MUpnpResponseHandler* handler = iPendingHandlers[index];
       
   237         iPendingHandlers.Remove(index);        
       
   238         handler->InterpretL(aAction->Error(), aAction);    
       
   239         }
       
   240     
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // CUpnpAVCPManager::StateUpdatedL
       
   245 //  Handles UPnP device events.
       
   246 // -----------------------------------------------------------------------------	
       
   247 void CUpnpAVCPManager::StateUpdatedL(CUpnpService*  aService) 
       
   248     {
       
   249     CUpnpPathResolver* resolver = CUpnpPathResolver::NewL(*this,aService->Device().Uuid());
       
   250     CleanupStack::PushL(resolver);
       
   251 
       
   252     TIdentityRelation<CUpnpPathResolver> matcher( CUpnpPathResolver::Match );
       
   253 	TInt idx = iPathResolvers.Find( resolver, matcher ); 
       
   254 	CleanupStack::PopAndDestroy(resolver);	    	    	
       
   255 	
       
   256 	if (idx != KErrNotFound)   
       
   257     	{	   
       
   258 	    CUpnpStateVariable* systemUpdateId = aService->StateVariable( UpnpAVCPEngine::KSystemUpdateID );
       
   259         if (systemUpdateId) 
       
   260             {
       
   261             (iPathResolvers[idx])->StateChangedL(systemUpdateId->Value()); 
       
   262             }
       
   263     	     
       
   264     	}
       
   265     }
       
   266 
       
   267 // --------------------------------------------------------------------------
       
   268 // CUpnpAVCPManager::HttpResponseReceivedL
       
   269 // Handles HTTP messages.
       
   270 // --------------------------------------------------------------------------
       
   271 void CUpnpAVCPManager::HttpResponseReceivedL(CUpnpHttpMessage* aMessage)
       
   272     {
       
   273     TInt sessionId = aMessage->SessionId();    
       
   274 	
       
   275     TInt index = HttpHandlerBySessionId(sessionId);
       
   276     if (index != KErrNotFound) 
       
   277         {
       
   278         MUpnpHttpResponseHandler* handler = iHttpPendingHandlers[index];        
       
   279         handler->InterpretL(aMessage->Error(), aMessage);    
       
   280         } 
       
   281     else 
       
   282     	{
       
   283     	index = index;
       
   284        	}   
       
   285     }
       
   286     
       
   287 // --------------------------------------------------------------------------
       
   288 // CUpnpAVCPManager::DeviceDiscoveredL
       
   289 // Handles UPnP device discoveries.
       
   290 // --------------------------------------------------------------------------
       
   291 void CUpnpAVCPManager::DeviceDiscoveredL(CUpnpDevice* aDevice)
       
   292     {
       
   293 	TPtrC8 uuid = aDevice->Uuid();    
       
   294 	TPtrC8 fn = aDevice->DescriptionProperty(KAVCPEngineFriendlyName);    	
       
   295 	DEBUGSTRING8(("DeviceDiscoveredL %S %S", &uuid, &fn));     
       
   296     }
       
   297 
       
   298 // --------------------------------------------------------------------------
       
   299 // CUpnpAVCPManager::DeviceDisappearedL(
       
   300 // Handles UPnP device disappears.
       
   301 // --------------------------------------------------------------------------
       
   302 void CUpnpAVCPManager::DeviceDisappearedL(CUpnpDevice* aDevice)
       
   303     {
       
   304 	TPtrC8 uuid = aDevice->Uuid();
       
   305 	DEBUGSTRING8(("DeviceDiscoveredL %S", &uuid));     
       
   306 
       
   307     CUpnpPathResolver* resolver = CUpnpPathResolver::NewL(*this,aDevice->Uuid());
       
   308     CleanupStack::PushL(resolver);
       
   309 
       
   310     TIdentityRelation<CUpnpPathResolver> matcher( CUpnpPathResolver::Match );
       
   311 	TInt idx = iPathResolvers.Find( resolver, matcher ); 
       
   312 	CleanupStack::PopAndDestroy(resolver);	    	    	
       
   313 	
       
   314 	if (idx != KErrNotFound)   
       
   315 		{    	    
       
   316 		   delete iPathResolvers[idx];   
       
   317 		   iPathResolvers.Remove(idx);
       
   318 		}	        
       
   319     }
       
   320     
       
   321 // end of file
       
   322 
       
   323 
       
   324