upnpframework/upnpcommand/src/upnptaskresourceallocator.cpp
changeset 0 7f85d04be362
child 38 5360b7ddc251
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /*
       
     2 * Copyright (c) 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:  A class that allocates resources for a task
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 // upnpframework / avcontroller api
       
    21 #include "upnpavcontrollerfactory.h"    // UPnPAVControllerFactory
       
    22 #include "upnpavcontroller.h"           // MUPnPAVController
       
    23 #include "upnpavdevice.h"               // CUpnpAVDevice
       
    24 #include "upnpavbrowsingsession.h"      // MUPnPAVBrowsingSession
       
    25 
       
    26 // upnpframework / common ui
       
    27 #include "upnpcommonui.h"               // CUPnPCommonUI
       
    28 
       
    29 // command internal
       
    30 #include "upnpnotehandler.h"            // CUpnpNoteHandler
       
    31 #include "upnptaskresourceallocator.h"  // myself
       
    32 
       
    33 _LIT( KComponentLogfile, "upnpcommand.log");
       
    34 #include "upnplog.h"
       
    35 
       
    36 
       
    37 // --------------------------------------------------------------------------
       
    38 // CUpnpTaskResourceAllocator::NewL
       
    39 // --------------------------------------------------------------------------
       
    40 CUpnpTaskResourceAllocator* CUpnpTaskResourceAllocator::NewL(
       
    41     CUPnPCommonUI& aCommonUI,
       
    42     TInt aMode )
       
    43     {
       
    44     CUpnpTaskResourceAllocator* self = new (ELeave)
       
    45         CUpnpTaskResourceAllocator( aCommonUI, aMode );
       
    46 
       
    47     return self;
       
    48     }
       
    49 
       
    50 // --------------------------------------------------------------------------
       
    51 // CUpnpTaskResourceAllocator::CUpnpTaskResourceAllocator
       
    52 // --------------------------------------------------------------------------
       
    53 CUpnpTaskResourceAllocator::CUpnpTaskResourceAllocator(
       
    54     CUPnPCommonUI& aCommonUI,
       
    55     TInt aMode )
       
    56     : CAsyncOneShot( EPriorityStandard )
       
    57     , iCommonUI( aCommonUI )
       
    58     {
       
    59     __LOG( "[UpnpCommand]\t CUpnpTaskResourceAllocator: constructor" );
       
    60 
       
    61     iState = EStateIdle;
       
    62     iErrorCode = KErrNone;
       
    63     iMode = aMode;
       
    64     iLocalMSSCompleted = EFalse;
       
    65 
       
    66     if ( iMode & EResourceLocalMediaServer ||
       
    67          iMode & EResourceSelectDevice )
       
    68         {
       
    69         // if any of these flags are set, AVCONTROLLER flag is mandatory.
       
    70         // assert that.
       
    71         __ASSERTD( iMode & EResourceAvController, __FILE__, __LINE__ );
       
    72         }
       
    73 
       
    74     if ( iMode & EResourceLocalMediaServer )
       
    75         {
       
    76         // if this flag is set, one of the device selection flags is mandatory.
       
    77         __ASSERTD( iMode & EResourceSelectDevice, __FILE__, __LINE__ );
       
    78         }
       
    79 
       
    80     }
       
    81 
       
    82 // --------------------------------------------------------------------------
       
    83 // CUpnpTaskResourceAllocator::~CUpnpTaskResourceAllocator
       
    84 // --------------------------------------------------------------------------
       
    85 CUpnpTaskResourceAllocator::~CUpnpTaskResourceAllocator()
       
    86     {
       
    87     if ( iState == EStateAllocating )
       
    88         {
       
    89         __ASSERTD( iNoteHandler, __FILE__, __LINE__ );
       
    90         
       
    91         SetErrorCode( KErrCancel );
       
    92         if ( iNoteHandler )
       
    93             {
       
    94             iNoteHandler->CloseWaitNote();
       
    95             }
       
    96         }
       
    97 
       
    98     Cleanup();
       
    99     }
       
   100 
       
   101 // --------------------------------------------------------------------------
       
   102 // CUpnpTaskResourceAllocator::Cleanup
       
   103 // --------------------------------------------------------------------------
       
   104 void CUpnpTaskResourceAllocator::Cleanup()
       
   105     {
       
   106     __LOG( "[UpnpCommand]\t CUpnpTaskResourceAllocator::Cleanup" );
       
   107 
       
   108     delete iSelectedDevice;
       
   109     iSelectedDevice = 0;
       
   110     
       
   111     if ( iMediaServerSession )
       
   112         {
       
   113         // Stop local file sharing (release)
       
   114         TRAPD( error, iMediaServerSession->ReleaseLocalMSServicesL() );
       
   115         if( error != KErrNone )
       
   116             {
       
   117             __LOG1( "[UpnpCommand]\t CUpnpTaskResourceAllocator::\
       
   118 ReleaseLocalMSService failed %d", error );
       
   119             }
       
   120         
       
   121         // Stop observing the rendering session
       
   122         iMediaServerSession->RemoveObserver();
       
   123 
       
   124         // Stop session
       
   125         iAVController->StopBrowsingSession( *iMediaServerSession );
       
   126         iMediaServerSession = NULL;
       
   127         }
       
   128 
       
   129     if( iAVController )
       
   130         {
       
   131         iAVController->RemoveDeviceObserver();
       
   132         iAVController->Release(); // Fixes ESLX-7BMJBN
       
   133         iAVController = NULL;
       
   134         }
       
   135 
       
   136     }
       
   137 
       
   138 
       
   139 
       
   140 
       
   141 // --------------------------------------------------------------------------
       
   142 // CUpnpTaskResourceAllocator::AllocateL
       
   143 // --------------------------------------------------------------------------
       
   144 void CUpnpTaskResourceAllocator::AllocateL()
       
   145     {
       
   146     __LOG( "[UpnpCommand]\t CUpnpTaskResourceAllocator::AllocateL" );
       
   147 
       
   148     __ASSERTD( iNoteHandler, __FILE__, __LINE__ );
       
   149     
       
   150     // make CAsyncOneShot to do its trick in parallel
       
   151     Call();
       
   152 
       
   153     // show progress note in sync.
       
   154     iState = EStateAllocating;
       
   155     TInt status = iNoteHandler->RunConnectingWaitNote();
       
   156     SetErrorCode( status );
       
   157     iState = EStateReady;
       
   158 
       
   159     // progress note has exited. Monitor errors from the progress note
       
   160 
       
   161     // in error situation clean up resources and leave
       
   162     if ( iErrorCode != KErrNone )
       
   163         {
       
   164         iState = EStateError;
       
   165         Cleanup();
       
   166         User::Leave( iErrorCode );
       
   167         }
       
   168     }
       
   169 
       
   170 // --------------------------------------------------------------------------
       
   171 // CUpnpTaskResourceAllocator::AVController
       
   172 // --------------------------------------------------------------------------
       
   173 MUPnPAVController& CUpnpTaskResourceAllocator::AVController()
       
   174     {
       
   175     __ASSERT( iState == EStateReady, __FILE__, __LINE__ );
       
   176     __ASSERT( iMode & EResourceAvController, __FILE__, __LINE__ );
       
   177 
       
   178     return *iAVController;
       
   179     }
       
   180 
       
   181 // --------------------------------------------------------------------------
       
   182 // CUpnpTaskResourceAllocator::SelectedDevice
       
   183 // --------------------------------------------------------------------------
       
   184 const CUpnpAVDevice& CUpnpTaskResourceAllocator::SelectedDevice()
       
   185     {
       
   186     __ASSERT( iState == EStateReady, __FILE__, __LINE__ );
       
   187     __ASSERT( iMode & EResourceSelectDevice, __FILE__, __LINE__ );
       
   188     __ASSERT( iSelectedDevice, __FILE__, __LINE__ );
       
   189 
       
   190     return *iSelectedDevice;
       
   191     }
       
   192 
       
   193 
       
   194 // --------------------------------------------------------------------------
       
   195 // CUpnpTaskResourceAllocator::RunL
       
   196 // --------------------------------------------------------------------------
       
   197 void CUpnpTaskResourceAllocator::RunL()
       
   198     {
       
   199     __LOG( "[UpnpCommand]\t CUpnpTaskResourceAllocator::RunL" );
       
   200 
       
   201     // make sure we are still on the go
       
   202     if ( iState == EStateAllocating )
       
   203         {
       
   204         // starting of AVController
       
   205         StartAvControllerL();
       
   206         }
       
   207     else
       
   208         {
       
   209         __LOG1( "[UpnpCommand]\t CUpnpTaskResourceAllocator::RunL \
       
   210     StartAvControllerL not done in state %d", iState );
       
   211         }
       
   212     // make sure we are still on the go
       
   213     if ( iState == EStateAllocating )
       
   214         {
       
   215         // select device
       
   216         SelectDeviceL();
       
   217         }
       
   218     else
       
   219         {
       
   220         __LOG1( "[UpnpCommand]\t CUpnpTaskResourceAllocator::RunL \
       
   221     SelectDeviceL not done in state %d", iState );
       
   222         }
       
   223     // make sure we are still on the go
       
   224     if ( iState == EStateAllocating )
       
   225         {
       
   226         // start the local mediaserver
       
   227         StartLocalMediaServerL();
       
   228         }
       
   229     else
       
   230         {
       
   231         __LOG1( "[UpnpCommand]\t CUpnpTaskResourceAllocator::RunL \
       
   232     StartLocalMediaServerL not done in state %d", iState );
       
   233         }        
       
   234     // make sure we are still on the go
       
   235     if ( iState == EStateAllocating )
       
   236         {
       
   237         // if local media server was started but not yet completed,
       
   238         // wait for callback
       
   239         if ( iMode & EResourceLocalMediaServer
       
   240             && !iLocalMSSCompleted )
       
   241             {
       
   242             __LOG( "[UpnpCommand]\t CUpnpTaskResourceAllocator::RunL \
       
   243     waiting for ReserveLocalMSServicesCompleted" );
       
   244             iState = EStateWaitingForLMS;
       
   245             }
       
   246         // otherwise we are done now
       
   247         else
       
   248             {
       
   249             // close the wait note
       
   250             __ASSERTD( iNoteHandler, __FILE__, __LINE__ );
       
   251             iNoteHandler->CloseWaitNote();
       
   252             }
       
   253         }
       
   254     else
       
   255         {
       
   256         __LOG1( "[UpnpCommand]\t CUpnpTaskResourceAllocator::RunL \
       
   257     CloseWaitNote not done in state %d", iState );
       
   258         }
       
   259     }
       
   260 
       
   261 // --------------------------------------------------------------------------
       
   262 // CUpnpTaskResourceAllocator::RunError
       
   263 // --------------------------------------------------------------------------
       
   264 TInt CUpnpTaskResourceAllocator::RunError( TInt aError )
       
   265     {
       
   266     __LOG1( "[UpnpCommand]\t CUpnpTaskResourceAllocator::RunError: %d",
       
   267         aError );
       
   268 
       
   269     __ASSERTD( iNoteHandler, __FILE__, __LINE__ );
       
   270     
       
   271     // asynchronous operation leaves. store the error code and make the
       
   272     // wait note exit.
       
   273     SetErrorCode( aError );
       
   274     iNoteHandler->CloseWaitNote();
       
   275         
       
   276     return KErrNone;
       
   277     }
       
   278 
       
   279 
       
   280 // --------------------------------------------------------------------------
       
   281 // CUpnpTaskResourceAllocator::StartAvControllerL
       
   282 // --------------------------------------------------------------------------
       
   283 void CUpnpTaskResourceAllocator::StartAvControllerL()
       
   284     {
       
   285     if ( iMode & EResourceAvController )
       
   286         {
       
   287         __LOG( "[UpnpCommand]\t CUpnpTaskResourceAllocator::\
       
   288 StartAvControllerL" );
       
   289 
       
   290         // Create new UpnpAvController client instance
       
   291         iAVController = UPnPAVControllerFactory::NewUPnPAVControllerL();
       
   292 
       
   293         // Register as an observer (for WLAN lost notification) 
       
   294         iAVController->SetDeviceObserver( *this );
       
   295         }
       
   296     else
       
   297         {
       
   298         __LOG( "[UpnpCommand]\t CUpnpTaskResourceAllocator::\
       
   299     AvController not started" );
       
   300         }
       
   301     }
       
   302 
       
   303 // --------------------------------------------------------------------------
       
   304 // CUpnpTaskResourceAllocator::StartLocalMediaServerL
       
   305 // --------------------------------------------------------------------------
       
   306 void CUpnpTaskResourceAllocator::StartLocalMediaServerL()
       
   307     {
       
   308     if ( iMode & EResourceLocalMediaServer )
       
   309         {
       
   310         __LOG( "[UpnpCommand]\t CUpnpTaskResourceAllocator::\
       
   311 StartLocalMediaServerL" );
       
   312 
       
   313         // create a dummy device
       
   314         CUpnpAVDevice* dummyDevice = CUpnpAVDevice::NewLC();
       
   315         dummyDevice->SetUuidL( KNullDesC8 );
       
   316         dummyDevice->SetDeviceType(CUpnpAVDevice::EMediaServer);    
       
   317         // create a session for mediaserver resources keepalive
       
   318         iMediaServerSession =
       
   319             &iAVController->StartBrowsingSessionL( *dummyDevice );
       
   320         iMediaServerSession->SetObserver( *this );
       
   321         CleanupStack::PopAndDestroy( dummyDevice );
       
   322         // now reserve
       
   323         iMediaServerSession->ReserveLocalMSServicesL();
       
   324         }
       
   325     }
       
   326 
       
   327 
       
   328 // --------------------------------------------------------------------------
       
   329 // CUpnpTaskResourceAllocator::SelectDeviceL
       
   330 // --------------------------------------------------------------------------
       
   331 //
       
   332 void CUpnpTaskResourceAllocator::SelectDeviceL()
       
   333     {
       
   334     if ( iMode & EResourceSelectDevice )
       
   335         {
       
   336         __LOG( "[UpnpCommand]\t CUpnpTaskResourceAllocator::SelectDeviceL" );
       
   337 
       
   338         TInt status = KErrNone;
       
   339         
       
   340         // decide device filters and popup title
       
   341         TUPnPDeviceTypesToSearch deviceFilter =
       
   342             EUPnPSearchAllDevices;
       
   343         TUPnPDialogTitle popupTitle = EUPnPSelectDeviceTitle;
       
   344 
       
   345         if ( iMode & EResourceSelectImageRenderer )
       
   346             {
       
   347             deviceFilter = EUPnPSearchRenderingDevicesWithImageCapability;
       
   348             }
       
   349         else if ( iMode & EResourceSelectCopyServer )
       
   350             {
       
   351             deviceFilter = EUPnPSearchServerDevicesWithCopyCapability;
       
   352             popupTitle = EUPnPCopyToTitle;
       
   353             }
       
   354         else if ( iMode & EResourceSelectMoveServer )
       
   355             {
       
   356             deviceFilter = EUPnPSearchServerDevicesWithCopyCapability;
       
   357             popupTitle = EUPnPMoveToTitle;
       
   358             }
       
   359         
       
   360         // Launch the device selection dialog
       
   361         CUpnpAVDevice* tempDevice = CUpnpAVDevice::NewL();
       
   362         CleanupStack::PushL( tempDevice );
       
   363         status = iCommonUI.SelectDeviceL(
       
   364                   *iAVController,
       
   365                   *tempDevice,
       
   366                   deviceFilter,
       
   367                   popupTitle );
       
   368 
       
   369         // Fix UpnpCommonUi return value
       
   370         if( status > 0 )
       
   371             {
       
   372             status = KErrNone;
       
   373             }
       
   374 
       
   375         if( status != KErrNone )
       
   376             {
       
   377             // failed
       
   378             User::Leave( status );
       
   379             }
       
   380 
       
   381         CleanupStack::Pop( tempDevice );
       
   382         iSelectedDevice = tempDevice;
       
   383 
       
   384         }
       
   385     }
       
   386 
       
   387 // --------------------------------------------------------------------------
       
   388 // CUpnpTaskResourceAllocator::SetErrorCode
       
   389 // --------------------------------------------------------------------------
       
   390 void CUpnpTaskResourceAllocator::SetErrorCode( TInt aErrorCode )
       
   391     {
       
   392     __LOG2( "[UpnpCommand]\t CUpnpTaskResourceAllocator::SetErrorCode \
       
   393 %d -> %d", iErrorCode, aErrorCode );
       
   394 
       
   395     if ( iErrorCode == KErrNone )
       
   396         {
       
   397         __LOG( "error code changed");
       
   398         iErrorCode = aErrorCode;
       
   399         } 
       
   400     // if user cancelled operation, we don't need other error codes
       
   401     // so it's ok to reset old error code
       
   402     else if ( aErrorCode == KErrCancel )
       
   403         {
       
   404         __LOG( "error code reseted");
       
   405         iErrorCode = aErrorCode;
       
   406         }
       
   407     }
       
   408 
       
   409 // ==========================================================================
       
   410 // Methods for AVController device observer
       
   411 // ==========================================================================
       
   412     
       
   413     
       
   414 // --------------------------------------------------------------------------
       
   415 // CUpnpTaskResourceAllocator::WLANConnectionLost
       
   416 // --------------------------------------------------------------------------
       
   417 void CUpnpTaskResourceAllocator::WLANConnectionLost()
       
   418     {
       
   419     __LOG( "[UpnpCommand]\t CUpnpTaskResourceAllocator::WLANConnectionLost" );
       
   420 
       
   421     if ( iState == EStateAllocating || iState == EStateWaitingForLMS )
       
   422         {
       
   423         __ASSERTD( iNoteHandler, __FILE__, __LINE__ );
       
   424         SetErrorCode( KErrDisconnected );
       
   425         iNoteHandler->CloseWaitNote();
       
   426         iState = EStateError;
       
   427         }
       
   428     }
       
   429 
       
   430 void CUpnpTaskResourceAllocator::UPnPDeviceDiscovered(
       
   431     const CUpnpAVDevice& /*aDevice*/ )
       
   432     {
       
   433     }
       
   434 
       
   435 void CUpnpTaskResourceAllocator::UPnPDeviceDisappeared(
       
   436     const CUpnpAVDevice& aDevice )
       
   437     {
       
   438     __ASSERTD( iNoteHandler, __FILE__, __LINE__ );
       
   439     if ( ( iState == EStateAllocating || iState == EStateWaitingForLMS )
       
   440          && iSelectedDevice != 0 )
       
   441         {
       
   442         if ( aDevice.Uuid() == iSelectedDevice->Uuid() )
       
   443             {
       
   444             __LOG( "[UpnpCommand]\t selected device lost !" );
       
   445             SetErrorCode( KErrDisconnected );
       
   446             iNoteHandler->CloseWaitNote();
       
   447             iState = EStateError;
       
   448             }
       
   449         }
       
   450     }
       
   451     
       
   452     
       
   453 // ==========================================================================
       
   454 // Methods for AVController browsing session observer
       
   455 // ==========================================================================
       
   456 
       
   457 // --------------------------------------------------------------------------
       
   458 // CUpnpTaskResourceAllocator::ReserveLocalMSServicesCompleted
       
   459 // --------------------------------------------------------------------------
       
   460 void CUpnpTaskResourceAllocator::ReserveLocalMSServicesCompleted( TInt aError )
       
   461     {
       
   462     __LOG1( "[UpnpCommand]\t CUpnpTaskResourceAllocator::\
       
   463 ReserveLocalMSServicesCompleted: %d", aError );
       
   464 
       
   465     if ( iState == EStateAllocating 
       
   466         || iState == EStateWaitingForLMS )
       
   467         {
       
   468         iLocalMSSCompleted = ETrue;
       
   469         SetErrorCode( aError );
       
   470         if ( aError == KErrNone )
       
   471             {
       
   472             iLocalMSStarted = ETrue;
       
   473             iMediaServerSession->RemoveObserver();
       
   474             }
       
   475         // allocation done, just waiting for this callback
       
   476         // close note and we are done
       
   477         if( iState == EStateWaitingForLMS )
       
   478             {
       
   479             iNoteHandler->CloseWaitNote();
       
   480             }
       
   481         }
       
   482     }
       
   483 
       
   484 
       
   485 void CUpnpTaskResourceAllocator::BrowseResponse(
       
   486     const TDesC8& /*aBrowseResponse*/,
       
   487     TInt /*aError*/,
       
   488     TInt /*aMatches*/,
       
   489     TInt /*aTotalCount*/,
       
   490     const TDesC8& /*aUpdateId*/ )
       
   491     {
       
   492     }
       
   493 
       
   494 void CUpnpTaskResourceAllocator::SearchResponse( 
       
   495     const TDesC8& /*aSearchResponse*/,
       
   496     TInt /*aError*/,
       
   497     TInt /*aMatches*/,
       
   498     TInt /*aTotalCount*/,
       
   499     const TDesC8& /*aUpdateId*/ )
       
   500     {
       
   501     }
       
   502 
       
   503 void CUpnpTaskResourceAllocator::SearchCapabilitiesResponse( 
       
   504     TInt /*aError*/,
       
   505     const TDesC8& /*aSearchCapabilities*/ )
       
   506     {
       
   507     }
       
   508 
       
   509 void CUpnpTaskResourceAllocator::CreateContainerResponse(
       
   510     TInt /*aError*/, 
       
   511     const TDesC8& /*aObjectId*/ )
       
   512     {
       
   513     }
       
   514 
       
   515 void CUpnpTaskResourceAllocator::DeleteObjectResponse(
       
   516     TInt /*aError*/ )
       
   517     {
       
   518     }
       
   519  
       
   520 void CUpnpTaskResourceAllocator::MediaServerDisappeared(
       
   521     TUPnPDeviceDisconnectedReason /*aReason*/ )
       
   522     {
       
   523     }
       
   524 
       
   525 
       
   526 
       
   527 // --------------------------------------------------------------------------
       
   528 // CUpnpTaskResourceAllocator::SetNoteHandlerL
       
   529 // Sets the pointer to the note handler.
       
   530 // --------------------------------------------------------------------------
       
   531 void CUpnpTaskResourceAllocator::SetNoteHandlerL( 
       
   532                                         CUpnpNoteHandler* aNoteHandler )
       
   533     {
       
   534     // Check parameter
       
   535     if( !aNoteHandler )
       
   536         {
       
   537         User::Leave( KErrArgument );
       
   538         }
       
   539 
       
   540     iNoteHandler = aNoteHandler;
       
   541     }