PECengine/PresenceServer2/ServerSrc/CPEngServer.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Presence Server class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  Include Files
       
    20 #include    <e32svr.h>
       
    21 #include    <ecom/ecom.h>
       
    22 
       
    23 #include    "CPEngServer.h"
       
    24 #include    "CPEngSession.h"
       
    25 #include    "CPEngSubSession.h"
       
    26 #include    "TPEngServerCommon.h"
       
    27 #include    "TPEngServerParams.h"
       
    28 #include    "CPEngCSPSessManager.h"
       
    29 #include    "CPengSessionSlotId.h"
       
    30 #include    "CPEngActiveScheduler.h"
       
    31 
       
    32 // Debug Prints
       
    33 #include    "PresenceDebugPrint.h"
       
    34 
       
    35 // CONSTANTS
       
    36 
       
    37 // Static data for Capability check configuration
       
    38 static const TInt KPEngServerRangeCount = 2;
       
    39 
       
    40 /**
       
    41  * Ranges for the Request values
       
    42  * All requests will fall in one range
       
    43  */
       
    44 static const TInt PEngServerRanges[ KPEngServerRangeCount ] =
       
    45     {
       
    46     0,  // Range from EMainSessShutdownServer to ESubSessFetchUpdateRqstResult
       
    47     EHighestServerRequest // range is from EHighestServerRequest request to KMaxInt
       
    48     };
       
    49 
       
    50 /**
       
    51  * Element indexes for the defined ranges
       
    52  * we have only one range and for it is defined only one Element
       
    53  */
       
    54 static const TUint8 KPEngServerElementsIndex[KPEngServerRangeCount] =
       
    55     {
       
    56     0, // First element in the element array will be applied for this range
       
    57     CPolicyServer::ENotSupported
       
    58     };
       
    59 
       
    60 // Policy elements
       
    61 static const CPolicyServer::TPolicyElement KPEngServerElements[] =
       
    62     {
       
    63         {
       
    64         _INIT_SECURITY_POLICY_C3( ECapabilityReadUserData,
       
    65         ECapabilityWriteUserData,
       
    66         ECapabilityNetworkServices ),
       
    67         CPolicyServer::EFailClient
       
    68         }
       
    69     };
       
    70 
       
    71 static const CPolicyServer::TPolicy KPEngServerPolicy =
       
    72     {
       
    73     // The index into Elements,that is used to check a connection attempt
       
    74 
       
    75     0,
       
    76     // Number of ranges in the iRanges array
       
    77     KPEngServerRangeCount,
       
    78 
       
    79     // A pointer to an array of ordered ranges of request numbers
       
    80     PEngServerRanges,
       
    81 
       
    82     // A pointer to an array of TUint8 values specifying
       
    83     // the appropriate action to take for each range in iRanges
       
    84     KPEngServerElementsIndex,
       
    85 
       
    86     // A pointer to an array of distinct policy elements
       
    87     KPEngServerElements
       
    88     };
       
    89 
       
    90 
       
    91 // ============================ MEMBER FUNCTIONS ===============================
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CPEngServer::CPEngServer
       
    95 // C++ default constructor can NOT contain any code, that might leave.
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 CPEngServer::CPEngServer(
       
    99     TInt aPriority )
       
   100         : CPolicyServer( aPriority, KPEngServerPolicy ),
       
   101         iRunning( EFalse ),
       
   102         iSessionsManagers( 1 ) // usually only one session
       
   103     {
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CPEngServer::ConstructL
       
   108 // Symbian 2nd phase constructor can leave.
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 void CPEngServer::ConstructL()
       
   112     {
       
   113     StartL( KServerName );
       
   114     User::LeaveIfError( iStoreAdmin.Connect() );
       
   115     iContainer = CObjectCon::NewL();
       
   116     // we need to assign some unique ID to the container, to own the object
       
   117     // stored in it
       
   118     iContainer->iUniqueID = 1;
       
   119     }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // CPEngServer::NewLC
       
   123 // Two-phased constructor.
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 CPEngServer* CPEngServer::NewL(
       
   127     TInt aPriority )
       
   128     {
       
   129     CPEngServer* self = CPEngServer::NewLC( aPriority );
       
   130     CleanupStack::Pop();
       
   131     return self;
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CPEngServer::NewLC
       
   136 // Two-phased constructor.
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 CPEngServer* CPEngServer::NewLC(
       
   140     TInt aPriority )
       
   141     {
       
   142     CPEngServer* self = new( ELeave ) CPEngServer( aPriority );
       
   143     CleanupStack::PushL( self );
       
   144     self->ConstructL();
       
   145     return self;
       
   146     }
       
   147 
       
   148 // Destructor (virtual by CBase)
       
   149 CPEngServer::~CPEngServer()
       
   150     {
       
   151     PENG_DP( D_PENG_LIT( "CPEngServer::~CPEngServer()" ) );
       
   152     delete iContainer;
       
   153     iStoreAdmin.Close();
       
   154     iSessionsManagers.ResetAndDestroy();
       
   155     REComSession::FinalClose();
       
   156     }
       
   157 
       
   158 
       
   159 // =============================================================================
       
   160 // =============== Functions from base class ===================================
       
   161 // =============================================================================
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // CPEngServer::ExecuteServerL()
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 TInt CPEngServer::ExecuteServerL(
       
   168     TPEngServerParams& aParams )
       
   169     {
       
   170     //Renaming must be done as early as possible
       
   171     aParams.RenameMainThread( KServerName );
       
   172 
       
   173     TInt res( KErrNone );
       
   174 
       
   175     // start scheduler and server
       
   176     CPEngActiveScheduler* pA = new( ELeave )CPEngActiveScheduler;
       
   177     CleanupStack::PushL( pA );
       
   178     CPEngActiveScheduler::Install( pA );
       
   179 
       
   180     //If exe server, call RunServerL directly.
       
   181     TRAP( res, RunServerL( aParams ) );
       
   182 
       
   183     CPEngActiveScheduler::Install( NULL );
       
   184     CleanupStack::PopAndDestroy();//pA
       
   185     return res;
       
   186     }
       
   187 
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CPEngServer::StopServer()
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 void CPEngServer::StopServer()
       
   194     {
       
   195     if ( iRunning )
       
   196         {
       
   197         CPEngActiveScheduler::Stop();
       
   198         }
       
   199     iRunning = EFalse;
       
   200     }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CPEngServer::RunServerL()
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 void CPEngServer::RunServerL(
       
   207     TPEngServerParams& aParams )
       
   208     {
       
   209     PENG_DP( D_PENG_LIT( "CPEngServer::RunServerL()" ) );
       
   210 
       
   211     //One instance of server must be allocated here.
       
   212     CPEngServer* server = CPEngServer::NewLC( KServerPriority );
       
   213 
       
   214     //must die if can't signal client
       
   215     aParams.Signal( );
       
   216     // start fielding requests from clients
       
   217     //Thread is ended when CPEngActiveScheduler::Stop is called.
       
   218     server->StartServer();
       
   219 
       
   220 
       
   221     CleanupStack::PopAndDestroy();//server
       
   222     // finished
       
   223     PENG_DP( D_PENG_LIT( "CPEngServer::RunServerL() - End" ) );
       
   224     }
       
   225 
       
   226 // =============================================================================
       
   227 // =============== Functions from the MPEngServer class =================
       
   228 // =============================================================================
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CPEngServer::SessionCreated()
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 void CPEngServer::SessionCreated()
       
   235     {
       
   236     iSessCount++;
       
   237     }
       
   238 
       
   239 // -----------------------------------------------------------------------------
       
   240 // CPEngServer::SessionDied()
       
   241 // -----------------------------------------------------------------------------
       
   242 //
       
   243 void CPEngServer::SessionDied()
       
   244     {
       
   245     iSessCount--;
       
   246     CheckServerAccess();
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CPEngServer::AddSubSessionL()
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 void CPEngServer::AddSubSessionL(
       
   254     CPEngSubSession& aSubSession )
       
   255     {
       
   256     iContainer->AddL( &aSubSession );
       
   257     }
       
   258 
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CPEngServer::RemoveSubSessionL()
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 void CPEngServer::RemoveSubSessionL(
       
   265     CPEngSubSession& aSubSession )
       
   266     {
       
   267     iContainer->Remove( &aSubSession );
       
   268     }
       
   269 
       
   270 
       
   271 // -----------------------------------------------------------------------------
       
   272 // CPEngServer::CSPSessionManagerL()
       
   273 // -----------------------------------------------------------------------------
       
   274 //
       
   275 CPEngCSPSessManager* CPEngServer::CSPSessionManagerLC(
       
   276     CPEngSessionSlotId& aSessionId )
       
   277     {
       
   278     // try to find session slot manager
       
   279     TInt count( iSessionsManagers.Count() );
       
   280     for ( TInt x( 0 ) ; x < count  ; ++x )
       
   281         {
       
   282         if ( KErrNone == iSessionsManagers[ x ]->SessionId().Match(
       
   283                  aSessionId ) )
       
   284             {
       
   285             // session manager exists, return it
       
   286             CPEngCSPSessManager* sessManager = iSessionsManagers[ x ];
       
   287             sessManager->Open(); // CSI: 65 #
       
   288             CleanupClosePushL( *sessManager );
       
   289             return sessManager;
       
   290             }
       
   291         }
       
   292     // Session Manager does not exist, create it
       
   293     CPEngCSPSessManager* sessManager = CPEngCSPSessManager::NewLC(
       
   294                                            *this,
       
   295                                            aSessionId,
       
   296                                            iStoreAdmin );
       
   297     iSessionsManagers.AppendL( sessManager );
       
   298     return sessManager;
       
   299     }
       
   300 
       
   301 
       
   302 // -----------------------------------------------------------------------------
       
   303 // CPEngServer::RemoveStateMachine()
       
   304 // -----------------------------------------------------------------------------
       
   305 //
       
   306 void CPEngServer::RemoveCSPSessManager(
       
   307     CPEngCSPSessManager* aManager )
       
   308     {
       
   309     TInt index(  iSessionsManagers.Find( aManager ) );
       
   310     if ( index != KErrNotFound )
       
   311         {
       
   312         iSessionsManagers.Remove( index );
       
   313         }
       
   314     CheckServerAccess();
       
   315     }
       
   316 
       
   317 // -----------------------------------------------------------------------------
       
   318 // CPEngServer::SessionOpened()
       
   319 // -----------------------------------------------------------------------------
       
   320 //
       
   321 void CPEngServer::SessionOpened()
       
   322     {
       
   323     iOpenedSessions++;
       
   324     }
       
   325 
       
   326 // -----------------------------------------------------------------------------
       
   327 // CPEngServer::SessionClosed()
       
   328 // -----------------------------------------------------------------------------
       
   329 //
       
   330 void CPEngServer::SessionClosed()
       
   331     {
       
   332     iOpenedSessions--;
       
   333     }
       
   334 
       
   335 // =============================================================================
       
   336 // =============== New private Functions of the main class =====================
       
   337 // =============================================================================
       
   338 
       
   339 
       
   340 // =============================================================================
       
   341 // =============== Functions from the CServer2 class ===========================
       
   342 // =============================================================================
       
   343 
       
   344 // -----------------------------------------------------------------------------
       
   345 // CPEngServer::NewSessionL()
       
   346 // -----------------------------------------------------------------------------
       
   347 //
       
   348 CSession2* CPEngServer::NewSessionL(
       
   349     const TVersion &aVersion,
       
   350     const RMessage2& /*aMessage*/ ) const
       
   351     {
       
   352     if ( !User::QueryVersionSupported( aVersion,
       
   353                                        TVersion( KClientVersionMajor,
       
   354                                                  KClientVersionMinor,
       
   355                                                  KClientVersionBuild ) ) )
       
   356         {
       
   357         User::Leave( KErrNotSupported );
       
   358         }
       
   359 
       
   360     return CPEngSession::NewL( const_cast<CPEngServer&>( *this ) );
       
   361     }
       
   362 
       
   363 // =============================================================================
       
   364 // =============== New Private methods from class ==============================
       
   365 // =============================================================================
       
   366 
       
   367 // -----------------------------------------------------------------------------
       
   368 // CPEngServer::StartServer()
       
   369 // Start Server
       
   370 // (other items were commented in a header).
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 void CPEngServer::StartServer()
       
   374     {
       
   375     iRunning = ETrue;
       
   376     PENG_DP( D_PENG_LIT( "PresenceServer::Started" ) );
       
   377     CPEngActiveScheduler::Start();
       
   378     PENG_DP( D_PENG_LIT( "PresenceServer::Stopped" ) );
       
   379     }
       
   380 
       
   381 // -----------------------------------------------------------------------------
       
   382 // CPEngServer::CheckServerAccess()
       
   383 // Check server access count
       
   384 // it also check is there is some active session
       
   385 // if server is no more need, it is stoped
       
   386 // (other items were commented in a header).
       
   387 // -----------------------------------------------------------------------------
       
   388 //
       
   389 void CPEngServer::CheckServerAccess()
       
   390     {
       
   391     if ( ( iSessCount + iSessionsManagers.Count() ) == 0 )
       
   392         {
       
   393         // stop server
       
   394         StopServer();
       
   395         }
       
   396     }
       
   397 
       
   398 //  End of File