upnp/upnpstack/upnputils/src/upnpsymbianserverbase.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2  * Copyright (c) 2005-2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies  this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:  Implementation of CUpnpSymbianServerBase
       
    15  *
       
    16  */
       
    17 
       
    18 #include <upnpsymbianserverbase.h>
       
    19 #include "upnpuheapmark.h"
       
    20 #include "upnpcustomlog.h"
       
    21 
       
    22 #include <ecom/ecom.h>
       
    23 
       
    24 const TInt KUpnpSymbianServerShutdownInterval = 2000000;
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // CUpnpSymbianServerBase::StartServerL
       
    28 // Create and start the server.
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 void CUpnpSymbianServerBase::StartServerL( const TDesC& aThreadName,
       
    32     TServerFactoryMethodLC aServerFactoryMethodLC )
       
    33     {
       
    34     User::LeaveIfError( User::RenameThread( aThreadName ) );
       
    35     // Construct active scheduler
       
    36     CActiveScheduler* activeScheduler = new( ELeave ) CActiveScheduler;
       
    37     CleanupStack::PushL( activeScheduler );
       
    38 
       
    39     // Install active scheduler
       
    40     // We don't need to check whether an active scheduler is already installed
       
    41     // as this is a new thread, so there won't be one
       
    42     CActiveScheduler::Install( activeScheduler );
       
    43 
       
    44     // Construct our server
       
    45     CUpnpSymbianServerBase* serverObject = aServerFactoryMethodLC();
       
    46     LOGS( "UpnpSymbianServer *** Ready to accept connections" );
       
    47 
       
    48     RProcess::Rendezvous( KErrNone );
       
    49 
       
    50     // Start handling requests
       
    51     CActiveScheduler::Start();
       
    52 
       
    53     LOGS( "UpnpSymbianServer *** Active scheduler stopped" );
       
    54     LOGS( "UpnpSymbianServer *** Prepared for shutdown" );
       
    55 
       
    56     CleanupStack::PopAndDestroy( serverObject );
       
    57     REComSession::FinalClose();
       
    58     CleanupStack::PopAndDestroy( activeScheduler );
       
    59     LOGS( "UpnpSymbianServer *** Shutdown complete" );
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CUpnpSymbianServerBase::StartServer
       
    64 // Create and start the server.
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 EXPORT_C TInt CUpnpSymbianServerBase::StartServer( const TDesC& aThreadName,
       
    68         TServerFactoryMethodLC aServerFactoryMethodLC )
       
    69     {
       
    70     __UPNP_UHEAP_MARK;
       
    71 
       
    72     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
    73     if ( !( cleanupStack ) )
       
    74         {
       
    75         User::Panic( aThreadName, ECreateTrapCleanup );
       
    76         }
       
    77 
       
    78     TRAPD( err, StartServerL( aThreadName, aServerFactoryMethodLC) );
       
    79     if ( err != KErrNone )
       
    80         {
       
    81         //there's no need to panic server
       
    82         //f.e. KErrAlreadyExist is valid in case of simultanous start
       
    83         }
       
    84 
       
    85     delete cleanupStack;
       
    86     cleanupStack = NULL;
       
    87 
       
    88     __UPNP_UHEAP_MARKEND;
       
    89     return err;
       
    90     }
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CUpnpSymbianServerBase::CUpnpSymbianServerBase
       
    94 // Constructor.
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 EXPORT_C CUpnpSymbianServerBase::CUpnpSymbianServerBase()
       
    98 : CServer2( EPriorityNormal )
       
    99     {
       
   100     }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CUpnpSymbianServerBase::~CUpnpSymbianServerBase
       
   104 // Destructor.
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 EXPORT_C CUpnpSymbianServerBase::~CUpnpSymbianServerBase()
       
   108     {
       
   109     delete iShutdownTimer;
       
   110     }
       
   111 
       
   112 // -----------------------------------------------------------------------------
       
   113 // CUpnpSymbianServerBase::BaseConstructL
       
   114 // Second phase base constructor. It must be called by derived class.
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 EXPORT_C void CUpnpSymbianServerBase::BaseConstructL()
       
   118     {
       
   119     StartL( ServerName() );
       
   120     iShutdownTimer = CUpnpNotifyTimer::NewL( this );
       
   121     SuggestShutdown();
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CUpnpSymbianServerBase::PanicClient
       
   126 // Panic client.
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 EXPORT_C void CUpnpSymbianServerBase::PanicClient(
       
   130         const RMessage2& aMessage, TInt aPanic ) const
       
   131     {
       
   132     LOG_FUNC_NAME;
       
   133     aMessage.Panic( ServerName(), aPanic );
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CUpnpSymbianServerBase::PanicServer
       
   138 // Panic server.
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 EXPORT_C void CUpnpSymbianServerBase::PanicServer( TInt aPanic ) const
       
   142     {
       
   143     User::Panic( ServerName(), aPanic );
       
   144     }
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 // CUpnpSymbianServerBase::IncrementSessionCount
       
   148 // Increment sessions.
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 void CUpnpSymbianServerBase::IncrementSessionCount()
       
   152     {
       
   153     LOG_FUNC_NAME;
       
   154     iSessionCount++;
       
   155     iShutdownTimer->Cancel();
       
   156     }
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // CUpnpSymbianServerBase::DecrementSessions
       
   160 // Decrement sessions.
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 void CUpnpSymbianServerBase::DecrementSessionCount()
       
   164     {
       
   165     LOG_FUNC_NAME;
       
   166     iSessionCount--;
       
   167     ASSERT( iSessionCount >= 0 );
       
   168     SuggestShutdown();
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CUpnpSymbianServerBase::SuggestShutdown
       
   173 // Checks every stop condidion and starts closing server timeout if all of them
       
   174 // are fulfilled.
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 EXPORT_C void CUpnpSymbianServerBase::SuggestShutdown()
       
   178     {
       
   179     if ( (iSessionCount <= 0) && CanBeStopped() )
       
   180         {
       
   181         iShutdownTimer->Start( KUpnpSymbianServerShutdownInterval );
       
   182         }
       
   183     }
       
   184 // -----------------------------------------------------------------------------
       
   185 // CUpnpSymbianServerBase::TimerEventL
       
   186 // End of shutdown timer event
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 EXPORT_C void CUpnpSymbianServerBase::TimerEventL( CUpnpNotifyTimer* aTimer )
       
   190     {
       
   191     ASSERT( iShutdownTimer == aTimer );
       
   192     ASSERT( (iSessionCount <= 0) && CanBeStopped() );
       
   193     CActiveScheduler::Stop();
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CUpnpSymbianServerBase::RunL
       
   198 // Invoked when server receives message.
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 EXPORT_C void CUpnpSymbianServerBase::RunL()
       
   202     {
       
   203     switch( Message().Function() )
       
   204         {
       
   205         case RMessage2::EConnect:
       
   206         IncrementSessionCount();
       
   207         break;
       
   208         case RMessage2::EDisConnect:
       
   209         DecrementSessionCount();
       
   210         break;
       
   211         }
       
   212     CServer2::RunL();
       
   213     }
       
   214 
       
   215 // -----------------------------------------------------------------------------
       
   216 // CUpnpSymbianServerBase::RunError
       
   217 // RunError is called when RunL leaves.
       
   218 // -----------------------------------------------------------------------------
       
   219 //
       
   220 EXPORT_C TInt CUpnpSymbianServerBase::RunError( TInt aError )
       
   221     {
       
   222     LOG_FUNC_NAME;
       
   223     if ( aError == KErrBadDescriptor )
       
   224         {
       
   225         // A bad descriptor error implies a badly programmed client, so panic it;
       
   226         // otherwise report the error to the client
       
   227         LOGS( "RunError - BadClient" );
       
   228         PanicClient( Message(), EBadDescriptor );
       
   229         }
       
   230     else if ( aError != KErrCorrupt )
       
   231         {
       
   232         LOGS( "RunError - Faulty Message" );
       
   233         if ( !Message().IsNull() )
       
   234             {
       
   235             Message().Complete( aError );
       
   236             }
       
   237         }
       
   238 
       
   239     // The leave will result in an early return from CServer::RunL(), skipping
       
   240     // the call to request another message. So do that now in order to keep the
       
   241     // server running.
       
   242     ReStart();
       
   243 
       
   244     // Handled the error fully
       
   245     return KErrNone;
       
   246     }
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // CUpnpSymbianServerBase::NewSessionL
       
   250 // Create new session or leaves with KErrNotSupported if version is unsupported.
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 EXPORT_C CSession2* CUpnpSymbianServerBase::NewSessionL(
       
   254         const TVersion& aVersion, const RMessage2& aMessage ) const
       
   255     {
       
   256     LOG_FUNC_NAME;
       
   257     // Check we're the right version
       
   258     if ( !User::QueryVersionSupported( SupportedVersion(), aVersion ) )
       
   259         {
       
   260         User::Leave( KErrNotSupported );
       
   261         }
       
   262     return NewSessionL( aMessage );
       
   263     }
       
   264 
       
   265 // -----------------------------------------------------------------------------
       
   266 // CUpnpSymbianServerBase::CanBeStopped
       
   267 // Default implementation for transient servers - no additional stopping conditions
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 EXPORT_C TBool CUpnpSymbianServerBase::CanBeStopped() const
       
   271     {
       
   272     return ETrue;
       
   273     }
       
   274 
       
   275 // End Of File