simpleengine/engine/src/simplepublisher.cpp
changeset 0 c8caa15ef882
equal deleted inserted replaced
-1:000000000000 0:c8caa15ef882
       
     1 /*
       
     2 * Copyright (c) 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:    Simple Engine
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include <e32std.h>
       
    24 #include <s32mem.h>
       
    25 
       
    26 // own simple
       
    27 #include "msimpleconnection.h"
       
    28 #include "simpleconnection.h"
       
    29 #include "simplecommon.h"
       
    30 #include "simpleenginerequest.h"
       
    31 #include "msimpledocument.h"
       
    32 #include "msimplepublishobserver.h"
       
    33 #include "msimpleetagobserver.h"
       
    34 #include "simplepublisher.h"
       
    35 
       
    36 
       
    37 #include "simplesipconnection.h"
       
    38 
       
    39 #ifdef _DEBUG
       
    40 #include "simpledebugutils.h"
       
    41 #endif
       
    42 
       
    43 const TInt KSimpleExpandSize = 512;
       
    44 
       
    45 // ================= MEMBER FUNCTIONS =======================
       
    46 //
       
    47 
       
    48 // ----------------------------------------------------------
       
    49 // CSimplePublisher::CSimplePublisher
       
    50 // ----------------------------------------------------------
       
    51 //
       
    52 CSimplePublisher::CSimplePublisher(
       
    53     MSimpleConnection& aConn,
       
    54     MSimplePublishObserver& aObserver )
       
    55 : CSimpleClient( aConn ),
       
    56   iPublished( EFalse ),
       
    57   iObserver( aObserver ), iBuffer(NULL), iETagObserver( NULL )
       
    58     {
       
    59     }
       
    60 
       
    61 // ----------------------------------------------------------
       
    62 // CSimplePublisher::~CSimplePublisher
       
    63 // ----------------------------------------------------------
       
    64 //
       
    65 CSimplePublisher::~CSimplePublisher()
       
    66     {
       
    67 #ifdef _DEBUG
       
    68     TSimpleLogger::Log(_L("Publisher: Destructor this=%d" ), (TInt)this );
       
    69 #endif    
       
    70     delete iBuffer;
       
    71     delete iETag;
       
    72     }
       
    73 
       
    74 // ----------------------------------------------------------
       
    75 // CSimplePublisher::ConstructL
       
    76 // ----------------------------------------------------------
       
    77 //
       
    78 void CSimplePublisher::ConstructL()
       
    79     {
       
    80     BaseConstructL();
       
    81     iBuffer = CBufFlat::NewL(KSimpleExpandSize);
       
    82     }
       
    83 
       
    84 // ----------------------------------------------------------
       
    85 // CSimplePublisher::NewL
       
    86 // ----------------------------------------------------------
       
    87 //
       
    88 CSimplePublisher* CSimplePublisher::NewL(
       
    89     MSimpleConnection& aConn,
       
    90     MSimplePublishObserver& aObserver )
       
    91     {
       
    92     CSimplePublisher* self = new (ELeave) CSimplePublisher(
       
    93         aConn, aObserver );
       
    94     CleanupStack::PushL( self );
       
    95     self->ConstructL();
       
    96     CleanupStack::Pop( self );
       
    97 #ifdef _DEBUG
       
    98     TSimpleLogger::Log(_L("Publisher: NewL this=%d" ), (TInt)self );
       
    99 #endif    
       
   100     return self;
       
   101     }
       
   102 
       
   103 // ----------------------------------------------------------
       
   104 // CSimplePublisher::SIPStatus
       
   105 // ----------------------------------------------------------
       
   106 //
       
   107 TUint CSimplePublisher::SIPStatus()
       
   108     {
       
   109     return DoSIPStatus();
       
   110     }
       
   111     
       
   112 // ----------------------------------------------------------
       
   113 // CSimplePublisher::SIPRetryAfter
       
   114 // ----------------------------------------------------------
       
   115 //
       
   116 TUint CSimplePublisher::SIPRetryAfter()
       
   117     {
       
   118     return DoRetryAfter();
       
   119     }    
       
   120 
       
   121 // ----------------------------------------------------------
       
   122 // CSimplePublisher::Connection
       
   123 // ----------------------------------------------------------
       
   124 //
       
   125 const MSimpleConnection& CSimplePublisher::Connection()
       
   126     {
       
   127     return iConn;
       
   128     }
       
   129 
       
   130 // ----------------------------------------------------------
       
   131 // CSimplePublisher::StartPublishL
       
   132 // ----------------------------------------------------------
       
   133 //
       
   134 TInt CSimplePublisher::StartPublishL( MSimpleDocument& aDocument,
       
   135     TBool aRefresh )
       
   136     {
       
   137 
       
   138     IncreaseOpId();
       
   139 
       
   140 #ifdef _DEBUG
       
   141     TSimpleLogger::Log(_L("Publisher: StartPublishL opid=%d" ), iOpId );
       
   142 #endif
       
   143 
       
   144     if ( iRequest != MSimpleEngineRequest::ENone )
       
   145         {
       
   146         User::Leave( KErrInUse );
       
   147         }
       
   148         
       
   149     return DoStartPublishL( aDocument, aRefresh, KNullDesC8 );        
       
   150     }
       
   151     
       
   152 // ----------------------------------------------------------
       
   153 // CSimplePublisher::ContinuePublishL
       
   154 // ----------------------------------------------------------
       
   155 //
       
   156 TInt CSimplePublisher::ContinuePublishL( MSimpleDocument& aDocument,
       
   157     TBool aRefresh, const TDesC8& aETag )
       
   158     {
       
   159     
       
   160     IncreaseOpId();
       
   161 
       
   162 #ifdef _DEBUG
       
   163     TBuf<100> myETag;
       
   164     myETag.Copy( aETag );
       
   165     TSimpleLogger::Log(_L("Publisher: ContinuePublishL opid=%d ETag=%S" ), iOpId, &myETag );
       
   166 #endif 
       
   167 
       
   168     return DoStartPublishL( aDocument, aRefresh, aETag ); 
       
   169     
       
   170     }
       
   171 
       
   172 // ----------------------------------------------------------
       
   173 // CSimplePublisher::ModifyPublishL
       
   174 // ----------------------------------------------------------
       
   175 //
       
   176 TInt CSimplePublisher::ModifyPublishL( MSimpleDocument& aDocument )
       
   177     {
       
   178 
       
   179 #ifdef _DEBUG
       
   180     TSimpleLogger::Log(_L("Publisher: ModifyPublishL opid=%d" ), iOpId );
       
   181 #endif
       
   182 
       
   183     // use the old opid and request
       
   184     CSimpleEngineRequest* req = SearchRequests( iOpId );
       
   185     if ( !req )
       
   186         {
       
   187         User::Leave( KErrNotFound );
       
   188         }
       
   189     req->ModifyType( MSimpleEngineRequest::EPublishModify );
       
   190 
       
   191     // add request data
       
   192     StreamDocumentL( *req, aDocument );
       
   193     
       
   194     // Set MIME type of the request
       
   195     RPointerArray<MSimpleContent> contents;
       
   196     CleanupClosePushL( contents );     
       
   197     aDocument.GetDirectContentsL( contents );
       
   198     if ( contents.Count() )
       
   199         {                                            
       
   200         req->SetRequestContentTypeL( KSimpleMultipartType );
       
   201         }
       
   202     else
       
   203         {
       
   204         req->SetRequestContentTypeL( KSimpleDocumentType );
       
   205         }
       
   206     CleanupStack::PopAndDestroy( &contents );    
       
   207 
       
   208     // send a request to engine DLL
       
   209     SendReqL( *req );
       
   210 
       
   211     iRequest = MSimpleEngineRequest::EPublishModify;
       
   212     iSipStatus = 0;
       
   213     return iOpId;
       
   214     }
       
   215 
       
   216 // ----------------------------------------------------------
       
   217 // CSimplePublisher::StopPublishL
       
   218 // ----------------------------------------------------------
       
   219 //
       
   220 TInt CSimplePublisher::StopPublishL()
       
   221     {
       
   222 
       
   223 #ifdef _DEBUG
       
   224     TSimpleLogger::Log(_L("Publisher: StopPublishL opid=%d" ), iOpId );
       
   225 #endif
       
   226 
       
   227     // use the old opid and request
       
   228     CSimpleEngineRequest* req = SearchRequests( iOpId );
       
   229     if ( !req )
       
   230         {
       
   231         User::Leave( KErrNotFound );
       
   232         }
       
   233     req->ModifyType( MSimpleEngineRequest::EStopPublish );
       
   234 
       
   235     // send a request to engine DLL
       
   236     SendReqL( *req );
       
   237 
       
   238     iRequest = MSimpleEngineRequest::EStopPublish;
       
   239     iPublished = EFalse;
       
   240     iSipStatus = 0;
       
   241     return iOpId;
       
   242     }
       
   243     
       
   244 // ----------------------------------------------------------
       
   245 // CSimplePublisher::StopPublishL
       
   246 // ----------------------------------------------------------
       
   247 //
       
   248 TInt CSimplePublisher::StopPublishL( const TDesC8& aETag )
       
   249     {
       
   250 
       
   251 #ifdef _DEBUG
       
   252     TBuf<100> myETag;
       
   253     myETag.Copy( aETag );
       
   254     TSimpleLogger::Log(_L("Publisher: StopPublishL opid=%d ETag=%S" ), iOpId, &myETag );
       
   255 #endif
       
   256 
       
   257     // use the old opid and request
       
   258     CSimpleEngineRequest* req = SearchRequests( iOpId );
       
   259 
       
   260     if ( iRequest != MSimpleEngineRequest::ENone && !req )
       
   261         {
       
   262         User::Leave( KErrNotFound );
       
   263         }
       
   264     else if ( !req )
       
   265         {
       
   266         IncreaseOpId();
       
   267         // create a new request when starting from scratch
       
   268         req = CSimpleEngineRequest::NewL(
       
   269             *this, MSimpleEngineRequest::EStopPublish, iOpId );
       
   270         iRequestList.AddLast( *req );              
       
   271         }
       
   272         
       
   273     req->ModifyType( MSimpleEngineRequest::EStopPublish );
       
   274     req->SetETagL( aETag );
       
   275 
       
   276     // send a request to engine DLL
       
   277     SendReqL( *req );
       
   278 
       
   279     iRequest = MSimpleEngineRequest::EStopPublish;
       
   280     iPublished = EFalse;
       
   281     iSipStatus = 0;
       
   282     return iOpId;
       
   283     }    
       
   284     
       
   285 // ----------------------------------------------------------
       
   286 // CSimplePublisher::SIPETag
       
   287 // ----------------------------------------------------------
       
   288 TPtrC8 CSimplePublisher::SIPETag()
       
   289     {
       
   290     return iETag ? iETag->Des() : TPtrC8();
       
   291     }
       
   292     
       
   293 // ----------------------------------------------------------
       
   294 // CSimplePublisher::SetSIPETagObserver
       
   295 // ----------------------------------------------------------
       
   296 void CSimplePublisher::SetSIPETagObserver( MSimpleETagObserver* aObs )
       
   297     {
       
   298     iETagObserver = aObs;     
       
   299     }
       
   300 
       
   301 // ----------------------------------------------------------
       
   302 // CSimplePublisher::Close
       
   303 // ----------------------------------------------------------
       
   304 //
       
   305 void CSimplePublisher::Close( )
       
   306     {
       
   307     delete this;
       
   308     }
       
   309 
       
   310 // ----------------------------------------------------------
       
   311 // CSimplePublisher::Complete
       
   312 // ----------------------------------------------------------
       
   313 //
       
   314 void CSimplePublisher::Complete(
       
   315     TInt aOpId, TInt aStatus, MSimpleEngineRequest& aReq )
       
   316     {
       
   317 #ifdef _DEBUG
       
   318     TSimpleLogger::Log(_L("Publisher: Complete opid=%d, status=%d" ),
       
   319     aOpId, aStatus );
       
   320 #endif
       
   321 
       
   322     MSimpleEngineRequest::TSimpleRequest origRequest = iRequest;
       
   323     TBool myTerminated( EFalse );
       
   324 
       
   325     GetSIPStatus( aOpId );
       
   326     
       
   327     // Allocate ETag when needed and call callback
       
   328     if ( SIPETag().CompareF( aReq.ETag() ))
       
   329         {
       
   330         delete iETag;
       
   331         iETag = NULL;
       
   332         TRAPD( errMem, iETag = aReq.ETag().AllocL() );  
       
   333         if ( iETagObserver && !errMem )
       
   334             {
       
   335             TRAP_IGNORE( iETagObserver->NewETagL( iETag->Des() ));                                  
       
   336             }
       
   337         }
       
   338         
       
   339     if ( aReq.ResponseMethod() == MSimpleEngineRequest:: EStatusETag )
       
   340         {
       
   341         // If only ETag was updated then all is done this time.
       
   342         return;
       
   343         }
       
   344 
       
   345     // Reset data buffer
       
   346     iBuffer->Reset();
       
   347 
       
   348     // Set the member to point to stack variable
       
   349     TBool   destroyed( EFalse );
       
   350     iDestroyedPtr = &destroyed;
       
   351 
       
   352     if ( !aStatus && origRequest == MSimpleEngineRequest::EStartPublish )
       
   353         {
       
   354         iPublished = ETrue;
       
   355         }
       
   356 
       
   357     // Select the right callback method
       
   358     if ( iPublished &&
       
   359          origRequest == MSimpleEngineRequest::EStartPublish &&
       
   360          aStatus )
       
   361         {
       
   362 #ifdef _DEBUG
       
   363         TSimpleLogger::Log(_L("Publisher: call PublishTerminatedL opid=%d" ), aOpId );
       
   364 #endif
       
   365         myTerminated = ETrue;
       
   366         TRAP_IGNORE( iObserver.PublishTerminatedL( aOpId ));
       
   367         // Check whether an application has called destructor in callback method.
       
   368         // Destructor will handle deletion of all the open requests.
       
   369         if ( destroyed )
       
   370             {
       
   371             return;
       
   372             }
       
   373         }
       
   374     else
       
   375         {
       
   376         // If modify fails then it does not always mean the whole
       
   377         // publish is failed.
       
   378 #ifdef _DEBUG
       
   379         TSimpleLogger::Log(_L("Publisher: call PublishReqCompleteL opid=%d status=%d" ),
       
   380             aOpId, aStatus );
       
   381 #endif
       
   382         TRAP_IGNORE( iObserver.PublishReqCompleteL( aOpId, aStatus ) );
       
   383         // Check whether an application has called destructor in callback method.
       
   384         // Destructor will handle deletion of all the open requests.
       
   385         if ( destroyed )
       
   386             {
       
   387             return;
       
   388             }
       
   389         if ( aStatus == KErrCompletion || aStatus == KErrDisconnected )
       
   390             {
       
   391             myTerminated = ETrue;            
       
   392             // KErrCompletion error code is used when publication is terminated.
       
   393             TRAP_IGNORE( iObserver.PublishTerminatedL( aOpId ) );
       
   394             // Check whether an application has called destructor in callback method.
       
   395             // Destructor will handle deletion of all the open requests.
       
   396             if ( destroyed )
       
   397                 {
       
   398                 return;
       
   399                 }
       
   400             }
       
   401         }
       
   402 
       
   403     // Delete a request when not needed anymore.
       
   404     if (( aStatus != KErrNone && origRequest == MSimpleEngineRequest::EStartPublish ) ||
       
   405           origRequest == MSimpleEngineRequest::EStopPublish  ||
       
   406           myTerminated )
       
   407         {
       
   408         iRequest = MSimpleEngineRequest::ENone;
       
   409         iPublished = EFalse;
       
   410 
       
   411         // delete corresponding request from another DLL too.
       
   412         aReq.ModifyType( MSimpleEngineRequest::EDestroy );
       
   413         TRAP_IGNORE( SendReqL( aReq ));
       
   414         // delete request from this DLL.
       
   415         aReq.Destroy();
       
   416         }
       
   417 
       
   418     // Set iRequest back to EStartPublish after Modify request.
       
   419     // This helps to handle error situations above.
       
   420     else if ( origRequest == MSimpleEngineRequest::EPublishModify )
       
   421         {
       
   422         iRequest = MSimpleEngineRequest::EStartPublish;
       
   423         aReq.ModifyType( MSimpleEngineRequest::EStartPublish );        
       
   424         }
       
   425 
       
   426     iDestroyedPtr = NULL; 
       
   427     }
       
   428 
       
   429 // ----------------------------------------------------------
       
   430 // CSimplePublisher::StreamDocumentL
       
   431 // ----------------------------------------------------------
       
   432 //
       
   433 void CSimplePublisher::StreamDocumentL(
       
   434     CSimpleEngineRequest& aReq,
       
   435     MSimpleDocument& aDocument )
       
   436     {            
       
   437     // add request data
       
   438     // externalize the document a stream
       
   439     iBuffer->Reset();
       
   440     RBufWriteStream stream( *iBuffer );
       
   441     stream.Open( *iBuffer );
       
   442     aDocument.ExternalizeL( stream );
       
   443     stream.Close();
       
   444     aReq.SetRequestData( iBuffer->Ptr(0) );
       
   445     }
       
   446     
       
   447 // ----------------------------------------------------------
       
   448 // CSimplePublisher::DoStartPublishL
       
   449 // ----------------------------------------------------------
       
   450 //
       
   451 TInt CSimplePublisher::DoStartPublishL( MSimpleDocument& aDocument,
       
   452     TBool aRefresh, const TDesC8& aETag )
       
   453     {
       
   454     if ( iRequest != MSimpleEngineRequest::ENone )
       
   455         {
       
   456         User::Leave( KErrInUse );
       
   457         }           
       
   458 
       
   459     CSimpleEngineRequest* req = CSimpleEngineRequest::NewL(
       
   460         *this, MSimpleEngineRequest::EStartPublish, iOpId );
       
   461     CleanupStack::PushL( req );
       
   462     req->SetRefresh( aRefresh );
       
   463     // add remote uri
       
   464     const TDesC8* p8 = aDocument.EntityURI();
       
   465     req->SetRemoteURIL( *p8 );
       
   466     // set ETag
       
   467     req->SetETagL( aETag );
       
   468 
       
   469     // add request data
       
   470     StreamDocumentL( *req, aDocument );
       
   471     
       
   472     // Set MIME type of the request
       
   473     RPointerArray<MSimpleContent> contents;
       
   474     CleanupClosePushL( contents );     
       
   475     aDocument.GetDirectContentsL( contents );
       
   476     if ( contents.Count() )
       
   477         {                                            
       
   478         req->SetRequestContentTypeL( KSimpleMultipartType );
       
   479         }
       
   480     else
       
   481         {
       
   482         req->SetRequestContentTypeL( KSimpleDocumentType );
       
   483         }
       
   484     CleanupStack::PopAndDestroy( &contents );        
       
   485 
       
   486     // send a request to engine DLL
       
   487     SendReqL( *req );
       
   488     iRequestList.AddLast( *req );
       
   489     CleanupStack::Pop( req );
       
   490 
       
   491     iRequest = MSimpleEngineRequest::EStartPublish;
       
   492     iSipStatus = 0;
       
   493     return iOpId;
       
   494     }    
       
   495 
       
   496