ncdengine/engine/transport/src/catalogsconnection.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2008-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:   ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "catalogsconnection.h"
       
    20 #include "catalogshttpconnectioncreator.h"
       
    21 #include "catalogsutils.h"
       
    22 
       
    23 #include "catalogsdebug.h"
       
    24 
       
    25 // Default for socket server message slots is 8 which is too little
       
    26 // when we use the same socket server connection for several HTTP
       
    27 // connections
       
    28 const TInt KSocketServerMessageSlots = 20;
       
    29 
       
    30     
       
    31 CCatalogsConnectionMonitor* CCatalogsConnectionMonitor::NewL( 
       
    32     MCatalogsConnectionStateObserver& aObserver )
       
    33     {
       
    34     CCatalogsConnectionMonitor* self = new( ELeave ) 
       
    35         CCatalogsConnectionMonitor( aObserver );
       
    36     CleanupStack::PushL( self );
       
    37     self->ConstructL();
       
    38     CleanupStack::Pop( self );
       
    39     return self;
       
    40     }
       
    41 
       
    42 
       
    43 CCatalogsConnectionMonitor::~CCatalogsConnectionMonitor()
       
    44     {
       
    45     DLTRACEIN((""));
       
    46     Cancel();
       
    47     iConnection.Close();
       
    48     iSs.Close();
       
    49     }
       
    50 
       
    51 
       
    52 void CCatalogsConnectionMonitor::StartL( 
       
    53     const TConnectionInfoBuf& aInfo )
       
    54     {
       
    55     DLTRACEIN((""));
       
    56     
       
    57     TNifProgress progress;
       
    58     User::LeaveIfError( iConnection.Attach( 
       
    59         aInfo, RConnection::EAttachTypeMonitor ) );
       
    60     
       
    61     iConnection.ProgressNotification( iProgressBuf, iStatus );
       
    62         
       
    63     TInt err = iConnection.Progress( progress );
       
    64     
       
    65     DLTRACE(("Progress stage: %d, error: %d", 
       
    66         progress.iStage, progress.iError ));
       
    67     
       
    68     if ( err != KErrNone )
       
    69         {
       
    70         DLERROR(("Error when getting progress info: %d", err));
       
    71         iConnection.CancelProgressNotification();
       
    72         // Prevent RunL from running
       
    73         User::WaitForRequest( iStatus );
       
    74         User::Leave( err );
       
    75         }
       
    76     else if ( progress.iStage == KConnectionUninitialised ) 
       
    77         {
       
    78         DLTRACE(("Connection already dead"));
       
    79         iConnection.CancelProgressNotification();
       
    80         iProgressBuf = progress;
       
    81         }
       
    82     SetActive();
       
    83     }
       
    84     
       
    85     
       
    86 void CCatalogsConnectionMonitor::RunL()
       
    87     {
       
    88     DLTRACEIN((""));
       
    89     LeaveIfNotErrorL( iStatus.Int(), KErrCancel );
       
    90 
       
    91     // Ensure that we don't miss any events
       
    92     if ( iStatus.Int() == KErrNone )
       
    93         {
       
    94         iConnection.ProgressNotification( iProgressBuf, iStatus );
       
    95         SetActive();
       
    96         }    
       
    97     
       
    98     TInt stage = iProgressBuf().iStage;    
       
    99     DLTRACE(("Stage: %d, error: %d", stage, iProgressBuf().iError ));
       
   100     
       
   101     // Take possible connection error code. This is needed for "Red phone"
       
   102     // detection
       
   103     if ( stage == KLinkLayerClosed ) 
       
   104         {
       
   105         TInt error = iProgressBuf().iError;
       
   106         if ( error ) 
       
   107             {
       
   108             stage = error;
       
   109             }
       
   110         }
       
   111     
       
   112     iObserver.ConnectionStateChangedL( stage );    
       
   113     }
       
   114     
       
   115     
       
   116 void CCatalogsConnectionMonitor::DoCancel()
       
   117     {
       
   118     iConnection.CancelProgressNotification();
       
   119     
       
   120     }
       
   121     
       
   122     
       
   123 TInt CCatalogsConnectionMonitor::RunError( TInt aError ) 
       
   124     {
       
   125     iObserver.ConnectionStateError( aError );
       
   126     return KErrNone;
       
   127     }
       
   128     
       
   129 CCatalogsConnectionMonitor::CCatalogsConnectionMonitor( 
       
   130     MCatalogsConnectionStateObserver& aObserver ) :
       
   131     CActive( EPriorityHigh ),
       
   132     iObserver( aObserver )
       
   133     {
       
   134     CActiveScheduler::Add( this );
       
   135     }
       
   136 
       
   137 void CCatalogsConnectionMonitor::ConstructL()
       
   138     {
       
   139     User::LeaveIfError( iSs.Connect() );
       
   140     User::LeaveIfError( iConnection.Open( iSs ) );
       
   141     }
       
   142     
       
   143 
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 // CCatalogsConnection
       
   147 // ---------------------------------------------------------------------------
       
   148 //    
       
   149 
       
   150 
       
   151     
       
   152     
       
   153 // ---------------------------------------------------------------------------
       
   154 // 
       
   155 // ---------------------------------------------------------------------------
       
   156 //    
       
   157 CCatalogsConnection* CCatalogsConnection::NewL(         
       
   158     CCatalogsHttpConnectionCreator& aConnectionCreator,
       
   159     CCatalogsHttpConnectionManager& aConnectionManager,
       
   160     MCatalogsConnectionObserver& aObserver )
       
   161     {
       
   162     CCatalogsConnection* self = new( ELeave ) 
       
   163         CCatalogsConnection( 
       
   164             aConnectionCreator, 
       
   165             aConnectionManager,
       
   166             aObserver );
       
   167     CleanupStack::PushL( self );
       
   168     self->ConstructL();
       
   169     CleanupStack::Pop( self );
       
   170     return self;
       
   171     }
       
   172 
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // 
       
   176 // ---------------------------------------------------------------------------
       
   177 //    
       
   178 void CCatalogsConnection::ConnectL( 
       
   179     const TCatalogsConnectionMethod& aMethod )
       
   180     {
       
   181     DLTRACEIN((""));
       
   182     iConnectionMethod = aMethod;
       
   183     iConnCreator.ConnectL( 
       
   184         iConnectionMethod, 
       
   185         iConnection,
       
   186         iStatus,
       
   187         &iConnManager );
       
   188         
       
   189     SetActive();
       
   190     }
       
   191 
       
   192 
       
   193 // ---------------------------------------------------------------------------
       
   194 // 
       
   195 // ---------------------------------------------------------------------------
       
   196 //    
       
   197 CCatalogsConnection::~CCatalogsConnection()
       
   198     {
       
   199     DLTRACEIN((""));
       
   200     Cancel();    
       
   201     iConnection.Close();
       
   202     iSs.Close();
       
   203     }
       
   204 
       
   205 
       
   206 // ---------------------------------------------------------------------------
       
   207 // 
       
   208 // ---------------------------------------------------------------------------
       
   209 //    
       
   210 RConnection& CCatalogsConnection::Connection()
       
   211     {
       
   212     return iConnection;
       
   213     }
       
   214     
       
   215 
       
   216 // ---------------------------------------------------------------------------
       
   217 // 
       
   218 // ---------------------------------------------------------------------------
       
   219 //    
       
   220 RSocketServ& CCatalogsConnection::SocketServer()
       
   221     {
       
   222     return iSs;
       
   223     }
       
   224     
       
   225     
       
   226 // ---------------------------------------------------------------------------
       
   227 // 
       
   228 // ---------------------------------------------------------------------------
       
   229 //    
       
   230 const TCatalogsConnectionMethod& CCatalogsConnection::ConnectionMethod() const
       
   231     {
       
   232     return iConnectionMethod;
       
   233     }
       
   234     
       
   235     
       
   236 // ---------------------------------------------------------------------------
       
   237 // 
       
   238 // ---------------------------------------------------------------------------
       
   239 //    
       
   240 TBool CCatalogsConnection::IsConnectedL()
       
   241     {
       
   242     DLTRACEIN((""));
       
   243     if ( iConnection.SubSessionHandle() ) 
       
   244         {
       
   245         
       
   246         TNifProgress progress;
       
   247         User::LeaveIfError( iConnection.Progress( progress ) );
       
   248         if ( progress.iStage == KLinkLayerOpen ) 
       
   249             {
       
   250             DLTRACEOUT(("Connected"));
       
   251             return ETrue;
       
   252             }
       
   253         }
       
   254     DLTRACEOUT(("Not connected"));
       
   255     return EFalse;
       
   256     }
       
   257 
       
   258 
       
   259 // ---------------------------------------------------------------------------
       
   260 // 
       
   261 // ---------------------------------------------------------------------------
       
   262 //    
       
   263 void CCatalogsConnection::RunL()
       
   264     {
       
   265     DLTRACEIN(("")); 
       
   266 
       
   267     // Let RunError handle errors
       
   268     User::LeaveIfError( iStatus.Int() );
       
   269 
       
   270     iObserver.ConnectionCreatedL( iConnectionMethod );
       
   271     }
       
   272     
       
   273     
       
   274 // ---------------------------------------------------------------------------
       
   275 // 
       
   276 // ---------------------------------------------------------------------------
       
   277 //    
       
   278 void CCatalogsConnection::DoCancel()
       
   279     {
       
   280     iConnCreator.Cancel();
       
   281     }
       
   282     
       
   283     
       
   284 // ---------------------------------------------------------------------------
       
   285 // 
       
   286 // ---------------------------------------------------------------------------
       
   287 //    
       
   288 TInt CCatalogsConnection::RunError( TInt aError )
       
   289     {
       
   290     DLTRACEIN(("aError: %d", aError));
       
   291     iObserver.ConnectionError( aError );
       
   292     return KErrNone;
       
   293     }
       
   294 
       
   295 
       
   296 // ---------------------------------------------------------------------------
       
   297 // 
       
   298 // ---------------------------------------------------------------------------
       
   299 //    
       
   300 CCatalogsConnection::CCatalogsConnection(
       
   301     CCatalogsHttpConnectionCreator& aConnectionCreator,
       
   302     CCatalogsHttpConnectionManager& aConnectionManager,
       
   303     MCatalogsConnectionObserver& aObserver ) :     
       
   304     iObserver( aObserver ),
       
   305     iConnCreator( aConnectionCreator ),
       
   306     iConnManager( aConnectionManager ),
       
   307     iConnectionMethod( 0, ECatalogsConnectionMethodTypeDeviceDefault )
       
   308     {
       
   309     CActiveScheduler::Add( this );
       
   310     }
       
   311 
       
   312 
       
   313 // ---------------------------------------------------------------------------
       
   314 // 
       
   315 // ---------------------------------------------------------------------------
       
   316 //    
       
   317 void CCatalogsConnection::ConstructL()
       
   318     {
       
   319     DLTRACEIN((""));
       
   320     User::LeaveIfError( iSs.Connect( KSocketServerMessageSlots ) );
       
   321     User::LeaveIfError( iConnection.Open( iSs ) );        
       
   322     }