applayerprotocols/httpservice/src/chttpnetworkconnectioninfo.cpp
branchRCL_3
changeset 7 2611c08ee28e
equal deleted inserted replaced
6:fa2fd8b2d6cc 7:2611c08ee28e
       
     1 // Copyright (c) 2003-2010 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 #include  <httpservice.h>
       
    16 #include <es_enum.h>
       
    17 #include "CConnectionMonitor.h"
       
    18 
       
    19 
       
    20 CHttpNetworkConnection::CHttpNetworkConnection()
       
    21     {
       
    22     }
       
    23 
       
    24 CHttpNetworkConnection::~CHttpNetworkConnection()
       
    25     {
       
    26     if (iConnInfo)
       
    27         {
       
    28         iConnInfo->iConnection.Close();
       
    29         iConnInfo->iSocketServ.Close();
       
    30         delete iConnInfo;
       
    31         }
       
    32     if (iConnectionMonitor)
       
    33         {
       
    34         delete iConnectionMonitor;
       
    35         }
       
    36     }
       
    37 CHttpNetworkConnection* CHttpNetworkConnection::New( )
       
    38     {
       
    39     CHttpNetworkConnection* self = new (ELeave)  CHttpNetworkConnection() ;
       
    40     self->ConstructL();
       
    41     return self;
       
    42     }
       
    43 
       
    44 void CHttpNetworkConnection::ConstructL()
       
    45     {
       
    46     iConnInfo = new (ELeave) CConnectionInfo();
       
    47     }
       
    48 
       
    49 void CHttpNetworkConnection::SetHttpService(CHttpService *aService)
       
    50     {
       
    51     iHttpService = aService;
       
    52     }
       
    53 
       
    54 /**
       
    55  * Start the network connection for  HTTP transactions.
       
    56  * Default connection prefernces will be used.
       
    57  * 
       
    58  * @return KErrNone if connection opening is successful, otherwise relevant errors.
       
    59  */
       
    60 EXPORT_C TInt CHttpNetworkConnection::Start()
       
    61     {
       
    62     TRequestStatus requestStatus;
       
    63     TInt status;
       
    64     if (! iConnInfo->iConnectionStatus)
       
    65         {
       
    66         status = SetupConnection();
       
    67         if (status == KErrNone)
       
    68             {
       
    69             iConnInfo->iConnection.Start( requestStatus);
       
    70             User::WaitForRequest(requestStatus);
       
    71             status = requestStatus.Int();
       
    72             }
       
    73         }
       
    74     else
       
    75         {
       
    76         status = KErrAlreadyExists;
       
    77         }
       
    78     if (status == KErrNone)
       
    79         {
       
    80         AssociateConnectionWithHttpSession();
       
    81         }
       
    82     return status;
       
    83     }
       
    84 
       
    85 /**
       
    86  * Start the network connection for  HTTP transactions.
       
    87  * 
       
    88  * @param aConnPref - connection prefernces can be specified.
       
    89  * @param aAttachType - EFalse 
       
    90  * 
       
    91  * @return KErrNoMemory incase of failure otherwise KErrNone for success
       
    92  * 
       
    93  */
       
    94 EXPORT_C TInt CHttpNetworkConnection::Start(TCommDbConnPref aConnPref, TBool aAttachType)
       
    95     {
       
    96     TInt status;
       
    97     iConnInfo->iConnPref = aConnPref;
       
    98 
       
    99     if (aAttachType && !iConnInfo->iConnectionStatus )
       
   100         {
       
   101         TUint count;
       
   102         TConnectionInfoBuf connInfoPckg;
       
   103         
       
   104         status = SetupConnection();
       
   105                 
       
   106         if ( status == KErrNone && iConnInfo->iConnection.EnumerateConnections(count) == KErrNone )
       
   107             {
       
   108             for (TUint i=1; i<=count; i++)
       
   109                 {
       
   110                 // Note  GetConnectionInfo expects 1-based index
       
   111                 if ( iConnInfo->iConnection.GetConnectionInfo( i, connInfoPckg ) == KErrNone )
       
   112                     {
       
   113                     if ( connInfoPckg().iIapId == iConnInfo->iConnPref.IapId() )
       
   114                         {
       
   115                         status= iConnInfo->iConnection.Attach(iConnInfo->iConnPref, RConnection::EAttachTypeNormal);
       
   116                         return status;
       
   117                         }
       
   118                     }   
       
   119                 }
       
   120             }
       
   121         return KErrNotFound;
       
   122         }
       
   123     else
       
   124         {
       
   125         if (!iConnInfo->iConnectionStatus)
       
   126             {
       
   127             status = SetupConnection();
       
   128             if (status == KErrNone)
       
   129                 {
       
   130                 TRequestStatus requestStatus;
       
   131                 iConnInfo->iConnection.Start(iConnInfo->iConnPref,requestStatus);
       
   132                 User::WaitForRequest(requestStatus);
       
   133                 status = requestStatus.Int();
       
   134                 }
       
   135           
       
   136             }
       
   137         else
       
   138             {
       
   139             status = KErrAlreadyExists;
       
   140             }
       
   141         if (status == KErrNone)
       
   142             {
       
   143             AssociateConnectionWithHttpSession();
       
   144             }
       
   145         
       
   146         return status;
       
   147         }
       
   148     }
       
   149 
       
   150 
       
   151 /**
       
   152  * To Stop the network connection 
       
   153  * 
       
   154   */
       
   155 EXPORT_C void CHttpNetworkConnection::Stop()
       
   156     {
       
   157     if (iConnInfo->iConnectionStatus)
       
   158         {
       
   159         iConnInfo->iConnectionStatus = EFalse;
       
   160         iConnInfo->iConnection.Close();
       
   161         iConnInfo->iSocketServ.Close();
       
   162         }
       
   163     }
       
   164 
       
   165 void CHttpNetworkConnection::AssociateConnectionWithHttpSession()
       
   166     {
       
   167     RStringPool strP = iHttpService->Session().StringPool();
       
   168     RHTTPConnectionInfo connInfo = iHttpService->Session().ConnectionInfo();
       
   169     connInfo.SetProperty(strP.StringF(HTTP::EHttpSocketServ,RHTTPSession::GetTable()), THTTPHdrVal(iConnInfo->iSocketServ.Handle()));
       
   170     TInt connPtr = reinterpret_cast<TInt>(&(iConnInfo->iConnection));
       
   171     connInfo.SetProperty(strP.StringF(HTTP::EHttpSocketConnection,RHTTPSession::GetTable()), THTTPHdrVal(connPtr));
       
   172     }
       
   173 
       
   174 TInt CHttpNetworkConnection::SetupConnection()
       
   175     {
       
   176     TInt status = KErrAlreadyExists;
       
   177     if (!iConnInfo->iConnectionStatus )
       
   178         {
       
   179         status = iConnInfo->iSocketServ.Connect() ;
       
   180         if (status == KErrNone)
       
   181             {
       
   182             status = iConnInfo->iConnection.Open ( iConnInfo->iSocketServ ) ;
       
   183             if (status != KErrNone)
       
   184                 {
       
   185                 iConnInfo->iSocketServ.Close();
       
   186                 }
       
   187             else
       
   188                 {
       
   189                 iConnectionMonitor = CConnectionMonitor::NewL(this); 
       
   190                 iConnInfo->iConnectionStatus = ETrue;
       
   191                 }
       
   192             }
       
   193         }
       
   194     return status;
       
   195     }
       
   196 
       
   197 void CHttpNetworkConnection::HandleConnectionEvent(TInt aError)
       
   198     {
       
   199     if (aError == KLinkLayerClosed || aError == KLinkLayerClosed)
       
   200         {
       
   201         iConnInfo->iConnectionStatus = EFalse;
       
   202         }
       
   203     }
       
   204