multimediacommsengine/tsrc/testdriver/testclient/filehandler/src/CTcFileHandlerServer.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2004 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:  Implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #include "CTcFileHandlerServer.h"
       
    19 #include "CTcFileHandlerSession.h"
       
    20 #include "FileHandlerConstants.h"
       
    21 
       
    22 #ifdef __WINS__
       
    23 #include <e32svr.h>		// UserSvr
       
    24 #endif
       
    25 
       
    26 void PanicServer( TInt aPanic )
       
    27 	{
       
    28 	User::Panic( KTcFileHandlerName, aPanic );
       
    29 	}
       
    30 
       
    31 CTcFileHandlerServer* CTcFileHandlerServer::NewLC()
       
    32 	{
       
    33 	CTcFileHandlerServer* self = new( ELeave ) CTcFileHandlerServer;
       
    34 	CleanupStack::PushL( self );
       
    35 
       
    36 	self->ConstructL();
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 CTcFileHandlerServer::~CTcFileHandlerServer()
       
    41 	{
       
    42 	}
       
    43 
       
    44 CTcFileHandlerServer::CTcFileHandlerServer()
       
    45 	: CServer2( EPriorityStandard )
       
    46 	{
       
    47 	}
       
    48 
       
    49 void CTcFileHandlerServer::ConstructL()
       
    50 	{
       
    51 	}
       
    52 
       
    53 TInt CTcFileHandlerServer::ThreadMain( TAny* /*aParam*/ )
       
    54 	{
       
    55 	__UHEAP_MARK;
       
    56 
       
    57 	// Create the cleanup stack
       
    58 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
    59 	if( cleanup == NULL )
       
    60 		{
       
    61 		PanicServer( KErrNoMemory );
       
    62 		}
       
    63 
       
    64 	// Initialize and start the server
       
    65 	TRAPD( error, InitServerL() );
       
    66 	if( error != KErrNone )
       
    67 		{
       
    68 		PanicServer( error );
       
    69 		}
       
    70 
       
    71 	// InitServerL() will hold execution for the lifetime
       
    72 	// of the server. The server has died now, delete cleanup stack.
       
    73 	delete cleanup;
       
    74 
       
    75 	__UHEAP_MARKEND;
       
    76 
       
    77 	return KErrNone;
       
    78 	}
       
    79 
       
    80 CSession2* CTcFileHandlerServer::NewSessionL( 
       
    81     const TVersion& aVersion,
       
    82 	const RMessage2& /*aMessage*/ ) const
       
    83 	{
       
    84 	// Check that the client version is OK
       
    85 	TVersion version( KTcFileHandlerMajorVersion,
       
    86 					  KTcFileHandlerMinorVersion,
       
    87 					  KTcFileHandlerBuildVersion );
       
    88 	if( !User::QueryVersionSupported( version, aVersion ) )
       
    89 		{
       
    90 		User::Leave( KErrNotSupported );
       
    91 		}
       
    92 
       
    93 	// Create a new session
       
    94 	return CTcFileHandlerSession::NewL( const_cast< CTcFileHandlerServer* >( this ) );
       
    95 	}
       
    96 
       
    97 TInt CTcFileHandlerServer::RunError( TInt aError )
       
    98 	{
       
    99 	// Panic client if we had an error that is caused by the client itself
       
   100 	if( ( aError == KErrBadDescriptor ) ||
       
   101 		( aError == KErrBadHandle ) )
       
   102 		{
       
   103 		Message().Panic( KTcFileHandlerName, aError );
       
   104 		}
       
   105 	// Otherwise just complete the request with an error code
       
   106 	else
       
   107 		{
       
   108 		Message().Complete( aError );
       
   109 		}
       
   110 
       
   111 	// Restart server as it was interrupted when
       
   112 	// the leave at CServer::RunL() happened
       
   113 	ReStart();
       
   114 
       
   115 	// We're fully recovered now
       
   116 	return KErrNone;
       
   117 	}
       
   118 
       
   119 void CTcFileHandlerServer::SessionAdded()
       
   120 	{
       
   121 	iSessionCount++;
       
   122 	}
       
   123 
       
   124 void CTcFileHandlerServer::SessionRemoved()
       
   125 	{
       
   126 	iSessionCount--;
       
   127 
       
   128 	if( iSessionCount == 0 )
       
   129 		{
       
   130 		// No more server users, shutdown server
       
   131 		CActiveScheduler::Stop();
       
   132 		}
       
   133 	}
       
   134 
       
   135 void CTcFileHandlerServer::InitServerL()
       
   136 	{
       
   137 	// Open the semaphore that was created by the first client
       
   138 	RSemaphore semaphore;
       
   139 	User::LeaveIfError( semaphore.OpenGlobal( KTcFileHandlerName ) );
       
   140 
       
   141 	CActiveScheduler* scheduler = NULL;
       
   142 	CTcFileHandlerServer* srv = NULL;
       
   143 
       
   144 	// We don't want the client waiting on the semaphore indefinitely
       
   145 	// even if server start fails.
       
   146 	TRAPD( err,
       
   147 		{
       
   148 		// Start scheduler and server
       
   149 		scheduler = new( ELeave ) CActiveScheduler;
       
   150 		CleanupStack::PushL( scheduler );
       
   151 		CActiveScheduler::Install( scheduler );
       
   152 
       
   153 		// Create server instance
       
   154 		srv = CTcFileHandlerServer::NewLC( );
       
   155 		// Start the server using CServer::StartL()
       
   156 		srv->StartL( KTcFileHandlerName );
       
   157 		// we have to pop this before crossing TRAP boundary
       
   158 		CleanupStack::Pop( 2 );	// srv, scheduler
       
   159 		} )
       
   160 
       
   161 	// Signal the client that we are ready and willing
       
   162 	semaphore.Signal();
       
   163 	semaphore.Close();
       
   164 	User::LeaveIfError( err );
       
   165 
       
   166 	// Start fielding requests from clients (execution stops here)
       
   167 	CActiveScheduler::Start();
       
   168 
       
   169 	// Remove the active scheduler
       
   170 	CActiveScheduler::Install( NULL );
       
   171 
       
   172 	delete srv;
       
   173 	delete scheduler;
       
   174 	}