loggingservices/eventlogger/LogServ/src/LogServViewWindowFetcher.cpp
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "LogServViewWindowFetcher.h"
       
    17 #include <s32mem.h>
       
    18 #include "LOGGET.H"
       
    19 #include "logservpanic.h"
       
    20 #include "LogServView.h"
       
    21 
       
    22 // Constants
       
    23 const TInt KLogViewWindowTransferBufferGranularity = 500;
       
    24 
       
    25 
       
    26 /////////////////////////////////////////////////////////////////////////////////////////
       
    27 // -----> CLogServViewWindowFetcher (source)
       
    28 /////////////////////////////////////////////////////////////////////////////////////////
       
    29 
       
    30 CLogServViewWindowFetcher::CLogServViewWindowFetcher(MLogServDatabaseTransactionInterface& aDatabase, TInt aPriority)
       
    31 :	CLogActive(aPriority), iDatabase(aDatabase)
       
    32 	{
       
    33 	}
       
    34 
       
    35 CLogServViewWindowFetcher::~CLogServViewWindowFetcher()
       
    36 	{
       
    37 	Cancel();
       
    38 	delete iBuffer;
       
    39 	delete iGetEvent;
       
    40 	delete iEvent;
       
    41 	}
       
    42 
       
    43 void CLogServViewWindowFetcher::ConstructL()
       
    44 	{
       
    45 	iBuffer = CBufFlat::NewL(KLogViewWindowTransferBufferGranularity);
       
    46 	iEvent = CLogEvent::NewL();
       
    47 	iGetEvent = CLogGetEvent::NewL(iDatabase, Priority());
       
    48 	}
       
    49 
       
    50 CLogServViewWindowFetcher* CLogServViewWindowFetcher::NewL(MLogServDatabaseTransactionInterface& aDatabase, TInt aPriority)
       
    51 	{
       
    52 	CLogServViewWindowFetcher* self = new(ELeave) CLogServViewWindowFetcher(aDatabase, aPriority);
       
    53 	CleanupStack::PushL(self);
       
    54 	self->ConstructL();
       
    55 	CleanupStack::Pop(self);
       
    56 	return self;
       
    57 	}
       
    58 
       
    59 /////////////////////////////////////////////////////////////////////////////////////////
       
    60 /////////////////////////////////////////////////////////////////////////////////////////
       
    61 /////////////////////////////////////////////////////////////////////////////////////////
       
    62 
       
    63 void CLogServViewWindowFetcher::StartL(TRequestStatus& aStatus, const CLogServViewBase& aView, const TLogTransferWindow& aWindow, const RMessage2& aMessage)
       
    64 	{
       
    65 	__ASSERT_ALWAYS(iState == EStateIdle, Panic(ELogViewWindowFetcherBadState));
       
    66 	//
       
    67 	Queue(aStatus);
       
    68 	//
       
    69 	iView = &aView;
       
    70 	iWindow = aWindow;
       
    71 	iMessage = &aMessage;
       
    72 	iBuffer->Reset();
       
    73 	iWindowIndex = 0;
       
    74 	iState = EStateStarting;
       
    75 	//
       
    76 	CompleteRequest();
       
    77 	}
       
    78 
       
    79 /////////////////////////////////////////////////////////////////////////////////////////
       
    80 /////////////////////////////////////////////////////////////////////////////////////////
       
    81 /////////////////////////////////////////////////////////////////////////////////////////
       
    82 
       
    83 void CLogServViewWindowFetcher::DoRunL()
       
    84 	{
       
    85 	const TInt error = iStatus.Int();
       
    86 	User::LeaveIfError(error);
       
    87 
       
    88 	switch(iState)
       
    89 		{
       
    90 	case EStateStarting:
       
    91 		GetNextEventL(iWindowIndex);
       
    92 		iState = EStateContinuing;
       
    93 		break;
       
    94 	case EStateContinuing:
       
    95 		ProcessEventL();
       
    96 		break;
       
    97 	default:
       
    98 		__ASSERT_ALWAYS(0, Panic(ELogViewWindowFetcherBadState2));
       
    99 		break;
       
   100 		}
       
   101 	}
       
   102 
       
   103 void CLogServViewWindowFetcher::ProcessEventL()
       
   104 	{
       
   105 	// Try and write the event to the buffer
       
   106 	const TInt sizeBefore = iBuffer->Size();
       
   107 	RBufWriteStream stream(*iBuffer, iBuffer->Size());
       
   108 	stream << *iEvent;
       
   109 	const TInt sizeAfter = iBuffer->Size();
       
   110 	//
       
   111 	TBool moreToFetch = EFalse;
       
   112 
       
   113 	const TInt numberToFetch = iWindow.Range();
       
   114 	if	(sizeAfter > iWindow.iBufferSize)
       
   115 		{
       
   116 		//The client buffer size is too small. It should be increased, if the client wants
       
   117 		//to get the server data. 
       
   118 		//The server sets iServerDataSize data member with the minimal size which the client
       
   119 		//side buffer should have - sizeAfter.
       
   120 		TPtrC8 ptr(reinterpret_cast <TUint8*> (&iWindow), sizeof(iWindow));
       
   121 		iWindow.iServerDataSize = sizeAfter;
       
   122 		iMessage->WriteL(2, ptr);
       
   123 
       
   124 		iBuffer->ResizeL(sizeBefore);
       
   125 		iWindowIndex -= 1; // we didn't get this event
       
   126 		}
       
   127 	else if	(iWindowIndex+1 < numberToFetch)
       
   128 		{
       
   129 		GetNextEventL(iWindowIndex+1);
       
   130 		moreToFetch = ETrue;
       
   131 		}
       
   132 	
       
   133 	if	(!moreToFetch)
       
   134 		{
       
   135 		// Write whatever we have back to the client's address space
       
   136 		TPtr8 pBuffer(iBuffer->Ptr(0));
       
   137 		iMessage->WriteL(3, pBuffer);
       
   138 		iState = EStateIdle;
       
   139 		}
       
   140 	}
       
   141 
       
   142 void CLogServViewWindowFetcher::DoCancel()
       
   143 	{
       
   144 	switch(iState)
       
   145 		{
       
   146 	case EStateStarting:
       
   147 		// Nothing to do, completed our own request status
       
   148 		break;
       
   149 	case EStateContinuing:
       
   150 		iGetEvent->Cancel();
       
   151 		break;
       
   152 	default:
       
   153 		__ASSERT_ALWAYS(0, Panic(ELogViewWindowFetcherBadState3));
       
   154 		break;
       
   155 		}
       
   156 	CLogActive::DoCancel();
       
   157 	}
       
   158 
       
   159 void CLogServViewWindowFetcher::DoComplete(TInt& aCompletionCode)
       
   160 	{
       
   161 	// Indicates to the client side how many records we retrieved.
       
   162 	if	(aCompletionCode == KErrNone)
       
   163 		aCompletionCode = iWindowIndex+1;
       
   164 	else
       
   165 		iState = EStateIdle;
       
   166 	}
       
   167 
       
   168 /////////////////////////////////////////////////////////////////////////////////////////
       
   169 /////////////////////////////////////////////////////////////////////////////////////////
       
   170 /////////////////////////////////////////////////////////////////////////////////////////
       
   171 
       
   172 void CLogServViewWindowFetcher::GetNextEventL(TInt aWindowIndex)
       
   173 	{
       
   174 	const TInt index = iWindow.iLower + aWindowIndex;
       
   175 	const TInt viewCount = iView->Count();
       
   176 	if	(index < 0)
       
   177 		{
       
   178 		::PanicClientL(*iMessage, ELogViewBadWindow);
       
   179 		}
       
   180 	else if (index >= viewCount)
       
   181 		{
       
   182 		// View is still catching up with changes which have been made in the server?
       
   183 		CLogEvent* event = CLogEvent::NewL();
       
   184 		delete iEvent;
       
   185 		iEvent = event;
       
   186 		CompleteRequest();
       
   187 		iWindowIndex = aWindowIndex;
       
   188 		}
       
   189 	else
       
   190 		{
       
   191 		const TLogId id = iView->At(index);
       
   192 		iEvent->SetId(id);
       
   193 		iGetEvent->StartL(*iEvent, iStatus, *iMessage);
       
   194 		iWindowIndex = aWindowIndex;
       
   195 		SetActive();
       
   196 		}
       
   197 	}
       
   198 
       
   199 void CLogServViewWindowFetcher::CompleteRequest()
       
   200 	{
       
   201 	TRequestStatus* status = &iStatus;
       
   202 	User::RequestComplete(status, KErrNone);
       
   203 	SetActive();
       
   204 	}