upnp/upnpstack/serviceframework/src/upnpeventqueuemanagerbase.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:  Base class for event queue manager
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "upnpeventqueuemanagerbase.h"
       
    20 #include "upnpgenamessagefactory.h"
       
    21 #include "upnpsubscriberlibraryelement.h"
       
    22 #include "upnpcommonupnplits.h"
       
    23 #include "upnpcommonstructs.h"
       
    24 
       
    25 
       
    26 #define KLogFile _L("UPnPStack.txt")
       
    27 #include "upnpcustomlog.h"
       
    28 
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CUpnpEventQueueManagerBase::CUpnpEventQueueManagerBase
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 CUpnpEventQueueManagerBase::CUpnpEventQueueManagerBase( 
       
    35                                 CUpnpServiceImplementation& aServiceImpl,
       
    36                                 CUpnpEventController& aEventController,
       
    37                                 CUpnpSubscriberLibrary* aSubscriberLibrary )
       
    38     :iServiceImpl( aServiceImpl ),
       
    39      iEventController( aEventController ),
       
    40      iSubscriberLibrary( aSubscriberLibrary )
       
    41     {
       
    42     // No implementation required
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CUpnpEventQueueManagerBase::~CUpnpEventQueueManagerBase
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CUpnpEventQueueManagerBase::~CUpnpEventQueueManagerBase ()
       
    50     {
       
    51     delete iBody;
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CUpnpEventQueueManagerBase::BaseConstructL
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 void CUpnpEventQueueManagerBase::BaseConstructL()
       
    59     {
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CUpnpEventQueueManagerBase::SendingCompleted
       
    64 // -----------------------------------------------------------------------------
       
    65 //    
       
    66 void CUpnpEventQueueManagerBase::SendingCompleted()
       
    67     {
       
    68     iCurrentSubscriberIndex = 0;
       
    69     delete iBody;
       
    70     iBody = NULL;
       
    71     iIsSending = EFalse;
       
    72     if ( iNewValuesWaitingToBeSend )
       
    73         {
       
    74         iNewValuesWaitingToBeSend = EFalse;
       
    75         SendEvent();
       
    76         }
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CUpnpEventQueueManagerBase::SendToNextL
       
    81 // -----------------------------------------------------------------------------
       
    82 //    
       
    83 void CUpnpEventQueueManagerBase::SendToNextL()
       
    84     {    
       
    85     PrepareTransactionAndStartSendingL( iCurrentSubscriberIndex );
       
    86     }
       
    87  
       
    88 // -----------------------------------------------------------------------------
       
    89 // CUpnpEventQueueManagerBase::PrepareTransactionAndStartSendingL
       
    90 // -----------------------------------------------------------------------------
       
    91 // 
       
    92 void CUpnpEventQueueManagerBase::PrepareTransactionAndStartSendingL( TInt aSubscriberNo )
       
    93     {
       
    94     if (  aSubscriberNo >= iSubscriberLibrary->SubscriberLibrary().Count()
       
    95        || aSubscriberNo < 0 )
       
    96         {
       
    97         User::Leave( KErrNotFound );
       
    98         }
       
    99         
       
   100     CUpnpSubscriberLibraryElement* subscriber( NULL );
       
   101     subscriber = iSubscriberLibrary->SubscriberLibrary()[aSubscriberNo];
       
   102     
       
   103     CUpnpHttpMessage* message = reinterpret_cast<CUpnpHttpMessage*>(
       
   104                         RUpnpGenaMessageFactory::EventingLC(
       
   105                           subscriber->CallbackPath(), 
       
   106                           subscriber->CallbackAddress(), 
       
   107                           subscriber->Sid(), 
       
   108                           subscriber->Seq() ) );
       
   109 
       
   110     message->SetTcpTimeout( KMessageTimeout );
       
   111     message->SetBodyL( *iBody );        
       
   112 
       
   113     CleanupStack::Pop( message ); //ownership passed to "transcation"
       
   114     CUpnpHttpTransaction* transaction = CreateTransactionL( message );
       
   115     
       
   116     iEventController.SendTransactionL( transaction );
       
   117        
       
   118     subscriber->IncreaseSeq();  
       
   119     }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // CUpnpEventQueueManagerBase::TransactionCompletedL
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void CUpnpEventQueueManagerBase::TransactionCompletedL( 
       
   126                                   CUpnpHttpTransaction* aCompletedTransaction  
       
   127                                                       )
       
   128     {
       
   129     ASSERT( iIsSending );
       
   130     if ( aCompletedTransaction->Response() && 
       
   131         !aCompletedTransaction->Response()->Is2xx() )
       
   132         {
       
   133         if ( aCompletedTransaction->Response()->Is3xx() )
       
   134             {
       
   135             //300 Responses are redirected by Symbian HTTP Client
       
   136             //This can be changed by removing HTTPFilter from HTTPTransaction
       
   137             }
       
   138         else
       
   139             {
       
   140             RemoveInvalidSubscriber( aCompletedTransaction );
       
   141             return;
       
   142             }
       
   143         }
       
   144 
       
   145     iCurrentSubscriberIndex++;
       
   146     if ( iCurrentSubscriberIndex >= iSubscriberLibrary->SubscriberLibrary().Count() )
       
   147         {        
       
   148         SendingCompleted();
       
   149         }
       
   150     else 
       
   151         {           
       
   152         SendToNextL();   
       
   153         }
       
   154     } 
       
   155         
       
   156 // -----------------------------------------------------------------------------
       
   157 // CUpnpEventQueueManagerBase::RemoveInvalidSubscriber
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 void CUpnpEventQueueManagerBase::RemoveInvalidSubscriber( 
       
   161                                     CUpnpHttpTransaction* aCompletedTransaction )
       
   162     {
       
   163     iSubscriberLibrary->Remove( static_cast<CUpnpGenaMessage*>( 
       
   164                                      aCompletedTransaction->Request() )->Sid() );
       
   165     }
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CUpnpEventQueueManagerBase::UpdateQueue
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 void CUpnpEventQueueManagerBase::UpdateQueue( TInt aPos )
       
   172     {
       
   173     if( iIsSending )
       
   174         {
       
   175         if ( aPos == iCurrentSubscriberIndex  )
       
   176             {
       
   177             TRAPD( err, SendToNextL() );
       
   178             if ( err )
       
   179                 {
       
   180                 SendingCompleted();
       
   181                 }
       
   182             return;
       
   183             }   
       
   184         
       
   185         if ( aPos < iCurrentSubscriberIndex )
       
   186             {
       
   187             iCurrentSubscriberIndex--;
       
   188             }           
       
   189         }
       
   190     }
       
   191     
       
   192 // -----------------------------------------------------------------------------
       
   193 // CUpnpEventQueueManagerBase::CreateEventL
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 HBufC8* CUpnpEventQueueManagerBase::CreateEventL
       
   197                         ( const RPointerArray<CUpnpStateVariable>& aVariableList )
       
   198     {      
       
   199     HBufC8* body = HBufC8::NewLC( KMaxGenaMessageLength );        
       
   200     TPtr8 bodyPtr = body->Des(); 
       
   201     bodyPtr.Zero();
       
   202     bodyPtr.Append( KGenaXml );
       
   203     bodyPtr.Append( KGenaPropertysetStart );
       
   204     
       
   205     TInt eventableVariableCount = FillEventBodyL( bodyPtr, aVariableList ); 
       
   206     if ( eventableVariableCount > 0 )
       
   207         {        
       
   208         bodyPtr.Append( KGenaPropertysetEnd );
       
   209         CleanupStack::Pop( body );
       
   210         return body;
       
   211         }
       
   212     else
       
   213         {
       
   214         CleanupStack::PopAndDestroy( body );
       
   215         return KNullDesC8().AllocL();             
       
   216         }
       
   217     }
       
   218    
       
   219 // -----------------------------------------------------------------------------
       
   220 // CUpnpEventQueueManagerBase::ClearVariables
       
   221 // -----------------------------------------------------------------------------
       
   222 //
       
   223 void CUpnpEventQueueManagerBase::ClearVariables()
       
   224     {   
       
   225     for ( TInt i = 0; i < iServiceImpl.EventedStateVariables().Count(); i++ )
       
   226         {
       
   227         if ( iServiceImpl.EventedStateVariables()[i]->MaxEventRate() > 0 && 
       
   228             !iServiceImpl.EventedStateVariables()[i]->ReadyForEventing() )
       
   229             {
       
   230             }
       
   231         else
       
   232             {
       
   233             iServiceImpl.EventedStateVariables().Remove( i );
       
   234             i--;
       
   235             }
       
   236         }
       
   237     iServiceImpl.EventedStateVariables().Compress();   
       
   238     }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // CUpnpEventQueueManagerBase::ClearVariables
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 void CUpnpEventQueueManagerBase::AddVariableToEventBody( 
       
   245                                             TDes8& aBuffer , 
       
   246                                             CUpnpStateVariable * aVariable )
       
   247     {
       
   248     aBuffer.Append( KGenaPropertyStart );
       
   249     aBuffer.Append( aVariable->Name() );
       
   250     aBuffer.Append( KCloseBracket );
       
   251     aBuffer.Append( aVariable->Value() );
       
   252     aBuffer.Append( KOpenBracket );
       
   253     aBuffer.Append( UpnpString::KSlash() );
       
   254     aBuffer.Append( aVariable->Name() );
       
   255     aBuffer.Append( KGenaPropertyEnd );
       
   256     }
       
   257 //end of file