upnpharvester/mdhserver/src/server/mdhserver.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /*
       
     2 * Copyright (c) 2006-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:      Server core
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 #include    <e32base.h>
       
    24 
       
    25 #include    "mdhserver.h"
       
    26 #include    "mdhcommon.h"
       
    27 #include    "mdhserversession.h"
       
    28 #include    "mdhmediaservercontainer.h"
       
    29 
       
    30 #include    "msdebug.h"
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS =============================
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // CCmMdhServer::CCmMdhServer
       
    36 // constructor
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 CCmMdhServer::CCmMdhServer()
       
    40     : CPolicyServer( CActive::EPriorityHigh, KCmMdhServerPolicy )
       
    41     {
       
    42     LOG(_L("[CmMdh Server]\t CCmMdhServer::CCmMdhServer"));
       
    43     iServerState = ECmMdhServerStateIdle;
       
    44     }
       
    45     
       
    46 // ---------------------------------------------------------------------------
       
    47 // CCmMdhServer::ConstructL
       
    48 // 2nd phase constructor
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 void CCmMdhServer::ConstructL()
       
    52     {
       
    53     LOG(_L("[CmMdh Server]\t CCmMdhServer::ConstructL"));
       
    54     
       
    55     iSessionCount = 0;
       
    56     
       
    57     StartL( KCmMdhServerName );
       
    58     
       
    59     LOG(_L("[CmMdh Server]\t CCmMdhServer::ConstructL end"));
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CCmMdhServer::NewLC
       
    64 // (other items were commented in a header).
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CCmMdhServer* CCmMdhServer::NewLC()
       
    68     {
       
    69     LOG(_L("[CmMdh Server]\t CCmMdhServer::NewLC"));
       
    70     CCmMdhServer *self = new (ELeave) CCmMdhServer();
       
    71     CleanupStack::PushL( self );
       
    72     self->ConstructL();
       
    73     return self;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CCmMdhServer::~CCmMdhServer
       
    78 // destructor
       
    79 // (other items were commented in a header).
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 CCmMdhServer::~CCmMdhServer()
       
    83     {
       
    84     LOG(_L("[CmMdh Server]\t CCmMdhServer::~CCmMdhServer"));
       
    85     delete iMediaserverContainer;
       
    86     LOG(_L("[CmMdh Server]\t CCmMdhServer::~CCmMdhServer end"));
       
    87     }
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // CCmMdhServer::
       
    91 // 
       
    92 // (other items were commented in a header).
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 void CCmMdhServer::CreateMediaserverContainerL()
       
    96     {
       
    97     LOG(_L("[CmMdh Server]\t CCmMdhServer::CreateMediaserverContainerL"));
       
    98     
       
    99     if ( !iMediaserverContainer )
       
   100         {
       
   101         iMediaserverContainer = 
       
   102             CCmMdhMediaserverContainer::NewL( *this );
       
   103         LOG(_L("[CmMdh Server]\t MediaserverContainer created"));
       
   104         }
       
   105     
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // CCmMdhServer::DestroyMediaserverContainer
       
   110 // 
       
   111 // (other items were commented in a header).
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 void CCmMdhServer::DestroyMediaserverContainer() 
       
   115     {
       
   116     LOG(_L("[CmMdh Server]\t CCmMdhServer::DestroyMediaserverContainer"));
       
   117     
       
   118     if ( iMediaserverContainer )
       
   119         {
       
   120         delete iMediaserverContainer; iMediaserverContainer = NULL;
       
   121         LOG(_L("[CmMdh Server]\t MediaserverContainer destroyed"));
       
   122         }
       
   123     }
       
   124     
       
   125 // ---------------------------------------------------------------------------
       
   126 // CCmMdhServer::MediaserverContainer
       
   127 // 
       
   128 // (other items were commented in a header).
       
   129 // ---------------------------------------------------------------------------
       
   130 //
       
   131 CCmMdhMediaserverContainer& CCmMdhServer::MediaserverContainer() 
       
   132     {
       
   133     return *iMediaserverContainer;
       
   134     }
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // CCmMdhServer::NewSessionL
       
   138 // Creates a new session to the server if version information is correct.
       
   139 // (other items were commented in a header).
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 CSession2* CCmMdhServer::NewSessionL( const TVersion & aVersion, 
       
   143                                       const RMessage2 &/*aMessage*/) const
       
   144     {
       
   145     LOG(_L("[CmMdh Server]\t CCmMdhServer::NewSessionL"));
       
   146 
       
   147 #ifdef _DEBUG
       
   148     
       
   149     TInt alloc;
       
   150     TInt cells = User::Heap().AllocSize( alloc );
       
   151     TInt size = User::Heap().Size();
       
   152     TRACE( Print(
       
   153         _L("[CmMdh Server]\t NewSessionL allocsize %d, size %d, cells %d"), 
       
   154         alloc, 
       
   155         size, 
       
   156         cells ) );
       
   157  
       
   158 #endif
       
   159     
       
   160     // check we're the right version
       
   161     TVersion version( KCmMdhServerMajor, 
       
   162                       KCmMdhServerMinor, 
       
   163                       KCmMdhServerBuild);
       
   164                       
       
   165     if( !User::QueryVersionSupported( version, aVersion ) )
       
   166         {
       
   167         User::Leave( KErrNotSupported );
       
   168         }
       
   169     // make new session
       
   170     return CCmMdhSession::NewL( iMediaserverContainer, 
       
   171         *const_cast<CCmMdhServer*>(this) );
       
   172     }
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // CCmMdhServer::SetServerStateL
       
   176 // 
       
   177 // (other items were commented in a header).
       
   178 // ---------------------------------------------------------------------------
       
   179 //
       
   180 void CCmMdhServer::SetServerStateL( TCmMdhServerState aServerState, 
       
   181                                      TInt aErrCode /* = KErrNone */)
       
   182     {
       
   183     LOG(_L("[CmMdh Server]\t CCmMdhServer::SetServerState"));
       
   184     TRACE( Print( _L("[CmMdh Server]\t Setting server state from %d to %d"), 
       
   185                iServerState, aServerState));
       
   186 
       
   187     if ( iServerState == aServerState ) 
       
   188         {
       
   189         return;
       
   190         }
       
   191     
       
   192     TBool validTransition = EFalse;
       
   193     
       
   194     switch ( iServerState )
       
   195         {
       
   196         case ECmMdhServerStateIdle:
       
   197             if ( aServerState ) // search or harvest started
       
   198                 {
       
   199                 validTransition = ETrue;
       
   200                 }
       
   201             break;
       
   202         case ECmMdhServerStateSearching:
       
   203             if ( aServerState == ECmMdhServerStateIdle ) // search complete
       
   204                 {
       
   205                 if ( aErrCode )
       
   206                     {
       
   207                     //DestroyMediaserverContainer();
       
   208                     }
       
   209                 iCurrentAsyncMsg.Complete( aErrCode );
       
   210                 validTransition = ETrue;    
       
   211                 }
       
   212             break;
       
   213         case ECmMdhServerStateHarvesting:
       
   214             if ( aServerState == ECmMdhServerStateIdle ) // harvest complete
       
   215                 {
       
   216                 //DestroyMediaserverContainer();
       
   217                 LOG(_L("[CmMdh Server]\t Completing..."));
       
   218                 iCurrentAsyncMsg.Complete( aErrCode );
       
   219                 LOG(_L("[CmMdh Server]\t Completed..."));
       
   220                 validTransition = ETrue;
       
   221                 if ( iSessionCount <= 0)
       
   222                     {
       
   223                     // shutdown server 
       
   224                     LOG(_L("[CmMdh Server]\t Harvest complete, \
       
   225                         shutting down.."));
       
   226                     CActiveScheduler::Stop();
       
   227                     }
       
   228                 }
       
   229             break;
       
   230          default:
       
   231             // nothing
       
   232             break;
       
   233         }
       
   234     if (validTransition) 
       
   235         {
       
   236         iServerState = aServerState;   
       
   237         }
       
   238     else
       
   239         {
       
   240         User::Leave( KErrNotReady );
       
   241         }
       
   242     }
       
   243 
       
   244 // ---------------------------------------------------------------------------
       
   245 // CCmMdhServer::DecrementSessions
       
   246 // 
       
   247 // (other items were commented in a header).
       
   248 // ---------------------------------------------------------------------------
       
   249 //
       
   250 void CCmMdhServer::DecrementSessions()
       
   251     {
       
   252     LOG(_L("[CmMdh Server]\t CCmMdhServer::DecrementSessions"));
       
   253     
       
   254     if ( --( iSessionCount) <= 0 
       
   255       && iServerState!= ECmMdhServerStateHarvesting ) 
       
   256       {
       
   257       LOG(_L("[CmMdh Server]\t Destroying media server container..."));
       
   258       DestroyMediaserverContainer();
       
   259       // shutdown server
       
   260       LOG(_L("[CmMdh Server]\t No sessions left, shutting down.."));
       
   261       CActiveScheduler::Stop();
       
   262       }
       
   263 #ifdef _DEBUG
       
   264     
       
   265     TInt alloc;
       
   266     TInt cells = User::Heap().AllocSize( alloc );
       
   267     TInt size = User::Heap().Size();
       
   268     TRACE( Print(
       
   269         _L(
       
   270         "[CmMdh Server]\t DecrementSessions allocsize %d, size %d, cells %d"
       
   271           ),
       
   272         alloc, 
       
   273         size, 
       
   274         cells )); 
       
   275 #endif      
       
   276     }
       
   277 
       
   278 // --------------------------------------------------------------------------
       
   279 // CCmMdhServer::IncrementSessions
       
   280 // --------------------------------------------------------------------------
       
   281 void CCmMdhServer::IncrementSessions() 
       
   282     {
       
   283     LOG(_L("[CmMdh Server]\t CCmMdhServer::IncrementSessions"))
       
   284     ++iSessionCount;    
       
   285     }
       
   286     
       
   287 // ---------------------------------------------------------------------------
       
   288 // CCmMdhServer::ServerState
       
   289 // 
       
   290 // (other items were commented in a header).
       
   291 // ---------------------------------------------------------------------------
       
   292 //
       
   293 TCmMdhServerState CCmMdhServer::ServerState()
       
   294     {
       
   295     LOG(_L("[CmMdh Server]\t CCmMdhServer::ServerState"));
       
   296     return iServerState;
       
   297     }
       
   298     
       
   299 // ---------------------------------------------------------------------------
       
   300 // CCmMdhServer::SetCurrentAsyncMsg
       
   301 // 
       
   302 // (other items were commented in a header).
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 void CCmMdhServer::SetCurrentAsyncMsg( const RMessage2& aMessage )
       
   306     {
       
   307     LOG(_L("[CmMdh Server]\t CCmServer::SetCurrentAsyncMsg"));
       
   308     iCurrentAsyncMsg = aMessage;
       
   309     }
       
   310 
       
   311 
       
   312 // ========================== OTHER EXPORTED FUNCTIONS =======================
       
   313 
       
   314 // ---------------------------------------------------------------------------
       
   315 // PanicServer
       
   316 // Server panic handler
       
   317 // Panic our own thread
       
   318 // ---------------------------------------------------------------------------
       
   319 //
       
   320 void PanicServer(TInt aPanic)
       
   321     {
       
   322     TRACE(Print(_L("[CmMdh Server]\t PanicServer: Reason = %d\n"), aPanic));
       
   323     User::Panic( KCmMdhServerPanic, aPanic );
       
   324     }
       
   325 
       
   326 // ---------------------------------------------------------------------------
       
   327 // PanicClient
       
   328 // Client panic handler
       
   329 // RMessage2::Panic() also completes the message. This is:
       
   330 // (a) important for efficient cleanup within the kernel
       
   331 // (b) a problem if the message is completed a second time
       
   332 // (other items were commented in a header).
       
   333 // ---------------------------------------------------------------------------
       
   334 //
       
   335 void PanicClient( const RMessage2& aMessage, TInt aPanic )
       
   336     {
       
   337     TRACE(Print(_L("[CmMdh Server]\t PanicClient: Reason = %d\n"), aPanic));
       
   338     aMessage.Panic( KCmMdhServerPanic, aPanic );
       
   339     }
       
   340 
       
   341 // End of File