landmarks/locationlandmarks/localaccess/src/epos_rposlmlocalnameindex.cpp
changeset 0 667063e416a2
child 8 6fcbaa43369c
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     1 /*
       
     2 * Copyright (c) 2007 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: This class is a handle to server-side landmark name index
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <e32base.h>
       
    21 #include <s32mem.h>
       
    22 #include <epos_poslmservercommon.h>
       
    23 
       
    24 #include "EPos_RPosLmLocalAccessSubsession.h"
       
    25 #include "epos_rposlmlocalnameindex.h"
       
    26 
       
    27 const TInt KInitialBufferSize = 8 * 1024; // 8K
       
    28 
       
    29 // ================= MEMBER FUNCTIONS =======================
       
    30 
       
    31 //--------------------------------------------------------------------
       
    32 // RPosLmLocalNameIndex::CIndexItem
       
    33 //--------------------------------------------------------------------
       
    34 //
       
    35 RPosLmLocalNameIndex::CIndexItem::CIndexItem()
       
    36 : iId ( KPosLmNullItemId )
       
    37     {
       
    38     }
       
    39 
       
    40 RPosLmLocalNameIndex::CIndexItem::~CIndexItem()
       
    41     {
       
    42     delete iName;
       
    43     }
       
    44 
       
    45 RPosLmLocalNameIndex::CIndexItem* RPosLmLocalNameIndex::CIndexItem::NewL( 
       
    46     TPosLmItemId aId, const TDesC& aName )
       
    47     {
       
    48     CIndexItem* self = new (ELeave) CIndexItem;
       
    49     self->iId = aId;
       
    50     self->iName = aName.AllocL();
       
    51     return self;
       
    52     }
       
    53 
       
    54 RPosLmLocalNameIndex::CIndexItem* RPosLmLocalNameIndex::CIndexItem::NewL( 
       
    55     RReadStream& aStream )
       
    56     {
       
    57     CIndexItem* self = new (ELeave) CIndexItem;
       
    58     CleanupStack::PushL( self );
       
    59 
       
    60     self->iId = aStream.ReadUint32L();
       
    61     TUint32 len = aStream.ReadUint32L();
       
    62     self->iName = HBufC::NewL( len );
       
    63     TPtr ptr = self->iName->Des();
       
    64     aStream.ReadL( ptr, len );
       
    65     
       
    66     CleanupStack::Pop( self );
       
    67     return self;
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 RPosLmLocalNameIndex::RPosLmLocalNameIndex( RPosLmLocalAccessSubsession& aSession )
       
    74 :   iSession( aSession ), iBuffer( NULL )
       
    75     {
       
    76     }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 TInt RPosLmLocalNameIndex::Open()
       
    82     {
       
    83     iBuffer = HBufC8::New( KInitialBufferSize );
       
    84     if ( !iBuffer )
       
    85         {
       
    86         return KErrNoMemory;
       
    87         }
       
    88     return iSession.SendReceive( EPosLmServerOpenNameIndex );
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 void RPosLmLocalNameIndex::Close()
       
    95     {
       
    96     iSession.SendReceive( EPosLmServerCloseNameIndex );
       
    97     delete iBuffer;
       
    98     iBuffer = NULL;
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 EXPORT_C TInt RPosLmLocalNameIndex::Count() const
       
   105     {
       
   106     TInt result = iSession.SendReceive( EPosLmServerNameIndexStatus );
       
   107     if ( result >= 0 )
       
   108         return result;
       
   109     else
       
   110         return 0;
       
   111     }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // -----------------------------------------------------------------------------
       
   115 //
       
   116 EXPORT_C TInt RPosLmLocalNameIndex::Status() const
       
   117     {
       
   118     TInt result = iSession.SendReceive( EPosLmServerNameIndexStatus );
       
   119     if ( result < 0 )
       
   120         return result;
       
   121     else
       
   122         return KErrNone;
       
   123     }
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 EXPORT_C TInt RPosLmLocalNameIndex::ReadSortedIds( 
       
   129     TPosLmItemId* aIdArray, TInt aStartPos, TInt aCount, TInt& aRemainder )
       
   130     {
       
   131     TPosLmServerReadArrayParam param;
       
   132     param.iFirst = aStartPos;
       
   133     param.iCount = aCount;
       
   134 
       
   135     TPckg<TPosLmServerReadArrayParam> pack( param );
       
   136     TPtr8 data( (TUint8*) aIdArray, sizeof( TPosLmItemId ) * aCount );
       
   137     TInt err = iSession.SendReceive( EPosLmServerReadSortedIds, TIpcArgs( &pack, &data ));
       
   138     if ( err >= 0 )
       
   139         {
       
   140         aRemainder = param.iRemainder;
       
   141         return param.iActualCount;
       
   142         }
       
   143     else
       
   144         {
       
   145         return err;
       
   146         }
       
   147     }
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 void RPosLmLocalNameIndex::AddL( const CPosLandmark& aLandmark )
       
   153     {
       
   154     RPointerArray<CPosLandmark> landmarks; // does not own items
       
   155     CleanupClosePushL( landmarks );
       
   156 
       
   157     landmarks.AppendL( &aLandmark );
       
   158     AddL( landmarks );
       
   159     
       
   160     CleanupStack::PopAndDestroy( &landmarks );
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 void RPosLmLocalNameIndex::AddL( RPointerArray<CPosLandmark>& aLandmarks )
       
   167     {
       
   168     SendLandmarkDataL( EPosLmServerAddLandmarks, aLandmarks );
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 void RPosLmLocalNameIndex::UpdateL( const CPosLandmark& aLandmark )
       
   175     {
       
   176     RPointerArray<CPosLandmark> landmarks; // does not own items
       
   177     CleanupClosePushL( landmarks );
       
   178 
       
   179     landmarks.AppendL( &aLandmark );
       
   180     UpdateL( landmarks );
       
   181     
       
   182     CleanupStack::PopAndDestroy( &landmarks );
       
   183     }
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 void RPosLmLocalNameIndex::UpdateL( RPointerArray<CPosLandmark>& aLandmarks )
       
   189     {
       
   190     SendLandmarkDataL( EPosLmServerUpdateLandmarks, aLandmarks );
       
   191     }
       
   192 
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // Server does not necessarily return all requested items at once
       
   196 // it depends on size of actual data. Client will need to repeat request
       
   197 // to get all needed landmarks.
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 EXPORT_C void RPosLmLocalNameIndex::ReadSortedLandmarksL( 
       
   201     TInt aFirst, TInt aCount,
       
   202     RPointerArray<CIndexItem>& aLandmarks,
       
   203     TInt& aRemainder)
       
   204     {
       
   205     TPosLmServerReadArrayParam params;
       
   206     params.iFirst = aFirst;
       
   207     params.iCount = aCount;
       
   208     params.iActualCount = 0;
       
   209     params.iRemainder = 0;
       
   210     params.iMinBufferNeeded = 0;
       
   211     TPckg<TPosLmServerReadArrayParam> packParams( params );
       
   212 
       
   213     TPtr8 buf = iBuffer->Des();
       
   214     buf.SetMax();
       
   215     
       
   216     User::LeaveIfError( iSession.SendReceive( 
       
   217         EPosLmServerReadNameIndex, TIpcArgs( &packParams, &buf ) ) );
       
   218         
       
   219     if ( params.iActualCount )
       
   220         {
       
   221         RDesReadStream readStream;
       
   222         readStream.Open( *iBuffer );
       
   223         CleanupClosePushL( readStream );
       
   224     
       
   225         for ( TInt i = 0; i < params.iActualCount; i++ )
       
   226             {
       
   227             CIndexItem* item = CIndexItem::NewL( readStream );
       
   228             CleanupStack::PushL( item );
       
   229             aLandmarks.AppendL( item );
       
   230             CleanupStack::Pop( item );
       
   231             }
       
   232         CleanupStack::PopAndDestroy( &readStream );
       
   233         }
       
   234 
       
   235     // check if server requests bigger buffer for next landmark
       
   236     if ( params.iMinBufferNeeded > 0 ) 
       
   237         {
       
   238         iBuffer = iBuffer->ReAllocL(iBuffer->Length() + params.iMinBufferNeeded );
       
   239         }
       
   240     
       
   241     aRemainder = params.iRemainder;
       
   242     }
       
   243 
       
   244 // -----------------------------------------------------------------------------
       
   245 // -----------------------------------------------------------------------------
       
   246 //
       
   247 void RPosLmLocalNameIndex::RemoveL( TPosLmItemId aLmid )
       
   248     {
       
   249     RArray<TPosLmItemId> ids;
       
   250     CleanupClosePushL( ids );
       
   251 
       
   252     ids.AppendL( aLmid );
       
   253     RemoveL( ids );
       
   254     
       
   255     CleanupStack::PopAndDestroy( &ids );
       
   256     }
       
   257 
       
   258 // -----------------------------------------------------------------------------
       
   259 // -----------------------------------------------------------------------------
       
   260 //
       
   261 void RPosLmLocalNameIndex::RemoveL( RArray<TPosLmItemId>& aLmids )
       
   262     {
       
   263     TInt lmCount = aLmids.Count();
       
   264     if ( lmCount < 1 )
       
   265         return;
       
   266     
       
   267     TPosLmItemId* ids = new (ELeave) TPosLmItemId[ lmCount ];
       
   268     
       
   269     for ( TUint32 i = 0; i < lmCount; i++ )
       
   270         {
       
   271         *(ids + i) = aLmids[i];
       
   272         }
       
   273         
       
   274     TPtrC8 idsDes( reinterpret_cast<const TUint8*>( ids ), sizeof( TPosLmItemId ) * lmCount );
       
   275 
       
   276     TInt err = iSession.SendReceive( EPosLmServerUpdateNameIndex, 
       
   277         TIpcArgs( EPosLmServerRemoveLandmarks, &idsDes ) );
       
   278     delete ids;
       
   279     User::LeaveIfError( err );
       
   280     }
       
   281 
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // -----------------------------------------------------------------------------
       
   285 //
       
   286 TInt RPosLmLocalNameIndex::BeginTransaction()
       
   287     {
       
   288     return iSession.SendReceive( 
       
   289         EPosLmServerIndexTransaction, TIpcArgs( EPosLmServerTransactionStart ) );
       
   290     }
       
   291 
       
   292 // -----------------------------------------------------------------------------
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 TInt RPosLmLocalNameIndex::CommitTransaction()
       
   296     {
       
   297     return iSession.SendReceive( 
       
   298         EPosLmServerIndexTransaction, TIpcArgs( EPosLmServerTransactionCommit ) );
       
   299     }
       
   300 
       
   301 // -----------------------------------------------------------------------------
       
   302 // -----------------------------------------------------------------------------
       
   303 //
       
   304 TInt RPosLmLocalNameIndex::RollbackTransaction()
       
   305     {
       
   306     return iSession.SendReceive( 
       
   307         EPosLmServerIndexTransaction, TIpcArgs( EPosLmServerTransactionRollback ) );
       
   308     }
       
   309 
       
   310 // -----------------------------------------------------------------------------
       
   311 // -----------------------------------------------------------------------------
       
   312 //
       
   313 TInt RPosLmLocalNameIndex::CalculateLandmarkBasicDataSize( 
       
   314     RPointerArray<CPosLandmark>& aLandmarks )
       
   315     {
       
   316     TInt dataSize = 0;
       
   317     for (int i = 0; i < aLandmarks.Count(); ++i)
       
   318         {
       
   319         TPtrC name;
       
   320         aLandmarks[i]->GetLandmarkName( name );
       
   321         dataSize += name.Size() 
       
   322             + sizeof( TPosLmItemId ) 
       
   323             + sizeof( TInt32 ); // + ID + name length
       
   324         }
       
   325     return dataSize + sizeof( TInt32 ); // + total count
       
   326     }
       
   327 
       
   328 // -----------------------------------------------------------------------------
       
   329 // -----------------------------------------------------------------------------
       
   330 //
       
   331 void RPosLmLocalNameIndex::SendLandmarkDataL( 
       
   332     TInt aFunction,
       
   333     RPointerArray<CPosLandmark>& aLandmarks )
       
   334     {
       
   335     if ( aLandmarks.Count() < 1 )
       
   336         return;
       
   337     
       
   338     TInt dataSize = CalculateLandmarkBasicDataSize( aLandmarks );
       
   339     
       
   340     CBufFlat* lmBuffer = CBufFlat::NewL( dataSize );
       
   341     CleanupStack::PushL( lmBuffer );
       
   342 
       
   343     RBufWriteStream writeStream;
       
   344     writeStream.Open( *lmBuffer );
       
   345     CleanupClosePushL( writeStream );
       
   346 
       
   347     writeStream.WriteInt32L( aLandmarks.Count() );
       
   348 
       
   349     for ( TInt i = 0; i < aLandmarks.Count(); i++ )
       
   350         {
       
   351         TPtrC name;
       
   352         aLandmarks[i]->GetLandmarkName( name );
       
   353         
       
   354         writeStream.WriteInt32L( aLandmarks[i]->LandmarkId() );
       
   355         writeStream.WriteInt32L( name.Length() );
       
   356         writeStream.WriteL( name );
       
   357         }
       
   358     writeStream.CommitL();
       
   359     CleanupStack::PopAndDestroy( &writeStream );
       
   360 
       
   361     TPtr8 lmBufferPtr( lmBuffer->Ptr( 0 ) );
       
   362     User::LeaveIfError( iSession.SendReceive( 
       
   363         EPosLmServerUpdateNameIndex, 
       
   364         TIpcArgs( aFunction, &lmBufferPtr ) ) );
       
   365 
       
   366     CleanupStack::PopAndDestroy( lmBuffer );
       
   367     }