upnp/upnpstack/serviceframework/src/upnpeventcontroller.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2008-2008 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:  Encapsulates http client for eventing
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <e32base.h>
       
    21 
       
    22 #include "upnpsettings.h"
       
    23 #include "upnpeventcontroller.h"
       
    24 #include "upnpgenamessagefactory.h"
       
    25 #include "upnphttpmessagefactory.h"
       
    26 #include "upnphttpinitialeventtransaction.h"
       
    27 #include "upnpnonmoderatedeventqueuemanager.h"
       
    28 #include "upnpmoderatedeventqueuemanager.h"
       
    29 #include "upnpinitialeventqueuemanager.h"
       
    30 #include "upnpdeviceimplementationbase.h"
       
    31 #include "upnpcommonupnplits.h"
       
    32 
       
    33 #define KLogFile _L("UPnPStack.txt")
       
    34 #include "upnpcustomlog.h"
       
    35   
       
    36   
       
    37 // -----------------------------------------------------------------------------
       
    38 // CUpnpEventController::CUpnpEventController
       
    39 // Default C++ constructor
       
    40 // -----------------------------------------------------------------------------
       
    41 //  
       
    42 CUpnpEventController::CUpnpEventController( CUpnpServiceImplementation& aServiceImpl )
       
    43     :iServiceImpl( aServiceImpl )
       
    44     {
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CUpnpEventController::CUpnpEventController
       
    49 // Destructor
       
    50 // -----------------------------------------------------------------------------
       
    51 //  
       
    52 CUpnpEventController::~CUpnpEventController()
       
    53     {
       
    54     iPendingTransactions.ResetAndDestroy();
       
    55     delete iModQueue;
       
    56     delete iNonModQueue;
       
    57     delete iInitialEventQueue;
       
    58     delete iSubscriberLibrary;
       
    59     delete iHttpEngine;
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CUpnpEventController::NewLC
       
    64 // Two phased constructor
       
    65 // -----------------------------------------------------------------------------
       
    66 //  
       
    67 CUpnpEventController* CUpnpEventController::NewLC( CUpnpServiceImplementation& aServiceImpl )
       
    68     {
       
    69     CUpnpEventController* self = new (ELeave) CUpnpEventController( aServiceImpl );
       
    70     CleanupStack::PushL(self);
       
    71     self->ConstructL();
       
    72     return self;
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CUpnpEventController::NewL
       
    77 // Two phased constructor
       
    78 // -----------------------------------------------------------------------------
       
    79 //  
       
    80 CUpnpEventController* CUpnpEventController::NewL( CUpnpServiceImplementation& aServiceImpl )
       
    81     {
       
    82     CUpnpEventController* self=CUpnpEventController::NewLC( aServiceImpl );
       
    83     CleanupStack::Pop( self );
       
    84     return self;
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CUpnpEventController::ConstructL
       
    89 // Two phased constructor
       
    90 // -----------------------------------------------------------------------------
       
    91 //  
       
    92 void CUpnpEventController::ConstructL()
       
    93     {
       
    94     iSubscriberLibrary = CUpnpSubscriberLibrary::NewL( *this );
       
    95     iHttpEngine = CUpnpHttpClientEngine::NewL( *this, CUpnpSettings::GetIapL() );
       
    96     iNonModQueue = CUpnpNonModeratedEventQueueManager::NewL( iServiceImpl , *this, iSubscriberLibrary );
       
    97     iModQueue =  CUpnpModeratedEventQueueManager::NewL(  iServiceImpl , *this, iSubscriberLibrary );
       
    98     iInitialEventQueue = CUpnpInitialEventQueueManager::NewL(  iServiceImpl , *this, iSubscriberLibrary );
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CUpnpEventController::SendInitialNotification
       
   103 // Sends initial event
       
   104 // -----------------------------------------------------------------------------
       
   105 //  
       
   106 void CUpnpEventController::SendInitialNotification( )
       
   107     {
       
   108     iInitialEventQueue->SendEvent();
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CUpnpEventController::SendNonModeratedNotification
       
   113 // Sends not-moderated events
       
   114 // -----------------------------------------------------------------------------
       
   115 //  
       
   116 void CUpnpEventController::SendNonModeratedNotification( )
       
   117     {
       
   118     iNonModQueue->SendEvent();
       
   119     }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // CUpnpEventController::SendModeratedNotification
       
   123 // Sends moderated event
       
   124 // -----------------------------------------------------------------------------
       
   125 //  
       
   126 void CUpnpEventController::SendModeratedNotification(  )
       
   127     {
       
   128     iModQueue->SendEvent();
       
   129     }
       
   130     
       
   131 // -----------------------------------------------------------------------------
       
   132 // CUpnpEventController::SendTransactionL
       
   133 // Sends event to subscriber
       
   134 // -----------------------------------------------------------------------------
       
   135 //  
       
   136 void  CUpnpEventController::SendTransactionL( CUpnpHttpTransaction* aTransaction )
       
   137     {
       
   138     CleanupStack::PushL( aTransaction );
       
   139     iPendingTransactions.AppendL( aTransaction );
       
   140     CleanupStack::Pop( aTransaction );
       
   141     
       
   142     iHttpEngine->SendL( *aTransaction );
       
   143     }
       
   144  
       
   145 // -----------------------------------------------------------------------------
       
   146 // CUpnpEventController::SubscribeL
       
   147 // Proccess subscribe message
       
   148 // -----------------------------------------------------------------------------
       
   149 //  
       
   150 void CUpnpEventController::SubscribeL( CUpnpGenaMessage* aMessage )
       
   151     {
       
   152     TInt subscribers = iSubscriberLibrary->SubscriberLibrary().Count();
       
   153         
       
   154     CUpnpSubscriberLibraryElement* subscriber( NULL );
       
   155     TUpnpErrorCode subscriptionState 
       
   156         = iSubscriberLibrary->AddInfoL( aMessage, &subscriber );
       
   157 
       
   158     // Responding:
       
   159     if ( subscriber && subscriptionState == EHttpOk )
       
   160         {
       
   161         // for a new subscriber: SID generation
       
   162         if ( subscribers < iSubscriberLibrary->SubscriberLibrary().Count() )
       
   163             {
       
   164             GenerateSidL( subscriber );
       
   165             }
       
   166             
       
   167         HBufC8* timeout( TimerHeaderLC( subscriber->Timeout() ) );
       
   168         
       
   169         CUpnpHttpMessage* messageOut = reinterpret_cast<CUpnpHttpMessage*>(
       
   170                                     RUpnpGenaMessageFactory::SubscribeResponseL(
       
   171                                                     aMessage->SessionId(), 
       
   172                                                     aMessage->Sender(), 
       
   173                                                     subscriber->Sid(), 
       
   174                                                     timeout->Des() )
       
   175                                                                             );
       
   176         CleanupStack::PopAndDestroy( timeout );                    
       
   177         timeout = NULL;
       
   178         CleanupStack::PushL( messageOut );
       
   179 
       
   180         iServiceImpl.SendL( messageOut );
       
   181 
       
   182         CleanupStack::PopAndDestroy( messageOut );
       
   183         messageOut = NULL;
       
   184         
       
   185             
       
   186         // for a new subscriber: initial event message
       
   187         if ( subscribers < iSubscriberLibrary->SubscriberLibrary().Count() )
       
   188             {
       
   189             SendInitialNotification( );
       
   190             }
       
   191         }
       
   192     else
       
   193         {       
       
   194         CUpnpHttpMessage* messageOut = RUpnpHttpMessageFactory::UpnpResponseErrorL( 
       
   195                                                                 aMessage, 
       
   196                                                                 subscriptionState 
       
   197                                                                                   );
       
   198         CleanupStack::PushL( messageOut );
       
   199         iServiceImpl.SendL( messageOut );
       
   200         CleanupStack::PopAndDestroy( messageOut );
       
   201         }       
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // CUpnpEventController::UnSubscribeL
       
   206 // Proccess unsubscribe message
       
   207 // -----------------------------------------------------------------------------
       
   208 //  
       
   209 void CUpnpEventController::UnSubscribeL( CUpnpGenaMessage* aMessage )
       
   210     {
       
   211     TUpnpErrorCode subscriptionState( CheckHeaders( aMessage ) );
       
   212 
       
   213     if ( subscriptionState == EUndefined )
       
   214         {
       
   215         subscriptionState = iSubscriberLibrary->Remove( aMessage->Sid() );
       
   216         } 
       
   217     
       
   218     if ( subscriptionState == EHttpOk )
       
   219         {
       
   220         CUpnpHttpMessage* messageOut = reinterpret_cast<CUpnpHttpMessage*>(
       
   221                 RUpnpGenaMessageFactory::UnsubscribeResponseL( aMessage->SessionId(), 
       
   222                                                                aMessage->Sender() ) 
       
   223                                                                            );
       
   224         CleanupStack::PushL( messageOut );        
       
   225         iServiceImpl.SendL( messageOut );
       
   226         CleanupStack::PopAndDestroy( messageOut );   
       
   227         }
       
   228     else
       
   229         {
       
   230         CUpnpHttpMessage* messageOut = RUpnpHttpMessageFactory::UpnpResponseErrorL( 
       
   231                                                                     aMessage,
       
   232                                                                     subscriptionState
       
   233                                                                                    );
       
   234         CleanupStack::PushL( messageOut );
       
   235         iServiceImpl.SendL( messageOut );
       
   236         CleanupStack::PopAndDestroy( messageOut );
       
   237         }
       
   238     } 
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // CUpnpEventController::CancelInvalidSessions
       
   242 // Cancels not completed transaction 
       
   243 // -----------------------------------------------------------------------------
       
   244 // 
       
   245 void CUpnpEventController::CancelInvalidSessions( const TDesC8& aSid )
       
   246     {
       
   247     for ( TInt i(0) ; i < iPendingTransactions.Count() ; i++ )
       
   248         {
       
   249         if ( static_cast<CUpnpGenaMessage*>( iPendingTransactions[i]->Request() )->Sid().Compare( aSid ) == 0 )
       
   250             {         
       
   251             iHttpEngine->Cancel( *iPendingTransactions[i] );
       
   252             }
       
   253         }
       
   254     }
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // CUpnpEventController::SubscribersAmount
       
   258 // Returns number of subscribers 
       
   259 // -----------------------------------------------------------------------------
       
   260 // 
       
   261 TInt CUpnpEventController::SubscribersAmount()
       
   262     {
       
   263     return iSubscriberLibrary->SubscriberLibrary().Count();
       
   264     }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CUpnpEventController::CheckHeader
       
   268 // Check if unsubscribe header are comlete 
       
   269 // -----------------------------------------------------------------------------
       
   270 // 
       
   271 TUpnpErrorCode CUpnpEventController::CheckHeaders( CUpnpGenaMessage* aMessage )
       
   272     {
       
   273     TUpnpErrorCode subscriptionState( EUndefined );
       
   274 
       
   275     // Errors: Incompatible headers
       
   276     if ( aMessage->Sid().Compare( KNoHeader) != 0 )
       
   277         {
       
   278         if ( aMessage->Nt().Compare( KNoHeader) != 0 )
       
   279             {
       
   280             subscriptionState = EBadRequest;
       
   281             }
       
   282         else if (aMessage->Callback().Compare( KNoHeader ) != 0 )
       
   283             {
       
   284             subscriptionState = EBadRequest;
       
   285             }
       
   286         else
       
   287             {
       
   288             }
       
   289         }
       
   290     return subscriptionState;
       
   291     }
       
   292 
       
   293 // -----------------------------------------------------------------------------
       
   294 // CUpnpEventController::GenerateSidL
       
   295 // Genereated uuid for a subscriber 
       
   296 // -----------------------------------------------------------------------------
       
   297 // 
       
   298 void CUpnpEventController::GenerateSidL( CUpnpSubscriberLibraryElement* aSubscriber )
       
   299     {
       
   300     HBufC8* uuid = iServiceImpl.DeviceImpl().GenerateUuidL();
       
   301     CleanupStack::PushL( uuid );
       
   302     aSubscriber->SetSidL( *uuid );
       
   303     CleanupStack::PopAndDestroy( uuid );
       
   304     LOGS("  new subscriber:");
       
   305     }
       
   306 // -----------------------------------------------------------------------------
       
   307 // CUpnpEventController::SubscribersAmount
       
   308 // Returns evented state variables
       
   309 // -----------------------------------------------------------------------------
       
   310 //    
       
   311 RPointerArray<CUpnpStateVariable>& CUpnpEventController::EventedStateVariables()
       
   312     {
       
   313     return iServiceImpl.EventedStateVariables();
       
   314     }
       
   315    
       
   316 // -----------------------------------------------------------------------------
       
   317 // CUpnpEventController::SubscriberRemoved
       
   318 // Resumes processing when subscriber  has been removed
       
   319 // -----------------------------------------------------------------------------
       
   320 //    
       
   321 void CUpnpEventController::SubscriberRemoved( CUpnpSubscriberLibraryElement* aSubscriber , TInt aPos )
       
   322     {
       
   323     CancelInvalidSessions( aSubscriber->Sid() );
       
   324     
       
   325     iNonModQueue->UpdateQueue( aPos );
       
   326     iModQueue->UpdateQueue( aPos );
       
   327     }
       
   328 
       
   329 // -----------------------------------------------------------------------------
       
   330 // CUpnpEventController::TimerHeaderLC
       
   331 // Creates timeout header
       
   332 // -----------------------------------------------------------------------------
       
   333 //    
       
   334 HBufC8* CUpnpEventController::TimerHeaderLC( const TDesC8& aTimeout )
       
   335     {
       
   336     HBufC8* timeout= HBufC8::NewLC( aTimeout.Length() + KTimeoutPrefix().Length() );
       
   337     timeout->Des().Zero();
       
   338     timeout->Des().Append( KTimeoutPrefix );
       
   339     timeout->Des().Append( aTimeout );
       
   340     return timeout;
       
   341     }
       
   342 // -----------------------------------------------------------------------------
       
   343 // CUpnpEventController::ClientResponseRecivedLD
       
   344 // Handles event response
       
   345 // -----------------------------------------------------------------------------
       
   346 //          
       
   347 void CUpnpEventController::ClientResponseRecivedLD( CUpnpHttpTransaction& aEndedTransaction )
       
   348 {
       
   349     TInt idx( iPendingTransactions.Find( &aEndedTransaction ) );
       
   350     ASSERT( idx >= 0 ); //it should never happen that we received transaction that wasn't sent by us
       
   351     iPendingTransactions.Remove( idx );
       
   352     aEndedTransaction.ProcessResponseL();
       
   353     delete ( &aEndedTransaction );
       
   354 }
       
   355 //end of file