00001 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). 00002 // All rights reserved. 00003 // This component and the accompanying materials are made available 00004 // under the terms of "Eclipse Public License v1.0" 00005 // which accompanies this distribution, and is available 00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". 00007 // 00008 // Initial Contributors: 00009 // Nokia Corporation - initial contribution. 00010 // 00011 // Contributors: 00012 // 00013 // Description: 00014 // This is simple example code that demonstrates the use of task scheduler. 00015 // The code demonstrates how to, 00016 // 1) Connect and register with the task scheduler 00017 // 2) Create a persistent schedule 00018 // 3) Add tasks to it 00019 // 4) Execute the schedule 00020 // 5) Delete the schedules and the tasks associated with them after 00021 // verifying that they are the schedules created by us. 00022 // 6) Create a transient schedule 00023 // 7) Add task to the transient schedule, edit the schedule and execute it 00024 // 00025 00026 00027 00031 #include "taskscheduler.h" 00032 #include <e32std.h> 00033 #include <schinfo.h> 00034 #include <e32base.h> 00035 00036 _LIT(KTaskName,"MyTaskName\n"); 00037 00038 _LIT(KTitle, "Task Scheduler example"); 00039 _LIT(KTextPressAKey, "\nPress any key to step through the example\n"); 00040 _LIT(KExit,"\nPress any key to exit the application"); 00041 _LIT(KPressAKey,"\nPress any key to continue\n"); 00042 00043 _LIT(KConnect, "\nConnecting the client to the task scheduler server"); 00044 _LIT(KRegisterClient,"\nRegistering the client\n"); 00045 _LIT(KCreateSchedule,"Creating a persistent schedule\n"); 00046 _LIT(KCreateTask,"Creating task for the schedule\n"); 00047 00048 _LIT(KPersistentWait,"Waiting for the persistent task to complete. This will take 20 seconds\n"); 00049 _LIT(KDone,"Task complete \n"); 00050 _LIT(KTransientSchedule,"A transient schedule"); 00051 _LIT(KTransientWait,"Waiting for the transient task to complete. This will take 20 seconds\n"); 00052 00053 _LIT(KCreateTransient,"\nCreating a transient schedule with non-repeating task\n"); 00054 _LIT(KDeleteAllTasks,"\nDeleting all the persistent tasks scheduled by this exe"); 00055 _LIT(KDeleteAllSchedules,"\nDeleting all schedules created by this exe\n"); 00056 _LIT(KTask, "Number of task(s) scheduled by us is(are) %d\n"); 00057 _LIT(KExists, "The tasks scheduled exist\n"); 00058 _LIT(KOtherTask,"Error! Unexpected schedules not scheduled by this exe exist\n"); 00059 00064 CTaskSchedule* CTaskSchedule::NewLC() 00065 { 00066 CTaskSchedule* schedule = new(ELeave) CTaskSchedule(); 00067 CleanupStack::PushL(schedule); 00068 schedule->ConstructL(); 00069 return schedule; 00070 } 00071 00075 CTaskSchedule::CTaskSchedule() 00076 { 00077 } 00078 00079 void CTaskSchedule::ConstructL() 00080 { 00081 iConsole = Console::NewL(KTitle,TSize(KConsFullScreen,KConsFullScreen)); 00082 iConsole->Printf (KTextPressAKey); 00083 iConsole->Getch (); 00084 } 00085 00089 CTaskSchedule::~CTaskSchedule() 00090 { 00091 iScheduler.Close(); 00092 iConsole->Printf(KExit); 00093 iConsole->Getch(); 00094 00095 delete iConsole; 00096 } 00097 00104 void CTaskSchedule::ConnectAndRegisterL() 00105 { 00106 00107 // Connect to the scheduler server 00108 iConsole->Printf(KConnect); 00109 User::LeaveIfError(iScheduler.Connect()); 00110 00111 _LIT(KTaskExec,"Taskexecutor"); 00112 TFileName filename; 00113 filename.Append(KTaskExec); 00114 00115 // Register with the scheduler 00116 iConsole->Printf(KRegisterClient); 00117 00118 // A priority value 00119 const TInt priority = 3; 00120 User::LeaveIfError(iScheduler.Register(filename, priority)); 00121 00122 } 00123 00130 void CTaskSchedule::PersistentScheduleL() 00131 { 00132 00133 iConsole->Printf(KCreateSchedule); 00134 TSchedulerItemRef scheduleHandle; 00135 TTime time; 00136 time.UniversalTime(); 00137 00138 // Assign an offset of 20 seconds. TTimeIntervalMinutes, 00139 // TTimeIntervalHours etc can be used for longer offsets. 00140 time += TTimeIntervalSeconds(20);// 20 secs in future 00141 00142 TTsTime time2 (time, ETrue); 00143 00144 iConsole->Printf(KCreateTask); 00145 CreatePersistentScheduleL(scheduleHandle,time2); 00146 00147 // Create the task to be scheduled 00148 TTaskInfo taskInfo; 00149 00150 const TInt priority = 2; 00151 const TInt numberOfRepeats = 2; 00152 // Name of the task 00153 taskInfo.iName = KTaskName; 00154 00155 // Task priority set by the client. 00156 // If the client has two tasks with different priorities, 00157 // the task with the higher priority will be executed first. 00158 taskInfo.iPriority = priority; 00159 00160 // Task repeats twice 00161 taskInfo.iRepeat = numberOfRepeats; 00162 00163 _LIT(KScheduleType," persistent schedule"); 00164 const TDesC* persistent = &KScheduleType; 00165 HBufC* data = persistent->AllocLC(); 00166 00167 // Add the task 00168 User::LeaveIfError(iScheduler.ScheduleTask(taskInfo, *data, scheduleHandle.iHandle)); 00169 00170 // Wait for the entry to fire 00171 iConsole->Printf(KPersistentWait); 00172 const TInt twentySecs = 20000000; 00173 User::After(twentySecs); // pause for 20 seconds 00174 00175 // Delete all the persistent schedules created 00176 DeleteSchedulesL(scheduleHandle, EAllSchedules); 00177 00178 CleanupStack::PopAndDestroy(1); // data 00179 00180 } 00181 00182 00197 void CTaskSchedule::CreatePersistentScheduleL(TSchedulerItemRef& aRef, const TTsTime& aStartTime) 00198 { 00199 CArrayFixFlat<TScheduleEntryInfo2>* cSchEntryInfoArray; 00200 cSchEntryInfoArray = new CArrayFixFlat<TScheduleEntryInfo2>(1); 00201 CleanupStack::PushL(cSchEntryInfoArray); 00202 00203 // Create an hourly schedule with StartTime of aStartTime 00204 TScheduleEntryInfo2 entry1; 00205 00206 // Set the first instance when the entry will 00207 // cause the execution of tasks. 00208 entry1.SetStartTime(aStartTime); 00209 00210 // Set the type of interval used between due times 00211 // for the scheduled entry 00212 entry1.SetIntervalType(TIntervalType(EHourly)); 00213 00214 // Set the period for which the entry is valid. After 8 hours the 00215 // tasks associated with the entry will not be eligible for execution. 00216 const TInt eightHours = 480; // in minutes 00217 entry1.SetValidityPeriod(TTimeIntervalMinutes (eightHours)); 00218 00219 // Set the interval between execution of tasks 00220 // Here the interval is 1 hour because the interval type is hourly. 00221 const TInt interval = 1; 00222 entry1.SetInterval(interval); 00223 00224 cSchEntryInfoArray->AppendL(entry1); 00225 00226 User::LeaveIfError(iScheduler.CreatePersistentSchedule(aRef, *cSchEntryInfoArray)); 00227 CleanupStack::PopAndDestroy(cSchEntryInfoArray); 00228 00229 } 00230 00231 00240 void CTaskSchedule::CreateTransientScheduleL() 00241 { 00242 iConsole->ClearScreen(); 00243 00244 TSchedulerItemRef ref; 00245 CArrayFixFlat<TScheduleEntryInfo2>* cSchEntryInfoArray; 00246 cSchEntryInfoArray = new CArrayFixFlat<TScheduleEntryInfo2>(1); 00247 00248 CleanupStack::PushL(cSchEntryInfoArray); 00249 ref.iName = KTransientSchedule; 00250 iConsole->Printf(KCreateTransient); 00251 00252 // Create a schedule entry 00253 TScheduleEntryInfo2 entry; 00254 TTime now; 00255 00256 // Set the date and time of this TTime to the universal time. 00257 now.UniversalTime(); 00258 00259 // Assign an offset of 15 seconds. TTimeIntervalMinutes, 00260 // TTimeIntervalHours etc can be used for longer offsets. 00261 TInt offset = 15; 00262 now += TTimeIntervalSeconds(offset); 00263 00264 // Constructs a TTsTime with a TTime object. 00265 // ETrue indicates that TTsTime is UTC based time 00266 TTsTime time (now, ETrue); 00267 00268 // Set the above time as the first time at which 00269 // the entry will cause execution of tasks. 00270 entry.SetStartTime(time); 00271 00272 // Set the type of interval used between due times for scheduled entry 00273 entry.SetIntervalType(TIntervalType(EHourly)); 00274 00275 // Set the period for which the entry is valid. 00276 // After 2 hours the tasks associated with the entry will not be eligible for execution 00277 TInt validity = 120; 00278 entry.SetValidityPeriod(TTimeIntervalMinutes (validity)); 00279 00280 // Set the interval between execution of tasks 00281 // Here the interval is 1 hour because the interval type is hourly. 00282 entry.SetInterval(1); 00283 cSchEntryInfoArray->AppendL(entry); 00284 00285 // Create a transient task to be scheduled 00286 TTaskInfo taskInfo; 00287 // Name of the task 00288 taskInfo.iName = KTransientSchedule; 00289 // Task id 00290 const TInt tId = 0; 00291 taskInfo.iTaskId = tId; 00292 00293 // The task repeats just once 00294 const TInt numberOfRepeats = 1; 00295 taskInfo.iRepeat = numberOfRepeats; 00296 00297 // Task priority set by the client. 00298 // Where a client has two tasks with different priorities, 00299 // the task with the higher priority will be executed first. 00300 const TInt priority = 2; 00301 taskInfo.iPriority = priority; 00302 00303 _LIT(KScheduleType," transient schedule"); 00304 const TDesC* transient = &KScheduleType; 00305 HBufC* data = transient->AllocLC(); 00306 00307 // Schedule the item 00308 User::LeaveIfError(iScheduler.ScheduleTask(taskInfo, *data, ref, *cSchEntryInfoArray)); 00309 00310 // Change the start time and validity duration for the schedule 00311 offset = 19; // 19 seconds in future 00312 now += TTimeIntervalSeconds(offset); 00313 TTsTime newTime (now, ETrue); 00314 entry.SetStartTime(newTime); 00315 validity = 300; 00316 entry.SetValidityPeriod(TTimeIntervalMinutes (validity)); 00317 cSchEntryInfoArray->AppendL(entry); 00318 00319 // Change the transient time based schedule 00320 User::LeaveIfError(iScheduler.EditSchedule(ref.iHandle, *cSchEntryInfoArray)); 00321 00322 CleanupStack::PopAndDestroy(2); // data, CSchEntryInfoArray 00323 00324 // Check if the tasks scheduled exist 00325 TBool exists; 00326 DoesScheduledItemExistL(ref, exists); 00327 if(!exists) 00328 { 00329 User::Leave(KErrNotFound); 00330 } 00331 // Wait for the task to fire and complete 00332 iConsole->Printf(KTransientWait); 00333 const TInt twentySecs = 20000000; 00334 User::After(twentySecs); // pause for 20 seconds 00335 00336 iConsole->Printf(KDone); 00337 00338 // Transient schedules are deleted automatically once they are executed 00339 // i.e check for count to be zero in the function being called 00340 DoesScheduledItemExistL(ref, exists); 00341 if(exists) 00342 { 00343 User::Leave(KErrGeneral); 00344 } 00345 } 00346 00353 void CTaskSchedule::DoesScheduledItemExistL(TSchedulerItemRef &aRef, TBool& aExists) 00354 // Extract schedule references from the schedule server based on a filter. 00355 { 00356 aExists = EFalse; 00357 CArrayFixFlat<TSchedulerItemRef>* CSchItemRefArray; 00358 00359 CSchItemRefArray = new CArrayFixFlat<TSchedulerItemRef>(3); 00360 CleanupStack::PushL(CSchItemRefArray); 00361 00362 User::LeaveIfError(iScheduler.GetScheduleRefsL(*CSchItemRefArray, EAllSchedules)); 00363 00364 TInt count = CSchItemRefArray->Count(); 00365 for(TInt i = 0; i < count; i++) 00366 { 00367 // Get a list of schedules created by this executable 00368 if(aRef.iHandle == (*CSchItemRefArray)[i].iHandle) 00369 { 00370 aExists = ETrue; 00371 iConsole->Printf(KExists); 00372 iConsole->Printf(KTask, count); 00373 } 00374 else 00375 { 00376 iConsole->Printf(KOtherTask); 00377 } 00378 } 00379 00380 CleanupStack::PopAndDestroy(); // CSchItemRefArray 00381 00382 } 00383 00395 void CTaskSchedule::DeleteSchedulesL(TSchedulerItemRef &aRef, TScheduleFilter aFilter) 00396 { 00397 CArrayFixFlat<TSchedulerItemRef>* CSchItemRefArray; 00398 00399 CSchItemRefArray = new CArrayFixFlat<TSchedulerItemRef>(3); 00400 CleanupStack::PushL(CSchItemRefArray); 00401 00402 User::LeaveIfError(iScheduler.GetScheduleRefsL(*CSchItemRefArray, aFilter)); 00403 00404 TInt count = CSchItemRefArray->Count(); 00405 iConsole->Printf(KTask, count); 00406 00407 for(TInt i = 0; i < count; i++) 00408 { 00409 // Check if the schedules are created by this exe or 00410 // some other exe 00411 if(aRef.iHandle == (*CSchItemRefArray)[i].iHandle) 00412 { 00413 // Delete the tasks scheduled by us (this exe) 00414 iConsole->Printf(KDeleteAllTasks); 00415 User::LeaveIfError(iScheduler.DeleteTask(aRef.iHandle)); 00416 // Delete the schedules created by us (this exe) 00417 iConsole->Printf(KDeleteAllSchedules); 00418 User::LeaveIfError(iScheduler.DeleteSchedule(aRef.iHandle)); 00419 00420 iConsole->Printf(KPressAKey); 00421 iConsole->Getch(); 00422 00423 } 00424 else 00425 { 00426 // Not deleting the tasks or schedules as, 00427 // they are not created by this exe 00428 iConsole->Printf(KOtherTask); 00429 } 00430 } 00431 CleanupStack::PopAndDestroy(); // CSchItemRefArray 00432 00433 } 00434 00435 void MainL() 00436 { 00437 // Create an Active Scheduler to handle asychronous calls 00438 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler; 00439 CleanupStack::PushL(scheduler); 00440 00441 // Install the active scheduler 00442 CActiveScheduler::Install( scheduler ); 00443 CTaskSchedule* app = CTaskSchedule::NewLC(); 00444 00445 // Connect and register with the task scheduler 00446 app->ConnectAndRegisterL(); 00447 00448 // Create a persistent schedule and add tasks to it 00449 app->PersistentScheduleL(); 00450 00451 // Create a transient schedule and add tasks to it 00452 app->CreateTransientScheduleL(); 00453 00454 CleanupStack::PopAndDestroy(2); // app, scheduler 00455 00456 } 00457 00458 TInt E32Main() 00459 { 00460 __UHEAP_MARK; 00461 CTrapCleanup* cleanup = CTrapCleanup::New(); 00462 if(cleanup == NULL) 00463 { 00464 return KErrNoMemory; 00465 } 00466 00467 TRAPD(err, MainL()); 00468 if(err != KErrNone) 00469 { 00470 User::Panic(_L("Failed to complete"),err); 00471 } 00472 delete cleanup; 00473 00474 __UHEAP_MARKEND; 00475 return KErrNone; 00476 }
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.