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