upnpsharing/upnpcontentserver/src/upnpcontentserversession.cpp
changeset 0 7f85d04be362
child 38 5360b7ddc251
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:      CUpnpContentServerSession class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 #include <e32debug.h>
       
    23 #include <e32def.h> //CArrayFix
       
    24 #include <s32mem.h> //RDesReadStream
       
    25 #include "upnpcontentserverdefs.h"
       
    26 #include "upnpcontentserversession.h"
       
    27 #include "upnpcontentserverclient.h"
       
    28 
       
    29 _LIT( KComponentLogfile, "contentserver.txt");
       
    30 #include "upnplog.h"
       
    31 
       
    32 using namespace UpnpContentServer;
       
    33 
       
    34 // --------------------------------------------------------------------------
       
    35 // CUpnpContentServerSession::NewL
       
    36 // 2-phased constructor.
       
    37 // --------------------------------------------------------------------------
       
    38 //
       
    39 CUpnpContentServerSession* CUpnpContentServerSession::NewL(
       
    40     CUpnpContentServer* aServer )
       
    41     {
       
    42     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
    43     CUpnpContentServerSession* self =
       
    44         new (ELeave) CUpnpContentServerSession();
       
    45     CleanupStack::PushL( self );
       
    46     self->ConstructL( aServer );
       
    47     CleanupStack::Pop( self );
       
    48     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
    49     return self;
       
    50     }
       
    51 
       
    52 // --------------------------------------------------------------------------
       
    53 // CUpnpContentServerSession::CUpnpContentServerSession
       
    54 // C++ constructor.
       
    55 // --------------------------------------------------------------------------
       
    56 //
       
    57 CUpnpContentServerSession::CUpnpContentServerSession( )
       
    58     {
       
    59     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
    60     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
    61     }
       
    62 
       
    63 // --------------------------------------------------------------------------
       
    64 // void CUpnpContentServerSession::ConstructL
       
    65 // 2nd phase constructor.
       
    66 // --------------------------------------------------------------------------
       
    67 //
       
    68 void CUpnpContentServerSession::ConstructL( CUpnpContentServer* aServer )
       
    69     {
       
    70     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
    71     if ( !aServer )
       
    72         {
       
    73         User::Leave( KErrArgument );
       
    74         }
       
    75     iServer = aServer;
       
    76     iHandler = iServer->Handler();
       
    77     iServer->AddSession();
       
    78     if ( iHandler )
       
    79         {
       
    80         iHandler->SetContentSharingObserverL( this );
       
    81         }
       
    82     else
       
    83         {
       
    84         User::Leave( KErrBadHandle );
       
    85         }
       
    86     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
    87     }
       
    88 
       
    89 // --------------------------------------------------------------------------
       
    90 // CUpnpContentServerSession::~CUpnpContentServerSession
       
    91 // C++ destructor.
       
    92 // --------------------------------------------------------------------------
       
    93 //
       
    94 CUpnpContentServerSession::~CUpnpContentServerSession()
       
    95     {
       
    96     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
    97 
       
    98     // Sharing is off, client is disconnected, no sharing operation is ongoing
       
    99     // -> close the server
       
   100     if( iServer && iHandler )
       
   101         {
       
   102         iServer->RemoveSession();
       
   103         if ( iHandler->CanStop() && iServer->CanStop() )
       
   104             {
       
   105             iServer->Stop();
       
   106             __LOG("CUpnpContentServerSession::~CUpnpContentServerSession, \
       
   107 CActiveScheduler stopped");
       
   108             }
       
   109         TRAP_IGNORE( iHandler->SetContentSharingObserverL( NULL ) )
       
   110         }
       
   111     delete iResultBuffer;
       
   112     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   113     }
       
   114 
       
   115 // --------------------------------------------------------------------------
       
   116 // CUpnpContentServerSession::ServiceL
       
   117 // From CSession2, passes the request forward to DispatchMessageL.
       
   118 // --------------------------------------------------------------------------
       
   119 //
       
   120 void CUpnpContentServerSession::ServiceL( const RMessage2& aMessage )
       
   121     {
       
   122     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   123     TRAPD(err,DispatchMessageL(aMessage));
       
   124     if ( err != KErrNone )
       
   125         {
       
   126         __LOG1( "Error: %d", err );
       
   127         aMessage.Complete( err );
       
   128         }
       
   129     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   130     }
       
   131 
       
   132 // --------------------------------------------------------------------------
       
   133 // CUpnpContentServerSession::DispatchMessageL
       
   134 // Handles the request from client.
       
   135 // --------------------------------------------------------------------------
       
   136 //
       
   137 void CUpnpContentServerSession::DispatchMessageL( const RMessage2& aMessage )
       
   138     {
       
   139     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   140     iHandler->SetContentSharingObserverL( this );
       
   141     switch( aMessage.Function() )
       
   142         {
       
   143     case ERequestSelectionContent :
       
   144         {
       
   145         GetSelectionContentL( aMessage );
       
   146         // completed separately
       
   147         }
       
   148         break;
       
   149     case EGetSelectionIndexes :
       
   150         {
       
   151         GetSelectionIndexesL( aMessage );
       
   152         aMessage.Complete( KErrNone );
       
   153         }
       
   154         break;
       
   155     case EChangeShareContent :
       
   156         {
       
   157         ChangeShareContentL( aMessage );
       
   158         aMessage.Complete( KErrNone );
       
   159         }
       
   160         break;
       
   161     case ERefreshShareContent :
       
   162         {
       
   163         RefreshShareContentL( aMessage );
       
   164         aMessage.Complete( KErrNone );
       
   165         }
       
   166         break;
       
   167     case ERequestProgressInfo :
       
   168         {
       
   169         RequestProgressInfoL( aMessage );
       
   170         aMessage.Complete( KErrNone );
       
   171         }
       
   172         break;
       
   173     case EStartConnectionMonitor :
       
   174         {
       
   175         StartConnectionMonitorL( aMessage );
       
   176         aMessage.Complete( KErrNone );
       
   177         }
       
   178         break;
       
   179     case EStopConnectionMonitor :
       
   180         {
       
   181         StopConnectionMonitorL();
       
   182         aMessage.Complete( KErrNone );
       
   183         }
       
   184         break;
       
   185 
       
   186     default:
       
   187         {
       
   188         aMessage.Complete( KErrArgument );
       
   189         }
       
   190         break;
       
   191         }
       
   192     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   193     }
       
   194 
       
   195 // --------------------------------------------------------------------------
       
   196 // CUpnpContentServerSession::GetSelectionContentL
       
   197 // asynchronous
       
   198 // --------------------------------------------------------------------------
       
   199 //
       
   200 void CUpnpContentServerSession::GetSelectionContentL(
       
   201     const RMessage2& aMessage )
       
   202     {
       
   203     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   204 
       
   205     TInt len( aMessage.GetDesLength(0) );
       
   206 
       
   207     HBufC8* buffer = HBufC8::NewLC( len );
       
   208     TPtr8 ptr( buffer->Des() );
       
   209     aMessage.ReadL( 0, ptr );
       
   210     RDesReadStream stream;
       
   211     stream.Open( ptr );
       
   212     CleanupClosePushL( stream );
       
   213 
       
   214     TInt mediaType( stream.ReadInt32L() );
       
   215     iSelectionContentMsg = aMessage;
       
   216 
       
   217     // call to server
       
   218     iHandler->GetSelectionContentL( mediaType );
       
   219 
       
   220     CleanupStack::PopAndDestroy( &stream );
       
   221     CleanupStack::PopAndDestroy( buffer );
       
   222 
       
   223     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   224     }
       
   225 
       
   226 // --------------------------------------------------------------------------
       
   227 // CUpnpContentServerSession::GetSelectionIndexesL
       
   228 // synchronous
       
   229 // --------------------------------------------------------------------------
       
   230 //
       
   231 void CUpnpContentServerSession::GetSelectionIndexesL(
       
   232     const RMessage2& aMessage )
       
   233     {
       
   234     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   235 
       
   236     TInt len( aMessage.GetDesLength(0) );
       
   237 
       
   238     RArray<TInt>* selections = new ( ELeave )
       
   239         RArray<TInt>( 5 );
       
   240     CleanupClosePushL( *selections );
       
   241     HBufC8* buffer = HBufC8::NewLC( len );
       
   242     TPtr8 ptr( buffer->Des() );
       
   243     aMessage.ReadL( 0, ptr );
       
   244     RDesReadStream stream;
       
   245     stream.Open( ptr );
       
   246     CleanupClosePushL( stream );
       
   247 
       
   248     TInt mediaType( stream.ReadInt32L() );
       
   249     // call to server
       
   250     iHandler->GetSelectionIndexesL( *selections, mediaType );
       
   251 
       
   252     TInt objectCount( selections->Count() );
       
   253 
       
   254     delete iResultBuffer;
       
   255     iResultBuffer = NULL;
       
   256 
       
   257     iResultBuffer = CBufFlat::NewL( KTintSize + KTintSize*objectCount );
       
   258     RBufWriteStream wstream;
       
   259     wstream.Open( *iResultBuffer );
       
   260     CleanupClosePushL( wstream );
       
   261 
       
   262     // append count and ints
       
   263     wstream.WriteInt32L( objectCount );
       
   264     for( TInt i(0); i < objectCount; i++ )
       
   265         {
       
   266         wstream.WriteInt32L( selections->operator[]( i ) );
       
   267         }
       
   268 
       
   269     // Write result back to client
       
   270     aMessage.WriteL( 1, iResultBuffer->Ptr(0) );
       
   271 
       
   272     CleanupStack::PopAndDestroy( &wstream );
       
   273     CleanupStack::PopAndDestroy( &stream );
       
   274     CleanupStack::PopAndDestroy( buffer );
       
   275     CleanupStack::PopAndDestroy( selections );
       
   276     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   277     }
       
   278 
       
   279 // --------------------------------------------------------------------------
       
   280 // CUpnpContentServerSession::ChangeShareContentL
       
   281 // Handles the async request from client.
       
   282 // --------------------------------------------------------------------------
       
   283 //
       
   284 void CUpnpContentServerSession::ChangeShareContentL(
       
   285     const RMessage2& aMessage )
       
   286     {
       
   287     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   288 
       
   289     TInt len( aMessage.GetDesLength(0) );
       
   290     RArray<TInt> selections( len );
       
   291     CleanupClosePushL( selections );
       
   292     HBufC8* buffer = HBufC8::NewLC( len );
       
   293     TPtr8 ptr( buffer->Des() );
       
   294     aMessage.ReadL( 0, ptr );
       
   295     RDesReadStream stream;
       
   296     stream.Open( ptr );
       
   297     CleanupClosePushL( stream );
       
   298 
       
   299     TInt mediaType( stream.ReadInt32L() );
       
   300     TInt objCount( stream.ReadInt32L() );
       
   301 
       
   302     // loop the TInt from stream
       
   303     for( TInt i(0); i< objCount; i++)
       
   304         {
       
   305         selections.AppendL( stream.ReadInt32L() );
       
   306         }
       
   307 
       
   308     iHandler->ChangeShareContentL( selections,  mediaType );
       
   309     
       
   310     CleanupStack::PopAndDestroy( &stream );
       
   311     CleanupStack::PopAndDestroy( buffer );
       
   312     CleanupStack::PopAndDestroy( &selections );
       
   313     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   314     }
       
   315 
       
   316 // --------------------------------------------------------------------------
       
   317 // CUpnpContentServerSession::RequestProgressInfoL
       
   318 // synchronous
       
   319 // --------------------------------------------------------------------------
       
   320 //
       
   321 void CUpnpContentServerSession::RequestProgressInfoL(
       
   322     const RMessage2& /*aMessage*/ )
       
   323     {
       
   324     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   325     // The progress should be read using pubsub
       
   326     // This call will be removed in upnpfw 2.1
       
   327     User::Leave( KErrNotSupported );
       
   328     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   329     }
       
   330 
       
   331 // --------------------------------------------------------------------------
       
   332 // CUpnpContentServerSession::RefreshShareContentL
       
   333 // asynchronous
       
   334 // --------------------------------------------------------------------------
       
   335 //
       
   336 void CUpnpContentServerSession::RefreshShareContentL(
       
   337     const RMessage2& aMessage )
       
   338     {
       
   339     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   340     TInt len( aMessage.GetDesLength(0) );
       
   341     HBufC8* buffer = HBufC8::NewLC( len );
       
   342     TPtr8 ptr( buffer->Des() );
       
   343     aMessage.ReadL( 0, ptr );
       
   344     RDesReadStream stream;
       
   345     stream.Open( ptr );
       
   346     CleanupClosePushL( stream );
       
   347 
       
   348     TInt mediaType( stream.ReadInt32L() );
       
   349 
       
   350     // The sharing and refresh cannot be used simultaneously
       
   351     // Using the same place to store message for completion
       
   352     //
       
   353     iChangeShareContentMsg = aMessage;
       
   354     // call to handler.
       
   355     iHandler->RefreshShareContentL( mediaType );
       
   356     CleanupStack::PopAndDestroy( &stream );
       
   357     CleanupStack::PopAndDestroy( buffer );
       
   358 
       
   359     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   360     }
       
   361 
       
   362 // --------------------------------------------------------------------------
       
   363 // CUpnpContentServerSession::CompleteSelectionContentL
       
   364 // Completes the async GetSelectionContentL call
       
   365 // --------------------------------------------------------------------------
       
   366 //
       
   367 void CUpnpContentServerSession::CompleteSelectionContentL(
       
   368     const CDesCArray& aMarkedItems )
       
   369     {
       
   370     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   371 
       
   372     TInt objectCount( aMarkedItems.MdcaCount() );
       
   373 
       
   374     delete iResultBuffer;
       
   375     iResultBuffer = NULL;
       
   376 
       
   377     iResultBuffer = CBufFlat::NewL( KTintSize + KTintSize*objectCount );
       
   378     RBufWriteStream wstream;
       
   379     wstream.Open( *iResultBuffer );
       
   380     CleanupClosePushL( wstream );
       
   381 
       
   382     // append count
       
   383     wstream.WriteInt32L( objectCount );
       
   384     // append descriptors
       
   385     for( TInt i(0); i<objectCount; i++ )
       
   386         {
       
   387         wstream.WriteInt32L( aMarkedItems[ i ].Length() );
       
   388         wstream.WriteL( aMarkedItems[ i ] );
       
   389         }
       
   390 
       
   391     // Write result back to client
       
   392     iSelectionContentMsg.WriteL( 1, iResultBuffer->Ptr(0) );
       
   393     iSelectionContentMsg.Complete( KErrNone );
       
   394     CleanupStack::PopAndDestroy( &wstream );
       
   395 
       
   396     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   397     }
       
   398 
       
   399 // --------------------------------------------------------------------------
       
   400 // CUpnpContentServerSession::StartConnectionMonitorL
       
   401 // synchronous
       
   402 // --------------------------------------------------------------------------
       
   403 //
       
   404 void CUpnpContentServerSession::StartConnectionMonitorL(
       
   405     const RMessage2& aMessage )
       
   406     {
       
   407     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   408     TInt len( aMessage.GetDesLength(0) );
       
   409     HBufC8* buffer = HBufC8::NewLC( len );
       
   410     TPtr8 ptr( buffer->Des() );
       
   411     aMessage.ReadL( 0, ptr );
       
   412     RDesReadStream stream;
       
   413     stream.Open( ptr );
       
   414     CleanupClosePushL( stream );
       
   415 
       
   416     TInt iapId( stream.ReadInt32L() );
       
   417 
       
   418     // call to server
       
   419     iServer->RequestConnectionLostL( iapId );
       
   420     CleanupStack::PopAndDestroy( &stream );
       
   421     CleanupStack::PopAndDestroy( buffer );
       
   422 
       
   423     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   424     }
       
   425 
       
   426 // --------------------------------------------------------------------------
       
   427 // CUpnpContentServerSession::StopConnectionMonitorL
       
   428 // cancel connection lost
       
   429 // --------------------------------------------------------------------------
       
   430 //
       
   431 void CUpnpContentServerSession::StopConnectionMonitorL( )
       
   432     {
       
   433     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   434     iServer->CancelConnectionLostL();
       
   435     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   436     }
       
   437 
       
   438 // End of file