multimediacommsengine/tsrc/testdriver/testclient/watcher/src/CTcWatcherServer.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 "CTcWatcherServer.h"
       
    19 #include "CTcWatcherSession.h"
       
    20 #include "CTcAppLauncher.h"
       
    21 #include "WatcherConstants.h"
       
    22 
       
    23 #ifdef __WINS__
       
    24 #include <e32svr.h>		// UserSvr
       
    25 #endif
       
    26 
       
    27 void PanicServer( TInt aPanic )
       
    28 	{
       
    29 	User::Panic( KTcWatcherName, aPanic );
       
    30 	}
       
    31 
       
    32 CTcWatcherServer* CTcWatcherServer::NewLC()
       
    33 	{
       
    34 	CTcWatcherServer* self = new( ELeave ) CTcWatcherServer;
       
    35 	CleanupStack::PushL( self );
       
    36 
       
    37 	self->ConstructL();
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 CTcWatcherServer::~CTcWatcherServer()
       
    42 	{
       
    43 	delete iShutdownTimer;
       
    44 	iLaunchers.ResetAndDestroy();
       
    45 	}
       
    46 
       
    47 CTcWatcherServer::CTcWatcherServer()
       
    48 	: CServer2( EPriorityStandard ), iLaunchers( 4 )
       
    49 	{
       
    50 	TCallBack cb( Shutdown );
       
    51 	iShutdownTimerEntry.Set( cb );
       
    52 	}
       
    53 
       
    54 void CTcWatcherServer::ConstructL()
       
    55 	{
       
    56 	iShutdownTimer = CDeltaTimer::NewL( CActive::EPriorityLow );
       
    57 	}
       
    58 
       
    59 TInt CTcWatcherServer::ThreadMain( TAny* /*aParam*/ )
       
    60 	{
       
    61 	__UHEAP_MARK;
       
    62 
       
    63 	// Create the cleanup stack
       
    64 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
    65 	if( cleanup == NULL )
       
    66 		{
       
    67 		PanicServer( KErrNoMemory );
       
    68 		}
       
    69 
       
    70 	// Initialize and start the server
       
    71 	TRAPD( error, InitServerL() );
       
    72 	if( error != KErrNone )
       
    73 		{
       
    74 		PanicServer( error );
       
    75 		}
       
    76 
       
    77 	// InitServerL() will hold execution for the lifetime
       
    78 	// of the server. The server has died now, delete cleanup stack.
       
    79 	delete cleanup;
       
    80 
       
    81 	__UHEAP_MARKEND;
       
    82 
       
    83 	return KErrNone;
       
    84 	}
       
    85 
       
    86 #ifdef __IPC_V2_PRESENT__
       
    87 CSession2* CTcWatcherServer::NewSessionL( const TVersion& aVersion,
       
    88 	    								  const RMessage2& /*aMessage*/ ) const
       
    89 #else
       
    90 CSharableSession* CTcWatcherServer::NewSessionL( const TVersion& aVersion ) const
       
    91 #endif
       
    92 	{
       
    93 	// Check that the client version is OK
       
    94 	TVersion version( KTcWatcherMajorVersion,
       
    95 					  KTcWatcherMinorVersion,
       
    96 					  KTcWatcherBuildVersion );
       
    97 	if( !User::QueryVersionSupported( version, aVersion ) )
       
    98 		{
       
    99 		User::Leave( KErrNotSupported );
       
   100 		}
       
   101 
       
   102 	// Create a new session
       
   103 #ifdef __IPC_V2_PRESENT__
       
   104 	return CTcWatcherSession::NewL( const_cast< CTcWatcherServer* >( this ) );
       
   105 #else
       
   106 	RThread client = Message().Client();
       
   107 	return CTcWatcherSession::NewL( client,
       
   108 									const_cast< CTcWatcherServer* >( this ) );
       
   109 #endif
       
   110 	}
       
   111 
       
   112 void CTcWatcherServer::Died( const TDesC& aAppName, TBool aDismissDialog )
       
   113 	{
       
   114 	TRAP_IGNORE(
       
   115 		{
       
   116 		CTcAppLauncher* launcher = CTcAppLauncher::NewLC( aAppName, aDismissDialog, iLaunchers );
       
   117 		iLaunchers.AppendL( launcher );
       
   118 		CleanupStack::Pop( launcher );
       
   119 		} )
       
   120 	}
       
   121 
       
   122 TInt CTcWatcherServer::RunError( TInt aError )
       
   123 	{
       
   124 	// Panic client if we had an error that is caused by the client itself
       
   125 	if( ( aError == KErrBadDescriptor ) ||
       
   126 		( aError == KErrBadHandle ) )
       
   127 		{
       
   128 		Message().Panic( KTcWatcherName, aError );
       
   129 		}
       
   130 	// Otherwise just complete the request with an error code
       
   131 	else
       
   132 		{
       
   133 		Message().Complete( aError );
       
   134 		}
       
   135 
       
   136 	// Restart server as it was interrupted when
       
   137 	// the leave at CServer::RunL() happened
       
   138 	ReStart();
       
   139 
       
   140 	// We're fully recovered now
       
   141 	return KErrNone;
       
   142 	}
       
   143 
       
   144 void CTcWatcherServer::SessionAdded()
       
   145 	{
       
   146 	iSessionCount++;
       
   147 	if( iShutdownTimer->IsActive() )
       
   148 		{
       
   149 		iShutdownTimer->Remove( iShutdownTimerEntry );
       
   150 		}
       
   151 	}
       
   152 
       
   153 void CTcWatcherServer::SessionRemoved()
       
   154 	{
       
   155 	iSessionCount--;
       
   156 
       
   157 	// way to go..
       
   158 	if( iSessionCount == 0 )
       
   159 		{
       
   160 		// Queue a shutdown after 30 seconds
       
   161 		iShutdownTimer->Queue( KTcShutdownTime, iShutdownTimerEntry );
       
   162 		}
       
   163 	}
       
   164 
       
   165 TInt CTcWatcherServer::Shutdown( TAny* /*aPtr*/)
       
   166 	{
       
   167 	CActiveScheduler::Stop();
       
   168 	return KErrNone;
       
   169 	}
       
   170 
       
   171 void CTcWatcherServer::InitServerL()
       
   172 	{
       
   173 	// Open the semaphore that was created by the first client
       
   174 	RSemaphore semaphore;
       
   175 	User::LeaveIfError( semaphore.OpenGlobal( KTcWatcherName ) );
       
   176 
       
   177 	CActiveScheduler* scheduler = NULL;
       
   178 	CTcWatcherServer* srv = NULL;
       
   179 
       
   180 	// We don't want the client waiting on the semaphore indefinitely
       
   181 	// even if server start fails.
       
   182 	TRAPD( err,
       
   183 		{
       
   184 		// Start scheduler and server
       
   185 		scheduler = new( ELeave ) CActiveScheduler;
       
   186 		CleanupStack::PushL( scheduler );
       
   187 		CActiveScheduler::Install( scheduler );
       
   188 
       
   189 		// Create server instance
       
   190 		srv = CTcWatcherServer::NewLC( );
       
   191 		// Start the server using CServer::StartL()
       
   192 		srv->StartL( KTcWatcherName );
       
   193 		// we have to pop this before crossing TRAP boundary
       
   194 		CleanupStack::Pop( 2 );	// srv, scheduler
       
   195 		} )
       
   196 
       
   197 	// Signal the client that we are ready and willing
       
   198 	semaphore.Signal();
       
   199 	semaphore.Close();
       
   200 	User::LeaveIfError( err );
       
   201 /*
       
   202 #ifdef __WINS__
       
   203 	// Notify the kernel that a server has been started.
       
   204 	UserSvr::ServerStarted();
       
   205 #endif
       
   206 */
       
   207 	// Start fielding requests from clients (execution stops here)
       
   208 	CActiveScheduler::Start();
       
   209 
       
   210 	// Remove the active scheduler
       
   211 	CActiveScheduler::Install( NULL );
       
   212 
       
   213 	delete srv;
       
   214 	delete scheduler;
       
   215 	}