upnpsharing/upnpcontentserver/src/upnpcontentserverclient.cpp
changeset 0 7f85d04be362
child 30 5ec426854821
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:      RUpnpContentServerClient class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 // INCLUDES
       
    24 #include <f32file.h>
       
    25 #include <s32mem.h>
       
    26 
       
    27 #include "upnpcontentserverclient.h"
       
    28 
       
    29 _LIT( KComponentLogfile, "contentserver.txt");
       
    30 #include "upnplog.h"
       
    31 
       
    32 // CONSTANTS 
       
    33 const TInt KDefaultBufferSize( 512 ); //default size for any receice buffer
       
    34 const TInt KContentBufferSize( 10*1024 ); //size for media content buffer
       
    35 
       
    36 using namespace UpnpContentServer;
       
    37 
       
    38 //---------------------------------------------------------------------------
       
    39 // LaunchServer
       
    40 // Launches the server
       
    41 //---------------------------------------------------------------------------
       
    42 //
       
    43 TInt LaunchServer()
       
    44     {
       
    45     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
    46     TInt res = KErrNone;
       
    47 
       
    48     // DLL launch
       
    49     RProcess server;
       
    50     res = server.Create( KUpnpContentServerNameAndDir, 
       
    51                          KNullDesC );
       
    52 
       
    53     // Loading failed.
       
    54     if ( res == KErrNone )
       
    55         {
       
    56         server.SetPriority( EPriorityLow );
       
    57         TRequestStatus status;
       
    58         server.Rendezvous(status);
       
    59 
       
    60         if ( status != KRequestPending)
       
    61             {
       
    62             server.Kill(0);     // abort startup
       
    63             server.Close();
       
    64             res = KErrGeneral;
       
    65             }
       
    66         if( !res )
       
    67             {
       
    68             server.Resume();    // Logon OK - start the server.
       
    69 
       
    70             User::WaitForRequest(status);
       
    71             server.Close();
       
    72             res = status.Int();
       
    73             }
       
    74         }
       
    75     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
    76     return res;
       
    77     }
       
    78 
       
    79 // --------------------------------------------------------------------------
       
    80 // RUpnpContentServerClient::RUpnpContentServerClient
       
    81 // C++ Constructor
       
    82 // --------------------------------------------------------------------------
       
    83 //
       
    84 EXPORT_C RUpnpContentServerClient::RUpnpContentServerClient()
       
    85     : iSendBufferPtr( NULL, 0 ),
       
    86       iSendBuffer( NULL ),
       
    87       iReceiveBufferPtr( NULL, 0 ),
       
    88       iReceiveBuffer( NULL ),
       
    89       iContentBufferPtr( NULL, 0 ),
       
    90       iContentBuffer( NULL )
       
    91     {
       
    92     }
       
    93 
       
    94 // --------------------------------------------------------------------------
       
    95 // RUpnpContentServerClient::OpenL
       
    96 // Creates connection to server
       
    97 // --------------------------------------------------------------------------
       
    98 //
       
    99 EXPORT_C void RUpnpContentServerClient::OpenL()
       
   100     {
       
   101     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   102     TVersion version( KUpnpContentServerVersionMajor,
       
   103                       KUpnpContentServerVersionMinor,
       
   104                       0 );
       
   105 
       
   106     TInt res = CreateSession( KUpnpContentServer, version,
       
   107                               KDefaultMessageSlots );
       
   108     if ( res != KErrNone )
       
   109         {
       
   110         __LOG1( "Error: %d", res );
       
   111         res = LaunchServer();
       
   112         if ( res != KErrNone )
       
   113             {
       
   114             __LOG1( "Error: %d", res );
       
   115             }
       
   116         User::LeaveIfError( res );
       
   117         res = CreateSession( KUpnpContentServer, version,
       
   118                              KDefaultMessageSlots );
       
   119         if ( res != KErrNone )
       
   120             {
       
   121             __LOG1( "Error: %d", res );
       
   122             }
       
   123         ShareAuto();
       
   124         }
       
   125     User::LeaveIfError( res );
       
   126     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   127     }
       
   128 
       
   129 // --------------------------------------------------------------------------
       
   130 // RUpnpContentServerClient::Close
       
   131 // close connection to server
       
   132 // --------------------------------------------------------------------------
       
   133 //
       
   134 EXPORT_C void RUpnpContentServerClient::Close()
       
   135     {
       
   136     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   137     // close session
       
   138     delete iSendBuffer;
       
   139     iSendBuffer = NULL;
       
   140     delete iReceiveBuffer;
       
   141     iReceiveBuffer = NULL;
       
   142     delete iContentBuffer;
       
   143     iContentBuffer = NULL;
       
   144     RSessionBase::Close();
       
   145     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   146     }
       
   147 
       
   148 // --------------------------------------------------------------------------
       
   149 // RUpnpContentServerClient::StartUploadListenerL
       
   150 // Starts the media server upload listener
       
   151 // --------------------------------------------------------------------------
       
   152 //
       
   153 EXPORT_C void RUpnpContentServerClient::StartUploadListenerL( )
       
   154     {
       
   155     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   156     User::Leave( KErrNotSupported );
       
   157     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   158     }
       
   159 
       
   160 // --------------------------------------------------------------------------
       
   161 // RUpnpContentServerClient::StopUploadListenerL
       
   162 // Stops the media server upload listener
       
   163 // --------------------------------------------------------------------------
       
   164 //
       
   165 EXPORT_C void RUpnpContentServerClient::StopUploadListenerL()
       
   166     {
       
   167     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   168     User::Leave( KErrNotSupported );
       
   169     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   170     }
       
   171 
       
   172 // --------------------------------------------------------------------------
       
   173 // RUpnpContentServerClient::GetSelectionContentL
       
   174 // Gets the names of existed albums/playlists
       
   175 // --------------------------------------------------------------------------
       
   176 //
       
   177 EXPORT_C void RUpnpContentServerClient::GetSelectionContentL(
       
   178     UpnpContentServer::TUpnpMediaType aType,
       
   179     TRequestStatus& aStatus )
       
   180     {
       
   181     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   182 
       
   183     delete iSendBuffer;
       
   184     iSendBuffer = NULL;
       
   185     iSendBuffer = CBufFlat::NewL( KTintSize );
       
   186 
       
   187     // Need to externalize a data to buffer to send it with IPC
       
   188     RBufWriteStream sendstream;
       
   189     sendstream.Open( *iSendBuffer );
       
   190     CleanupClosePushL( sendstream );
       
   191     sendstream.WriteInt32L( aType );
       
   192     iSendBufferPtr.Set( iSendBuffer->Ptr(0) );
       
   193 
       
   194     // Prepare receive buffers
       
   195     delete iContentBuffer;
       
   196     iContentBuffer = NULL;
       
   197     iContentBuffer = HBufC8::NewL( KContentBufferSize );
       
   198     iContentBufferPtr.Set( iContentBuffer->Des() );
       
   199 
       
   200     SendReceive( ERequestSelectionContent,
       
   201                  TIpcArgs( &iSendBufferPtr, &iContentBufferPtr ), 
       
   202                  aStatus );
       
   203     CleanupStack::PopAndDestroy( &sendstream );
       
   204 
       
   205     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   206     }
       
   207 
       
   208 // --------------------------------------------------------------------------
       
   209 // RUpnpContentServerClient::GetSelectionContentResultL
       
   210 // Ask the result of GetSelectionContentL call
       
   211 // --------------------------------------------------------------------------
       
   212 //
       
   213 EXPORT_C void RUpnpContentServerClient::GetSelectionContentResultL(
       
   214     CDesCArray& aIDArray )
       
   215     {
       
   216     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   217 
       
   218     if ( !iContentBuffer )
       
   219         {
       
   220         User::Leave( KErrNotReady );
       
   221         }
       
   222 
       
   223     RDesReadStream stream;
       
   224     stream.Open( iContentBufferPtr );
       
   225     CleanupClosePushL( stream );
       
   226     // leaves -25 if empty stream
       
   227     TInt itemCount( 0 );
       
   228     TRAP_IGNORE( itemCount = stream.ReadUint32L() );
       
   229 
       
   230     for ( TInt i(0); i < itemCount; i++ )
       
   231         {
       
   232         const TInt desLen( static_cast<TInt>( stream.ReadUint32L() ));
       
   233         TBuf<KMaxFileName> item;
       
   234         stream.ReadL( item, desLen );
       
   235         aIDArray.AppendL( item );
       
   236         }
       
   237 
       
   238     CleanupStack::PopAndDestroy( &stream );
       
   239     delete iContentBuffer;
       
   240     iContentBuffer = NULL;
       
   241 
       
   242     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   243     }
       
   244 
       
   245 // --------------------------------------------------------------------------
       
   246 // RUpnpContentServerClient::ChangeSharedContentL
       
   247 // Sends the selected indexes to server and starts sharing
       
   248 // --------------------------------------------------------------------------
       
   249 //
       
   250 EXPORT_C void RUpnpContentServerClient::ChangeSharedContentL(
       
   251     UpnpContentServer::TUpnpMediaType aType,
       
   252     const CArrayFix<TInt>& aMarkedItems,
       
   253     TRequestStatus& aStatus )
       
   254     {
       
   255     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   256 
       
   257     TInt objectCount( aMarkedItems.Count() );
       
   258     if ( objectCount < 1 )
       
   259         {
       
   260         __LOG1( "Error: %d", __LINE__ );
       
   261         User::Leave( KErrArgument );
       
   262         }
       
   263 
       
   264     delete iSendBuffer;
       
   265     iSendBuffer = NULL;
       
   266     // Calculate correct size for send buffer on basis of the amount
       
   267     // of items user has selected
       
   268     iSendBuffer = CBufFlat::NewL( objectCount * KTintSize + 2*KTintSize );
       
   269 
       
   270     // Need to externalize a data to buffer to send it with IPC
       
   271     RBufWriteStream stream;
       
   272     stream.Open( *iSendBuffer );
       
   273     CleanupClosePushL( stream );
       
   274     stream.WriteInt32L( aType );
       
   275     stream.WriteInt32L( objectCount );
       
   276     for( TInt i(0); i< objectCount; i++ )
       
   277         {
       
   278         stream.WriteInt32L( aMarkedItems[ i ] );
       
   279         }
       
   280     iSendBufferPtr.Set( iSendBuffer->Ptr(0) );
       
   281 
       
   282     SendReceive( EChangeShareContent,
       
   283                  TIpcArgs( &iSendBufferPtr ),
       
   284                  aStatus );   
       
   285     CleanupStack::PopAndDestroy( &stream );
       
   286 
       
   287     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   288     }
       
   289 
       
   290 // --------------------------------------------------------------------------
       
   291 // RUpnpContentServerClient::RefreshSharedContentL
       
   292 // Refresh the shared content
       
   293 // --------------------------------------------------------------------------
       
   294 //
       
   295 EXPORT_C void RUpnpContentServerClient::RefreshSharedContentL(
       
   296     UpnpContentServer::TUpnpMediaType aType,
       
   297     TRequestStatus& aStatus )
       
   298     {
       
   299     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   300 
       
   301     delete iSendBuffer;
       
   302     iSendBuffer = NULL;
       
   303     iSendBuffer = CBufFlat::NewL( KTintSize );
       
   304 
       
   305     // Need to externalize a data to buffer to send it with IPC
       
   306     RBufWriteStream stream;
       
   307     stream.Open( *iSendBuffer );
       
   308     CleanupClosePushL( stream );
       
   309     stream.WriteInt32L( aType );
       
   310     iSendBufferPtr.Set( iSendBuffer->Ptr(0) );
       
   311 
       
   312     SendReceive( ERefreshShareContent,
       
   313                  TIpcArgs( &iSendBufferPtr ),
       
   314                  aStatus );
       
   315     CleanupStack::PopAndDestroy( &stream );
       
   316 
       
   317     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   318     }
       
   319 
       
   320 // --------------------------------------------------------------------------
       
   321 // RUpnpContentServerClient::GetSelectionIndexesL
       
   322 // Gets the users previous selections as list of selected indexes
       
   323 // --------------------------------------------------------------------------
       
   324 //
       
   325 EXPORT_C void RUpnpContentServerClient::GetSelectionIndexesL(
       
   326     UpnpContentServer::TUpnpMediaType aType,
       
   327     TRequestStatus& aStatus )
       
   328     {
       
   329     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   330 
       
   331     delete iSendBuffer;
       
   332     iSendBuffer = NULL;
       
   333     iSendBuffer = CBufFlat::NewL( KTintSize );
       
   334 
       
   335     // Need to externalize a data to buffer to send it with IPC
       
   336     RBufWriteStream sendstream;
       
   337     sendstream.Open( *iSendBuffer );
       
   338     CleanupClosePushL( sendstream );
       
   339     sendstream.WriteInt32L( aType );
       
   340     iSendBufferPtr.Set( iSendBuffer->Ptr(0) );
       
   341 
       
   342     // Prepare receive buffers
       
   343     PrepareReceiveBuffersL( KDefaultBufferSize );
       
   344 
       
   345     SendReceive( EGetSelectionIndexes,
       
   346                  TIpcArgs( &iSendBufferPtr, &iReceiveBufferPtr ),
       
   347                  aStatus );
       
   348     CleanupStack::PopAndDestroy( &sendstream );
       
   349 
       
   350     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   351     }
       
   352 
       
   353 // --------------------------------------------------------------------------
       
   354 // RUpnpContentServerClient::GetSelectionIndexesResultL
       
   355 // Ask the result of GetSelectionIndexesL call
       
   356 // --------------------------------------------------------------------------
       
   357 //
       
   358 EXPORT_C void RUpnpContentServerClient::GetSelectionIndexesResultL(
       
   359     CArrayFix<TInt>& aMarkedItems )
       
   360     {
       
   361     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   362 
       
   363     if ( !iReceiveBuffer )
       
   364         {
       
   365         User::Leave( KErrNotReady );
       
   366         }
       
   367 
       
   368     RDesReadStream stream;
       
   369     stream.Open( iReceiveBufferPtr );
       
   370     CleanupClosePushL( stream );
       
   371 
       
   372     // leaves -25 if empty stream
       
   373     TInt itemCount( 0 );
       
   374     TRAP_IGNORE( itemCount = stream.ReadUint32L() );
       
   375 
       
   376     for ( TInt i(0); i < itemCount; i++ )
       
   377         {
       
   378         const TInt val = static_cast<TInt>( stream.ReadUint32L() );
       
   379         aMarkedItems.AppendL( val );
       
   380         }
       
   381 
       
   382     CleanupStack::PopAndDestroy( &stream );
       
   383     delete iReceiveBuffer;
       
   384     iReceiveBuffer = NULL;
       
   385 
       
   386     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   387     }
       
   388 
       
   389 // --------------------------------------------------------------------------
       
   390 // RUpnpContentServerClient::StartConnectionMonitorL
       
   391 // Start connection monitor in content server
       
   392 // --------------------------------------------------------------------------
       
   393 //
       
   394 EXPORT_C void RUpnpContentServerClient::StartConnectionMonitorL( 
       
   395     TInt aIapId,
       
   396     TRequestStatus& aStatus )
       
   397     {
       
   398     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   399 
       
   400     delete iSendBuffer;
       
   401     iSendBuffer = NULL;
       
   402     iSendBuffer = CBufFlat::NewL( KTintSize );
       
   403 
       
   404     // Need to externalize a data to buffer to send it with IPC
       
   405     RBufWriteStream sendstream;
       
   406     sendstream.Open( *iSendBuffer );
       
   407     CleanupClosePushL( sendstream );
       
   408     sendstream.WriteInt32L( aIapId );
       
   409     iSendBufferPtr.Set( iSendBuffer->Ptr(0) );
       
   410 
       
   411     SendReceive( EStartConnectionMonitor,
       
   412                  TIpcArgs( &iSendBufferPtr ),
       
   413                  aStatus );
       
   414     CleanupStack::PopAndDestroy( &sendstream );
       
   415 
       
   416     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   417     }
       
   418 
       
   419 // --------------------------------------------------------------------------
       
   420 // RUpnpContentServerClient::StopConnectionMonitorL
       
   421 // Stop connection monitor in content server
       
   422 // --------------------------------------------------------------------------
       
   423 //
       
   424 EXPORT_C void RUpnpContentServerClient::StopConnectionMonitorL( 
       
   425     TRequestStatus& aStatus )
       
   426     {
       
   427     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   428     SendReceive( EStopConnectionMonitor, aStatus );
       
   429     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   430     }
       
   431 
       
   432 // --------------------------------------------------------------------------
       
   433 // RUpnpContentServerClient::PrepareReceiveBuffers
       
   434 // Prepares the receive buffers
       
   435 // --------------------------------------------------------------------------
       
   436 //
       
   437 void RUpnpContentServerClient::PrepareReceiveBuffersL( TInt aSize )
       
   438     {
       
   439     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   440     delete iReceiveBuffer;
       
   441     iReceiveBuffer = NULL;
       
   442     iReceiveBuffer = HBufC8::NewL( aSize );
       
   443     iReceiveBufferPtr.Set( iReceiveBuffer->Des() );
       
   444     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   445     }
       
   446 
       
   447 //End of File