natfw/tsrc/natfwtestconsoles/natfwtestconsole/src/testclient.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2007 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:    
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "testclient.h"
       
    22 
       
    23 #include <in_sock.h>
       
    24 #include <e32debug.h>
       
    25 #include <commdbconnpref.h>
       
    26 
       
    27 
       
    28 CTestClient* CTestClient::NewL( MTestClientNotifier* aNotifier )
       
    29     {
       
    30     CTestClient* self = CTestClient::NewLC( aNotifier );
       
    31     CleanupStack::Pop( self );
       
    32     return self;
       
    33     }
       
    34 
       
    35 CTestClient* CTestClient::NewLC( MTestClientNotifier* aNotifier )
       
    36     {   
       
    37     CTestClient* self = new( ELeave ) CTestClient( aNotifier );
       
    38     CleanupStack::PushL( self );
       
    39     self->ConstructL( );
       
    40     return self;
       
    41     }
       
    42     
       
    43 CTestClient::CTestClient( MTestClientNotifier* aNotifier ) :
       
    44     CActive( EPriorityStandard ), iNotifier( aNotifier )
       
    45     {
       
    46     CActiveScheduler::Add( this );
       
    47     }
       
    48 
       
    49 void CTestClient::ConstructL()
       
    50     {
       
    51     RDebug::Print( _L( "CTestClient::ConstructL" ) );
       
    52     User::LeaveIfError( iServer.Connect() );
       
    53     
       
    54     iTimer = CTimeOutTimer::NewL( EPriorityStandard, *this );
       
    55     }
       
    56 
       
    57 CTestClient::~CTestClient()
       
    58     {
       
    59     RDebug::Print( _L( "CTestClient::~CTestClient" ) );
       
    60     Cancel();
       
    61     iSocket.Close();
       
    62     iConnection.Close();
       
    63     iServer.Close();
       
    64     iTimer->Cancel();
       
    65     delete iTimer;
       
    66     } 
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // CTestClient::OpenL
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 void CTestClient::OpenL( TUint aIapId )
       
    73     {
       
    74     RDebug::Print( _L( "TestClient::OpenL" ) );
       
    75     TCommDbConnPref pref;
       
    76     pref.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
    77     pref.SetIapId( aIapId );
       
    78                                                  
       
    79     User::LeaveIfError( iConnection.Open( iServer ) );
       
    80     
       
    81     iConnection.Start( pref, iStatus );
       
    82     
       
    83     iState = EOpen;
       
    84     
       
    85     SetActive();
       
    86     }
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // CTestClient::ConnectL
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 void CTestClient::ConnectL( TInetAddr& aAddr, TUint aTimeoutTime )
       
    93     {
       
    94     RDebug::Print( _L( "TestClient::ConnectL" ) );
       
    95     __ASSERT_ALWAYS( EReady == iState, User::Leave( KErrInUse ) );
       
    96 
       
    97     iSocket.Connect( aAddr, iStatus );
       
    98     iState = EConnecting;
       
    99     
       
   100     if ( aTimeoutTime )
       
   101         {
       
   102         iTimer->After( aTimeoutTime );        
       
   103         }
       
   104 
       
   105     SetActive();
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // CTestClient::RunL
       
   110 // from CActive
       
   111 // ---------------------------------------------------------------------------
       
   112 //
       
   113 void CTestClient::RunL()
       
   114     {
       
   115     RDebug::Print( _L( "CTestClient::RunL, State: %d, iStatus: %d" ), iState, iStatus.Int() );
       
   116     iTimer->Cancel();
       
   117     
       
   118     switch( iState )
       
   119         {
       
   120         case EOpen:
       
   121             if ( KErrNone == iStatus.Int() )
       
   122                 {
       
   123                 User::LeaveIfError( iSocket.Open( iServer, KAfInet, KSockStream, KProtocolInetTcp ) );
       
   124                 User::LeaveIfError( iSocket.SetOpt( KSoReuseAddr, KProtocolInetIp, 1 ) );
       
   125             
       
   126                 iNotifier->Notify( MTestClientNotifier::EOpened, KErrNone );
       
   127                 iState = EReady;
       
   128                 }
       
   129             else
       
   130                 {
       
   131                 iNotifier->Notify( MTestClientNotifier::EOpened, iStatus.Int() );
       
   132                 }
       
   133             break;
       
   134         case EConnecting:
       
   135             iState = EConnected;
       
   136             iNotifier->Notify( MTestClientNotifier::EConnected, iStatus.Int() );
       
   137             break;   
       
   138         case EConnected:
       
   139             iNotifier->Notify( MTestClientNotifier::EConnected, iStatus.Int() );
       
   140             break;
       
   141         case EReady:
       
   142             break;
       
   143         }
       
   144     }
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // CTestClient::DoCancel
       
   148 // from CActive
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 void CTestClient::DoCancel()
       
   152     {
       
   153     iTimer->Cancel();
       
   154     }
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // CTestClient::TimerExpired
       
   158 // from MTimeoutNotifier
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void CTestClient::TimerExpired()
       
   162     {
       
   163     RDebug::Print( _L( "CTestClient::TimerExpired" ) );
       
   164     iNotifier->Notify( MTestClientNotifier::ETimerOccured, KErrNone );
       
   165     }
       
   166     
       
   167 // ---------------------------------------------------------------------------
       
   168 // CTestClient::ResolveLocalAddrL
       
   169 // ---------------------------------------------------------------------------
       
   170 //
       
   171 void CTestClient::ResolveLocalAddrL( TInetAddr& aAddr, TUint32 aIap )
       
   172     {
       
   173     RDebug::Print( _L( "CTestClient::ResolveLocalAddrL" ) );
       
   174     
       
   175     TBuf<60> buffer;
       
   176   
       
   177     RSocket socket;
       
   178     CleanupClosePushL( socket );
       
   179     
       
   180     User::LeaveIfError( socket.Open( iServer, KAfInet, KSockDatagram, 
       
   181                                      KProtocolInetUdp ) );
       
   182 
       
   183     if ( socket.SetOpt( KSoInetEnumInterfaces, KSolInetIfCtrl ) == KErrNone )
       
   184         {
       
   185         TPckgBuf<TSoInetInterfaceInfo> opt;
       
   186 
       
   187         while ( KErrNone == socket.GetOpt(
       
   188             KSoInetNextInterface, KSolInetIfCtrl, opt ) )
       
   189             {
       
   190             TPckgBuf<TSoInetIfQuery> optifquery;
       
   191             optifquery().iName = opt().iName;
       
   192 
       
   193             if( KErrNone == socket.GetOpt(
       
   194                 KSoInetIfQueryByName, KSolInetIfQuery, optifquery ) )
       
   195                 {
       
   196                 CheckAndSetAddr( aAddr, ( TInetAddr& )opt().iAddress,
       
   197                                  optifquery().iZone[1], aIap );
       
   198                 }
       
   199             }
       
   200         }
       
   201     aAddr.Output( buffer );
       
   202     
       
   203     RDebug::Print( _L( "CTestClient::ResolveLocalAddrL() - ADDRESS: %S" ),
       
   204         &buffer );
       
   205 
       
   206     CleanupStack::PopAndDestroy( &socket );
       
   207     }
       
   208 
       
   209 // ---------------------------------------------------------------------------
       
   210 // CTestClient::CheckAndSetAddr
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 void CTestClient::CheckAndSetAddr( TInetAddr& aTarget,
       
   214                                                 TInetAddr& aCandidate,
       
   215                                                 TUint32 aCandidateIap,
       
   216                                                 TUint32 aSpecifiedIap ) const
       
   217     {
       
   218     if ( !aCandidate.IsUnspecified() && !aCandidate.IsLoopback() )
       
   219         {
       
   220         if ( aCandidate.IsV4Mapped())
       
   221             {
       
   222             aCandidate.ConvertToV4();
       
   223             }
       
   224         if ( aCandidateIap == aSpecifiedIap )
       
   225             {
       
   226             SetAddr( aTarget, aCandidate );
       
   227             }
       
   228         }
       
   229     }
       
   230 
       
   231 // ---------------------------------------------------------------------------
       
   232 // CTestClient::SetAddr
       
   233 // ---------------------------------------------------------------------------
       
   234 //
       
   235 void CTestClient::SetAddr( TInetAddr& aTarget,
       
   236     const TInetAddr& aSource ) const
       
   237     {
       
   238     if ( KAfInet6 == aSource.Family() )
       
   239         {
       
   240         if ( !aSource.IsLinkLocal() )
       
   241             {
       
   242             aTarget = aSource;
       
   243             aTarget.SetScope( 0 );
       
   244             }
       
   245         }
       
   246     else
       
   247         {
       
   248         aTarget = aSource;
       
   249         aTarget.SetScope( 0 );
       
   250         }
       
   251     }