contentpublishingsrv/contentpublishingserver/cpserver/src/cpserver.cpp
changeset 73 4bc7b118b3df
parent 66 32469d7d46ff
child 80 397d00875918
child 81 5ef31a21fdd5
equal deleted inserted replaced
66:32469d7d46ff 73:4bc7b118b3df
     1 /*
       
     2 * Copyright (c) 2008 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:  Content publisher Server
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <liwcommon.h>
       
    20 #include <ecom/ecom.h>
       
    21 #include <fbs.h>
       
    22 
       
    23 #include "cpublisherregistrymap.h"
       
    24 #include "cpserver.h"
       
    25 #include "cpglobals.h"
       
    26 #include "cpdebug.h"
       
    27 #include "cpserversession.h"
       
    28 #include "cpserverdatamanager.h"
       
    29 #include "cpactionhandlerthread.h"
       
    30 
       
    31 
       
    32 // Policy Server
       
    33 // ============================================================================
       
    34 const TUint KServerPolicyRangeCount = 3;
       
    35 const TInt KServerPolicyRanges[KServerPolicyRangeCount] =
       
    36     {
       
    37     0, // range 0 -- ECpServerExecuteAction - 1
       
    38     ECpServerGetListSize, // range ECpServerGetListSize -- ECpServerExecuteAction - 1
       
    39     ECpServerExecuteAction
       
    40     // range ECpServerExecuteAction -- inf        
       
    41     };
       
    42 
       
    43 const TUint8 KServerPolicyElementsIndex[KServerPolicyRangeCount] =
       
    44     {
       
    45     0, // applies to 0th range
       
    46     1, // applies to 1st range
       
    47     CPolicyServer::EAlwaysPass
       
    48     };
       
    49 
       
    50 const CPolicyServer::TPolicyElement KServerPolicyElements[] =
       
    51     {
       
    52         {
       
    53         _INIT_SECURITY_POLICY_C1(ECapability_None), CPolicyServer::EFailClient
       
    54         },
       
    55         {
       
    56         _INIT_SECURITY_POLICY_C1(ECapability_None), CPolicyServer::EFailClient
       
    57         }
       
    58     };
       
    59 
       
    60 const CPolicyServer::TPolicy KServerPolicy =
       
    61     {
       
    62     CPolicyServer::EAlwaysPass, // specifies all connect attempts should pass
       
    63     KServerPolicyRangeCount,
       
    64     KServerPolicyRanges,
       
    65     KServerPolicyElementsIndex, 
       
    66     KServerPolicyElements
       
    67     };
       
    68 // ============================================================================
       
    69 
       
    70 
       
    71 // ============================ MEMBER FUNCTIONS ===============================
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CCPServer::CCPServer
       
    75 // C++ default constructor can NOT contain any code, that
       
    76 // might leave.
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 CCPServer::CCPServer( TInt aPriority ) :
       
    80     CPolicyServer( aPriority, KServerPolicy, EUnsharableSessions)
       
    81     {
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CCPServer::ConstructL
       
    86 // Symbian 2nd phase constructor can leave.
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 void CCPServer::ConstructL()
       
    90     {
       
    91     CP_DEBUG( _L8("CCPServer::ConstructL()" ) );
       
    92     StartL(KCPServerName);
       
    93     iBURListener = CCPServerBURListener::NewL(this);
       
    94     iCountSession = 0;
       
    95     iBURLock = iBURListener->CheckBUR();
       
    96     iDataManager = CCPDataManager::NewL(iBURLock);
       
    97     iActionHandlerThread = CCPActionHandlerThread::NewL();
       
    98     if (!iBURLock)
       
    99         {
       
   100         TRAP_IGNORE( DeactivatePublishersL() );
       
   101         }
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CCPServer::NewL
       
   106 // Two-phased constructor.
       
   107 // -------------------------------------- ---------------------------------------
       
   108 //
       
   109 CCPServer* CCPServer::NewLC()
       
   110     {
       
   111     CP_DEBUG( _L8("CCPServer::NewLC()" ) );
       
   112     CCPServer* self = new( ELeave ) CCPServer( EPriorityNormal );
       
   113     CleanupStack::PushL( self );
       
   114     self->ConstructL( );
       
   115     return self;
       
   116     }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CCPServer::~CCPServer()
       
   120 // Destructor.
       
   121 // -------------------------------------- ---------------------------------------
       
   122 //    
       
   123 CCPServer::~CCPServer()
       
   124     {
       
   125     CP_DEBUG( _L8("CCPServer::~CCPServer()" ) );
       
   126     for ( TInt i(0); i< iNotifications.Count( ); i++ )
       
   127         {
       
   128         iNotifications[i]->Close( );
       
   129         }
       
   130     iNotifications.Close( );    
       
   131     delete iDataManager;
       
   132     delete iActionHandlerThread;
       
   133     delete iBURListener;
       
   134     }
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // 
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140 void CCPServer::RemoveSession()
       
   141     {
       
   142     CP_DEBUG( _L8("CCPServer::RemoveSession()" ) );
       
   143     iCountSession--;
       
   144     if ( !iCountSession )
       
   145         {
       
   146         Stop( );
       
   147         }
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // 
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 void CCPServer::AddSession()
       
   155     {
       
   156     CP_DEBUG( _L8("CCPServer::AddSession()" ) );
       
   157     iCountSession++;
       
   158     }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // CCPServer::HandleBUREventL
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 void CCPServer::HandleBUREventL( TBURStatus aStatus )
       
   165     {
       
   166     CP_DEBUG( _L8("CCPServer::HandleBUREventL()" ) );
       
   167     if ( (aStatus == EBURStatusBackup ) || (aStatus == EBURStatusRestore ) )
       
   168         {
       
   169         iBURLock = ETrue;
       
   170         iDataManager->CloseDatabase( );
       
   171         }
       
   172     else
       
   173         {
       
   174         iDataManager->OpenDatabaseL( );
       
   175         iBURLock = EFalse;
       
   176         }
       
   177     }
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // CCPServer::GetLock
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 TBool CCPServer::GetLock()
       
   184     {
       
   185     return iBURLock;
       
   186     }
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // CCPServer::GetNotifications
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 RPointerArray<CLiwDefaultList>& CCPServer::GetNotifications( ) 
       
   193     {
       
   194     return iNotifications;
       
   195     }
       
   196 
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CCPServer::NewSessionL
       
   200 // Create new session.
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 CSession2* CCPServer::NewSessionL( const TVersion& /*aVersion*/,
       
   204     const RMessage2& /*aMessage*/) const
       
   205     {
       
   206     CP_DEBUG( _L8("CCPServer::NewSessionL()" ) );
       
   207     TPointersForSession passer;
       
   208     passer.iDataManager = iDataManager;
       
   209     passer.iServer = const_cast<CCPServer*>(this);
       
   210     passer.iActionHandlerThread = iActionHandlerThread;
       
   211     return CCPServerSession::NewL( passer );
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CCPServer::RunError
       
   216 // RunError is called when RunL leaves.
       
   217 // -----------------------------------------------------------------------------
       
   218 //
       
   219 TInt CCPServer::RunError( TInt /*aError*/)
       
   220     {
       
   221     ReStart( );
       
   222     return KErrNone;
       
   223     }
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 // CCPServer::PanicClient
       
   227 // Panic client.
       
   228 // -----------------------------------------------------------------------------
       
   229 //
       
   230 void CCPServer::PanicClient( const RMessage2& aMessage, TCPServerPanic aPanic )
       
   231     {
       
   232     aMessage.Panic( KCPServerName, aPanic );
       
   233     }
       
   234 
       
   235 // -----------------------------------------------------------------------------
       
   236 // CCPServer::PanicServer
       
   237 // Panic server.
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 void CCPServer::PanicServer( TCPServerPanic aPanic )
       
   241     {
       
   242     User::Panic( KCPServerName, aPanic );
       
   243     }
       
   244 
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // CCPServer::SendNotificationL
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 void CCPServer::AppendNotificationL( CCPLiwMap* aMap ) 
       
   251     {
       
   252     aMap->InsertL( KType, TLiwVariant( KPublisher ) );
       
   253     aMap->InsertL( KOperation, TLiwVariant( KOperationUpdate ) );
       
   254     CLiwDefaultList* list = CLiwDefaultList::NewLC( );
       
   255     list->AppendL( TLiwVariant( aMap ) );
       
   256     iNotifications.AppendL( list );
       
   257     CleanupStack::Pop( list );
       
   258     }
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CCPServer::Stop
       
   262 // Stop serwer
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 void CCPServer::Stop()
       
   266     {
       
   267     CP_DEBUG( _L8("CCPServer::Stop()" ) );
       
   268     CActiveScheduler::Stop( );
       
   269     }
       
   270 
       
   271 // -----------------------------------------------------------------------------
       
   272 // CCPServer::ThreadStart(void)
       
   273 // Starts server.
       
   274 // -----------------------------------------------------------------------------
       
   275 //
       
   276 TInt CCPServer::ThreadStart( void )
       
   277     {
       
   278     __UHEAP_MARK;
       
   279 	// Create the server, if one with this name does not already exist.
       
   280 	TFindServer findCountServer( KCPServerName );
       
   281 	TFullName name;
       
   282 	
       
   283 	CTrapCleanup* trapCleanup = CTrapCleanup::New();
       
   284     if ( !trapCleanup )
       
   285         {
       
   286         PanicServer( ECPServerSrvCreateServer );
       
   287         }
       
   288     CActiveScheduler* activeScheduler = new CActiveScheduler;
       
   289     if ( !activeScheduler )
       
   290         {
       
   291         PanicServer( ECPServerSrvCreateServer );
       
   292         }
       
   293     CActiveScheduler::Install( activeScheduler );
       
   294 
       
   295     // Need to check that the server exists.
       
   296     if ( findCountServer.Next( name ) != KErrNone )
       
   297         {
       
   298         User::RenameThread( KCPServerName );
       
   299 
       
   300         TRAPD( err, CreateAndRunServerL() );
       
   301         if ( err != KErrNone )
       
   302             {
       
   303             PanicServer( ECPServerSrvCreateServer );
       
   304             }
       
   305         }
       
   306     
       
   307     delete activeScheduler;
       
   308     delete trapCleanup;
       
   309     __UHEAP_MARKEND;
       
   310     return KErrNone;
       
   311     }
       
   312 
       
   313 // -----------------------------------------------------------------------------
       
   314 // CCPServer::CreateAndRunServerL(void)
       
   315 // Creates and runs server.
       
   316 // -----------------------------------------------------------------------------
       
   317 //
       
   318 void CCPServer::CreateAndRunServerL( void )
       
   319     {
       
   320     // Construct server
       
   321     //
       
   322     CCPServer* server = CCPServer::NewLC( );
       
   323     // Initialisation complete, now signal the client
       
   324     //
       
   325     RProcess::Rendezvous( KErrNone );
       
   326     CActiveScheduler::Start( );    
       
   327     CleanupStack::PopAndDestroy( server );
       
   328     }
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 //
       
   332 // -----------------------------------------------------------------------------
       
   333 //	
       
   334 void CCPServer::DeactivatePublishersL()
       
   335     {
       
   336     CLiwGenericParamList* publishers = CLiwGenericParamList::NewLC( );
       
   337     CCPLiwMap* map = CPublisherRegistryMap::NewLC( );
       
   338     iDataManager->GetActivePublishersL( publishers );
       
   339     TInt pos( 0 );
       
   340     while( pos != KErrNotFound )
       
   341         {
       
   342         publishers->FindFirst( pos, KListMap );
       
   343 
       
   344         if( pos != KErrNotFound )
       
   345             {
       
   346             TLiwVariant variant = ( *publishers )[pos++].Value( );
       
   347 	        map->Reset( );
       
   348             variant.Get( *map );
       
   349             
       
   350 	        // update flag value in the database
       
   351 	        TRAP_IGNORE( ResetAndUpdateFlagL( map ) );
       
   352 
       
   353             //append update notification
       
   354             TRAP_IGNORE( AppendNotificationL( map ) );
       
   355 
       
   356          	variant.Reset( );
       
   357             }
       
   358         }
       
   359     CleanupStack::PopAndDestroy( map );
       
   360     CleanupStack::PopAndDestroy( publishers );
       
   361     }
       
   362 
       
   363 // -----------------------------------------------------------------------------
       
   364 // CCPServer::ResetAndUpdateFlagL
       
   365 // Resets Activate flag and update item in the DB
       
   366 // -----------------------------------------------------------------------------
       
   367 //	
       
   368 void CCPServer::ResetAndUpdateFlagL( CCPLiwMap* aMap )
       
   369     {
       
   370    	TInt32 flag(0);
       
   371     if( !aMap->GetProperty( KFlag, flag ))
       
   372         {
       
   373         User::Leave(KErrNotFound);
       
   374         }
       
   375     aMap->Remove( KFlag );
       
   376     flag &= 0xFFFFFFFE;
       
   377     aMap->InsertL( KFlag, TLiwVariant( flag ) );                
       
   378     iDataManager->AddDataL( *aMap );
       
   379     }
       
   380 
       
   381 // ============================= LOCAL FUNCTIONS ===============================
       
   382 
       
   383 // -----------------------------------------------------------------------------
       
   384 // E32Main entry point.
       
   385 // Returns: KErrNone
       
   386 // -----------------------------------------------------------------------------
       
   387 //
       
   388 TInt E32Main()
       
   389     {
       
   390     return CCPServer::ThreadStart( );
       
   391     }
       
   392 
       
   393 // End of File