supl/supltiapiimplementation/src/lbssupltirequestmanager.cpp
changeset 0 667063e416a2
child 32 b12ea03c50a3
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     1 /*
       
     2 * Copyright (c) 2005 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:  CCLbsSuplTiRequestManager Class Implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #include <lbs/supltiapi.h>
       
    19 
       
    20 #include "lbssupltirequestmanager.h"
       
    21 #include "lbssupltiapirequestor.h"
       
    22 #include "epos_tiapiimpltrace.h"
       
    23 
       
    24 
       
    25 _LIT(KTraceFileName,"lbssupltiapirequestmanager.cpp");
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // CCLbsSuplTiRequestManager::CCLbsSuplTiRequestManager
       
    29 // Other items were commented in a header
       
    30 // -----------------------------------------------------------------------------
       
    31 //
       
    32 CCLbsSuplTiRequestManager::CCLbsSuplTiRequestManager(MLbsSuplTiObserver& aObserver):CActive(EPriorityStandard), iObserver(aObserver)
       
    33     {
       
    34     // No implementation required
       
    35     }
       
    36 // -----------------------------------------------------------------------------
       
    37 // CCLbsSuplTiRequestManager::~CCLbsSuplTiRequestManager
       
    38 // Other items were commented in a header
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CCLbsSuplTiRequestManager::~CCLbsSuplTiRequestManager()
       
    42     {
       
    43     CloseSession();
       
    44     iRequestorCollection.Close();
       
    45     if(iTrace)
       
    46         {
       
    47         delete iTrace;
       
    48         iTrace = NULL;
       
    49         }
       
    50    if (IsActive())      
       
    51       	Cancel();
       
    52     }
       
    53 // -----------------------------------------------------------------------------
       
    54 // CCLbsSuplTiRequestManager::NewLC
       
    55 // Other items were commented in a header
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CCLbsSuplTiRequestManager* CCLbsSuplTiRequestManager::NewLC(MLbsSuplTiObserver& aObserver)
       
    59     {
       
    60     CCLbsSuplTiRequestManager* self =
       
    61             new (ELeave) CCLbsSuplTiRequestManager(aObserver);
       
    62     CleanupStack::PushL(self);
       
    63     self->ConstructL();
       
    64     return self;
       
    65     }
       
    66 // -----------------------------------------------------------------------------
       
    67 // CCLbsSuplTiRequestManager::NewL
       
    68 // Other items were commented in a header
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CCLbsSuplTiRequestManager* CCLbsSuplTiRequestManager::NewL(MLbsSuplTiObserver& aObserver)
       
    72     {
       
    73     CCLbsSuplTiRequestManager* self = CCLbsSuplTiRequestManager::NewLC(aObserver);
       
    74     CleanupStack::Pop(); // self;
       
    75     return self;
       
    76     }
       
    77 // -----------------------------------------------------------------------------
       
    78 // CCLbsSuplTiRequestManager::ConstructL
       
    79 // Other items were commented in a header
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 void CCLbsSuplTiRequestManager::ConstructL()
       
    83     {
       
    84 	CActiveScheduler::Add(this);
       
    85     iTrace = COMASuplTrace::NewL(); 
       
    86     iTrace->Trace(_L("CCLbsSuplTiRequestManager::ConstructL()"), KTraceFileName, __LINE__); 
       
    87 
       
    88     }
       
    89 // -----------------------------------------------------------------------------
       
    90 // CCLbsSuplTiRequestManager::RequestLocation
       
    91 // Other items were commented in a header
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 void CCLbsSuplTiRequestManager::RequestLocation(const TLbsNetSessionId& aSessionId, const TLbsNetPosRequestOptionsBase& aOptions, const TLbsNetPosRequestMethod& aMethod)
       
    95     {
       
    96     iTrace->Trace(_L("CCLbsSuplTiRequestManager::RequestLocation()"), KTraceFileName, __LINE__);
       
    97     
       
    98      if(!iServer.Handle())
       
    99     	{
       
   100     	iTrace->Trace(_L("Invalid Server Handle. Opening new connection"), KTraceFileName, __LINE__);
       
   101     	TInt err = iServer.Connect(iStatus);  
       
   102     	if(err != KErrNone)
       
   103     	  {
       
   104     	  iTrace->Trace(_L("Opening new connection failed"), KTraceFileName, __LINE__);
       
   105     	  iObserver.RequestComplete(err,aSessionId);
       
   106     	  return;
       
   107     	  }
       
   108     	iSessionId = aSessionId;
       
   109      	iOptions = aOptions;
       
   110      	iMethod = aMethod;
       
   111     	SetActive();
       
   112     	}
       
   113     else
       
   114     	{
       
   115     	iTrace->Trace(_L("Valid Server Handle exist.Using same one..."), KTraceFileName, __LINE__);	
       
   116     	iSessionId = aSessionId;
       
   117      	iOptions = aOptions;
       
   118      	iMethod = aMethod;
       
   119     	InitiateLocationRequest();
       
   120     	}
       
   121     
       
   122     }
       
   123 // -----------------------------------------------------------------------------
       
   124 // CCLbsSuplTiRequestManager::CancelRequest
       
   125 // Other items were commented in a header
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 TInt CCLbsSuplTiRequestManager::CancelRequest(const TLbsNetSessionId& aSessionId)
       
   129     {
       
   130     iTrace->Trace(_L("CCLbsSuplTiRequestManager::CancelRequest"), KTraceFileName, __LINE__);
       
   131     TInt index = FindSessionId(aSessionId);
       
   132     if(index != KErrNotFound)
       
   133         {
       
   134         iTrace->Trace(_L("session found calling cancel"), KTraceFileName, __LINE__);
       
   135         iRequestorCollection[index]->Cancel();
       
   136         delete iRequestorCollection[index];
       
   137         iRequestorCollection.Remove(index);
       
   138         return KErrNone;
       
   139         }
       
   140     iTrace->Trace(_L("session not found "), KTraceFileName, __LINE__);
       
   141     return KErrNotFound;
       
   142     }
       
   143 // -----------------------------------------------------------------------------
       
   144 // CCLbsSuplTiRequestManager::GetPosition
       
   145 // Other items were commented in a header
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 TInt CCLbsSuplTiRequestManager::GetPosition(const TLbsNetSessionId& aSessionId, TPositionInfoBase& aPositionInfo)
       
   149     {
       
   150     iTrace->Trace(_L("CCLbsSuplTiRequestManager::GetPosition"), KTraceFileName, __LINE__);
       
   151     TInt index = FindSessionId(aSessionId);
       
   152     if(index != KErrNotFound)
       
   153         {
       
   154         iTrace->Trace(_L("session found calling getposition"), KTraceFileName, __LINE__);
       
   155         return iRequestorCollection[index]->GetPosition(aPositionInfo);
       
   156         }
       
   157     iTrace->Trace(_L("session not found found for getposition"), KTraceFileName, __LINE__);
       
   158     return KErrNotFound;
       
   159     }
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // CCLbsSuplTiRequestManager::CloseSession
       
   163 // Other items were commented in a header
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 void CCLbsSuplTiRequestManager::CloseSession()
       
   167     {
       
   168     iTrace->Trace(_L("CCLbsSuplTiRequestManager::CloseSession() start"), KTraceFileName, __LINE__);
       
   169     iRequestorCollection.ResetAndDestroy(); //cancels all active requests
       
   170     iServer.Close();
       
   171     iTrace->Trace(_L("CCLbsSuplTiRequestManager::CloseSession() end"), KTraceFileName, __LINE__);
       
   172     }
       
   173 // -----------------------------------------------------------------------------
       
   174 // CCLbsSuplTiRequestManager::FindSessionId
       
   175 // Other items were commented in a header
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 TInt CCLbsSuplTiRequestManager::FindSessionId(const TLbsNetSessionId& aSessionId)
       
   179     {
       
   180     TInt count = iRequestorCollection.Count();
       
   181     for ( TInt index = 0; index < count; index++ )
       
   182         {
       
   183         if ( iRequestorCollection[index]->GetSessionId().SessionNum() == aSessionId.SessionNum() )
       
   184             {
       
   185             return index;
       
   186             }
       
   187         }
       
   188     return KErrNotFound;
       
   189     }
       
   190 
       
   191 void CCLbsSuplTiRequestManager::RunL()
       
   192 	{
       
   193 	if(iStatus.Int() == KErrNone) // if server connect was successful
       
   194 	{
       
   195 		iTrace->Trace(_L("CCLbsSuplTiRequestManager::RunL called successfully..."), KTraceFileName, __LINE__);
       
   196 		iTrace->Trace(_L("Putting new request..."), KTraceFileName, __LINE__);
       
   197 		InitiateLocationRequest();
       
   198 	}	
       
   199 	else //opening connection failed
       
   200 		{
       
   201 		iTrace->Trace(_L("Opening new connection failed"), KTraceFileName, __LINE__);
       
   202     iObserver.RequestComplete(iStatus.Int(),iSessionId);
       
   203 		}
       
   204 	}
       
   205 
       
   206    // For cancelling Ashnchronous requests
       
   207 void CCLbsSuplTiRequestManager::DoCancel()
       
   208 	{
       
   209 	
       
   210 	}
       
   211 
       
   212    // Override to handle leaves from RunL().
       
   213 TInt CCLbsSuplTiRequestManager::RunError(TInt aError)
       
   214 	{
       
   215 	return aError;
       
   216 	}
       
   217    
       
   218 void CCLbsSuplTiRequestManager::InitiateLocationRequest()
       
   219 	{
       
   220 	if(FindSessionId(iSessionId) == KErrNotFound) // no request exists with this session id so make a new request
       
   221 		{
       
   222 		iTrace->Trace(_L("Session Id not found creating new requestor"), KTraceFileName, __LINE__);
       
   223 		//Create a new requestor object
       
   224 		CCLbsSuplTiApiRequestor* newRequestor = NULL;
       
   225 		TRAPD(err,newRequestor = CCLbsSuplTiApiRequestor::NewL(iObserver,iServer));
       
   226 		if(err != KErrNone)
       
   227 			{
       
   228 			iTrace->Trace(_L("Creating New Requestor Failed"), KTraceFileName, __LINE__);
       
   229 			iObserver.RequestComplete(err,iSessionId);
       
   230 			return;
       
   231 			}
       
   232 		//add to requestor collection, ownership is transferred to the collection array
       
   233 		iRequestorCollection.Append(newRequestor);
       
   234 		//ask requestor to make location request
       
   235 		newRequestor->RequestLocation(iSessionId,iOptions,iMethod);
       
   236 		}
       
   237 	else //request was already made with this session id
       
   238 		{
       
   239 		iTrace->Trace(_L("Session Id already exits"), KTraceFileName, __LINE__);
       
   240 		iObserver.RequestComplete(KErrAlreadyExists,iSessionId);
       
   241 		}
       
   242 	}