upnp/upnpstack/serviceframework/src/upnpsubscriberlibraryelement.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:  CUpnpSubscriberLibraryElement
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32base.h>
       
    22 #include <uri8.h> 
       
    23 #include "upnpsubscriberlibraryelement.h"
       
    24 #include "upnpgenamessage.h"
       
    25 #include "upnpstring.h"
       
    26 #include "upnpcommonupnplits.h"
       
    27 #include "upnpcommonstructs.h"
       
    28 
       
    29 // CONSTANTS
       
    30 _LIT8(KCallbackPrefix, "<http://");
       
    31 
       
    32 
       
    33 // ============================ MEMBER FUNCTIONS ===============================
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CUpnpSubscriberLibraryElement::CUpnpSubscriberLibraryElement
       
    37 // C++ default constructor can NOT contain any code, that
       
    38 // might leave.
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CUpnpSubscriberLibraryElement::CUpnpSubscriberLibraryElement( 
       
    42                                     MUpnpTimeoutElementParent& aParent)
       
    43                 : CUpnpTimeoutElement(aParent)
       
    44 {
       
    45 
       
    46 }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CUpnpSubscriberLibraryElement::ConstructL
       
    50 // Symbian 2nd phase constructor can leave.
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 void CUpnpSubscriberLibraryElement::ConstructL()
       
    54 {
       
    55     BaseConstructL();
       
    56     iSid = HBufC8::NewL(0);
       
    57     iCallbackPath = HBufC8::NewL(0);
       
    58     iTimeout = HBufC8::NewL(0);
       
    59 }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CUpnpSubscriberLibraryElement::NewL
       
    63 // Two-phased constructor.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CUpnpSubscriberLibraryElement* CUpnpSubscriberLibraryElement::NewL(
       
    67                                     MUpnpTimeoutElementParent& aParent )
       
    68 {
       
    69     CUpnpSubscriberLibraryElement* self = new (ELeave) 
       
    70                                         CUpnpSubscriberLibraryElement(aParent);
       
    71     
       
    72     CleanupStack::PushL( self );
       
    73     self->ConstructL();
       
    74     CleanupStack::Pop( self );
       
    75     
       
    76     return self;
       
    77 }
       
    78 
       
    79 // Destructor
       
    80 CUpnpSubscriberLibraryElement::~CUpnpSubscriberLibraryElement()
       
    81 {
       
    82     delete iSid;
       
    83     delete iCallbackPath;
       
    84     delete iTimeout;
       
    85     
       
    86     Cancel();
       
    87 }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CUpnpSubscriberLibraryElement::Sid
       
    91 //
       
    92 // (other items were commented in a header).
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 TDesC8& CUpnpSubscriberLibraryElement::Sid()
       
    96 {
       
    97     return *iSid;
       
    98 }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CUpnpSubscriberLibraryElement::CallbackAddress
       
   102 //
       
   103 // (other items were commented in a header).
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 TInetAddr& CUpnpSubscriberLibraryElement::CallbackAddress()
       
   107 {
       
   108     return iCallbackAddress;
       
   109 }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CUpnpSubscriberLibraryElement::CallbackPath
       
   113 //
       
   114 // (other items were commented in a header).
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 TDesC8& CUpnpSubscriberLibraryElement::CallbackPath()
       
   118 {
       
   119     return *iCallbackPath;
       
   120 }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // CUpnpSubscriberLibraryElement::Seq
       
   124 //
       
   125 // (other items were commented in a header).
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 TUint32 CUpnpSubscriberLibraryElement::Seq() const
       
   129 {
       
   130     return iSeq;
       
   131 }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CUpnpSubscriberLibraryElement::Timeout
       
   135 //
       
   136 // (other items were commented in a header).
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 TDesC8& CUpnpSubscriberLibraryElement::Timeout()
       
   140 {
       
   141     return *iTimeout;
       
   142 }
       
   143 
       
   144 // -----------------------------------------------------------------------------
       
   145 // CUpnpSubscriberLibraryElement::AddSubscriberL
       
   146 //
       
   147 // (other items were commented in a header).
       
   148 // -----------------------------------------------------------------------------
       
   149 //
       
   150 TUpnpErrorCode CUpnpSubscriberLibraryElement::AddSubscriberL( 
       
   151                                 CUpnpGenaMessage* aMessage)
       
   152     {
       
   153     if (aMessage == NULL)
       
   154         {
       
   155         return EUndefined;
       
   156         }
       
   157     
       
   158     // Errors: Missing or invalid CALLBACK
       
   159     if (aMessage->Callback().Compare(KNoHeader) == 0)
       
   160         {   
       
   161         return EPreconditionFailed;
       
   162         }
       
   163     
       
   164     // Errors: Invalid NT
       
   165     if (aMessage->Nt().Compare(UpnpGENA::KDefaultNt) != 0)
       
   166         {
       
   167         return EPreconditionFailed;
       
   168         }
       
   169     
       
   170     TPtrC8 temp;
       
   171     TPtrC8 sid(KNullDesC8);
       
   172     TPtrC8 timeout;
       
   173     
       
   174     // only 1 address allowed with this method
       
   175     temp.Set( aMessage->Callback() );
       
   176     if ( temp.Find( KCallbackPrefix ) != KErrNotFound 
       
   177          && temp.Length() < KUrlMaxLength )
       
   178         {
       
   179         temp.Set( temp.Mid( temp.Find( KLessThan8 ) + 1 ) );
       
   180         if ( temp.Find( KGreaterThan8 )  != KErrNotFound )
       
   181             {
       
   182             temp.Set( temp.Left( temp.Find( KGreaterThan8 ) ) );
       
   183             }
       
   184         else
       
   185             {
       
   186             return EPreconditionFailed;
       
   187             }
       
   188         }
       
   189     else
       
   190         {
       
   191         return EPreconditionFailed;
       
   192         }
       
   193 
       
   194 
       
   195     TPtrC8 callbackIp;
       
   196     TPtrC8 callbackPort;
       
   197     TPtrC8 callbackPath;
       
   198     
       
   199     TUriParser8 uriParser;
       
   200     if ( !uriParser.Parse(temp) )
       
   201         {
       
   202         callbackIp.Set( uriParser.Extract( EUriHost ) );
       
   203         if ( callbackIp == KNullDesC8() )
       
   204 		    	{
       
   205 		    	return EPreconditionFailed;	
       
   206 		    	}	
       
   207         callbackPort.Set( uriParser.Extract( EUriPort ) );
       
   208         if ( callbackPort == KNullDesC8() )
       
   209             {
       
   210             callbackPort.Set(KDefaultHostPort);            
       
   211             }        
       
   212         callbackPath.Set( uriParser.Extract( EUriPath ) );       
       
   213         }
       
   214     else
       
   215         {
       
   216         // invalid callback
       
   217         return EPreconditionFailed;
       
   218         }
       
   219     
       
   220     temp.Set(aMessage->Timeout());
       
   221     
       
   222     if (temp.Find(KTimeoutPrefix) != KErrNotFound)
       
   223         {
       
   224         timeout.Set( temp.Mid( temp.Find( KTimeoutPrefix) + 
       
   225                         KTimeoutPrefix().Length() ) );
       
   226         }
       
   227     else
       
   228         {
       
   229         // timeout according to DLNA recommendation: 300 seconds
       
   230         timeout.Set(KDefaultTimeout);
       
   231         }
       
   232     
       
   233     AddInfoL(sid,
       
   234              callbackIp,
       
   235              callbackPort,
       
   236              callbackPath,
       
   237              timeout);
       
   238              
       
   239     return EHttpOk;
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CUpnpSubscriberLibraryElement::RenewSubscriberL
       
   244 //
       
   245 // (other items were commented in a header).
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 TUpnpErrorCode CUpnpSubscriberLibraryElement::RenewSubscriberL(
       
   249                                 CUpnpGenaMessage* aMessage )
       
   250 {
       
   251     TPtrC8 temp;
       
   252     // Errors: Incompatible headers (SID already found)
       
   253     
       
   254     if (aMessage == NULL)
       
   255     {
       
   256         return EUndefined;
       
   257     }
       
   258     
       
   259     if (aMessage->Nt().Compare(KNoHeader) != 0)
       
   260     {
       
   261         return EBadRequest;
       
   262     }
       
   263     else if (aMessage->Callback().Compare(KNoHeader) != 0)
       
   264     {
       
   265         return EBadRequest;
       
   266     }
       
   267     else
       
   268     {
       
   269     }
       
   270     
       
   271     TPtrC8 timeout;
       
   272     temp.Set(aMessage->Timeout());
       
   273     
       
   274     if (temp.Find(KTimeoutPrefix) != KErrNotFound)
       
   275     {
       
   276         timeout.Set( temp.Mid( temp.Find(KTimeoutPrefix) + 
       
   277                         KTimeoutPrefix().Length() ) );
       
   278     }
       
   279     else
       
   280     {
       
   281         // timeout according to DLNA recommendation: 300 seconds
       
   282         timeout.Set(KDefaultTimeout);
       
   283     }
       
   284     
       
   285     SetTimeoutL(timeout);
       
   286     
       
   287     return EHttpOk;
       
   288 }
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // CUpnpSubscriberLibraryElement::AddInfoL
       
   292 //
       
   293 // (other items were commented in a header).
       
   294 // -----------------------------------------------------------------------------
       
   295 //
       
   296 void CUpnpSubscriberLibraryElement::AddInfoL( const TDesC8& aSid,  
       
   297                                                        const TDesC8& aCallbackIp, 
       
   298                                                        const TDesC8& aCallbackPort, 
       
   299                                                        const TDesC8& aCallbackPath, 
       
   300                                                        const TDesC8& aTimeout)
       
   301 {
       
   302     // Adding subscriber information
       
   303 
       
   304     SetSidL(aSid);
       
   305     SetCallbackAddress(aCallbackIp, aCallbackPort);
       
   306     SetCallbackPathL(aCallbackPath);
       
   307     SetTimeoutL(aTimeout);
       
   308     SetSeq(0);
       
   309 }
       
   310 
       
   311 // -----------------------------------------------------------------------------
       
   312 // CUpnpSubscriberLibraryElement::SetSidL
       
   313 //
       
   314 // (other items were commented in a header).
       
   315 // -----------------------------------------------------------------------------
       
   316 //
       
   317 void CUpnpSubscriberLibraryElement::SetSidL(const TDesC8& aSid)
       
   318 {
       
   319     delete iSid;
       
   320     iSid = NULL;
       
   321     iSid = aSid.AllocL();
       
   322 }
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // CUpnpSubscriberLibraryElement::SetCallbackAddress
       
   326 //
       
   327 // (other items were commented in a header).
       
   328 // -----------------------------------------------------------------------------
       
   329 //
       
   330 void CUpnpSubscriberLibraryElement::SetCallbackAddress(const TDesC8& aIp, 
       
   331                                                             const TDesC8& aPort)
       
   332 {
       
   333     CUpnpHttpMessage::AddrInput(iCallbackAddress, aIp);
       
   334     
       
   335     TLex8 lex(aPort);
       
   336     TInt port;
       
   337     lex.Val(port);
       
   338     
       
   339     iCallbackAddress.SetPort(port);
       
   340     iCallbackAddress.SetFamily(KAfInet);
       
   341 }
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // CUpnpSubscriberLibraryElement::SetCallbackPathL
       
   345 //
       
   346 // (other items were commented in a header).
       
   347 // -----------------------------------------------------------------------------
       
   348 //
       
   349 void CUpnpSubscriberLibraryElement::SetCallbackPathL( const TDesC8& aCallbackPath)
       
   350 {
       
   351     delete iCallbackPath;
       
   352     iCallbackPath = NULL;
       
   353     iCallbackPath = aCallbackPath.AllocL();
       
   354 }
       
   355 
       
   356 // -----------------------------------------------------------------------------
       
   357 // CUpnpSubscriberLibraryElement::SetSeq
       
   358 //
       
   359 // (other items were commented in a header).
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 void CUpnpSubscriberLibraryElement::SetSeq(TUint32 aSeq)
       
   363 {
       
   364     iSeq = aSeq;
       
   365 }
       
   366 
       
   367 // -----------------------------------------------------------------------------
       
   368 // CUpnpSubscriberLibraryElement::IncreaseSeq
       
   369 //
       
   370 // (other items were commented in a header).
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 void CUpnpSubscriberLibraryElement::IncreaseSeq()
       
   374 {
       
   375     if (iSeq < KMaxSeq)
       
   376     {
       
   377         iSeq++;
       
   378     }
       
   379     else
       
   380     {
       
   381         iSeq = KMinSeq;
       
   382     }
       
   383 }
       
   384 
       
   385 // -----------------------------------------------------------------------------
       
   386 // CUpnpSubscriberLibraryElement::SetSessId
       
   387 //
       
   388 // (other items were commented in a header).
       
   389 // -----------------------------------------------------------------------------
       
   390 //
       
   391 void CUpnpSubscriberLibraryElement::SetSessId(TInt aSessId)
       
   392 {
       
   393     iSessId = aSessId;
       
   394 }
       
   395 
       
   396 // -----------------------------------------------------------------------------
       
   397 // CUpnpSubscriberLibraryElement::SessId
       
   398 //
       
   399 // (other items were commented in a header).
       
   400 // -----------------------------------------------------------------------------
       
   401 //
       
   402 TInt CUpnpSubscriberLibraryElement::SessId()
       
   403 {
       
   404     return iSessId;
       
   405 }
       
   406 
       
   407 // -----------------------------------------------------------------------------
       
   408 // CUpnpSubscriberLibraryElement::SetTimeoutL
       
   409 //
       
   410 // (other items were commented in a header).
       
   411 // -----------------------------------------------------------------------------
       
   412 //
       
   413 void CUpnpSubscriberLibraryElement::SetTimeoutL(const TDesC8& /*aTimeout*/)
       
   414 {
       
   415     
       
   416 
       
   417 
       
   418         // 7.2.22.1 UPnP devices must send events to all properly subscribed 
       
   419         // UPnP control points.  
       
   420         // The device must enforce a subscription TIMEOUT value of 5 minutes.
       
   421     SetTimeout( KDefaultTimeoutValue );
       
   422     SetRenew(CUpnpTimeoutElement::EOnce);
       
   423     
       
   424     // set text-formed timeout
       
   425     delete iTimeout;
       
   426     iTimeout = NULL;
       
   427     iTimeout = KDefaultTimeout().AllocL();
       
   428 
       
   429 }
       
   430 
       
   431 //  End of File