landmarks/locationlandmarks/server/src/EPos_CPosLmServerOperation.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: provides functionality common to all server operations
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <EPos_Landmarks.h>
       
    21 #include "EPos_CPosLmServerOperation.h"
       
    22 #include "EPos_LandmarksServerPanics.h"
       
    23 #include "EPos_MPosLmServerOperationObserver.h"
       
    24 #include "EPos_PosLmServerCommon.h"
       
    25 #include "EPos_LmServerGlobal.h"
       
    26 
       
    27 const TInt KDrivePosition = 7;
       
    28 
       
    29 // ============================ MEMBER FUNCTIONS ===============================
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 CPosLmServerOperation::CPosLmServerOperation(
       
    35     CPosLmServer& aServer,
       
    36     MPosLmServerOperationObserver* aObserver ) :
       
    37     CActive(EPriorityIdle),
       
    38     iServer( aServer ),
       
    39     iObserver(aObserver),
       
    40     iProgress(0),
       
    41     iStatusFlag(KPosLmOperationNotComplete)
       
    42     {
       
    43     CActiveScheduler::Add(this);
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 void CPosLmServerOperation::BaseConstructL(
       
    50     const TDesC& aUri)
       
    51     {
       
    52     iDb = CPosLmLocalDbAccess::NewL();
       
    53     User::LeaveIfError(iDb->OpenDatabaseL(aUri));
       
    54 
       
    55     iDiskUtilities = CPosLmDiskUtilities::NewL();
       
    56 
       
    57     iDbDrive = aUri[KDrivePosition];
       
    58     iDbUri = aUri.AllocL();
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 CPosLmServerOperation::~CPosLmServerOperation()
       
    65     {
       
    66     Cancel();
       
    67     if (iStatusFlag == KErrNone)
       
    68         {
       
    69         iStatusFlag = KErrServerTerminated;
       
    70         }
       
    71     CompleteAndRemoveMessagesFromSyncWaitList();
       
    72     CompleteAndRemoveMessagesFromAsyncWaitList();
       
    73     iSyncWaitList.Close();
       
    74     iAsyncWaitList.Close();
       
    75     delete iDiskUtilities;
       
    76     delete iDb;
       
    77     iClientSessions.Close();
       
    78     delete iDbUri;
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 TInt CPosLmServerOperation::RunError( TInt aError )
       
    85     {
       
    86     iStatusFlag = aError;
       
    87     CompleteAndRemoveMessagesFromSyncWaitList();
       
    88     CompleteAndRemoveMessagesFromAsyncWaitList();
       
    89     iObserver->HandleOperationStatus(this, iStatusFlag);
       
    90 
       
    91     return KErrNone;
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 void CPosLmServerOperation::DoCancel()
       
    98     {
       
    99     iStatusFlag = KErrCancel;
       
   100     CompleteAndRemoveMessagesFromSyncWaitList();
       
   101     CompleteAndRemoveMessagesFromAsyncWaitList();
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 void CPosLmServerOperation::AddToSyncWaitListL(
       
   108     const RMessage2& aMessage,
       
   109     TAny* aSession)
       
   110     {
       
   111     User::LeaveIfError(iSyncWaitList.Append(Message(aMessage, aSession)));
       
   112     if (iClientSessions.Find(static_cast<TUint*>(aSession)) == KErrNotFound)
       
   113         {
       
   114         User::LeaveIfError(iClientSessions.Append(
       
   115                     static_cast<TUint*>(aSession)));
       
   116         }
       
   117 
       
   118     if ( iStatusFlag != KPosLmOperationNotComplete )
       
   119         {
       
   120         NotifyClients();
       
   121         }
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 void CPosLmServerOperation::AddToAsyncWaitListL(
       
   128     const RMessage2& aMessage,
       
   129     TAny* aSession )
       
   130     {
       
   131     TPckgBuf<TReal32> progress;
       
   132     TInt desMaxLength = aMessage.GetDesMaxLength(KPosLmServerProgressArg);
       
   133     if (desMaxLength != progress.Length())
       
   134         {
       
   135         PanicClient(aMessage, EPosUnableToReadOrWriteDataToClient);
       
   136         return;
       
   137         }
       
   138     User::LeaveIfError(iAsyncWaitList.Append(Message(aMessage, aSession)));
       
   139     if (iClientSessions.Find(static_cast<TUint*>(aSession)) == KErrNotFound)
       
   140         {
       
   141         User::LeaveIfError(iClientSessions.Append(
       
   142             static_cast<TUint*>(aSession)));
       
   143         }
       
   144     
       
   145     if ( iStatusFlag != KPosLmOperationNotComplete )
       
   146         {
       
   147         // this client should only be informed about operation
       
   148         // status (because it has not been yet) and removed from
       
   149         // operation's clients
       
   150         NotifyClients();
       
   151         RemoveSessionMessages( aSession );
       
   152         }
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 void CPosLmServerOperation::RemoveFromAsyncWaitList(
       
   159     const RMessage2& aMessage,
       
   160     TAny* aSession)
       
   161     {
       
   162     RemoveFromWaitList(iAsyncWaitList, aSession);
       
   163     LmServerGlobal::Complete(aMessage, KErrNone);
       
   164 
       
   165     TInt position = iClientSessions.Find(static_cast<TUint*>(aSession));
       
   166     if (position != KErrNotFound)
       
   167         {
       
   168         iClientSessions.Remove(position);
       
   169         }
       
   170     iObserver->HandleOperationStatus( this, iStatusFlag );
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // -----------------------------------------------------------------------------
       
   175 //
       
   176 void CPosLmServerOperation::RemoveSessionMessages(
       
   177     TAny* aSession)
       
   178     {
       
   179     RemoveFromWaitList(iAsyncWaitList, aSession);
       
   180     RemoveFromWaitList(iSyncWaitList, aSession);
       
   181 
       
   182     TInt position = iClientSessions.Find(static_cast<TUint*>(aSession));
       
   183     if (position != KErrNotFound)
       
   184         {
       
   185         iClientSessions.Remove(position);
       
   186         }
       
   187     iObserver->HandleOperationStatus( this, iStatusFlag );
       
   188     }
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 void CPosLmServerOperation::CompleteAndRemoveMessagesFromAsyncWaitList()
       
   194     {
       
   195     for (TInt i = 0; i < iAsyncWaitList.Count(); i++)
       
   196         {
       
   197         Write(iAsyncWaitList[i].iMsg);
       
   198         LmServerGlobal::Complete(iAsyncWaitList[i].iMsg, iStatusFlag);
       
   199         }
       
   200 
       
   201     iAsyncWaitList.Reset();
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 void CPosLmServerOperation::CompleteAndRemoveMessagesFromSyncWaitList()
       
   208     {
       
   209     for (TInt i = 0; i < iSyncWaitList.Count(); i++)
       
   210         {
       
   211         LmServerGlobal::Complete(iSyncWaitList[i].iMsg, iStatusFlag);
       
   212         }
       
   213 
       
   214     iSyncWaitList.Reset();
       
   215     }
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // -----------------------------------------------------------------------------
       
   219 //
       
   220 void CPosLmServerOperation::Write(
       
   221     const RMessage2& aMessage)
       
   222     {
       
   223     // It is not necessary to return the error code, because the client will be
       
   224     // panicked if the write fails. Hence, the client won't receive the error
       
   225     // anyway.
       
   226     TPckgBuf<TInt> statusFlag(iStatusFlag);
       
   227     TInt err = LmServerGlobal::Write(aMessage, KPosLmServerStatusArg,
       
   228         statusFlag);
       
   229 
       
   230     if (err == KErrNone)
       
   231         {
       
   232         TPckgBuf<TReal32> progress(iProgress);
       
   233         err = LmServerGlobal::Write(aMessage, KPosLmServerProgressArg,
       
   234             progress);
       
   235 
       
   236         if (err == KErrNone && iStatusFlag == KErrNone)
       
   237             {
       
   238             // Reset init flag on client side
       
   239             TPckgBuf<TUint> initFlag(ENoInit);
       
   240             LmServerGlobal::Write(aMessage, KPosLmServerInitFlagArg, initFlag);
       
   241             }
       
   242         }
       
   243     }
       
   244 
       
   245 // -----------------------------------------------------------------------------
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 void CPosLmServerOperation::NotifyClients()
       
   249     {
       
   250     // Check if there are any clients left listening for status
       
   251     if (iClientSessions.Count() == 0)
       
   252         {
       
   253         Cancel();
       
   254         iObserver->HandleOperationStatus(this, iStatusFlag);
       
   255         return;
       
   256         }
       
   257 
       
   258     CompleteAndRemoveMessagesFromAsyncWaitList();
       
   259 
       
   260     if ( iStatusFlag == KErrNone )
       
   261         {
       
   262         CompleteAndRemoveMessagesFromSyncWaitList();
       
   263         iObserver->HandleOperationStatus(this, iStatusFlag);
       
   264         }
       
   265     }
       
   266 
       
   267 // -----------------------------------------------------------------------------
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 void CPosLmServerOperation::CompleteSelf()
       
   271     {
       
   272     TRequestStatus* status = &iStatus;
       
   273     User::RequestComplete(status, KErrNone);
       
   274     SetActive();
       
   275     }
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // -----------------------------------------------------------------------------
       
   279 //
       
   280 void CPosLmServerOperation::RemoveFromWaitList(
       
   281     RArray<TPosMessage>& aWaitList,
       
   282     TAny* aSession)
       
   283     {
       
   284     for (TInt i = 0; i < aWaitList.Count(); i++)
       
   285         {
       
   286         if (aSession == aWaitList[i].iSession)
       
   287             {
       
   288             LmServerGlobal::Complete(aWaitList[i].iMsg, KErrCancel);
       
   289             aWaitList.Remove(i--);
       
   290             }
       
   291         }
       
   292     }
       
   293 
       
   294 // -----------------------------------------------------------------------------
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 CPosLmServerOperation::TPosMessage CPosLmServerOperation::Message(
       
   298     const RMessage2& aMessage,
       
   299     TAny* aSession)
       
   300     {
       
   301     TPosMessage msg;
       
   302     msg.iSession = aSession;
       
   303     msg.iMsg = aMessage;
       
   304 
       
   305     return msg;
       
   306     }
       
   307 
       
   308 // -----------------------------------------------------------------------------
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 TInt CPosLmServerOperation::Status() const
       
   312     {
       
   313     return iStatusFlag;
       
   314     }
       
   315 
       
   316 // -----------------------------------------------------------------------------
       
   317 // -----------------------------------------------------------------------------
       
   318 //
       
   319 TBool CPosLmServerOperation::IsRegistered( TAny* aSession ) const
       
   320     {
       
   321     return iClientSessions.Find( static_cast<TUint*>( aSession ) ) != KErrNotFound;
       
   322     }
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // -----------------------------------------------------------------------------
       
   326 //
       
   327 TBool CPosLmServerOperation::HasRequests( TAny* aSession ) const
       
   328     {
       
   329     for ( TInt i = 0; i < iAsyncWaitList.Count(); i++ )
       
   330         {
       
   331         if ( iAsyncWaitList[i].iSession == aSession )
       
   332             return ETrue;
       
   333         }
       
   334     for ( TInt i = 0; i < iSyncWaitList.Count(); i++ )
       
   335         {
       
   336         if ( iSyncWaitList[i].iSession == aSession )
       
   337             return ETrue;
       
   338         }
       
   339     return EFalse;
       
   340     }
       
   341 
       
   342 // -----------------------------------------------------------------------------
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 TBool CPosLmServerOperation::HasClients() const
       
   346     {
       
   347     return iClientSessions.Count() > 0;;
       
   348     }