landmarks/locationlandmarks/server/src/EPos_CPosLmServer.cpp
changeset 0 667063e416a2
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     1 /*
       
     2 * Copyright (c) 2002-2005 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: The main server side class of Landmarks Server.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32svr.h>
       
    20 #include <epos_cposlmresourcereader.h>
       
    21 #include <eposlmserver.rsg>
       
    22 #include "EPos_CPosLmDbRegistry.h"
       
    23 #include "EPos_CPosLmServerSession.h"
       
    24 #include "EPos_CPosLmServerSettingsHandler.h"
       
    25 #include "EPos_PosLmServerCommon.h"
       
    26 #include "EPos_CPosLmServerDelayedShutdown.h"
       
    27 #include "EPos_LmServerGlobal.h"
       
    28 #include "EPos_LandmarksServerPanics.h"
       
    29 #include "EPos_CPosLmBackupListener.h"
       
    30 #include "EPos_LmServerGlobal.h"
       
    31 #include "epos_cposlmindexmanager.h"
       
    32 #include "EPos_CPosLmServer.h"
       
    33 
       
    34 _LIT(KPosResourceFile, "\\Private\\101fdf81\\eposlmserver.rsc");
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS ===============================
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CPosLmServer::CPosLmServer(TInt aPriority)
       
    42     : CPolicyServer(aPriority, KPolicy)
       
    43     {
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 void CPosLmServer::ConstructL()
       
    50     {
       
    51     iBackupListener = CPosLmBackupListener::NewL(this);
       
    52     LeaveIfBackupIsRunningL();
       
    53 
       
    54     iServerSettingsHandler = CPosLmServerSettingsHandler::NewL(*this);
       
    55     User::LeaveIfError(iServerSettingsHandler->StartListening());
       
    56     iShutdown = CPosLmServerDelayedShutdown::NewL();
       
    57 
       
    58     CPosLmResourceReader* resources =
       
    59         CPosLmResourceReader::NewLC(KPosResourceFile);
       
    60     iSrvShutdownDelay = resources->ReadInt32L(R_POS_LM_SERVER_SHUTDOWN_DELAY);
       
    61 
       
    62     iDbPath = resources->ReadHBufCL(R_POS_LM_SERVER_DB_PATH_FULL);
       
    63     CleanupStack::PopAndDestroy(resources);
       
    64 
       
    65     iIndexManager = CPosLmIndexManager::NewL( *this );
       
    66     
       
    67     User::LeaveIfError(iFs.Connect());
       
    68     StartL(KPosLandmarksServerName);
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 CPosLmServer* CPosLmServer::NewL()
       
    75     {
       
    76     CPosLmServer* self = new (ELeave) CPosLmServer(EPriorityHigh);
       
    77     CleanupStack::PushL(self);
       
    78     self->ConstructL();
       
    79     CleanupStack::Pop();
       
    80     return self;
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 CPosLmServer::~CPosLmServer()
       
    87     {
       
    88     Cancel();
       
    89     iManagers.DeleteAll();
       
    90     delete iIndexManager;
       
    91     delete iServerSettingsHandler;
       
    92     delete iShutdown;
       
    93     CloseAllRegistries();
       
    94     iRegistryArray.Close();
       
    95     delete iDbPath;
       
    96     delete iBackupListener;
       
    97     iFs.Close();
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 TInt CPosLmServer::RunError(
       
   104     TInt aError)
       
   105     {
       
   106     Message().Complete(aError);
       
   107     ReStart();
       
   108     return KErrNone;
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void CPosLmServer::HandleManagerFinished()
       
   115     {
       
   116     for (TInt i = 0; i < iManagers.Count(); i++)
       
   117         {
       
   118         if (iManagers[i] && iManagers[i]->Count() == 0)
       
   119             {
       
   120             // Manager is not used any more, so delete it.
       
   121             delete iManagers[i];
       
   122             iManagers[i] = NULL;
       
   123             }
       
   124         }
       
   125     }
       
   126 
       
   127 // ---------------------------------------------------------
       
   128 // ---------------------------------------------------------
       
   129 //
       
   130 void CPosLmServer::HandleBackupEvent()
       
   131     {
       
   132     CloseAllRegistries();
       
   133     }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 CSession2* CPosLmServer::NewSessionL(
       
   139     const TVersion& aVersion, const RMessage2& /*aMessage*/) const
       
   140     {
       
   141     if (iNumSessions == 0)
       
   142         {
       
   143         // OK to call cancel even if shutdown is not started
       
   144         iShutdown->Cancel();
       
   145         }
       
   146 
       
   147     // Check we're the right version
       
   148     TVersion version(KPosLmServerMajorVersionNumber,
       
   149                      KPosLmServerMinorVersionNumber,
       
   150                      KPosLmServerBuildVersionNumber);
       
   151     if (!User::QueryVersionSupported(version, aVersion))
       
   152         {
       
   153         User::Leave(KErrNotSupported);
       
   154         }
       
   155 
       
   156     // Make a new session
       
   157     CPosLmServerSession* newSession = CPosLmServerSession::NewL(
       
   158         *(const_cast<CPosLmServer*> (this)));
       
   159 
       
   160     return newSession;
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 CPolicyServer::TCustomResult CPosLmServer::CustomSecurityCheckL(
       
   167     const RMessage2& aMsg,
       
   168     TInt& /*aAction*/,
       
   169     TSecurityInfo& /*aMissing*/)
       
   170     {
       
   171     switch (aMsg.Function())
       
   172         {
       
   173         case EPosLmServerDbLock:
       
   174             if (static_cast<TBool>(aMsg.Int1()))
       
   175                 {
       
   176                 // Write lock
       
   177                 if (aMsg.HasCapability(ECapabilityReadUserData,
       
   178                     ECapabilityWriteUserData,
       
   179                     __PLATSEC_DIAGNOSTIC_STRING(
       
   180                         "Landmarks: Attempt made to fetch database write lock."
       
   181                         )))
       
   182                     {
       
   183                     return EPass;
       
   184                     }
       
   185                 }
       
   186             else
       
   187                 {
       
   188                 // Read lock
       
   189                 if (aMsg.HasCapability(ECapabilityReadUserData,
       
   190                     __PLATSEC_DIAGNOSTIC_STRING(
       
   191                     "Landmarks: Attempt made to fetch database read lock."
       
   192                     )))
       
   193                     {
       
   194                     return EPass;
       
   195                     }
       
   196                 }
       
   197             break;
       
   198         default:
       
   199             break;
       
   200         }
       
   201 
       
   202     return EFail;
       
   203     }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // -----------------------------------------------------------------------------
       
   207 //
       
   208 CPosLmOperationManager* CPosLmServer::OperationManagerL( TInt aFunction )
       
   209     {
       
   210     // Initialize to dummy value
       
   211     CPosLmOperationManager::TPosOperationManagerType type =
       
   212         CPosLmOperationManager::EPosMaxNumberOfManagers;
       
   213 
       
   214     switch (aFunction)
       
   215         {
       
   216         case EPosLmServerInitializeSync:
       
   217         case EPosLmServerInitializeAsync:
       
   218         case EPosLmServerInitializeCancel:
       
   219         case EPosLmServerLangSwitchSync:
       
   220         case EPosLmServerLangSwitchAsync:
       
   221         case EPosLmServerLangSwitchCancel:
       
   222         case EPosLmServerRecoverSync:
       
   223         case EPosLmServerRecoverAsync:
       
   224         case EPosLmServerRecoverCancel:
       
   225             type = CPosLmOperationManager::EPosInitializerManager;
       
   226             break;
       
   227         default:
       
   228             User::Leave(KErrArgument);
       
   229             break;
       
   230         }
       
   231 
       
   232     if (!iManagers[type])
       
   233         {
       
   234         iManagers[type] = CPosLmOperationManager::NewL( *this, type, this );
       
   235         }
       
   236 
       
   237     return iManagers[type];
       
   238     }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // -----------------------------------------------------------------------------
       
   242 //
       
   243 void CPosLmServer::IncrementSessions()
       
   244     {
       
   245     iNumSessions++;
       
   246     }
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 void CPosLmServer::DecrementSessions()
       
   252     {
       
   253     // This panic handles an internal error.
       
   254     __ASSERT_DEBUG(iNumSessions > 0,
       
   255         PanicServer(EPosLmServerPanicSessionsCountInconsistency));
       
   256 
       
   257     if (--iNumSessions == 0)
       
   258         {
       
   259         iManagers.DeleteAll(); // Stops all initialize operations
       
   260         iManagers.Reset();
       
   261         CloseAllRegistries();
       
   262 
       
   263         if (!(iShutdown->IsActive()))
       
   264             {
       
   265             iShutdown->Start(iSrvShutdownDelay);
       
   266             }
       
   267         }
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // -----------------------------------------------------------------------------
       
   272 //
       
   273 TInt CPosLmServer::StartSettingsListenerIfNeeded()
       
   274     {
       
   275     if (iServerSettingsHandler->IsActive())
       
   276         {
       
   277         return KErrNone;
       
   278         }
       
   279 
       
   280     return iServerSettingsHandler->StartListening();
       
   281     }
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // -----------------------------------------------------------------------------
       
   285 //
       
   286 TBool CPosLmServer::CanAcquireLock(
       
   287     TBool aWriteLock,
       
   288     const TDesC& aUri)
       
   289     {
       
   290     iSessionIter.SetToFirst();
       
   291     CPosLmServerSession* session =
       
   292         static_cast<CPosLmServerSession*>(iSessionIter++);
       
   293 
       
   294     // check locks from every session/subsession
       
   295     while (session)
       
   296         {
       
   297         if ((aWriteLock && session->HasAnyLock(aUri)) ||
       
   298             (!aWriteLock && session->HasWriteLock(aUri)))
       
   299             {
       
   300             return EFalse;
       
   301             }
       
   302         session = static_cast<CPosLmServerSession*>(iSessionIter++);
       
   303         }
       
   304     
       
   305     // check index manager as well
       
   306     if ( aWriteLock && IndexManager().HasAnyLock( aUri ) ||
       
   307         (!aWriteLock && IndexManager().HasWriteLock( aUri )))
       
   308         {
       
   309         return EFalse;
       
   310         }
       
   311     
       
   312     return ETrue;
       
   313     }
       
   314 
       
   315 // -----------------------------------------------------------------------------
       
   316 // -----------------------------------------------------------------------------
       
   317 //
       
   318 void CPosLmServer::RemoveSessionMessages(
       
   319     TAny* aSession)
       
   320     {
       
   321     for (TInt i = 0; i < iManagers.Count(); i++)
       
   322         {
       
   323         if (iManagers[i])
       
   324             {
       
   325             iManagers[i]->RemoveSessionMessages(aSession);
       
   326             }
       
   327         }
       
   328     }
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 // -----------------------------------------------------------------------------
       
   332 //
       
   333 void CPosLmServer::ForwardEventToAllSessionsL(
       
   334     TPosLmDatabaseEvent aEvent,
       
   335     const TDesC& aUri)
       
   336     {
       
   337     // For all database management sessions, call HandleOperationEventL()
       
   338     iSessionIter.SetToFirst();
       
   339     CPosLmServerSession* session =
       
   340         static_cast<CPosLmServerSession*>(iSessionIter++);
       
   341 
       
   342     while (session)
       
   343         {
       
   344         session->HandleOperationEventL(aEvent, aUri);
       
   345         session = static_cast<CPosLmServerSession*>(iSessionIter++);
       
   346         }
       
   347     }
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // -----------------------------------------------------------------------------
       
   351 //
       
   352 void CPosLmServer::ForwardEventToAllSessionsL(
       
   353     TPosLmEvent aEvent,
       
   354     const TDesC& aUri,
       
   355     TBool aCheckUri)
       
   356     {
       
   357      // For all Local Access sessions, call HandleOperationEventL()
       
   358     iSessionIter.SetToFirst();
       
   359     CPosLmServerSession* session =
       
   360         static_cast<CPosLmServerSession*>(iSessionIter++);
       
   361 
       
   362     while (session)
       
   363         {
       
   364         session->HandleOperationEventL(aEvent, aUri, aCheckUri);
       
   365         session = static_cast<CPosLmServerSession*>(iSessionIter++);
       
   366         }
       
   367     }
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // -----------------------------------------------------------------------------
       
   371 //
       
   372 void CPosLmServer::CompleteOutstandingEventRequests(
       
   373     TInt aError)
       
   374     {
       
   375     // For all Local Access sessions, call HandleOperationEventL()
       
   376     iSessionIter.SetToFirst();
       
   377     CPosLmServerSession* session =
       
   378         static_cast<CPosLmServerSession*>(iSessionIter++);
       
   379 
       
   380     while (session)
       
   381         {
       
   382         session->CompleteOutstandingEventRequest(aError);
       
   383         session = static_cast<CPosLmServerSession*>(iSessionIter++);
       
   384         }
       
   385     }
       
   386 
       
   387 // -----------------------------------------------------------------------------
       
   388 // -----------------------------------------------------------------------------
       
   389 //
       
   390 CPosLmDbRegistry* CPosLmServer::GetRegistryL(TChar aDrive, TBool aCreateDb)
       
   391     {
       
   392     LeaveIfBackupIsRunningL();
       
   393 
       
   394     aDrive.UpperCase();
       
   395 
       
   396     HBufC* buf = HBufC::NewLC(iDbPath->Length() + 1);
       
   397     buf->Des().Append(aDrive);
       
   398     buf->Des().Append(*iDbPath);
       
   399 
       
   400     //Return the registry if it is already opened for the certain drive.
       
   401     for (TInt i = 0; i < iRegistryArray.Count(); i++)
       
   402         {
       
   403         TRegistryInfo regInfo = iRegistryArray[i];
       
   404         if (regInfo.iDrive == aDrive)
       
   405             {
       
   406             CleanupStack::PopAndDestroy(buf);
       
   407             return regInfo.iRegistry;
       
   408             }
       
   409         }
       
   410 
       
   411     CPosLmDbRegistry* dbReg = CPosLmDbRegistry::NewL();
       
   412     CleanupStack::PushL(dbReg);
       
   413 
       
   414     TInt err = dbReg->Open(iFs, *buf);
       
   415     if (err == KErrNotFound ||
       
   416         err == KErrPathNotFound ||
       
   417         err == KErrCorrupt)
       
   418         {
       
   419         if (!aCreateDb)
       
   420             {
       
   421             CleanupStack::PopAndDestroy(2, buf); //dbReg
       
   422             return NULL;
       
   423             }
       
   424 
       
   425         TInt driveNr;
       
   426         User::LeaveIfError(RFs::CharToDrive(aDrive, driveNr));
       
   427         err = iFs.CreatePrivatePath(driveNr);
       
   428         if (err == KErrNone)
       
   429             {
       
   430             iFs.SetSessionToPrivate(driveNr);
       
   431             }
       
   432         dbReg->CreateL(iFs, *buf);
       
   433         }
       
   434     else if (!aCreateDb && (err == KErrNotReady || err == KErrAccessDenied))
       
   435         {
       
   436         // If media is not present or access denied and
       
   437         // not create the db, otherwise leave with err
       
   438         CleanupStack::PopAndDestroy(2, buf); //dbReg
       
   439         return NULL;
       
   440         }
       
   441     else
       
   442         {
       
   443         User::LeaveIfError(err);
       
   444         }
       
   445 
       
   446     TRegistryInfo regInfo;
       
   447     regInfo.iDrive = aDrive;
       
   448     regInfo.iRegistry = dbReg;
       
   449 
       
   450     User::LeaveIfError(iRegistryArray.Append(regInfo));
       
   451 
       
   452     CleanupStack::Pop(dbReg);
       
   453     CleanupStack::PopAndDestroy(buf);
       
   454     return dbReg;
       
   455     }
       
   456 
       
   457 // -----------------------------------------------------------------------------
       
   458 // -----------------------------------------------------------------------------
       
   459 //
       
   460 RFs& CPosLmServer::FileSession()
       
   461     {
       
   462     return iFs;
       
   463     }
       
   464 
       
   465 // -----------------------------------------------------------------------------
       
   466 // -----------------------------------------------------------------------------
       
   467 //
       
   468 void CPosLmServer::LeaveIfBackupIsRunningL()
       
   469     {
       
   470     if (iBackupListener->IsBackupRunning())
       
   471         {
       
   472         User::Leave(KErrNotReady);
       
   473         }
       
   474     }
       
   475 
       
   476 // -----------------------------------------------------------------------------
       
   477 // -----------------------------------------------------------------------------
       
   478 //
       
   479 CPosLmIndexManager& CPosLmServer::IndexManager()
       
   480     {
       
   481     return *iIndexManager;
       
   482     }
       
   483 
       
   484 // -----------------------------------------------------------------------------
       
   485 // -----------------------------------------------------------------------------
       
   486 //
       
   487 void CPosLmServer::CloseAllRegistries()
       
   488     {
       
   489     for (TInt i = 0; i < iRegistryArray.Count(); i++)
       
   490         {
       
   491         delete iRegistryArray[i].iRegistry;
       
   492         iRegistryArray[i].iRegistry = NULL;
       
   493         }
       
   494     iRegistryArray.Reset();
       
   495     }
       
   496