upnpharvester/mdhserver/src/server/mdhmediaservercontainer.cpp
changeset 0 7f85d04be362
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:      Metadata Harvester server's mediaserver container component
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 // INCLUDE FILES
       
    25 #include    <e32property.h>
       
    26 #include    <f32file.h>
       
    27 
       
    28 #include    "cmsettingsfactory.h"
       
    29 #include    "cmsettings.h"
       
    30 #include    "cmdmmain.h"
       
    31 #include    "cmmediaserverfull.h"
       
    32 #include    "cmsearchresponsehash.h"
       
    33 #include    "cmsettings.h"
       
    34 #include    <upnpstring.h>
       
    35 #include    <upnperrors.h>
       
    36 #include    "mdhconnectionmonitor.h"
       
    37 #include    "mdhmediaservercontainer.h"
       
    38 #include    "mdhmetadatacollector.h"
       
    39 #include    "mdhserver.h"
       
    40 #include    "msdebug.h"
       
    41 
       
    42 
       
    43 // CONSTANTS
       
    44 #ifdef __SERIES60_31__
       
    45 _LIT8( KAVControlPointFriendlyName,  "AVDevice" );
       
    46 #endif //__SERIES60_31__
       
    47 _LIT8( KMediaServer,                 "MediaServer" );
       
    48 #ifdef _DEBUG
       
    49 _LIT8( KFriendlyNameTag,             "friendlyName" );
       
    50 #endif
       
    51 
       
    52 const TInt KDefaultCaculateSize = 100;
       
    53 const TInt KDefaultChunkSize = 150;
       
    54 const TInt KDefaultAddGranularity = 500;
       
    55 
       
    56 // amount of microseconds in one second
       
    57 const TInt KMicrosecondsInSecond = 1000000;
       
    58 // ---------------------------------------------------------------------------
       
    59 // Two-phase API constructor
       
    60 // ---------------------------------------------------------------------------
       
    61 // 
       
    62 CCmMdhMediaserverInfo* CCmMdhMediaserverInfo::NewL( )
       
    63     {  
       
    64     CCmMdhMediaserverInfo* self = CCmMdhMediaserverInfo::NewLC( );
       
    65     CleanupStack::Pop( self );
       
    66     return self;
       
    67     }
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // Two-phase API constructor
       
    71 // ---------------------------------------------------------------------------
       
    72 //     
       
    73 CCmMdhMediaserverInfo* CCmMdhMediaserverInfo::NewLC()
       
    74     {   
       
    75     CCmMdhMediaserverInfo* self = new ( ELeave ) CCmMdhMediaserverInfo( );
       
    76     CleanupStack::PushL( self );
       
    77     self->ConstructL( );    
       
    78     return self;  
       
    79     }    
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // Destructor
       
    83 // ---------------------------------------------------------------------------
       
    84 // 
       
    85 CCmMdhMediaserverInfo::~CCmMdhMediaserverInfo()
       
    86     {
       
    87     delete iUuid;
       
    88     }
       
    89     
       
    90 // ---------------------------------------------------------------------------
       
    91 // C++ default constructor
       
    92 // ---------------------------------------------------------------------------
       
    93 //            
       
    94 CCmMdhMediaserverInfo::CCmMdhMediaserverInfo( )
       
    95     {
       
    96     }
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // ConstructL
       
   100 // ---------------------------------------------------------------------------
       
   101 //  
       
   102 void CCmMdhMediaserverInfo::ConstructL( )
       
   103     {
       
   104                                   
       
   105     } 
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // Two-phased constructor.
       
   109 // ---------------------------------------------------------------------------
       
   110 CCmMdhMediaserverContainer* CCmMdhMediaserverContainer::NewL(
       
   111     CCmMdhServer& aMdhServer)
       
   112     {
       
   113     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::NewL"));
       
   114 
       
   115     CCmMdhMediaserverContainer* self =
       
   116         new (ELeave) CCmMdhMediaserverContainer(aMdhServer);
       
   117     CleanupStack::PushL( self );
       
   118     self->ConstructL();
       
   119     CleanupStack::Pop( self );
       
   120     return self;
       
   121     }
       
   122 
       
   123 // ---------------------------------------------------------------------------
       
   124 // Constructor.
       
   125 // ---------------------------------------------------------------------------
       
   126 CCmMdhMediaserverContainer::CCmMdhMediaserverContainer(
       
   127     CCmMdhServer& aMdhServer)
       
   128     :iMdhServer( aMdhServer )
       
   129     {
       
   130     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   131                              CCmMdhMediaserverContainer"));
       
   132     iAvControlPoint = NULL;
       
   133     iMetadataCollector = NULL;
       
   134     iTimer = NULL;
       
   135     iMediaserverIndex = 0;
       
   136     iResponsesPending = 0;
       
   137     iTotalItemCount = 0;
       
   138     iHarvestErrCode = KErrNone;
       
   139     iHarvestActive = EFalse;
       
   140     iPropertyItemIndex = 0;
       
   141     iProgressInfo.iService = ECmServiceHarvest;
       
   142     }
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // 2nd phase constructor.
       
   146 // ---------------------------------------------------------------------------
       
   147 void CCmMdhMediaserverContainer::ConstructL()
       
   148     {
       
   149     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::ConstructL"));
       
   150     iDbManager = CCmDmMain::NewL();
       
   151     iSettings = CCmSettingsFactory::NewCmSettingsEngineL();
       
   152     iSettings->GetIapL( iIap );
       
   153     TRACE( Print( _L("[CmMdh Server]\t Monitoring iap %d"), iIap ));
       
   154     iMdHConMon = CMdHConnectionMonitor::NewL( *this, iIap );
       
   155     }
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // Destructor.
       
   159 // ---------------------------------------------------------------------------
       
   160 CCmMdhMediaserverContainer::~CCmMdhMediaserverContainer()
       
   161     {
       
   162     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   163                              ~CCmMdhMediaserverContainer"));
       
   164 
       
   165     TRACE( Print( _L("[CmMdh Server]\t deleting %d media servers"),
       
   166         iMediaservers.Count()));
       
   167 
       
   168     iMediaservers.ResetAndDestroy();
       
   169     iOldMediaServers.ResetAndDestroy();
       
   170 
       
   171     LOG(_L("[CmMdh Server]\t deleting settings engine.."));
       
   172 	if ( iSettings )
       
   173 	    {
       
   174 	    iSettings->Close();    
       
   175 	    }
       
   176 
       
   177     LOG(_L("[CmMdh Server]\t deleting db manager.."));
       
   178     if ( iDbManager )
       
   179         {
       
   180         delete iDbManager;    
       
   181         }    
       
   182 
       
   183     LOG(_L("[CmMdh Server]\t deleting metadata collector.."));
       
   184     if ( iMetadataCollector )
       
   185         {
       
   186         delete iMetadataCollector;
       
   187         }
       
   188 
       
   189     LOG(_L("[CmMdh Server]\t deleting av control point.."));
       
   190     if ( iAvControlPoint )
       
   191         {
       
   192         delete iAvControlPoint;    
       
   193         }
       
   194 
       
   195     LOG(_L("[CmMdh Server]\t deleting timer.."));
       
   196     if ( iTimer )
       
   197         {
       
   198         delete iTimer;    
       
   199         }
       
   200     
       
   201     LOG(_L("[CmMdh Server]\t deleting connection monitor.."));
       
   202     if ( iMdHConMon )
       
   203         {
       
   204         delete iMdHConMon;    
       
   205         }
       
   206     
       
   207     LOG(_L("[CmMdh Server]\t All deleted.."));
       
   208     }
       
   209         
       
   210 // ---------------------------------------------------------------------------
       
   211 // Harvest a media server
       
   212 // ---------------------------------------------------------------------------
       
   213 void CCmMdhMediaserverContainer::DoHarvestMediaserverL(
       
   214     TInt aMediaserverIndex )
       
   215     {
       
   216     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   217                              DoHarvestMediaserverL"));
       
   218     iMdhServer.SetServerStateL(ECmMdhServerStateHarvesting);
       
   219     if ( iMediaservers[aMediaserverIndex]->iMediaserverState
       
   220         == ECmMdhMediaserverNotSynchronized)
       
   221         {
       
   222         iMetadataCollector->HarvestMediaserverL(
       
   223             *iMediaservers[aMediaserverIndex]->iUuid );
       
   224         iHarvestActive = ETrue;
       
   225         }
       
   226     else
       
   227         {
       
   228         LOG(_L("[CmMdh Server]\t No search caps or \
       
   229                                  no sync needed, skipping.."));
       
   230         HarvestCompleteL( KErrNone );
       
   231         }
       
   232     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   233                              DoHarvestMediaserverL end"));
       
   234     }
       
   235 
       
   236 // ---------------------------------------------------------------------------
       
   237 // Adds a media server to database if it doesn't exist there
       
   238 // ---------------------------------------------------------------------------
       
   239 TBool CCmMdhMediaserverContainer::AddToDbIfNewL( CUpnpDevice& aMediaserver )
       
   240     {
       
   241     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::AddToDbIfNewL"));
       
   242 
       
   243     CCmMediaServerFull* tempServer = CCmMediaServerFull::NewLC();
       
   244     tempServer->SetUDNL( aMediaserver.Uuid() );
       
   245     iDbManager->PrepareQueryCmdL( EMediaServerInfoQyery );
       
   246     TInt errCode = iDbManager->QueryMediaServerL( tempServer );
       
   247     TBool ret = EFalse;
       
   248     
       
   249     if ( errCode != KErrNotFound )
       
   250         {
       
   251         TRACE( Print( _L("[CmMdh Server]\t FillUsage: %d"),
       
   252         		tempServer->FillUsage()));
       
   253         ret = (TBool)tempServer->FillUsage();
       
   254         iAllFound = ETrue;
       
   255         for( TInt i = 0 ; i < iOldMediaServers.Count(); i++ )
       
   256             {
       
   257             if( KErrNotFound != aMediaserver.Uuid().Match(
       
   258                 iOldMediaServers[i]->MediaServer() ) )
       
   259                 {
       
   260                 TRACE( Print( _L("[CmMdh Server]\t In db...")));
       
   261                 iOldMediaServers[i]->SetFillUsage( EFalse );
       
   262                 }
       
   263             if( iOldMediaServers[i]->FillUsage())
       
   264                 {
       
   265                 TRACE( Print( _L("[CmMdh Server]\t All not found")));
       
   266                 iAllFound = EFalse;
       
   267                 }
       
   268             }
       
   269         }
       
   270     CleanupStack::PopAndDestroy( tempServer );
       
   271     return ret;
       
   272     }
       
   273 
       
   274 // ---------------------------------------------------------------------------
       
   275 // Decreases pending responses counter and finishes search if ready
       
   276 // ---------------------------------------------------------------------------
       
   277 void CCmMdhMediaserverContainer::DecResponsesAndFinishIfReadyL()
       
   278     {
       
   279     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   280                              DecResponsesAndFinishIfReadyL"));
       
   281     if ( !(--iResponsesPending )  )
       
   282         {
       
   283         if( iAllFound )
       
   284 	        {
       
   285 	        DoSearchTimerCompletedL();	
       
   286 	        }
       
   287         }
       
   288     }
       
   289 
       
   290 // ---------------------------------------------------------------------------
       
   291 // Static timer callback function.
       
   292 // ---------------------------------------------------------------------------
       
   293 TInt CCmMdhMediaserverContainer::SearchTimerCompletedL( TAny* aInstance )
       
   294     {
       
   295     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   296                              SearchTimerCompleted"));
       
   297     return ((CCmMdhMediaserverContainer*)aInstance)
       
   298         ->DoSearchTimerCompletedL();
       
   299    }
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 // Timer callback function.
       
   303 // ---------------------------------------------------------------------------
       
   304 TInt CCmMdhMediaserverContainer::DoSearchTimerCompletedL()
       
   305     {
       
   306     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   307         DoSearchTimerCompleted"));
       
   308     delete iTimer;
       
   309     iTimer = NULL;
       
   310     TRACE( Print( _L("[CmMdh Server]\t Found %d media servers"),
       
   311         iMediaservers.Count()));
       
   312 
       
   313     if ( !iResponsesPending )
       
   314         {
       
   315         iOldMediaServers.ResetAndDestroy();
       
   316         iMdhServer.SetServerStateL(ECmMdhServerStateIdle);
       
   317         }
       
   318     return KErrNone;
       
   319    }
       
   320 
       
   321 // ---------------------------------------------------------------------------
       
   322 // Handles UPnP device discoveries.
       
   323 // ---------------------------------------------------------------------------
       
   324 void CCmMdhMediaserverContainer::DeviceDiscoveredL( CUpnpDevice* aDevice )
       
   325     {
       
   326     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::DeviceDiscoveredL"));
       
   327 
       
   328 #ifdef _DEBUG
       
   329 
       
   330     HBufC* uuid = UpnpString::ToUnicodeL( aDevice->Uuid() );
       
   331     HBufC* name = UpnpString::ToUnicodeL(
       
   332                     aDevice->DescriptionProperty( KFriendlyNameTag() ));
       
   333     HBufC* type = UpnpString::ToUnicodeL( aDevice->DeviceType() );
       
   334     TRACE( Print(_L("[CmMdh Server]\t type: %S uuid: %S, name %S"),
       
   335         type, uuid, name ));
       
   336     delete uuid;
       
   337     delete name;
       
   338     delete type;
       
   339 
       
   340 #endif
       
   341 
       
   342 
       
   343     if (!iTimer) // search is no more active
       
   344         {
       
   345         LOG(_L("[CmMdh Server]\t Search no more active, returning.."));
       
   346         return;
       
   347         }
       
   348 
       
   349     if( aDevice->DeviceType().Find(KMediaServer) != KErrNotFound && 
       
   350         !iAllFound )
       
   351         {
       
   352         LOG(_L("[CmMdh Server]\t Found a media server"));
       
   353 
       
   354         if ( AddToDbIfNewL( *aDevice ) )
       
   355             {
       
   356             ++iResponsesPending;
       
   357             iAvControlPoint->CdsSearchCapabilitiesActionL( aDevice->Uuid() );
       
   358             }
       
   359 #ifdef _DEBUG
       
   360 
       
   361         else
       
   362             {
       
   363             LOG(_L("[CmMdh Server]\t not active, skipping.."));
       
   364             }
       
   365 #endif
       
   366 
       
   367         }
       
   368 
       
   369     }
       
   370 
       
   371 // ---------------------------------------------------------------------------
       
   372 // Handles UPnP device disappears.
       
   373 // ---------------------------------------------------------------------------
       
   374 void CCmMdhMediaserverContainer::DeviceDisappearedL(
       
   375     CUpnpDevice* /* aDevice */ )
       
   376     {
       
   377     LOG( _L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   378              DeviceDisappearedL") );
       
   379     }
       
   380 
       
   381 // ---------------------------------------------------------------------------
       
   382 // Searches UPnP Media servers.
       
   383 // ---------------------------------------------------------------------------
       
   384 void CCmMdhMediaserverContainer::SearchMediaserversL()
       
   385     {
       
   386     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   387                              SearchMediaServersL"));
       
   388 
       
   389     iResponsesPending = 0;
       
   390     iProgressInfo.iTotalItems = 0;
       
   391     iProgressInfo.iProcessedItems = 0;
       
   392 
       
   393     if ( !iAvControlPoint )
       
   394         {
       
   395         iSettings->GetIapL( iIap );
       
   396         // Create and start (automatic) the AV control point
       
   397         TRACE( Print( _L("[CmMdh Server]\t IAP to use is  %d"), iIap));
       
   398         LOG( _L("[CmMdh Server]\t Creating AV control point..." ) );
       
   399 
       
   400         iAvControlPoint = CUpnpAVControlPoint::NewL( *this );
       
   401         }
       
   402         
       
   403     LOG( _L("[CmMdh Server]\t Creating CPeriodic timer..." ) );
       
   404     iTimer = CPeriodic::NewL(EPriorityHigh);
       
   405 
       
   406     TInt deviceDiscoveryDelay;
       
   407     iSettings->GetDiscoveryDelay( deviceDiscoveryDelay );
       
   408     TRACE(Print(_L("[CmMdh Server]\t using device discovery delay %d\n"),
       
   409         deviceDiscoveryDelay));
       
   410 
       
   411     iTimer->Start(deviceDiscoveryDelay * KMicrosecondsInSecond,
       
   412                   0,
       
   413                   TCallBack(SearchTimerCompletedL, this));
       
   414 
       
   415     iMdhServer.SetServerStateL(ECmMdhServerStateSearching);
       
   416 
       
   417     iOldMediaServers.ResetAndDestroy();
       
   418     iDbManager->GetMediaServersL( iOldMediaServers );
       
   419     
       
   420     for ( TInt i = 0; i < iOldMediaServers.Count(); i++ )
       
   421         {
       
   422         if ( !iOldMediaServers[i]->FillUsage() &&
       
   423              iOldMediaServers[i]->SystemUpdateID() != KErrNotFound )
       
   424             {
       
   425             iOldMediaServers[i]->SetSystemUpdateID( KErrNotFound );
       
   426             iDbManager->UpdateMediaServerInfo( iOldMediaServers[i] );
       
   427             }
       
   428         }
       
   429     
       
   430     iAllFound = EFalse;
       
   431     SendProgressInfo ( 0 );
       
   432     }
       
   433 
       
   434 // ---------------------------------------------------------------------------
       
   435 // Harvests media servers
       
   436 // ---------------------------------------------------------------------------
       
   437 void CCmMdhMediaserverContainer::HarvestMediaserversL()
       
   438     {
       
   439     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   440                              HarvestMediaserversL"));
       
   441 
       
   442     if ( !iAvControlPoint )
       
   443         {
       
   444         User::Leave( KErrNotReady );
       
   445         }
       
   446 
       
   447     if ( !iMediaservers.Count() )
       
   448         {
       
   449         User::Leave( KErrNotFound );
       
   450         }
       
   451 
       
   452     TBool harvestNeeded = EFalse;
       
   453     for ( TInt i = 0; i < iMediaservers.Count(); i++ )
       
   454         {
       
   455         if (iMediaservers[i]->iMediaserverState
       
   456             == ECmMdhMediaserverNotSynchronized )
       
   457             {
       
   458             harvestNeeded = ETrue;
       
   459             i = iMediaservers.Count(); // break from the loop
       
   460             }
       
   461         }
       
   462 
       
   463     if ( !harvestNeeded )
       
   464         {
       
   465         User::Leave( KErrAlreadyExists );
       
   466         }
       
   467 
       
   468     if ( !iMetadataCollector )
       
   469         {
       
   470         LOG( _L("[CmMdh Server]\t Creating metadata collector.." ) );
       
   471 
       
   472         TInt searchCount = KDefaultChunkSize;
       
   473         TInt addGranularity = KDefaultAddGranularity;
       
   474         iSettings->GetSearchCount( searchCount );
       
   475         iSettings->GetAddCount( addGranularity );
       
   476         TRACE( Print(
       
   477             _L("[CmMdh Server]\t using chunk size %d and addgran %d"),
       
   478             searchCount, addGranularity));
       
   479         iMetadataCollector =
       
   480             CCmMdhMetadataCollector::NewL(
       
   481                 *this, *iAvControlPoint, searchCount, addGranularity );
       
   482 
       
   483         }
       
   484     iMediaserverIndex = 0;
       
   485     iHarvestErrCode = KErrNone;
       
   486     DoHarvestMediaserverL( iMediaserverIndex );
       
   487 
       
   488     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   489                              HarvestMediaserversL end"));
       
   490 
       
   491     }
       
   492 
       
   493 // ---------------------------------------------------------------------------
       
   494 // Harvest completion callback function
       
   495 // ---------------------------------------------------------------------------
       
   496 void CCmMdhMediaserverContainer::HarvestCompleteL( TInt aErr )
       
   497     {
       
   498     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::HarvestComplete"));
       
   499     iHarvestActive = EFalse;
       
   500     if ( aErr != KErrNone )
       
   501         {
       
   502         iHarvestErrCode = aErr;
       
   503         }
       
   504     else
       
   505         {
       
   506         TInt newUpdateId =
       
   507             iMediaservers[iMediaserverIndex]->iSystemUpdateId;
       
   508         if ( newUpdateId ) // was set to 0 if no update needed
       
   509             {
       
   510             // store changed systemupdateid
       
   511             CCmMediaServerFull* tempServer = CCmMediaServerFull::NewLC();
       
   512             tempServer->SetUDNL(
       
   513                 *(iMediaservers[iMediaserverIndex]->iUuid) );
       
   514             iDbManager->PrepareQueryCmdL( EMediaServerInfoQyery );
       
   515             TInt errCode = iDbManager->QueryMediaServerL( tempServer );
       
   516             if ( errCode != KErrNotFound )
       
   517                 {
       
   518                 TRACE( Print( _L
       
   519                     ("[CmMdh Server]\t Updating systemupdateid to %d.."),
       
   520                     newUpdateId ));
       
   521 
       
   522                 tempServer->SetSystemUpdateID( newUpdateId );
       
   523                 iDbManager->UpdateMediaServerInfo( tempServer );
       
   524                 }
       
   525             CleanupStack::PopAndDestroy( tempServer );
       
   526             }
       
   527         }
       
   528 
       
   529     if ( ++iMediaserverIndex < iMediaservers.Count() )
       
   530         {
       
   531         // harvest next
       
   532         DoHarvestMediaserverL( iMediaserverIndex );
       
   533         }
       
   534     else
       
   535         {
       
   536         // no more media servers
       
   537         iMdhServer.SetServerStateL(ECmMdhServerStateIdle, iHarvestErrCode);
       
   538         }
       
   539     }
       
   540 
       
   541 
       
   542 // ---------------------------------------------------------------------------
       
   543 // Cancels search
       
   544 // ---------------------------------------------------------------------------
       
   545 void CCmMdhMediaserverContainer::CancelSearchL()
       
   546     {
       
   547     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::CancelSearchL"));
       
   548     if (!iTimer) // no search is active
       
   549         {
       
   550         User::Leave( KErrNotReady );
       
   551         }
       
   552 
       
   553     iTimer->Cancel();
       
   554     iMediaservers.ResetAndDestroy();
       
   555     delete iTimer;
       
   556     iTimer = NULL;
       
   557     iMdhServer.SetServerStateL(ECmMdhServerStateIdle, KErrCancel);
       
   558     }
       
   559 
       
   560 // ---------------------------------------------------------------------------
       
   561 // Cancels harvest
       
   562 // ---------------------------------------------------------------------------
       
   563 void CCmMdhMediaserverContainer::CancelHarvestL()
       
   564     {
       
   565     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::CancelHarvestL"));
       
   566     iHarvestActive = EFalse;
       
   567     iMetadataCollector->CancelHarvest();
       
   568     iMdhServer.SetServerStateL(ECmMdhServerStateIdle, KErrCancel);
       
   569     }
       
   570 
       
   571 // ---------------------------------------------------------------------------
       
   572 // Gets media server id from database
       
   573 // ---------------------------------------------------------------------------
       
   574 TInt CCmMdhMediaserverContainer::IdForMediaServerL( TDesC8& aUuid )
       
   575     {
       
   576     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::IdForMediaServerL"));
       
   577 
       
   578     iDbManager->PrepareQueryCmdL( EMediaServerIdQuery );
       
   579 
       
   580     TInt id = iDbManager->QueryMediaServerId( aUuid );
       
   581     TRACE( Print( _L("[CmMdh Server]\t returning id %d"), id));
       
   582 
       
   583     return id;
       
   584     }
       
   585 
       
   586 // ---------------------------------------------------------------------------
       
   587 // Sends progress info
       
   588 // ---------------------------------------------------------------------------
       
   589 void CCmMdhMediaserverContainer::SendProgressInfo( TInt aProgress )
       
   590     {
       
   591     TRACE( Print( _L(
       
   592         "[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   593                           SendProgressInfo( %d )"),
       
   594         aProgress));
       
   595 
       
   596 #ifdef _DEBUG
       
   597 
       
   598     TInt alloc;
       
   599     TInt cells = User::Heap().AllocSize( alloc );
       
   600     TInt size = User::Heap().Size();
       
   601     TRACE(Print(_L("[CmMdh Server]\t heap allocsize %d, size %d, cells %d"),
       
   602         alloc, size, cells ));
       
   603 
       
   604 #endif
       
   605 
       
   606 
       
   607     if ( iProgressInfo.iTotalItems > iProgressInfo.iProcessedItems )
       
   608         {
       
   609         iProgressInfo.iProcessedItems += aProgress;
       
   610         TCmProgressInfoPckg progressPckg( iProgressInfo );
       
   611         TRACE( Print( _L("[CmMdh Server]\t processed: %d total %d"),
       
   612                 iProgressInfo.iProcessedItems, iProgressInfo.iTotalItems) );
       
   613         
       
   614         if ( ( iProgressInfo.iTotalItems <= iProgressInfo.iProcessedItems ) ||
       
   615              ( iProgressInfo.iTotalItems > iProgressInfo.iProcessedItems &&
       
   616                iProgressInfo.iProcessedItems > 0 &&
       
   617                ( iProgressInfo.iProcessedItems - iPropertyItemIndex ) >=
       
   618                iProgressInfo.iTotalItems / KDefaultCaculateSize )
       
   619            )
       
   620             {
       
   621             iPropertyItemIndex = iProgressInfo.iProcessedItems;
       
   622             TInt err = RProperty::Set( KCmPropertyCat, KCmProperty, 
       
   623                                    progressPckg );
       
   624             TRACE( Print( _L("[CmMdh Server]\t RProperty::Set returned %d"), 
       
   625                    err)); 
       
   626             }
       
   627         else
       
   628             {
       
   629             LOG(_L("[CmMdh Server]\t progress small no need to pub&sub"));
       
   630             }
       
   631         }
       
   632 
       
   633     else
       
   634         {
       
   635         iProgressInfo.iProcessedItems += aProgress;
       
   636         LOG(_L("[CmMdh Server]\t Bigger than 100%% or not beginning"));
       
   637         }
       
   638 
       
   639     }
       
   640 
       
   641 
       
   642 // Responses
       
   643 
       
   644 // ---------------------------------------------------------------------------
       
   645 // Observer callback for Content Directory Search function.
       
   646 // ---------------------------------------------------------------------------
       
   647 void CCmMdhMediaserverContainer::CdsSearchResponse(
       
   648         const TDesC8& aUuid,
       
   649         TInt aSessionId,
       
   650         TInt aErr,
       
   651         const TDesC8& /*aContainerId*/,
       
   652         const TDesC8& /*aSearchCriteria*/,
       
   653         const TDesC8& /*aFilter*/,
       
   654         TInt /*aIndex*/,
       
   655         TInt /*aRequest*/,
       
   656         const TDesC8& /*aSortCriteria*/,
       
   657         const TDesC8& aResult,
       
   658         TInt aReturned,
       
   659         TInt aMatches,
       
   660         const TDesC8& /*aUpdateID*/)
       
   661     {
       
   662     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   663                              CdsSearchResponse"));
       
   664 
       
   665     TRAPD( err, CdsSearchResponseL( aUuid, 
       
   666                                     aSessionId, 
       
   667                                     aErr, 
       
   668                                     aResult,
       
   669                                     aReturned,
       
   670                                     aMatches ) );
       
   671     if ( err ) 
       
   672         {
       
   673         TRACE( Print(_L("[CmMdh Server]\t CdsSearchResponseL leaved with err:\
       
   674              %d"), err ) );
       
   675         }
       
   676     }
       
   677 
       
   678 // ---------------------------------------------------------------------------
       
   679 // Leaving version of CdsSearchResponseL callback.
       
   680 // ---------------------------------------------------------------------------
       
   681 void CCmMdhMediaserverContainer::CdsSearchResponseL(
       
   682         const TDesC8& aUuid,
       
   683         TInt aSessionId,
       
   684         TInt aErr,
       
   685         const TDesC8& aResult,
       
   686         TInt aReturned,
       
   687         TInt aMatches
       
   688         )
       
   689     {
       
   690     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   691                              CdsSearchResponseL"));
       
   692     if ( iHarvestActive )
       
   693         {
       
   694         iMetadataCollector->CdsSearchResponse(
       
   695             aSessionId, aErr, aResult, aReturned, aMatches );
       
   696         }
       
   697     else if ( iTimer || iResponsesPending ) // media server search active
       
   698         {
       
   699         if ( aErr != EHttpOk )
       
   700             {
       
   701             for( TInt i = 0; i < iMediaservers.Count(); i++ )
       
   702                 {
       
   703                 if ( *(iMediaservers[i]->iUuid) == aUuid )
       
   704                     {
       
   705                     LOG(_L("[CmMdh Server]\t search error, \
       
   706                             will not sync this.."));
       
   707                     iMediaservers[i]->iMediaserverState =
       
   708                         ECmMdhMediaserverNoSearchCaps;
       
   709                     iMediaservers[i]->iSystemUpdateId = 0;
       
   710                     }
       
   711                 }
       
   712             }
       
   713         else
       
   714             {
       
   715             iProgressInfo.iTotalItems += aMatches;
       
   716             TRACE( Print( _L("[CmMdh Server]\t total item count \
       
   717                                 increased to %d"),
       
   718             iProgressInfo.iTotalItems));
       
   719             if( iAllFound )
       
   720                 {
       
   721                 // when all servers are discovered,1 percentage is 
       
   722                 // set.
       
   723                 SendProgressInfo( iProgressInfo.iTotalItems / 
       
   724                 				  KDefaultCaculateSize );
       
   725                 iProgressInfo.iProcessedItems = 0;
       
   726                 delete iTimer;
       
   727                 iTimer = NULL;
       
   728                 iOldMediaServers.ResetAndDestroy();
       
   729                 TRACE( Print( _L("[CmMdh Server]\t All servers found!!!")));
       
   730                 }
       
   731             }
       
   732         DecResponsesAndFinishIfReadyL();
       
   733         }
       
   734     }
       
   735 
       
   736 // ---------------------------------------------------------------------------
       
   737 // Observer callback for Content Directory SearchCapabilities function.
       
   738 // ---------------------------------------------------------------------------
       
   739 void CCmMdhMediaserverContainer::CdsSearchCapabilitiesResponse(
       
   740             const TDesC8& aUuid,
       
   741             TInt /* aSessionId */,
       
   742             TInt aErr,
       
   743             const TDesC8& aSearchCaps )
       
   744     {
       
   745 
       
   746     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   747                              CdsSearchCapabilitiesResponse"));
       
   748 
       
   749     TRAPD( err, CdsSearchCapabilitiesResponseL( aUuid, 
       
   750                                                 aErr, 
       
   751                                                 aSearchCaps ) );
       
   752     if ( err ) 
       
   753         {
       
   754         TRACE( Print(_L("[CmMdh Server]\t CdsSearchCapabilitiesResponseL \
       
   755             leaved with err: %d"), err ) );
       
   756         }
       
   757     }
       
   758 
       
   759 // ---------------------------------------------------------------------------
       
   760 //  Leaving version of CdsSystemUpdateIdResponse callback.
       
   761 // ---------------------------------------------------------------------------
       
   762 void CCmMdhMediaserverContainer::CdsSearchCapabilitiesResponseL(
       
   763             const TDesC8& aUuid,
       
   764             TInt aErr,
       
   765             const TDesC8& aSearchCaps )
       
   766     {
       
   767 
       
   768     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   769                              CdsSearchCapabilitiesResponseL"));
       
   770 
       
   771 #ifdef _DEBUG
       
   772 
       
   773     HBufC* sc = UpnpString::ToUnicodeL( aSearchCaps );
       
   774     TRACE( Print(_L("[CmMdh Server]\t err: %d caps: %S"),
       
   775         aErr, sc ));
       
   776     delete sc;
       
   777 
       
   778 #endif
       
   779 
       
   780 
       
   781     HBufC8* uuid = HBufC8::NewL( aUuid.Length() );
       
   782     uuid->Des().Copy( aUuid );
       
   783     CleanupStack::PushL( uuid );
       
   784     CCmMdhMediaserverInfo* info = CCmMdhMediaserverInfo::NewL( );
       
   785     CleanupStack::PushL( info );
       
   786     info->iUuid = uuid;
       
   787     iMediaservers.AppendL( info );
       
   788 
       
   789     if ( aErr == EHttpOk && aSearchCaps.Length() ) // has search capabilities
       
   790         {
       
   791         info->iMediaserverState = ECmMdhMediaserverNotSynchronized;
       
   792         iAvControlPoint->CdsSystemUpdateIdActionL( aUuid );
       
   793         }
       
   794     else
       
   795         {
       
   796         DecResponsesAndFinishIfReadyL();
       
   797         info->iSystemUpdateId = 0;
       
   798         info->iMediaserverState = ECmMdhMediaserverNoSearchCaps;
       
   799         }
       
   800 
       
   801     CleanupStack::Pop( info );
       
   802     CleanupStack::Pop( uuid );
       
   803     }
       
   804 
       
   805 
       
   806 // ---------------------------------------------------------------------------
       
   807 // Observer callback for Content Directory GetSystemUpdateID function.
       
   808 // ---------------------------------------------------------------------------
       
   809 void CCmMdhMediaserverContainer::CdsSystemUpdateIdResponse(
       
   810             const TDesC8& aUuid,
       
   811             TInt /* aSessionId */,
       
   812             TInt aErr,
       
   813             TInt aSystemUpdateId )
       
   814     {
       
   815     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   816                              CdsSystemUpdateIdResponse"));
       
   817 
       
   818     TRACE( Print(_L("[CmMdh Server]\t err: %d suid: %d"),
       
   819         aErr, aSystemUpdateId ));
       
   820     
       
   821     TRAPD( err, CdsSystemUpdateIdResponseL( aUuid, 
       
   822                                             aErr, 
       
   823                                             aSystemUpdateId ) );
       
   824     if ( err ) 
       
   825         {
       
   826         TRACE( Print(_L("[CmMdh Server]\t CdsSystemUpdateIdResponseL leaved \
       
   827             with err: %d"), err ) );
       
   828         }
       
   829     }
       
   830 
       
   831 // ---------------------------------------------------------------------------
       
   832 // Leaving version of CdsSystemUpdateIdResponse callback.
       
   833 // ---------------------------------------------------------------------------
       
   834 void CCmMdhMediaserverContainer::CdsSystemUpdateIdResponseL(
       
   835             const TDesC8& aUuid,
       
   836             TInt aErr,
       
   837             TInt aSystemUpdateId )
       
   838     {
       
   839     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   840                              CdsSystemUpdateIdResponseL"));
       
   841 
       
   842     TRACE( Print(_L("[CmMdh Server]\t err: %d suid: %d"),
       
   843         aErr, aSystemUpdateId ));
       
   844     for( TInt i = 0; i < iMediaservers.Count(); i++ )
       
   845         {
       
   846         if ( *(iMediaservers[i]->iUuid) == aUuid )
       
   847 
       
   848             {
       
   849             LOG(_L("[CmMdh Server]\t checking \
       
   850                      media server's systemupdateid.."));
       
   851 
       
   852             TInt errCode = KErrNotFound;
       
   853             if ( aErr == EHttpOk )
       
   854                 {
       
   855                 CCmMediaServerFull* tempServer = CCmMediaServerFull::NewLC();
       
   856                 tempServer->SetUDNL( aUuid );
       
   857                 iDbManager->PrepareQueryCmdL( EMediaServerInfoQyery );
       
   858                 errCode = iDbManager->QueryMediaServerL( tempServer );
       
   859                 if ( errCode != KErrNotFound )
       
   860                     {
       
   861                     TRACE( Print( _L
       
   862                         ("[CmMdh Server]\t returned id %d, old id %d"),
       
   863                         aSystemUpdateId, tempServer->SystemUpdateID() ));
       
   864 
       
   865 
       
   866                     if ( aSystemUpdateId == tempServer->SystemUpdateID() )
       
   867                         {
       
   868                         LOG(_L("[CmMdh Server]\t systemupdateid \
       
   869                             match, no sync needed.."));
       
   870                         iMediaservers[i]->iMediaserverState =
       
   871                             ECmMdhMediaserverSynchronized;
       
   872                         iMediaservers[i]->iSystemUpdateId = 0;
       
   873                         DecResponsesAndFinishIfReadyL();
       
   874                         }
       
   875                     else
       
   876                         {
       
   877                         iMediaservers[i]->iSystemUpdateId = aSystemUpdateId;
       
   878                         errCode = iAvControlPoint->CdsSearchActionL( aUuid,
       
   879                                           KRootContainerId,
       
   880                                           KSearchCriteria,
       
   881                                           KSearchFilter,
       
   882                                           0,
       
   883                                           1,
       
   884                                           KSortCriteria );
       
   885                         TRACE( Print( _L("[CmMdh Server]\t search \
       
   886                             request returned %d"), errCode));
       
   887                         }
       
   888                     }
       
   889                 CleanupStack::PopAndDestroy( tempServer );
       
   890                 }
       
   891             if ( aErr != EHttpOk || errCode < 0 )
       
   892                 {
       
   893                 LOG(_L("[CmMdh Server]\t error with systemupdate \
       
   894                     id response, not syncing this.."));
       
   895                 iMediaservers[i]->iMediaserverState =
       
   896                     ECmMdhMediaserverSynchronized;
       
   897                 iMediaservers[i]->iSystemUpdateId = 0;
       
   898                 DecResponsesAndFinishIfReadyL();
       
   899                 }
       
   900             }
       
   901         }
       
   902     }
       
   903 
       
   904 
       
   905 // ---------------------------------------------------------------------------
       
   906 // Observer callback for upnp utilities
       
   907 // Called when wlan connection is lost
       
   908 // ---------------------------------------------------------------------------
       
   909 void CCmMdhMediaserverContainer::ConnectionLost()
       
   910     {
       
   911     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::\
       
   912                              ConnectionLost"));
       
   913     iHarvestActive = EFalse;
       
   914     if( iMetadataCollector )
       
   915         {
       
   916         iMetadataCollector->CancelHarvest();
       
   917         }
       
   918     TRAP_IGNORE( iMdhServer.SetServerStateL( ECmMdhServerStateIdle, 
       
   919                                              KErrDisconnected ) );
       
   920     }
       
   921 
       
   922 // ---------------------------------------------------------------------------
       
   923 // CCmMdhMediaserverContainer::AddHashValueForResponseL
       
   924 // ---------------------------------------------------------------------------        
       
   925 TInt CCmMdhMediaserverContainer::AddHashValueForResponseL(
       
   926                                                           TInt aMediaServerId, 
       
   927                                                           TInt aSearchIndex, 
       
   928                                                           TInt aItemCount, 
       
   929                                                           const TDesC8& aHash ) 
       
   930     {
       
   931     CCmSearchResponseHash* newHash = CCmSearchResponseHash::NewL( aSearchIndex,
       
   932                                                                   aItemCount,
       
   933                                                                   aHash );
       
   934     CleanupStack::PushL( newHash );
       
   935     TInt errCode = iDbManager->AddHashValue( aMediaServerId, newHash );
       
   936     CleanupStack::PopAndDestroy( newHash );  
       
   937     return errCode;                                                                
       
   938     }
       
   939     
       
   940 // ---------------------------------------------------------------------------
       
   941 // Gets media server hash values
       
   942 // ---------------------------------------------------------------------------
       
   943 void CCmMdhMediaserverContainer::HashValuesForMediaServerL( TInt aMediaServerId,
       
   944     RPointerArray<CCmSearchResponseHash>& aHashValues )
       
   945     {
       
   946     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::HashValuesForMediaServerL"));
       
   947 
       
   948     iDbManager->PrepareQueryCmdL( EHashValueQuery );
       
   949     
       
   950     TInt err = iDbManager->QueryHashValuesL( aMediaServerId, aHashValues );
       
   951 
       
   952     return;
       
   953     }
       
   954 
       
   955 // ---------------------------------------------------------------------------
       
   956 // Deletes old hash values
       
   957 // ---------------------------------------------------------------------------    
       
   958 void CCmMdhMediaserverContainer::DeleteOldHashValuesL( const TInt aMediaserverId,
       
   959     const TInt aSearchIndex )
       
   960     {
       
   961     LOG(_L("[CmMdh Server]\t CCmMdhMediaserverContainer::DeleteOldHashValuesL"));
       
   962     iDbManager->DeleteHashValuesL( aMediaserverId, aSearchIndex );
       
   963     }
       
   964     
       
   965 // End of file
       
   966 
       
   967