sysstatemgmt/systemstarter/sysmonsrc/sysmonserver.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent 
       
    19 */
       
    20 
       
    21 #include "sysmoncliserv.h"
       
    22 #include "sysmonserver.h"
       
    23 #include "monitor.h"
       
    24 #include "sysmonservsess.h"
       
    25 #include "timerlist.h"
       
    26 
       
    27 #include "../src/SysStartDebug.h"
       
    28 
       
    29 
       
    30 CSysMonServer* CSysMonServer::NewLC()
       
    31 	{
       
    32 	CSysMonServer* server = new(ELeave) CSysMonServer();
       
    33 	CleanupStack::PushL(server);
       
    34 	server->ConstructL();
       
    35 	return server;		
       
    36 	}
       
    37 
       
    38 
       
    39 void CSysMonServer::ConstructL()
       
    40 	{
       
    41 	StartL(KSysMonServerName);	
       
    42 	}
       
    43 
       
    44 
       
    45 CSysMonServer::CSysMonServer()
       
    46 	:CServer2(EPriorityHigh),
       
    47 	 iShutDownInProgress(EFalse),
       
    48 	 iMonitors(CMonitor::iOffset),
       
    49 	 iIter(iMonitors) 
       
    50 	{
       
    51 	}
       
    52 
       
    53 
       
    54 CSysMonServer::~CSysMonServer()	
       
    55 	{
       
    56 	CancelAllMonitors();
       
    57 	delete iTimerList;
       
    58 	}
       
    59 
       
    60 TBool CSysMonServer::ShutDownInProgress() const
       
    61 	{
       
    62 	return iShutDownInProgress;
       
    63 	}
       
    64 
       
    65 void CSysMonServer::CancelAllMonitors()
       
    66 	{
       
    67 	iShutDownInProgress = ETrue;
       
    68 
       
    69 	iIter.SetToFirst();
       
    70 	CMonitor *monitor = NULL;
       
    71 	while (iIter++ != NULL)
       
    72 		{
       
    73 		monitor = iIter;
       
    74 		iMonitors.Remove(*monitor); // removing each of the monitors from the list
       
    75 		delete monitor; 	// delete will also cancel monitoring
       
    76 		}	
       
    77 	}
       
    78 
       
    79 
       
    80 /*
       
    81 Call CSysMonServer::AddMonitorL to add monitor to the linked list.
       
    82 Also calls SetActive() on aMonitor.
       
    83 */	
       
    84 void CSysMonServer::AddAndStartMonitorL(CMonitor* aMonitor)
       
    85 	{
       
    86 	iIter.SetToFirst();
       
    87 
       
    88 	TProcessId id = aMonitor->ProcessId();
       
    89 	CMonitor *monitor = NULL;
       
    90 	while ((monitor = iIter++) != NULL)
       
    91 		{
       
    92 		if (monitor->ProcessId() == id) //  check if the process is already been monitored
       
    93 			{
       
    94 			DEBUGPRINT1(_L("This process is already been monitored."));
       
    95 			User::Leave(KErrAlreadyExists);
       
    96 			}			
       
    97 		}
       
    98 
       
    99 	aMonitor->Start(); // activate the monitoring
       
   100 
       
   101 	iMonitors.AddFirst(*aMonitor);
       
   102 	CleanupStack::Pop(aMonitor);
       
   103 
       
   104 	DEBUGPRINT2(_L("Monitor inserted into SysMon server. ProcessId=%Lu"), id.Id());
       
   105 	}
       
   106 	
       
   107 
       
   108 void CSysMonServer::CancelMonitor(const TProcessId& aProcessId)
       
   109 	{
       
   110 	iIter.SetToFirst();
       
   111 	CMonitor *monitor = NULL;
       
   112 	while ((monitor = iIter++) != NULL)
       
   113 		{
       
   114 		if (monitor->ProcessId() == aProcessId) // find the process in the list
       
   115 			{
       
   116 			break;
       
   117 			}		
       
   118 		}
       
   119 	
       
   120 	if (monitor == NULL)
       
   121 		{
       
   122 		return;
       
   123 		}
       
   124 
       
   125 	iMonitors.Remove(*monitor);// remove the monitor from the list.
       
   126 	delete monitor;	// delete will also cancel monitoring
       
   127 	}
       
   128 
       
   129 CTimerList& CSysMonServer::TimerListL()
       
   130 	{
       
   131 	if (iTimerList == NULL)
       
   132 		{
       
   133 		iTimerList = CTimerList::NewL(EPriorityLow);
       
   134 		}
       
   135 
       
   136 	return *iTimerList;
       
   137 	}
       
   138 
       
   139 
       
   140 CSession2* CSysMonServer::NewSessionL(const TVersion& /*aVersion*/, const RMessage2& /*aMessage*/) const
       
   141 	{
       
   142 	return CSysMonSession::NewL();
       
   143 	}
       
   144 
       
   145 
       
   146 static void RunServerL()
       
   147 //
       
   148 // Perform all server initialisation, in particular creation of the
       
   149 // scheduler and server and then run the scheduler
       
   150 //
       
   151 	{
       
   152 	DEBUGPRINT1(_L("SysMon: Going to run server"));	
       
   153 
       
   154 	User::SetCritical(User::ESystemCritical);
       
   155 	
       
   156 	// create and install the active scheduler we need
       
   157 	CActiveScheduler* s = new(ELeave) CActiveScheduler;
       
   158 	CleanupStack::PushL(s);
       
   159 	CActiveScheduler::Install(s);
       
   160 
       
   161 	DEBUGPRINT1(_L("SysMon: Creating Server..."));	
       
   162 	// create the server (leave it on the cleanup stack)
       
   163 	CSysMonServer::NewLC();
       
   164 	
       
   165 	DEBUGPRINT1(_L("Doing Rendezvous..."));	
       
   166 	// Initialisation complete, now signal the client
       
   167 	RProcess::Rendezvous(KErrNone);
       
   168 	
       
   169 	DEBUGPRINT1(_L("SysMon: Starting Scheduler..."));	
       
   170 	// Ready to run
       
   171 	CActiveScheduler::Start();
       
   172 
       
   173 	// Cleanup the server and scheduler
       
   174 	CleanupStack::PopAndDestroy(2, s);
       
   175 	}
       
   176 
       
   177 TInt E32Main()
       
   178 //
       
   179 // Server process entry-point
       
   180 //
       
   181 	{
       
   182 	__UHEAP_MARK;
       
   183 	
       
   184 	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   185 	TInt r = KErrNoMemory;
       
   186 	if (cleanup)
       
   187 		{
       
   188 		TRAP(r,RunServerL());
       
   189 		delete cleanup;
       
   190 		}
       
   191 	
       
   192 	__UHEAP_MARKEND;
       
   193 	return r;
       
   194 	}