mtpdataproviders/mtpimagedp/mediasyncserver/src/cmediasyncserversession.cpp
changeset 51 64200268cac2
parent 50 965bb42340b2
child 52 866b4af7ffbe
equal deleted inserted replaced
50:965bb42340b2 51:64200268cac2
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include "cmediasyncserversession.h"
       
    22 #include "cmediasyncserver.h"
       
    23 #include "cmediasyncdatabase.h"
       
    24 #include "cmediasyncobserver.h"
       
    25 #include "cmediasyncdatawriter.h"
       
    26 
       
    27 __FLOG_STMT(_LIT8(KComponent,"MediaSyncServerSession");)
       
    28 
       
    29 const TInt KDefGlobalSharedHeapSize = 64 * 1024;//64K byte memory
       
    30 const TInt KReduceFactor  = 2; 
       
    31 const TInt KMaxRetryTimes = 3; 
       
    32 
       
    33 CMediaSyncServerSession::CMediaSyncServerSession(CMediaSyncServer* aServer) : 
       
    34     iServer(aServer),
       
    35     iAllocated(EFalse)
       
    36     {
       
    37     __FLOG_OPEN(KMSSSubsystem, KComponent);
       
    38     __FLOG(_L8("CMediaSyncServerSession::CMediaSyncServerSession - Entry"));
       
    39     __FLOG(_L8("CMediaSyncServerSession::CMediaSyncServerSession - Exit"));    
       
    40     }
       
    41     
       
    42 /**
       
    43 Destructor.
       
    44 */
       
    45 CMediaSyncServerSession::~CMediaSyncServerSession()
       
    46     {
       
    47     __FLOG(_L8("CMediaSyncServerSession::~CMediaSyncServerSession - Entry"));
       
    48     
       
    49     if (iAllocated)
       
    50         {
       
    51         iGlobalSharedHeap.Close();
       
    52         }
       
    53     
       
    54     __FLOG(_L8("CMediaSyncServerSession::~CMediaSyncServerSession - Exit"));
       
    55     __FLOG_CLOSE;    
       
    56     }
       
    57 
       
    58 // --------------------------------------------------------------------------
       
    59 // 
       
    60 // From CSession2, passes the request forward to DispatchMessageL.
       
    61 // --------------------------------------------------------------------------
       
    62 //
       
    63 void CMediaSyncServerSession::ServiceL(const RMessage2& aMessage)
       
    64     {
       
    65     __FLOG_VA((_L8("CMediaSyncServerSession::ServiceL - Function: %d"), aMessage.Function()));
       
    66     
       
    67     DispatchMessageL(aMessage);
       
    68     }
       
    69 
       
    70 void CMediaSyncServerSession::DispatchMessageL(const RMessage2& aMessage)
       
    71     {
       
    72     __FLOG(_L8("CMediaSyncServerSession::DispatchMessageL - Entry"));
       
    73        
       
    74     switch( aMessage.Function() )
       
    75         {
       
    76         case EMediaSyncClientGetGSHHandle:
       
    77             {
       
    78             AllocateGlobalSharedHeapL(aMessage);            
       
    79             aMessage.Complete(iGlobalSharedHeap);
       
    80             }
       
    81             break;
       
    82             
       
    83         case EMediaSyncClientGetChanges:
       
    84             aMessage.Complete(GetChangesL(aMessage));
       
    85             break;
       
    86             
       
    87         case EMediaSyncClientRemoveAllRecords:
       
    88             {
       
    89             iServer->MediaSyncDatabase()->RemoveAllNotificationsL();
       
    90             iGlobalSharedHeap.Close();
       
    91             iAllocated = EFalse;
       
    92             aMessage.Complete(KErrNone);
       
    93             }
       
    94             break;
       
    95             
       
    96         case EMediaSyncClientEnableMonitor:
       
    97             iServer->MediaSyncObserver()->SubscribeForChangeNotificationL();
       
    98             aMessage.Complete(KErrNone);
       
    99             break;
       
   100             
       
   101         case EMediaSyncClientDisableMonitor:
       
   102             iServer->MediaSyncObserver()->UnsubscribeForChangeNotificationL();
       
   103             aMessage.Complete(KErrNone);
       
   104             break;
       
   105             
       
   106         case EMediaSyncClientNeedFullSync:
       
   107             aMessage.Complete(GetFullSyncFlag(aMessage));
       
   108             break;
       
   109             
       
   110         case EMediaSyncClientClearFullSync:
       
   111             iServer->ClearFullSyncFlag();
       
   112             aMessage.Complete(KErrNone);
       
   113             break;
       
   114             
       
   115         case EMediaSyncClientShutdown:
       
   116             CActiveScheduler::Stop();
       
   117             aMessage.Complete(KErrNone);
       
   118             break;
       
   119             
       
   120         default:
       
   121             aMessage.Panic(KMediaSyncClientPanicCategory, EBadRequest);
       
   122             break;
       
   123         }
       
   124 
       
   125     __FLOG(_L8("CMediaSyncServerSession::DispatchMessageL - Exit"));
       
   126     }
       
   127 
       
   128 void CMediaSyncServerSession::AllocateGlobalSharedHeapL(const RMessage2& aMessage)
       
   129     {
       
   130     __FLOG(_L8("CMediaSyncServerSession::AllocateGlobalSharedHeapL - Entry"));
       
   131     
       
   132     if (!iAllocated)
       
   133         {
       
   134         TInt attemptedSize = aMessage.Int0();
       
   135         if (attemptedSize > KDefGlobalSharedHeapSize || attemptedSize <= 0)
       
   136             {
       
   137             attemptedSize = KDefGlobalSharedHeapSize;
       
   138             }
       
   139         
       
   140         TInt retryCount = KMaxRetryTimes;
       
   141         TInt redFactor = KReduceFactor;    
       
   142         TInt result = KErrNone;
       
   143         
       
   144         for (; retryCount > 0; retryCount--)
       
   145             {
       
   146             result = iGlobalSharedHeap.CreateGlobal(KNullDesC, attemptedSize, attemptedSize);
       
   147             
       
   148             if (result == KErrNone)
       
   149                 {
       
   150                 // We have succesfully allocated a GSH
       
   151                 break;
       
   152                 }
       
   153             else
       
   154                 {
       
   155                 // Reduce the size of the GSH by a scale factor
       
   156                 attemptedSize = attemptedSize / redFactor;
       
   157                 }
       
   158             }
       
   159             
       
   160         User::LeaveIfError(result); 
       
   161         iAllocated = ETrue;
       
   162         }
       
   163     
       
   164     __FLOG(_L8("CMediaSyncServerSession::AllocateGlobalSharedHeapL - Exit"));
       
   165     }
       
   166 
       
   167 TInt CMediaSyncServerSession::GetChangesL(const RMessage2& aMessage)
       
   168     {
       
   169     __FLOG(_L8("CMediaSyncServerSession::GetChangesL - Entry"));
       
   170 
       
   171     TInt maxFetchCount = aMessage.Int0();
       
   172     CMediaSyncDataWriter* writer = CMediaSyncDataWriter::NewLC(iGlobalSharedHeap);
       
   173     TBool finished = EFalse;
       
   174     
       
   175     iServer->MediaSyncDatabase()->FetchNotificationsL(*writer, maxFetchCount, finished);
       
   176     TPtr8 finishPtr((TUint8*)&finished, sizeof(TBool), sizeof(TBool));
       
   177     
       
   178     aMessage.Write(1, finishPtr);
       
   179     
       
   180     CleanupStack::PopAndDestroy(writer);        
       
   181     
       
   182     __FLOG(_L8("CMediaSyncServerSession::GetChangesL - Exit"));
       
   183     
       
   184     return KErrNone;
       
   185     }
       
   186 
       
   187 TInt CMediaSyncServerSession::GetFullSyncFlag(const RMessage2& aMessage)
       
   188     {
       
   189     __FLOG(_L8("CMediaSyncServerSession::GetFullSyncFlagL - Entry"));
       
   190         
       
   191     TBool needFullSync = iServer->NeedFullSync();
       
   192     TPtr8 finishPtr((TUint8*)&needFullSync, sizeof(TBool), sizeof(TBool)); 
       
   193     aMessage.Write(0, finishPtr);    
       
   194     
       
   195     __FLOG(_L8("CMediaSyncServerSession::GetFullSyncFlagL - Exit"));
       
   196     
       
   197     return KErrNone;
       
   198     }