appfw/viewserver/server/VWSQUEUE.CPP
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 1999-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 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    17 #include "vwsinternal.h"
       
    18 #include "vwsdefpartner.h"
       
    19 #endif //SYMBIAN_ENABLE_SPLIT_HEADERS
       
    20 #include "VWSQUEUE.H"
       
    21 
       
    22 
       
    23 //
       
    24 // CVwsEventQueue.
       
    25 //
       
    26 
       
    27 CVwsEventQueue::CVwsEventQueue() 
       
    28 	{
       
    29 	}
       
    30 
       
    31 #ifdef __DO_LOGGING__
       
    32 CVwsEventQueue::CVwsEventQueue(const TDesC& aName) : iQueueName(aName)
       
    33 	{
       
    34 	}
       
    35 
       
    36 void CVwsEventQueue::SetName(const TDesC& aName)
       
    37 	{
       
    38 	iQueueName=aName;
       
    39 	}
       
    40 #endif
       
    41 
       
    42 CVwsEventQueue::~CVwsEventQueue()
       
    43 	{
       
    44 	do
       
    45 		{
       
    46 		delete Head();
       
    47 		}
       
    48 	while (RemoveHead() == KErrNone);
       
    49 	}
       
    50 
       
    51 void CVwsEventQueue::ProcessEventL(CVwsEvent* aEvent)
       
    52 	{
       
    53 	TInt err=AddToTail(aEvent);
       
    54 	if(err!=KErrNone)
       
    55 		{
       
    56 		if(aEvent!=NULL)
       
    57 			{
       
    58 			delete aEvent;
       
    59 			aEvent=NULL;
       
    60 			}
       
    61 		User::Leave(err);
       
    62 		}
       
    63 
       
    64 	if (aEvent)
       
    65 		{
       
    66 		switch (iState)
       
    67 			{
       
    68 			case EEmpty:
       
    69 				{
       
    70 				iState=EProcessing;
       
    71 				TRAPD(err,aEvent->ProcessEventL());
       
    72 				if (err)
       
    73 					{
       
    74 					DeleteHead();
       
    75 					iState=EEmpty;
       
    76 					}
       
    77 				User::LeaveIfError(err);
       
    78 				}
       
    79 				break;
       
    80 			case EProcessing:
       
    81 				// Wait until current event has finished being processed.
       
    82 				break;
       
    83 			default:
       
    84 				ASSERT(EFalse);
       
    85 			}
       
    86 		}
       
    87 	}
       
    88 
       
    89 void CVwsEventQueue::HandleEventProcessed()
       
    90 	{
       
    91 	if (iQueueSize==1)
       
    92 		{
       
    93 		Head()->HandleLastOnQueue();
       
    94 		DeleteHead();
       
    95 		LOG3(CVwsLog::ELoud,_L("Completed event at head of %S, queue empty"),&iQueueName);
       
    96 		iState=EEmpty;
       
    97 		}
       
    98 	else if (iQueueSize>0)
       
    99 		{
       
   100 		DeleteHead();
       
   101 		LOG4(CVwsLog::ELoud,_L("Completed event at head of %S, processing next [queue size %d]"),&iQueueName,iQueueSize);
       
   102 		// Start processing next event in the queue.
       
   103 		TInt err=KErrGeneral;
       
   104 		while (iQueueSize>0 && err)
       
   105 				{
       
   106 				TRAP(err, Head()->ProcessEventL());
       
   107 				if (err)
       
   108 					{
       
   109 					DeleteHead();
       
   110 					if (0==iQueueSize)
       
   111 						{
       
   112 						iState=EEmpty;
       
   113 						}
       
   114 					}
       
   115 				}
       
   116 		}
       
   117 	}
       
   118 
       
   119 CVwsEvent* CVwsEventQueue::Head() const
       
   120 	{
       
   121 	if (iQueueSize==0)
       
   122 		return NULL;
       
   123 
       
   124 	return iQueue[iQueueStart];
       
   125 	}
       
   126 
       
   127 CVwsEvent* CVwsEventQueue::Tail() const
       
   128 	{
       
   129 	if (iQueueSize==0)
       
   130 		return NULL;
       
   131 
       
   132 	return iQueue[(iQueueEnd+KQueueSize-1)%KQueueSize];
       
   133 	}
       
   134 
       
   135 TInt CVwsEventQueue::Count() const
       
   136 	{
       
   137 	return iQueueSize;
       
   138 	}
       
   139 
       
   140 void CVwsEventQueue::DeleteHead()
       
   141 	{
       
   142 	delete Head();
       
   143 	RemoveHead();
       
   144 	}
       
   145 
       
   146 void CVwsEventQueue::HandleSessionRemoval(const TThreadId& aClientThreadId)
       
   147 	{
       
   148 	if (iQueueSize>0)
       
   149 		{
       
   150 		LOG4(CVwsLog::ELoud,_L("Session removed - handling removal in head event of %S [queue size %d]"),&iQueueName,iQueueSize);
       
   151 		Head()->HandleSessionRemoval(aClientThreadId);
       
   152 		}
       
   153 	}
       
   154 
       
   155 TInt CVwsEventQueue::AddToTail(CVwsEvent*& aEvent)
       
   156 	{
       
   157 	switch (aEvent->Type())
       
   158 		{
       
   159 		case CVwsEvent::ENormal:
       
   160 			return DoAddToTail(aEvent);
       
   161 		case CVwsEvent::ERejectPairs:
       
   162 			return AddToTailIfNotPair(aEvent);
       
   163 		default:
       
   164 			ASSERT(EFalse);
       
   165 			return KErrGeneral;
       
   166 		}
       
   167 	}
       
   168 
       
   169 TInt CVwsEventQueue::RemoveHead()
       
   170 	{
       
   171 	if (iQueueSize>0)
       
   172 		{
       
   173 		iQueue[iQueueStart]=NULL;
       
   174 		iQueueStart=(iQueueStart+1)%KQueueSize;
       
   175 		iQueueSize--;
       
   176 		return KErrNone;
       
   177 		}
       
   178 
       
   179 	return KErrUnderflow;
       
   180 	}
       
   181 
       
   182 TInt CVwsEventQueue::DoAddToTail(CVwsEvent* aEvent)
       
   183 	{
       
   184 	if (iQueueSize<KQueueSize)
       
   185 		{
       
   186 		iQueue[iQueueEnd]=aEvent;
       
   187 		iQueueEnd=(iQueueEnd+1)%KQueueSize;
       
   188 		iQueueSize++;
       
   189 		aEvent->HandleAddedToQueue();
       
   190 		LOG4(CVwsLog::ELoud,_L("Added event to tail of %S [queue size %d]"),&iQueueName,iQueueSize);
       
   191 		return KErrNone;
       
   192 		}
       
   193 
       
   194 	return KErrOverflow;
       
   195 	}
       
   196 
       
   197 TInt CVwsEventQueue::AddToTailIfNotPair(CVwsEvent*& aEvent)
       
   198 	{
       
   199 	if (iQueueSize==0)
       
   200 		{
       
   201 		// Nothing in queue, so safe to add.
       
   202 		return DoAddToTail(aEvent);
       
   203 		}
       
   204 
       
   205 	// Check for type match in queue.
       
   206 	if (iQueueSize>2)
       
   207 		{
       
   208 		if ((Tail()->Type()==aEvent->Type()) && (At(iQueueSize-2)->Type()==aEvent->Type()) && (At(iQueueSize-3)->Type()==aEvent->Type()))
       
   209 			{
       
   210 			LOG3(CVwsLog::ENormal,_L("Discarded event - one already in %S"),&iQueueName);
       
   211 			delete aEvent;
       
   212 			aEvent=NULL;
       
   213 			return KErrNone;
       
   214 			}
       
   215 		}
       
   216 
       
   217 	// No match, do add to tail.
       
   218 	return DoAddToTail(aEvent);
       
   219 	}
       
   220 
       
   221 CVwsEvent* CVwsEventQueue::At(TInt aIndex)
       
   222 	{
       
   223 	CVwsEvent* event=NULL;
       
   224 
       
   225 	if (aIndex<iQueueSize)
       
   226 		{
       
   227 		event=iQueue[(iQueueStart+aIndex)%KQueueSize];
       
   228 		}
       
   229 
       
   230 	return event;
       
   231 	}
       
   232 
       
   233 void CVwsEventQueue::KickStart()
       
   234 	{
       
   235 	//Attempt to restart stalled q
       
   236 	DeleteHead();
       
   237 	iState=EEmpty;
       
   238 	TInt err = KErrGeneral;
       
   239 	while(iQueueSize>0 && err)
       
   240 		{
       
   241 		iState=EProcessing;
       
   242 		TRAP(err, Head()->ProcessEventL());
       
   243 		if (err)
       
   244 			{
       
   245 			DeleteHead();
       
   246 			iState=EEmpty;
       
   247 			}
       
   248 		}
       
   249 	}
       
   250 
       
   251 //
       
   252 // CVwsEvent.
       
   253 //
       
   254 
       
   255 CVwsEvent::CVwsEvent(TType aType,CVwsEventQueue& aQueue)
       
   256 	:iType(aType),iQueue(aQueue)
       
   257 	{
       
   258 	}
       
   259 
       
   260 CVwsEvent::~CVwsEvent()
       
   261 	{
       
   262 	}
       
   263 
       
   264 CVwsEvent::TType CVwsEvent::Type()
       
   265 	{
       
   266 	return iType;
       
   267 	}
       
   268 
       
   269 void CVwsEvent::ReportEventProcessed()
       
   270 	{
       
   271 	iQueue.HandleEventProcessed();
       
   272 	}
       
   273 
       
   274 void CVwsEvent::HandleAddedToQueue()
       
   275 	{
       
   276 	}
       
   277 
       
   278 void CVwsEvent::HandleLastOnQueue()
       
   279 	{
       
   280 	}
       
   281 
       
   282 void CVwsEvent::HandleSessionRemoval(const TThreadId& /*aClientThreadId*/)
       
   283 	{
       
   284 	}