locationrequestmgmt/locationserver/src/EPos_CPosLocMonitorReqHandlerBase.cpp
changeset 0 9cfd9a3ee49c
equal deleted inserted replaced
-1:000000000000 0:9cfd9a3ee49c
       
     1 /*
       
     2 * Copyright (c) 2009 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:  	The base class that provides the common functionalities of 
       
    15 *				all the requests to the Location Monitor
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include "EPos_CPosLocMonitorReqHandlerBase.h"
       
    22 
       
    23 
       
    24 // ============================== MEMBER FUNCTIONS ===================================================
       
    25 /**
       
    26  * C++ default constructor
       
    27  */
       
    28 CPosLocMonitorReqHandlerBase::CPosLocMonitorReqHandlerBase() :
       
    29 CActive(EPriorityStandard),
       
    30 iSubsessionIdentity(CPosLocMonitorReqHandlerBase::MatchSessionAndSubsession),
       
    31 iSessionIdentity(CPosLocMonitorReqHandlerBase::MatchSession)
       
    32 	{
       
    33 	
       
    34 	
       
    35 	}
       
    36 
       
    37 /**
       
    38  * Destructor
       
    39  */
       
    40 CPosLocMonitorReqHandlerBase::~CPosLocMonitorReqHandlerBase()
       
    41 	{
       
    42 	QRequestsComplete(KErrNotFound);
       
    43 
       
    44 	iLocMonitorReqQ.Close();
       
    45 	}
       
    46 
       
    47 /**
       
    48  * RequestComplete
       
    49  * 				>> Complete a request with the given code
       
    50  * @param aMessage 		- The request to be completed
       
    51  * @param aCompleteCode - The completion code
       
    52  */
       
    53 void CPosLocMonitorReqHandlerBase::RequestComplete(const RMessage2& aMessage, TInt aCompleteCode)
       
    54 	{
       
    55 	// Complete the client request with the respective status code
       
    56 	if (!aMessage.IsNull())
       
    57 		{
       
    58 		aMessage.Complete(aCompleteCode);
       
    59 		}
       
    60 	}
       
    61 
       
    62 /** 
       
    63  * CheckAndAddReqToQueueL 
       
    64  * 		>> If the client has issued a duplicate request on a session or subsession when one is already oustanding,
       
    65  * 		   panic the client. Otherwise add the request to the queue.
       
    66  *
       
    67  * @param  aRequestType      - Type of request [ on session or subsession ]
       
    68  * @param  aMessage  		 - The new request from the client 
       
    69  */
       
    70 void CPosLocMonitorReqHandlerBase::CheckAndAddReqToQueueL(TLocMonitorRequestType aRequestType, const RMessage2& aMessage)
       
    71 	{
       
    72 		// Find if any request from this client already exists on the queue
       
    73     	TInt reqQMatchIndex=KErrNotFound;
       
    74     	reqQMatchIndex = MatchRequestInQueue(aRequestType,aMessage);
       
    75     	
       
    76     	if (KErrNotFound==reqQMatchIndex)
       
    77     		{
       
    78     		// Request has to be queued
       
    79     		iLocMonitorReqQ.AppendL(aMessage);
       
    80     		}
       
    81     		else
       
    82     		{
       
    83     		// Remove the request from the queue
       
    84     		iLocMonitorReqQ.Remove(reqQMatchIndex);
       
    85  
       
    86     		aMessage.Panic(KPosClientFault, EPositionDuplicateRequest);
       
    87     		}
       
    88 	}
       
    89 
       
    90 /** 
       
    91  * CancelRequest 
       
    92  * 		>> Complete the cancel request with KErrNone, remove the original request from the queue
       
    93  * 		   and complete it with KErrCancel.
       
    94  *
       
    95  * @param  aRequestType      - Type of request [ on session or subsession ]
       
    96  * @param  aMessage  		 - The new request from the client 
       
    97  */
       
    98 void CPosLocMonitorReqHandlerBase::CancelRequest(TLocMonitorRequestType aRequestType, const RMessage2& aMessage)
       
    99 	{
       
   100 	// Atleast one oustanding request exists
       
   101 	if (iLocMonitorReqQ.Count()>0)
       
   102 		{
       
   103 		// [The request queue is not empty]
       
   104 		if (iLocMonitorReqQ.Count()==1)
       
   105 			{
       
   106 			// If there is currently only one ongoing request and a cancel has been issued
       
   107 			// Complete the requests immediately and then call cancel on the active object
       
   108 			// which cancels the request to the location monitor
       
   109 			
       
   110 			// Complete the pending last known position request with KErrCancel
       
   111 			RequestComplete(iLocMonitorReqQ[0], KErrCancel);
       
   112 			
       
   113 			// Remove the request from the queeu
       
   114 			iLocMonitorReqQ.Remove(0);
       
   115 
       
   116 			// Complete the request to cancel the last known position request with KErrNone
       
   117 			RequestComplete(aMessage, KErrNone);
       
   118 			
       
   119 			// Calling Cancel inturn calls DoCancel which cancels the request with the 
       
   120 			// location monitor
       
   121 			Cancel();
       
   122 			}
       
   123 		else
       
   124 			{
       
   125 			FindRequestAndCancel(aRequestType, aMessage);
       
   126 			}
       
   127 		}
       
   128 
       
   129 	else
       
   130 		{
       
   131 		// No request found (on the queue) -> No oustanding request
       
   132 		RequestComplete(aMessage, KErrNotFound);
       
   133 		}
       
   134 	}
       
   135 
       
   136 /** 
       
   137  * FindRequestAndCancel 
       
   138  * 		>> Find the matching request in the queue using session / subsession id based on aRequestType
       
   139  *         and complete it with KErrCancel. Complete the cancel request with KErrNone.
       
   140  *
       
   141  * @param  aRequestType      - Type of request [ on session or subsession ]
       
   142  * @param  aMessage  		 - The new request from the client 
       
   143  */
       
   144 void CPosLocMonitorReqHandlerBase::FindRequestAndCancel(TLocMonitorRequestType aRequestType, const RMessage2& aMessage)
       
   145 	{
       
   146 	// Called only if there is more than one request on the queue
       
   147 	// Find the index of the request to be removed from the queue
       
   148 	TInt reqQMatchIndex=KErrNotFound;
       
   149 	reqQMatchIndex = MatchRequestInQueue(aRequestType, aMessage);
       
   150 
       
   151 	if (KErrNotFound==reqQMatchIndex)
       
   152 		{
       
   153 		RequestComplete(aMessage,KErrNotFound); // TODO KErrNotFound is correct ? 
       
   154 		}
       
   155 	else
       
   156 		{
       
   157 		// Complete the pending LKPosArea request with KErrCancel
       
   158 		RequestComplete(iLocMonitorReqQ[reqQMatchIndex], KErrCancel);
       
   159 		// Remove the request from the queue
       
   160 		iLocMonitorReqQ.Remove(reqQMatchIndex);
       
   161 
       
   162 		// Complete the request to cancel LKPosArea request with KErrNone
       
   163 		RequestComplete(aMessage,KErrNone);
       
   164 		}
       
   165 	
       
   166 	}
       
   167 
       
   168 /** 
       
   169  * MatchRequestInQueue 
       
   170  * 		>> Find the matching request in the queue using session / subsession id.
       
   171  *
       
   172  * @param  aRequestType      - Type of request [ on session or subsession ]
       
   173  * @param  aMessage  		 - The new request from the client 
       
   174  */
       
   175 TInt CPosLocMonitorReqHandlerBase::MatchRequestInQueue(TLocMonitorRequestType aRequestType, const RMessage2& aMessage)
       
   176 	{
       
   177 	TInt reqQMatchIndex=KErrNotFound;
       
   178 	
       
   179 	switch (aRequestType)
       
   180 		{
       
   181 		case EReqOnSession :
       
   182 			// Match the session
       
   183 			reqQMatchIndex = iLocMonitorReqQ.Find(aMessage,iSessionIdentity);
       
   184 			break;
       
   185 		case EReqOnSubSession :
       
   186 			// Match the session and the subsession
       
   187 			reqQMatchIndex = iLocMonitorReqQ.Find(aMessage,iSubsessionIdentity);
       
   188 			break;
       
   189 		default :
       
   190 			break;
       
   191 		}
       
   192 	
       
   193 	return reqQMatchIndex;
       
   194 	}
       
   195 
       
   196 /** 
       
   197  * MatchSession 
       
   198  * 		>> Check if  the Session corresponding to an outstanding request [on the queue] 
       
   199  * 		   matches that of the new request
       
   200  *
       
   201  * @param  qClient    - An entry from the queue of oustanding requests
       
   202  * @param  newClient  - The new request from the client 
       
   203  */
       
   204 TBool CPosLocMonitorReqHandlerBase::MatchSession(const RMessage2& qClient, const RMessage2& newClient)
       
   205 	{
       
   206 	return ( qClient.Session()== newClient.Session() );
       
   207 	}
       
   208 
       
   209 /** 
       
   210  * MatchSubsession 
       
   211  * 		>> Check if  the Subsession corresponding to an outstanding request [on the queue] 
       
   212  * 		   matches that of the new request
       
   213  *
       
   214  * @param  qClient    - An entry from the queue of oustanding requests
       
   215  * @param  newClient  - The new request from the client 
       
   216  */
       
   217 TBool CPosLocMonitorReqHandlerBase::MatchSessionAndSubsession(const RMessage2& qClient, const RMessage2& newClient)
       
   218 	{
       
   219 	return ( (qClient.Session()== newClient.Session()) && (qClient.Int3()== newClient.Int3()) );
       
   220 	}
       
   221 
       
   222 /** 
       
   223  * QRequestsComplete 
       
   224  * 		>> Complete all the requests in the queue with the given errorcode.
       
   225  *
       
   226  * @param  aCompleteCode   - Requests completed with this code.
       
   227  */
       
   228 void CPosLocMonitorReqHandlerBase::QRequestsComplete(TInt aCompleteCode)
       
   229 	{
       
   230 	// Complete all the requests on the queue with the given completion code
       
   231 	while(iLocMonitorReqQ.Count()>0)
       
   232 		{
       
   233 		TInt index = iLocMonitorReqQ.Count()-1;
       
   234 		// Complete all the requests in the queue with KErrServerTerminated
       
   235 		if (!iLocMonitorReqQ[index].IsNull())
       
   236 			{
       
   237 			iLocMonitorReqQ[index].Complete(aCompleteCode);
       
   238 			}
       
   239 		iLocMonitorReqQ.Remove(index);
       
   240 		}
       
   241 	
       
   242 	}
       
   243 
       
   244 /** 
       
   245  * NotifySubSessionClosed 
       
   246  * 		>> Clear the queue of requests issued on subsession if it is closed prematurely.
       
   247  *
       
   248  * @param  aMessage   - The client request used to retrieve the subsession id.
       
   249  */
       
   250 void CPosLocMonitorReqHandlerBase::NotifySubSessionClosed(const RMessage2& aMessage)
       
   251 	{
       
   252 	// If the subsession is closed before the Last Known Position is received from the Loc Monitor,
       
   253 	// then complete the requests from that subsession with KErrDisconnected
       
   254 	TInt reqQMatchIndex=KErrNotFound;
       
   255 	reqQMatchIndex = iLocMonitorReqQ.Find(aMessage,iSubsessionIdentity);
       
   256 
       
   257 	if (reqQMatchIndex != KErrNotFound)
       
   258 		{
       
   259 		if ((iLocMonitorReqQ.Count()==1)&&(iStatus==KRequestPending))
       
   260 			{
       
   261 			// Only one outstanding request - issue a cancel to the location monitor
       
   262 			Cancel();
       
   263 			}
       
   264 		if (iLocMonitorReqQ.Count()>=1)
       
   265 			{
       
   266 			// Remove the request from the queue
       
   267 			iLocMonitorReqQ.Remove(reqQMatchIndex);
       
   268 			}
       
   269 		}
       
   270 	}
       
   271 
       
   272 /** 
       
   273  * NotifySubSessionClosed 
       
   274  * 		>> Clear the queue of requests issued on the session & subsession if 
       
   275  * 		   the session is closed prematurely.
       
   276  *
       
   277  * @param  aSessionPtr   - The session id [session pointer].
       
   278  */
       
   279 void CPosLocMonitorReqHandlerBase::NotifySessionClosed(const CSession2* aSessionPtr)
       
   280 	{
       
   281 	// As it is easier to delete the entries from the end of the queue
       
   282 	// store the index of the final element
       
   283 	TInt index = iLocMonitorReqQ.Count()-1;
       
   284 
       
   285 	// Clear all the outstanding Last Known Position Requests on the queue 
       
   286 	// and panic the client with EPositionRequestsNotCancelled
       
   287 	while (index>=0)
       
   288 		{
       
   289 		if ( aSessionPtr ==  const_cast<const CSession2*>( (iLocMonitorReqQ[index].Session()) ) )
       
   290 			{
       
   291 			//TODO Panic or RequestComplete? RequestComplete(iLKPosQ[index],KErrDisconnected);
       
   292 			iLocMonitorReqQ[index].Panic(KPosClientFault, EPositionRequestsNotCancelled);
       
   293 			iLocMonitorReqQ.Remove(index);
       
   294 			}
       
   295 		index--;
       
   296 		}
       
   297 
       
   298 	// No more request on the queue - So issue a cancel to the location monitor
       
   299 	if ((iLocMonitorReqQ.Count()<=0)&&(iStatus==KRequestPending))
       
   300 		{
       
   301 		Cancel();
       
   302 		}
       
   303 	}
       
   304 
       
   305 /** 
       
   306  * NotifyServerShutDown 
       
   307  * 		>> Clear the queue of requests.
       
   308  */
       
   309 void CPosLocMonitorReqHandlerBase::NotifyServerShutDown()
       
   310 	{
       
   311 		// Complete all the requests on the queue with KErrServerTerminated
       
   312 		// The Client/Server framework doesnot guarantee the completion
       
   313 		QRequestsComplete(KErrServerTerminated);
       
   314 	}