genericservices/taskscheduler/SCHSVR/SCHCLI.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2004-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 // System includes
       
    17 #include <logwraplimits.h>
       
    18 #include <logwrap.h>
       
    19 #include <schtask.h>
       
    20 
       
    21 
       
    22 // User includes
       
    23 #include "SCHCLI.H"
       
    24 #include "SchLogger.h"
       
    25 #include "SCHEXEC.H"
       
    26 #include "SCHLOG.h"
       
    27 
       
    28 
       
    29 // Constants
       
    30 #define KEllipsis 0x2026
       
    31 const TInt KTaskArrayGranularity = 1;
       
    32 _LIT(KTaskNameSeparator, ", ");
       
    33 
       
    34 
       
    35 
       
    36 //
       
    37 // CClientProxy
       
    38 //
       
    39 CClientProxy::CClientProxy(RFs& aFsSession, CSchLogManager& aLogManager)
       
    40 :	iFsSession(aFsSession), 
       
    41 	iTasks(CScheduledTask::Offset()),
       
    42 	iSchLogManager(aLogManager)
       
    43 	{
       
    44 	}
       
    45 
       
    46 CClientProxy::CClientProxy(RFs& aFsSession, TInt aPriority, CSchLogManager& aLogManager)
       
    47 :	iFsSession(aFsSession), 
       
    48 	iTasks(CScheduledTask::Offset()),
       
    49 	iSchLogManager(aLogManager)
       
    50 	{
       
    51 	iPriLink.iPriority = aPriority;
       
    52 	}
       
    53 
       
    54 CClientProxy::~CClientProxy()
       
    55 	{
       
    56 	LOGSTRING("CClientProxy::~CClientProxy - start");
       
    57 
       
    58 	TDblQueIter<CScheduledTask> taskIter(iTasks);
       
    59 	taskIter.SetToFirst();
       
    60 	CScheduledTask* task;
       
    61 	while ((task = taskIter++) != NULL)
       
    62 		{
       
    63 		RemoveTask(task);
       
    64 		}
       
    65 	delete iFileName;
       
    66 	LOGSTRING("CClientProxy::~CClientProxy - end");
       
    67 	} 
       
    68 
       
    69 void CClientProxy::ConstructL(const TDesC& aFileName)
       
    70 	{
       
    71 	iFileName = aFileName.AllocL();
       
    72 	}
       
    73 
       
    74 CClientProxy* CClientProxy::NewL(RFs& aFsSession, 
       
    75 								const TDesC& aFileName, 
       
    76 								TInt aPriority,
       
    77 								CSchLogManager& aLogManager)
       
    78 	{
       
    79 	CClientProxy* self = new(ELeave) CClientProxy(aFsSession, aPriority,aLogManager);
       
    80 	CleanupStack::PushL(self);
       
    81 	self->ConstructL(aFileName);
       
    82 	CleanupStack::Pop();
       
    83 	return self;
       
    84 	}
       
    85 
       
    86 CClientProxy* CClientProxy::NewL(RFs& aFsSession, 
       
    87 								RReadStream& aStream,
       
    88 								CSchLogManager& aLogManager)
       
    89 	{
       
    90 	CClientProxy* self = new(ELeave) CClientProxy(aFsSession,aLogManager);
       
    91 	CleanupStack::PushL(self);
       
    92 	aStream >> *self;
       
    93 	CleanupStack::Pop();
       
    94 	return self;
       
    95 	}
       
    96 
       
    97 TBool CClientProxy::IsEqual(const TDesC& aFilename, TInt aPriority) const
       
    98 	{
       
    99 	// Equality here means: same DLL filename, DLL ordinal & priority
       
   100 	return ((*iFileName == aFilename) && (iPriLink.iPriority == aPriority));
       
   101 	}
       
   102 
       
   103 void CClientProxy::AddTask(CScheduledTask& aTask)
       
   104 	{
       
   105 	LOGSTRING("CClientProxy::AddTask - start");
       
   106 	iTasks.Add(aTask);
       
   107 	iUsers++;
       
   108 	LOGSTRING("CClientProxy::AddTask - end");
       
   109 	}
       
   110 
       
   111 void CClientProxy::RemoveTask(CScheduledTask* aTask)
       
   112 	{
       
   113 	LOGSTRING("CClientProxy::RemoveTask - start");
       
   114 	iUsers--;
       
   115 	aTask->Remove();
       
   116 	delete aTask;
       
   117 	LOGSTRING("CClientProxy::RemoveTask - end");
       
   118 	}
       
   119 
       
   120 //
       
   121 static void BuildTaskErrorMessage(const CArrayPtr<CScheduledTask>& aTasks, TDes& aErrorMessage)
       
   122 //
       
   123 //	Establish the names of all the tasks that potentially might fail
       
   124 //	when executing the task. This is done *before* actually executing the
       
   125 //	tasks as its not possible to discern (at failure time) which tasks
       
   126 //	were actually executing (because we don't know at what time the task
       
   127 //	executor was spawned).
       
   128 //
       
   129 	{
       
   130 	const TInt count = aTasks.Count();
       
   131 	for(TInt i=0; i<count; i++)
       
   132 		{
       
   133 		const TDesC& taskName = aTasks.At(i)->Info().iName;
       
   134 		if	(aErrorMessage.Length() + taskName.Length() > KLogMaxSubjectLength - KTaskNameSeparator().Length())
       
   135 			{
       
   136 			// The task name that still needs adding to the subject line (of the
       
   137 			// log entry) is too big to fit within KLogCallEventTypeUid characters.
       
   138 			// Trim the task name so that it will fit, and append the ellipsis
       
   139 			// character to indicate that more tasks were present but unable to be
       
   140 			// shown.
       
   141 			TInt lengthOfTaskNameToAccept = (KLogMaxSubjectLength - aErrorMessage.Length()) - KTaskNameSeparator().Length();
       
   142 			if	(lengthOfTaskNameToAccept <= 0)
       
   143 				break;
       
   144 			aErrorMessage += taskName.Left(lengthOfTaskNameToAccept);
       
   145 			aErrorMessage.Append(KEllipsis);
       
   146 			
       
   147 			// Exit the 'for' loop as there isn't any more room to hold subsequent
       
   148 			// tasks that potentially failed...
       
   149 			break;
       
   150 			}
       
   151 		else
       
   152 			{
       
   153 			aErrorMessage += taskName;
       
   154 			}
       
   155 
       
   156 		if	(i<count-1) // not the last taskName
       
   157 			{
       
   158 			// Append a comma
       
   159 			aErrorMessage += KTaskNameSeparator;
       
   160 			}
       
   161 		}
       
   162 	}
       
   163 	
       
   164 void CClientProxy::ExecuteTasks()
       
   165 	{
       
   166 	// generate an array of due tasks
       
   167 	CArrayPtr<CScheduledTask>* tasks = NULL;
       
   168 	TRAPD(err, tasks = DueTasksL());
       
   169 	if(err)
       
   170 		{
       
   171 		iSchLogManager.LogError(err);
       
   172 		return;
       
   173 		}
       
   174 	
       
   175 	// Create the task error message, just in case an error occurs
       
   176 	TBuf<KLogMaxSubjectLength> errorMessage;
       
   177 	BuildTaskErrorMessage(*tasks, errorMessage);
       
   178 
       
   179 	TInt error =DoExecuteTasks(*tasks, errorMessage);
       
   180 	if(error)
       
   181 		iSchLogManager.LogError(errorMessage,error);
       
   182 	delete tasks;
       
   183 	}
       
   184 
       
   185 TInt CClientProxy::DoExecuteTasks(const CArrayPtr<CScheduledTask>& aTasks,
       
   186 									const TDesC& aErrorMessage)
       
   187 	{
       
   188 	// Generate a unique task name
       
   189 	TTime timenow;
       
   190 	timenow.UniversalTime();
       
   191 	TBuf<24> filename; // 20 is the longest decimal char length of a 64-bit number + 4 for extension
       
   192 	filename.Format(_L("%Ld.tmp"),timenow.Int64());
       
   193 	
       
   194 	// Save & perform execution
       
   195 	TRAPD(error, DoExecuteTasksL(aTasks, filename, aErrorMessage));
       
   196 
       
   197 	if (error != KErrNone)
       
   198 		{
       
   199 		LOGSTRING2("CClientProxy::ExecuteTasksL - attempted to execute tasks, error was %d", error);
       
   200 		// Cleanup the file that was created if the execution fails in any way. Note
       
   201 		// that it might not have been created
       
   202 
       
   203 		// If a debug build - record error
       
   204 		TInt fileDeleteErr=iFsSession.Delete(filename);
       
   205 		if (fileDeleteErr != KErrNone)
       
   206 			{
       
   207 			LOGSTRING2("CClientProxy::DoExecuteTasks - Failed to delete file. Error = %d", fileDeleteErr);
       
   208 			}
       
   209 		}
       
   210 	return error;
       
   211 	}
       
   212 
       
   213 void CClientProxy::DoExecuteTasksL(const CArrayPtr<CScheduledTask>& aTasks, 
       
   214 									const TDesC& aFileName,
       
   215 									const TDesC& aErrorMessage)
       
   216 	{
       
   217 	// Save the tasks to disk
       
   218 	SaveTasksToFileL(aTasks, aFileName);
       
   219 	
       
   220 	// Spawn the task executor active object (deletes itself upon completion)
       
   221 	CTaskExecutor* executor = CTaskExecutor::NewLC(aErrorMessage,aFileName,*iFileName,iSchLogManager);
       
   222 	executor->ExecuteL();
       
   223 	CleanupStack::Pop(executor);
       
   224 	// Don't delete executor here as it does it itself once 
       
   225 	// the task has finished (delete this in its RunL() )
       
   226 	}
       
   227 
       
   228 
       
   229 CArrayPtr<CScheduledTask>* CClientProxy::DueTasksL()
       
   230 //
       
   231 //	Returns a copy of any due tasks
       
   232 //
       
   233 	{
       
   234 	CArrayPtrFlat<CScheduledTask>* tasks = new(ELeave)CArrayPtrFlat<CScheduledTask> (KTaskArrayGranularity);
       
   235 	CleanupStack::PushL(tasks);
       
   236 	TDblQueIter<CScheduledTask> taskIter(iTasks);
       
   237 	taskIter.SetToFirst();
       
   238 	CScheduledTask* task;
       
   239 	while ((task=taskIter++)!=NULL)
       
   240 		{
       
   241 		if (task->Due())
       
   242 			{
       
   243 			tasks->AppendL(task);
       
   244 			}
       
   245 		}
       
   246 	CleanupStack::Pop(tasks);
       
   247 	return tasks;
       
   248 	}
       
   249 	
       
   250 
       
   251 CFileStore* CClientProxy::CreateStoreL(const TDesC& aName,TUint aFileMode)
       
   252 //
       
   253 //	To make the code clearer - called within a trap frame that shouldn't leave anything 
       
   254 //	on cleanup stack.
       
   255 //
       
   256 	{
       
   257 	CFileStore* store = CDirectFileStore::CreateLC(iFsSession, aName, aFileMode);
       
   258 	CleanupStack::Pop(store);
       
   259 	return store;
       
   260 	}
       
   261 
       
   262 void CClientProxy::SaveTasksToFileL(const CArrayPtr<CScheduledTask>& aTasks, const TDesC& aFileName)
       
   263 	{
       
   264 	LOGSTRING("CClientProxy::SaveTasksToFileL - start");
       
   265 
       
   266 	// Create the file store
       
   267 	LOGSTRING("CClientProxy::SaveTasksToFileL - creating store");
       
   268 	CFileStore* store = CreateStoreL(aFileName, EFileWrite);
       
   269 	CleanupStack::PushL(store);
       
   270 
       
   271 	// Save the actual tasks to the store
       
   272 	LOGSTRING("CClientProxy::SaveTasksToFileL - saving tasks");
       
   273 	SaveTasksL(*store, aTasks);
       
   274 	CleanupStack::PopAndDestroy(store);
       
   275 
       
   276 	LOGSTRING("CClientProxy::SaveTasksToFileL - end");
       
   277 	}
       
   278 
       
   279 void CClientProxy::SaveTasksL(CFileStore& aStore, const CArrayPtr<CScheduledTask>& aTasks)
       
   280 	{
       
   281 	LOGSTRING("CClientProxy::SaveTasksL - start");
       
   282 	aStore.SetTypeL(KDirectFileStoreLayoutUid);	
       
   283 
       
   284 	// Save the tasks
       
   285 	RStoreWriteStream outstream;
       
   286 	TStreamId id = outstream.CreateLC(aStore);
       
   287 	const TInt count = aTasks.Count();
       
   288 	LOGSTRING2("CClientProxy::SaveTasksL - saving %d tasks", count);
       
   289 	outstream.WriteInt32L(count);
       
   290 	for (TInt i=0;i<count;i++)
       
   291 		{
       
   292 		CScheduledTask* task = aTasks.At(i);
       
   293 		task->ExternalizeL(outstream);
       
   294 		} 
       
   295 	outstream.CommitL();
       
   296 	CleanupStack::PopAndDestroy(); // outstream
       
   297 
       
   298 	aStore.SetRootL(id);							
       
   299 	aStore.CommitL();							
       
   300 	LOGSTRING("CClientProxy::SaveTasksL - end");
       
   301 	}
       
   302 
       
   303 void CClientProxy::RemoveDueTasks()
       
   304 	{
       
   305 	LOGSTRING("CClientProxy::RemoveDueTasks - start");
       
   306 
       
   307 	TDblQueIter<CScheduledTask> taskIter(iTasks);
       
   308 	taskIter.SetToFirst();
       
   309 	CScheduledTask* task;
       
   310 	//
       
   311 	while ((task=taskIter++)!=NULL)
       
   312 		{
       
   313 		if (task->Due())
       
   314 			{
       
   315 			LOGSTRING3("CClientProxy::RemoveDueTasks - found due task %S, %d", &task->Info().iName, task->Info().iTaskId);
       
   316 
       
   317 			if	(!task->Info().iRepeat)
       
   318 				{
       
   319 				LOGSTRING("CClientProxy::RemoveDueTasks - task has no repeats left - it's being removed");
       
   320 				RemoveTask(task);
       
   321 				}
       
   322 			else
       
   323 				{
       
   324 				LOGSTRING("CClientProxy::RemoveDueTasks - task still has some repeats, setting as 'no longer due'");
       
   325 				task->SetDue(EFalse);
       
   326 				}
       
   327 			}
       
   328 		}
       
   329 	iReadyToExecute = EFalse;
       
   330 	LOGSTRING("CClientProxy::RemoveDueTasks - end");
       
   331 	}
       
   332 
       
   333 void CClientProxy::TransferTasks(CClientProxy& aTargetClient)
       
   334 	{
       
   335 	TDblQueIter<CScheduledTask> taskIter(iTasks);
       
   336 	taskIter.SetToFirst();
       
   337 	CScheduledTask* task;
       
   338 	while ((task=taskIter++)!=NULL)
       
   339 		{
       
   340 		task->Remove();
       
   341 		aTargetClient.AddTask(*task);
       
   342 		}
       
   343 	}
       
   344 
       
   345 void CClientProxy::InternalizeL(RReadStream& aReadStream)
       
   346 	{
       
   347 	LOGSTRING("CClientProxy::InternalizeL - start");
       
   348 
       
   349 	HBufC* fileName = HBufC::NewL(aReadStream, KMaxFileName);
       
   350 	delete iFileName;
       
   351 	iFileName = fileName;
       
   352 	//
       
   353 	iPriLink.iPriority = aReadStream.ReadInt32L();
       
   354 
       
   355 	const TInt count = aReadStream.ReadInt32L();
       
   356 	for(TInt i=0; i<count; i++)
       
   357 		{
       
   358 		CScheduledTask* task = CScheduledTask::NewLC(aReadStream);
       
   359 		AddTask(*task);
       
   360 		CleanupStack::Pop(task);
       
   361 		}
       
   362 
       
   363 	LOGSTRING("CClientProxy::InternalizeL - end");
       
   364 	}
       
   365 
       
   366 void CClientProxy::ExternalizeL(RWriteStream& aWriteStream) const
       
   367 	{
       
   368 	LOGSTRING("CClientProxy::ExternalizeL - start");
       
   369 
       
   370 	aWriteStream << *iFileName;
       
   371 	aWriteStream.WriteInt32L(iPriLink.iPriority);
       
   372 
       
   373 	TDblQueIter<CScheduledTask> taskIter(const_cast<CClientProxy*>(this)->iTasks);
       
   374 	taskIter.SetToFirst();
       
   375 	CScheduledTask* task;
       
   376 
       
   377 	// Count tasks
       
   378 	TInt count = 0;
       
   379 	while ((task=taskIter++)!=NULL)
       
   380 		{
       
   381 		if(task->Persists())
       
   382 			{	
       
   383 			count++;
       
   384 			}
       
   385 		}
       
   386 
       
   387 	// Store leading count
       
   388 	aWriteStream.WriteInt32L(count);
       
   389 
       
   390 	// Store tasks
       
   391 	taskIter.SetToFirst();
       
   392 	while ((task=taskIter++)!=NULL)
       
   393 		{
       
   394 		if(task->Persists())
       
   395 		  {
       
   396 		  aWriteStream << *task;
       
   397 		  }
       
   398 		}
       
   399 
       
   400 	LOGSTRING("CClientProxy::ExternalizeL - end");
       
   401 	}
       
   402 
       
   403 
       
   404 TDblQueIter<CScheduledTask> CClientProxy::TaskIterator()
       
   405 	{
       
   406 	return TDblQueIter<CScheduledTask>(iTasks);
       
   407 	}
       
   408 
       
   409 
       
   410