upnp/upnpstack/messagehandler/src/upnpssdphandlerup.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2005-2006 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 // INCLUDE FILES
       
    20 #include "upnpssdphandlerup.h"
       
    21 #include "upnpssdpserver.h"
       
    22 #include "upnpmsearchtimer.h"
       
    23 
       
    24 #define KLogFile _L("UPnPStack.txt")
       
    25 #include "upnpcustomlog.h"
       
    26 
       
    27 #include "upnpmessagehandler.pan"
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CUpnpSsdpHandlerUp::CUpnpSsdpHandlerUp
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 CUpnpSsdpHandlerUp::CUpnpSsdpHandlerUp(
       
    34                             MUpnpSsdpServerObserver& aSsdpServerObserver )
       
    35                             : CUpnpSsdpHandlerBase( aSsdpServerObserver )
       
    36     {
       
    37     }
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CUpnpSsdpHandlerUp::~CUpnpSsdpHandlerUp
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CUpnpSsdpHandlerUp::~CUpnpSsdpHandlerUp()
       
    44    {
       
    45    ClearTimers();
       
    46    delete iSsdpServer;
       
    47    delete iMulticastSsdpServer;
       
    48    }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CUpnpSsdpHandlerUp::CUpnpSsdpHandlerUp
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CUpnpSsdpHandlerUp* CUpnpSsdpHandlerUp::NewL(
       
    55                               MUpnpSsdpServerObserver& aSsdpServerObserver )
       
    56     {
       
    57     CUpnpSsdpHandlerUp* self = new ( ELeave )
       
    58                                CUpnpSsdpHandlerUp( aSsdpServerObserver );
       
    59     CleanupStack::PushL( self );
       
    60     self->ConstructL();
       
    61     CleanupStack::Pop( self );
       
    62     return self;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CUpnpSsdpHandlerUp::ConstructL
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 void CUpnpSsdpHandlerUp::ConstructL()
       
    70     {
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CUpnpSsdpHandlerUp::StartL
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 void CUpnpSsdpHandlerUp::StartL( RSocketServ& aSocketServ )
       
    78     {
       
    79     if ( !iSsdpServer || !iMulticastSsdpServer )
       
    80         {
       
    81         delete iSsdpServer;
       
    82         iSsdpServer = NULL;
       
    83         iSsdpServer = CUpnpSsdpServer::NewL( &iSsdpServerObserver,
       
    84                                              &aSocketServ,
       
    85                                              KDefaultStartPort );
       
    86 
       
    87         delete iMulticastSsdpServer;
       
    88         iMulticastSsdpServer = NULL;
       
    89         iMulticastSsdpServer = CUpnpSsdpServer::NewL( &iSsdpServerObserver,
       
    90                                                       &aSocketServ,
       
    91                                                       KDefaultMulticastPort,
       
    92                                                       iSsdpServer );
       
    93         }
       
    94     iSsdpServer->StartL();
       
    95     iMulticastSsdpServer->StartL();
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CUpnpSsdpHandlerUp::Stop
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 void CUpnpSsdpHandlerUp::Stop()
       
   103     {
       
   104     __ASSERT_DEBUG( iSsdpServer && iMulticastSsdpServer,
       
   105         User::Panic( KMessageHandler, EMessageHandlerBadState ) );
       
   106     ClearTimers();
       
   107     iSsdpServer->Stop();
       
   108     iMulticastSsdpServer->Stop();
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CUpnpSsdpHandlerUp::AddressChangeL
       
   113 // -----------------------------------------------------------------------------
       
   114 //     
       
   115 void CUpnpSsdpHandlerUp::AddressChangeL( TInetAddr& aAddress )
       
   116     {
       
   117     iSsdpServer->AddressChangedL( aAddress );
       
   118     iMulticastSsdpServer->AddressChangedL( aAddress );  
       
   119     }
       
   120     
       
   121 // -----------------------------------------------------------------------------
       
   122 // CUpnpSsdpHandlerUp::SsdpSearchL
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void CUpnpSsdpHandlerUp::SsdpSearchL( const TDesC8& aSearchString, TDesC8& aMX )
       
   126     {
       
   127     __ASSERT_DEBUG( iSsdpServer && iMulticastSsdpServer,
       
   128         User::Panic( KMessageHandler, EMessageHandlerBadState ) );
       
   129 
       
   130     if ( iCPCounter == 0 )
       
   131         {
       
   132         for (TInt i(0); i < iTimers.Count(); i++ )
       
   133             {
       
   134             CUpnpMSearchTimer* timer = iTimers[i];
       
   135             iTimers.Remove( i );
       
   136             delete timer;
       
   137             }
       
   138         return;
       
   139         }
       
   140 
       
   141     TBuf8<KMXTimeBuffer> time;
       
   142 
       
   143     CUpnpMSearchTimer* timer = CUpnpMSearchTimer::NewLC( this, aSearchString, aMX );
       
   144     iTimers.AppendL( timer );
       
   145     CleanupStack::Pop(timer);
       
   146 
       
   147     TInt mx = timer->GetNextValue();
       
   148     TInt t = timer->GetNextValue();
       
   149     if ( mx > 0 )
       
   150         {
       
   151         time.Num(mx);
       
   152         iSsdpServer->SearchL( const_cast<TDesC8&>( aSearchString ), time );
       
   153         }
       
   154     if ( t > 0 )
       
   155         {
       
   156         t = t * KSecond;
       
   157         ( iTimers[( iTimers.Count() - 1 )] )->Start( t );
       
   158         }
       
   159     else
       
   160         {
       
   161         iTimers.Remove( iTimers.Count() - 1 );
       
   162         delete timer;
       
   163         }
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CUpnpSsdpHandlerUp::AddLocalControlPoint
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 TInt CUpnpSsdpHandlerUp::AddLocalControlPoint( )
       
   171     {
       
   172     return iCPCounter++;
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CUpnpSsdpHandlerUp::RemoveLocalControlPoint
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 TInt CUpnpSsdpHandlerUp::RemoveLocalControlPoint( )
       
   180     {
       
   181     iCPCounter--;
       
   182     __ASSERT_ALWAYS( iCPCounter >= 0,
       
   183         User::Panic( KMessageHandler, EMessageHandlerRemoveNonExistingCp ) );
       
   184     return iCPCounter;
       
   185     }
       
   186 
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // CUpnpSsdpHandlerUp::AdvertiseDeviceL
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 void CUpnpSsdpHandlerUp::AdvertiseDeviceL( TInt aLive,  CUpnpDeviceLibraryElement& aElement )
       
   193     {
       
   194     LOGSH( 0, "CUpnpMessageHandlerEngine::AdvertiseDeviceL");
       
   195 
       
   196     __ASSERT_DEBUG( iSsdpServer && iMulticastSsdpServer,
       
   197         User::Panic( KMessageHandler, EMessageHandlerBadState ) );
       
   198     // forward advertisement requests to ssdp server
       
   199     // depending on the element status, advertise ssdp:alive or ssdp:byebye
       
   200 
       
   201     LOGSH( 0, "MESSAGE HANDLER ENGINE ADVERTISING");
       
   202 
       
   203     if ( aLive )
       
   204         {
       
   205         iSsdpServer->AdvertiseAliveL( aElement );
       
   206         }
       
   207     else
       
   208         {
       
   209         iSsdpServer->AdvertiseByebyeL( aElement );
       
   210         }
       
   211     }
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CUpnpSsdpHandlerUp::TimerEventL
       
   215 // Callback function
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void CUpnpSsdpHandlerUp::TimerEventL( CUpnpNotifyTimer* aTimer )
       
   219     {
       
   220     if ( !(aTimer->iStatus == KErrNone) )
       
   221         {
       
   222         return;
       
   223         }
       
   224 
       
   225     TInt i(0);
       
   226     if ( !iCPCounter )
       
   227         {
       
   228         for (; i < iTimers.Count(); i++ )
       
   229             {
       
   230             CUpnpMSearchTimer* timer = iTimers[i];
       
   231             iTimers.Remove( i );
       
   232             delete timer;
       
   233             }
       
   234         return;
       
   235         }
       
   236 
       
   237     for (; i < iTimers.Count(); i++ )
       
   238         {
       
   239         if ( iTimers[i] == aTimer )
       
   240             {
       
   241             break;
       
   242             }
       
   243         }
       
   244     if (i < iTimers.Count() )
       
   245         {
       
   246         TBuf8<KMXTimeBuffer> time;
       
   247 
       
   248         TInt mx = iTimers[i]->GetNextValue();
       
   249         TInt t = iTimers[i]->GetNextValue();
       
   250         if ( mx > 0 )
       
   251             {
       
   252             time.Num( mx );
       
   253             TPtrC8 ptr = iTimers[i]->GetTarget();
       
   254             iSsdpServer->SearchL(ptr, time);
       
   255             }
       
   256         if ( t > 0 )
       
   257             {
       
   258             t = t * KSecond;
       
   259             ( iTimers[( iTimers.Count() - 1 )])->Start( t );
       
   260             }
       
   261         else
       
   262             {
       
   263             CUpnpMSearchTimer* timer = iTimers[i];
       
   264             iTimers.Remove(i);
       
   265             delete timer;
       
   266             }
       
   267         }
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // CUpnpSsdpHandlerUp::ClearTimers
       
   272 // -----------------------------------------------------------------------------
       
   273 //
       
   274 void CUpnpSsdpHandlerUp::ClearTimers()
       
   275     {
       
   276     for ( TInt i(0); i < iTimers.Count(); i++ )
       
   277         {
       
   278         iTimers[i]->Cancel();
       
   279         }
       
   280     iTimers.ResetAndDestroy();
       
   281     }
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // CUpnpSsdpHandlerUp::IsStarted
       
   285 // -----------------------------------------------------------------------------
       
   286 //
       
   287 TBool CUpnpSsdpHandlerUp::IsStarted()
       
   288     {
       
   289     return ( iSsdpServer && iMulticastSsdpServer );
       
   290     }
       
   291 
       
   292 // End of File