natfw/natfwclient/tsrc/ut_natfwclient/stubs/natfwconnectionhandler.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 "mncmconnectionmultiplexer.h"
       
    22 #include <e32std.h>
       
    23 #include <e32base.h>
       
    24 #include "natfwcandidate.h"
       
    25 #include "natfwconnectionhandler.h"
       
    26 
       
    27 #include "natfwpluginlogs.h"
       
    28 #include "natfwcandidate.h"
       
    29 #include "natfwcandidatepair.h"
       
    30 #include "mnatfwpluginobserver.h"
       
    31 
       
    32 const TUint32 KErrorTestAddress = INET_ADDR( 255, 255, 255, 255 );
       
    33 const TUint32 KLocalAddress = INET_ADDR( 100, 100, 100, 100 );
       
    34 const TUint KLocalPort = 1000;
       
    35 const TUint KTimerVal = 1000000; //1000ms
       
    36 
       
    37 
       
    38 // ======== MEMBER FUNCTIONS ========
       
    39 
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // Symbian constructor
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 CNATFWConnectionHandler* CNATFWConnectionHandler::NewL( 
       
    46     MNATFWPluginObserver* aPluginObserver, CNATFWPluginApi& aPlugin )
       
    47     {
       
    48     __NATPLUGIN( "CNATFWConnectionHandler::NewL" )
       
    49     CNATFWConnectionHandler* self = 
       
    50         new( ELeave ) CNATFWConnectionHandler( aPluginObserver, aPlugin );
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL();
       
    53     CleanupStack::Pop( self );
       
    54     return self;
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // C++ default constructor
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CNATFWConnectionHandler::CNATFWConnectionHandler(
       
    62     MNATFWPluginObserver* aPluginObserver,
       
    63     CNATFWPluginApi& aPlugin ) :
       
    64     iPluginObserver(aPluginObserver), iPlugin( aPlugin ),
       
    65     CActive( CActive::EPriorityStandard )
       
    66     {
       
    67     CActiveScheduler::Add( this );
       
    68     }
       
    69 
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // Destructor
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 CNATFWConnectionHandler::~CNATFWConnectionHandler()
       
    76     {
       
    77     delete iDomain;
       
    78     iConnMux = NULL;
       
    79     delete iCandidate;    
       
    80     delete iCandidatePair;
       
    81     Cancel();
       
    82     iTimer.Cancel();
       
    83     iTimer.Close();
       
    84     
       
    85     }
       
    86 
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // Symbian constructor
       
    90 // Creates NATFWStunConnectionHandler.
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 void CNATFWConnectionHandler::ConstructL( )
       
    94     {
       
    95     User ::LeaveIfError(iTimer.CreateLocal()) ;
       
    96     }
       
    97 
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // Initializes STUN Connection Handler.
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 void CNATFWConnectionHandler::PluginInitializeL(
       
   104     TUint32 aIapId,
       
   105     const TDesC8& aDomain,
       
   106     MNcmConnectionMultiplexer& aMultiplexer )
       
   107     {
       
   108     iConnMux = &aMultiplexer;
       
   109     iDomain = aDomain.AllocL();
       
   110     iIapId = aIapId;
       
   111     }
       
   112     
       
   113 // ---------------------------------------------------------------------------
       
   114 // NAT FW Client calls to fetch candidate. Forwards request to 
       
   115 // Stun Connection Handler.
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 void CNATFWConnectionHandler::FetchCandidateL( TUint aStreamId )
       
   119     {
       
   120     iStreamId = aStreamId;
       
   121     
       
   122     iState = EFetchCandidate;
       
   123         
       
   124     TInetAddr publicAddr( KLocalAddress, KLocalPort );
       
   125     
       
   126     delete iCandidate;
       
   127     iCandidate = NULL;
       
   128     
       
   129     iCandidate = CNATFWCandidate::NewL();
       
   130     
       
   131     //Set candidate parameters
       
   132     iCandidate->SetStreamId( aStreamId );
       
   133     iCandidate->SetType( CNATFWCandidate::EServerReflexive );
       
   134     iCandidate->SetTransportAddrL( publicAddr );
       
   135     
       
   136     if ( iFetchQueue == 0 )
       
   137         {
       
   138         iTimer.After( iStatus, TTimeIntervalMicroSeconds32( KTimerVal ) );
       
   139         SetActive();
       
   140         }
       
   141     iFetchQueue++;
       
   142     }
       
   143 
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 // CNATFWConnectionHandler::FetchCandidatesL
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 void CNATFWConnectionHandler::FetchCandidatesL( TUint aStreamId )
       
   150     {
       
   151     iStreamId = aStreamId;
       
   152     
       
   153     iState = EFetchCandidates;
       
   154         
       
   155     TInetAddr publicAddr( KLocalAddress, KLocalPort );
       
   156     
       
   157     delete iCandidate;
       
   158     iCandidate = NULL;
       
   159     
       
   160     iCandidate = CNATFWCandidate::NewL();
       
   161     
       
   162     //Set candidate parameters
       
   163     iCandidate->SetStreamId( aStreamId );
       
   164     iCandidate->SetType( CNATFWCandidate::EServerReflexive );
       
   165     iCandidate->SetTransportAddrL( publicAddr );
       
   166     
       
   167     iTimer.After( iStatus, TTimeIntervalMicroSeconds32( KTimerVal ) );
       
   168     SetActive();
       
   169     }
       
   170 
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // CNATFWConnectionHandler::PerformConnectivityChecksL
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 void CNATFWConnectionHandler::PerformConnectivityChecksL()
       
   177     {
       
   178     const TUint KStreamId = 99;
       
   179     
       
   180     iStreamId = KStreamId;
       
   181     TInetAddr publicAddr( KLocalAddress, KLocalPort );
       
   182     
       
   183     iState = EConnectivityChecks;
       
   184     
       
   185     CNATFWCandidate* remoteCand = CNATFWCandidate::NewLC();
       
   186     CNATFWCandidate* localCand = CNATFWCandidate::NewLC();
       
   187     
       
   188     //Set candidate parameters
       
   189     remoteCand->SetStreamId( KStreamId );
       
   190     remoteCand->SetType( CNATFWCandidate::EServerReflexive );
       
   191     remoteCand->SetTransportAddrL( publicAddr );
       
   192     
       
   193     localCand->SetStreamId( KStreamId );
       
   194     localCand->SetType( CNATFWCandidate::EServerReflexive );
       
   195     localCand->SetTransportAddrL( publicAddr );
       
   196     
       
   197     delete iCandidatePair;
       
   198     iCandidatePair = NULL;
       
   199     
       
   200     iCandidatePair = CNATFWCandidatePair::NewL( *localCand, *remoteCand );
       
   201     
       
   202     CleanupStack::PopAndDestroy( 2 );
       
   203     
       
   204     iTimer.After( iStatus, TTimeIntervalMicroSeconds32( KTimerVal ) );
       
   205     SetActive();
       
   206     }
       
   207 
       
   208 
       
   209 // ---------------------------------------------------------------------------
       
   210 // Connect. Not in use.
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 void CNATFWConnectionHandler::ConnectL()
       
   214     {
       
   215     User::Leave( KErrNotSupported );
       
   216     }
       
   217 
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // Notifies the Plug-in's client of stream's current sending status.
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 void CNATFWConnectionHandler::SetReceivingStateL( TUint aStreamId,
       
   224         TNATFWStreamingState aState )
       
   225     {
       
   226     iStreamId = aStreamId;
       
   227     iStreamingStatus = aState;
       
   228     iState = ESetReceivingState;
       
   229     
       
   230     iTimer.After( iStatus, TTimeIntervalMicroSeconds32( KTimerVal ) );
       
   231     SetActive();
       
   232     }
       
   233 
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // Notifies the Plug-in's client of stream's current sending status.
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 void CNATFWConnectionHandler::SetSendingStateL( TUint aStreamId,
       
   240                                           const TInetAddr& aDestAddr,
       
   241                                           TNATFWStreamingState aState )
       
   242     {
       
   243     iStreamId = aStreamId;
       
   244     iStreamingStatus = aState;
       
   245     
       
   246     if ( KErrorTestAddress == aDestAddr.Address() )
       
   247         {
       
   248         iState = EErrorTestAddress;
       
   249         }
       
   250     else
       
   251         {
       
   252         iState = ESetSendingState;
       
   253         }
       
   254 
       
   255     iTimer.After( iStatus, TTimeIntervalMicroSeconds32( KTimerVal ) );
       
   256     SetActive();
       
   257     }
       
   258 
       
   259 // ---------------------------------------------------------------------------
       
   260 // CNATFWConnectionHandler::RunL
       
   261 // ---------------------------------------------------------------------------
       
   262 //
       
   263 void CNATFWConnectionHandler::RunL()
       
   264     {
       
   265     switch( iState )
       
   266         {
       
   267         case EFetchCandidate:
       
   268             iPluginObserver->NewLocalCandidateFound( iPlugin, iCandidate );
       
   269             iPluginObserver->Notify( iPlugin, iStreamId, 
       
   270                 MNATFWPluginObserver::EFetchingCompleted, KErrNone );
       
   271             
       
   272             iFetchQueue--;
       
   273             
       
   274             if ( iFetchQueue > 0 )
       
   275                 {
       
   276                 iTimer.After( iStatus,
       
   277                     TTimeIntervalMicroSeconds32( KTimerVal ) );
       
   278                 SetActive();
       
   279                 iState = EFetchCandidate;
       
   280                 }
       
   281             else
       
   282                 {
       
   283                 iState = EIdle;
       
   284                 }
       
   285             break;
       
   286             
       
   287         case EFetchCandidates:
       
   288             iPluginObserver->NewLocalCandidateFound( iPlugin, iCandidate );
       
   289             iPluginObserver->Notify( iPlugin, iStreamId, 
       
   290                 MNATFWPluginObserver::EFetchingCompleted, KErrNone );
       
   291             iState = EIdle;
       
   292             break;
       
   293         case EConnectivityChecks:
       
   294             iPluginObserver->NewCandidatePairFound( iPlugin, iCandidatePair );
       
   295             iPluginObserver->Notify( iPlugin, iStreamId, 
       
   296                 MNATFWPluginObserver::EConnChecksCompleted, KErrNone );
       
   297             iState = EIdle;
       
   298             break;
       
   299         case ESetReceivingState:
       
   300             if ( EStreamingStateActive == iStreamingStatus )
       
   301                 {
       
   302                 iPluginObserver->Notify( iPlugin, iStreamId, 
       
   303                     MNATFWPluginObserver::EReceivingActivated, KErrNone );
       
   304                 }
       
   305             else
       
   306                 {
       
   307                 iPluginObserver->Notify( iPlugin, iStreamId, 
       
   308                     MNATFWPluginObserver::EReceivingDeactivated, KErrNone );
       
   309                 }
       
   310             iState = EIdle;
       
   311             break;
       
   312             
       
   313         case ESetSendingState:
       
   314             if (  EStreamingStateActive == iStreamingStatus )
       
   315                 {
       
   316                 iPluginObserver->Notify( iPlugin, iStreamId, 
       
   317                     MNATFWPluginObserver::ESendingActivated, KErrNone );
       
   318                 }
       
   319             else
       
   320                 {
       
   321                 iPluginObserver->Notify( iPlugin, iStreamId, 
       
   322                     MNATFWPluginObserver::ESendingDeactivated, KErrNone );
       
   323                 }
       
   324             iState = EIdle;
       
   325             break;
       
   326         case EErrorTestAddress:
       
   327             iPluginObserver->Error( iPlugin, iStreamId, KErrArgument );
       
   328             iState = EIdle;
       
   329             break;
       
   330         default:
       
   331             iState = EIdle;
       
   332             break;
       
   333         }
       
   334     }
       
   335 
       
   336 // ---------------------------------------------------------------------------
       
   337 // CNATFWConnectionHandler::DoCancel
       
   338 // ---------------------------------------------------------------------------
       
   339 //    
       
   340 void CNATFWConnectionHandler::DoCancel()
       
   341     {
       
   342     iTimer.Cancel();
       
   343     }
       
   344     
       
   345 // End of file