natfw/natfwconnectionmultiplexer/src/cncmstream.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2006-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:    Connection multiplexer stream abstraction.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "cncmstream.h"
       
    22 #include "ncmconnectionmultiplexerlogs.h"
       
    23 #include "ncmconnectionmultiplexerassert.h"
       
    24 #include "natfwmediawrapper.h"
       
    25 #include "cncmconnectionobserverhandler.h"
       
    26 #include "cncmconnection.h"
       
    27 
       
    28 const TUint KFirstConnectionId = 1;
       
    29 
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // CNcmStream::CNcmStream
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CNcmStream::CNcmStream( TUint aSessionId,
       
    36     TUint aStreamId, TInt aQos, TUint aProtocol ):
       
    37     iSessionId( aSessionId ), iStreamId( aStreamId ), iQos( aQos ),
       
    38     iStreamProtocol( aProtocol ),
       
    39     iNextConnectionId( KFirstConnectionId ),
       
    40     iMediaConnectionId( 0 )
       
    41     {
       
    42     }
       
    43 
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // CNcmStream::NewL
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CNcmStream* CNcmStream::NewL( TUint aSessionId,
       
    50     TUint aStreamId, TInt aQos, TUint aProtocol )
       
    51     {
       
    52     __CONNECTIONMULTIPLEXER( "CNcmStream::NewL" )
       
    53     
       
    54     CNcmStream* self =
       
    55         CNcmStream::NewLC( aSessionId, aStreamId, aQos, aProtocol );
       
    56             
       
    57     CleanupStack::Pop( self );
       
    58     return self;
       
    59     }
       
    60 
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CNcmStream::NewLC
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CNcmStream* CNcmStream::NewLC( TUint aSessionId,
       
    67     TUint aStreamId, TInt aQos, TUint aProtocol )
       
    68     {
       
    69     __CONNECTIONMULTIPLEXER( "CNcmStream::NewLC" )
       
    70     
       
    71     CNcmStream* self =
       
    72         new( ELeave ) CNcmStream( aSessionId, aStreamId, aQos, aProtocol );
       
    73         
       
    74     CleanupStack::PushL( self );
       
    75     self->ConstructL();
       
    76     return self;
       
    77     }
       
    78 
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 // CNcmStream::ConstructL
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 void CNcmStream::ConstructL()
       
    85     {
       
    86     iConnectionObserverHandler = CNcmConnectionObserverHandler::NewL();
       
    87     }
       
    88 
       
    89     
       
    90 // ---------------------------------------------------------------------------
       
    91 // CNcmStream::~CNcmStream
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 CNcmStream::~CNcmStream()
       
    95     {
       
    96     __CONNECTIONMULTIPLEXER_INT1( "CNcmStream::~CNcmStream, CONN_COUNT: ", 
       
    97         iConnections.Count() )
       
    98     
       
    99     TInt ind( KErrNotFound );
       
   100     while ( KErrNotFound != ( ind = iConnections.Count() - 1 ) )
       
   101         {
       
   102         TUint connectionId = iConnections[ind]->ConnectionId();
       
   103         
       
   104         delete iConnections[ind];
       
   105         iConnections.Remove( ind );
       
   106         
       
   107         if ( iConnectionObserverHandler )
       
   108             {
       
   109             iConnectionObserverHandler->ConnectionNotify( iStreamId,
       
   110                 connectionId, EMultiplexerConnectionRemoved, KErrNone );
       
   111             }
       
   112         }
       
   113     
       
   114     iConnections.Close();
       
   115     
       
   116     if ( iWrapper )
       
   117         {
       
   118         iWrapper->Close();  
       
   119         }
       
   120     
       
   121     iWrapper = NULL;
       
   122     delete iConnectionObserverHandler;
       
   123     }
       
   124 
       
   125    
       
   126 // ---------------------------------------------------------------------------
       
   127 // CNcmStream::CreateConnectionL
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 TUint CNcmStream::CreateConnectionL(
       
   131     MNcmMultiplexerConnectionObserver& aObserver,
       
   132     CNcmConnectionObserverHandler& aConnectionObserverHandler,
       
   133     RSocketServ& aSocketServ,
       
   134     RConnection& aConnection,
       
   135     const TInetAddr& aLocalAddress,
       
   136     TBool aReuseAddress )
       
   137     {   
       
   138     CNcmConnection* connection(
       
   139         CNcmConnection::NewLC( iSessionId, iStreamId,
       
   140             iNextConnectionId, aLocalAddress, aObserver,
       
   141             aConnectionObserverHandler, iQos,
       
   142             aSocketServ, aConnection, iStreamProtocol, aReuseAddress ) );
       
   143 
       
   144     iConnections.AppendL( connection );
       
   145     
       
   146     iNextConnectionId++;
       
   147     CleanupStack::Pop( connection ); 
       
   148     return connection->ConnectionId();
       
   149     }
       
   150 
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // CNcmStream::RemoveConnectionL
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 void CNcmStream::RemoveConnectionL( TUint aConnectionId )
       
   157     {
       
   158     __CONNECTIONMULTIPLEXER_INT1(
       
   159         "CNcmStream::Remove - RemoveConnectionL: ", aConnectionId )
       
   160     
       
   161     TInt count( iConnections.Count() );
       
   162     TBool found( EFalse );
       
   163         
       
   164     for ( TInt i = 0; i < count; i++ )
       
   165         {
       
   166         if ( iConnections[i]->ConnectionId() == aConnectionId )
       
   167             {      
       
   168             delete iConnections[i];  
       
   169             iConnections.Remove( i );
       
   170             found = ETrue;
       
   171             break;
       
   172             }
       
   173         }
       
   174        
       
   175     if ( !found )
       
   176         {
       
   177         User::Leave( KErrNotFound );
       
   178         }
       
   179     }
       
   180 
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // CNcmStream::StreamId
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 TUint CNcmStream::StreamId() const
       
   187     {
       
   188     return iStreamId;
       
   189     }
       
   190 
       
   191 
       
   192 // ---------------------------------------------------------------------------
       
   193 // CNcmStream::Qos
       
   194 // ---------------------------------------------------------------------------
       
   195 //
       
   196 TInt CNcmStream::Qos() const
       
   197     {
       
   198     return iQos;
       
   199     }
       
   200 
       
   201 
       
   202 // ---------------------------------------------------------------------------
       
   203 // CNcmStream::SetQos
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 void CNcmStream::SetQos( TInt aQos )
       
   207     {
       
   208     __CONNECTIONMULTIPLEXER_INT1( "CNcmStream::SetQos - aQos: ", aQos )
       
   209     
       
   210     iQos = aQos;
       
   211     }
       
   212 
       
   213 
       
   214 // ---------------------------------------------------------------------------
       
   215 // CNcmStream::ConnectionL
       
   216 // ---------------------------------------------------------------------------
       
   217 //
       
   218 CNcmConnection* CNcmStream::ConnectionL(
       
   219     TUint aConnectionId )
       
   220     {
       
   221     __CONNECTIONMULTIPLEXER_INT1(
       
   222         "CNcmStream::ConnectionL - aConnectionId: ", aConnectionId )
       
   223     
       
   224     TInt count( iConnections.Count() );
       
   225     
       
   226     for ( TInt i = 0; i < count; i++ )
       
   227         {
       
   228         if ( iConnections[i]->ConnectionId() == aConnectionId )
       
   229             {
       
   230             return iConnections[i];
       
   231             }
       
   232         }
       
   233     User::Leave( KErrNotFound );    
       
   234 #ifndef _DEBUG_EUNIT
       
   235     return NULL;
       
   236 #endif
       
   237     }
       
   238 
       
   239 
       
   240 // ---------------------------------------------------------------------------
       
   241 // CNcmStream::RegisterWrapperL
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 void CNcmStream::RegisterWrapperL(
       
   245     MNATFWMediaWrapper* aWrapper )
       
   246     {
       
   247     __CONNECTIONMULTIPLEXER( 
       
   248         "CNcmStream::RegisterWrapperL" )
       
   249     __CONNECTIONMULTIPLEXER_ASSERT_L( NULL != aWrapper, KErrArgument );
       
   250     __CONNECTIONMULTIPLEXER_ASSERT_L( NULL == iWrapper, KErrAlreadyExists );
       
   251         
       
   252     iWrapper = aWrapper;
       
   253     }
       
   254 
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // CNcmStream::WrapperL
       
   258 // ---------------------------------------------------------------------------
       
   259 //
       
   260 MNATFWMediaWrapper* CNcmStream::WrapperL()
       
   261     {
       
   262     __CONNECTIONMULTIPLEXER( "CNcmStream::WrapperL" )
       
   263     __CONNECTIONMULTIPLEXER_ASSERT_L( NULL != iWrapper, KErrNotReady );
       
   264 
       
   265     return iWrapper;
       
   266     }
       
   267 
       
   268 
       
   269 // ---------------------------------------------------------------------------
       
   270 // CNcmStream::StreamProtocol
       
   271 // ---------------------------------------------------------------------------
       
   272 //
       
   273 TUint CNcmStream::StreamProtocol() const
       
   274     {
       
   275     __CONNECTIONMULTIPLEXER( "CNcmStream::StreamProtocol" )
       
   276 
       
   277     return iStreamProtocol;
       
   278     }
       
   279 
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // CNcmStream::SetStreamProtocol
       
   283 // ---------------------------------------------------------------------------
       
   284 //
       
   285 void CNcmStream::SetStreamProtocol(
       
   286     TUint aProtocol )
       
   287     {
       
   288     __CONNECTIONMULTIPLEXER( "CNcmStream::SetStreamProtocol" )
       
   289         
       
   290     iStreamProtocol = aProtocol;
       
   291     }
       
   292 
       
   293 
       
   294 
       
   295 // ---------------------------------------------------------------------------
       
   296 // CNcmStream::MediaConnectionId
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 TUint CNcmStream::MediaConnectionId() const
       
   300     {
       
   301     __CONNECTIONMULTIPLEXER( "CNcmStream::MediaConnectionId ")
       
   302         
       
   303     return iMediaConnectionId;
       
   304     }
       
   305     
       
   306 
       
   307 // ---------------------------------------------------------------------------
       
   308 // CNcmStream::SetMediaConnectionId
       
   309 // ---------------------------------------------------------------------------
       
   310 //
       
   311 void CNcmStream::SetMediaConnectionId( TUint aMediaConnectionId )
       
   312     {
       
   313     __CONNECTIONMULTIPLEXER( "CNcmStream::SetMediaConnectionId ")
       
   314         
       
   315     iMediaConnectionId = aMediaConnectionId;
       
   316     }
       
   317 
       
   318 
       
   319 // -----------------------------------------------------------------------------
       
   320 // CNcmStream::ConnectionObserverHandler
       
   321 // -----------------------------------------------------------------------------
       
   322 //    
       
   323 CNcmConnectionObserverHandler& CNcmStream::ConnectionObserverHandler()   
       
   324     {
       
   325     return *iConnectionObserverHandler;
       
   326     }
       
   327 
       
   328 
       
   329 // ---------------------------------------------------------------------------
       
   330 // CNcmStream::ConnectionIdByDestinationAddressL(
       
   331 // ---------------------------------------------------------------------------
       
   332 //
       
   333 CNcmConnection* CNcmStream::ConnectionByDestinationAddressL(
       
   334     const TInetAddr& aDestinationAddress )
       
   335     {
       
   336     __CONNECTIONMULTIPLEXER( "CNcmStream::ConnectionByDestinationAddressL" ) 
       
   337     
       
   338     TInt ind( iConnections.Count() );
       
   339     TInt genericConnectionInd( KErrNotFound );
       
   340     
       
   341     while ( ind-- )
       
   342         {
       
   343         if ( iConnections[ind]->IsGenericDestination() )
       
   344             {
       
   345             genericConnectionInd = ind;
       
   346             }
       
   347         
       
   348         if ( iConnections[ind]->
       
   349             CheckSenderValidityToSending( aDestinationAddress ) )
       
   350             {
       
   351             return iConnections[ind];
       
   352             }
       
   353         }
       
   354     
       
   355     // as a last resort generic connection can be used if one exists
       
   356     if ( KErrNotFound != genericConnectionInd )
       
   357         {
       
   358         return iConnections[genericConnectionInd];
       
   359         }
       
   360     
       
   361     User::Leave( KErrNotFound );
       
   362 #ifndef _DEBUG_EUNIT
       
   363     return NULL;
       
   364 #endif
       
   365     }   
       
   366 
       
   367 
       
   368 // ---------------------------------------------------------------------------
       
   369 // CNcmStream::RegisterMediaSourceL
       
   370 // ---------------------------------------------------------------------------
       
   371 //
       
   372 void CNcmStream::RegisterMediaSourceL( CNcmMediaSource* aMediaSource )
       
   373     {
       
   374     iConnectionObserverHandler->RegisterMediaSourceL( aMediaSource );
       
   375     }