00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00108 iConsole->Printf(KConnect);
00109 User::LeaveIfError(iScheduler.Connect());
00110
00111 _LIT(KTaskExec,"Taskexecutor");
00112 TFileName filename;
00113 filename.Append(KTaskExec);
00114
00115
00116 iConsole->Printf(KRegisterClient);
00117
00118
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
00139
00140 time += TTimeIntervalSeconds(20);
00141
00142 TTsTime time2 (time, ETrue);
00143
00144 iConsole->Printf(KCreateTask);
00145 CreatePersistentScheduleL(scheduleHandle,time2);
00146
00147
00148 TTaskInfo taskInfo;
00149
00150 const TInt priority = 2;
00151 const TInt numberOfRepeats = 2;
00152
00153 taskInfo.iName = KTaskName;
00154
00155
00156
00157
00158 taskInfo.iPriority = priority;
00159
00160
00161 taskInfo.iRepeat = numberOfRepeats;
00162
00163 _LIT(KScheduleType," persistent schedule");
00164 const TDesC* persistent = &KScheduleType;
00165 HBufC* data = persistent->AllocLC();
00166
00167
00168 User::LeaveIfError(iScheduler.ScheduleTask(taskInfo, *data, scheduleHandle.iHandle));
00169
00170
00171 iConsole->Printf(KPersistentWait);
00172 const TInt twentySecs = 20000000;
00173 User::After(twentySecs);
00174
00175
00176 DeleteSchedulesL(scheduleHandle, EAllSchedules);
00177
00178 CleanupStack::PopAndDestroy(1);
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
00204 TScheduleEntryInfo2 entry1;
00205
00206
00207
00208 entry1.SetStartTime(aStartTime);
00209
00210
00211
00212 entry1.SetIntervalType(TIntervalType(EHourly));
00213
00214
00215
00216 const TInt eightHours = 480;
00217 entry1.SetValidityPeriod(TTimeIntervalMinutes (eightHours));
00218
00219
00220
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
00253 TScheduleEntryInfo2 entry;
00254 TTime now;
00255
00256
00257 now.UniversalTime();
00258
00259
00260
00261 TInt offset = 15;
00262 now += TTimeIntervalSeconds(offset);
00263
00264
00265
00266 TTsTime time (now, ETrue);
00267
00268
00269
00270 entry.SetStartTime(time);
00271
00272
00273 entry.SetIntervalType(TIntervalType(EHourly));
00274
00275
00276
00277 TInt validity = 120;
00278 entry.SetValidityPeriod(TTimeIntervalMinutes (validity));
00279
00280
00281
00282 entry.SetInterval(1);
00283 cSchEntryInfoArray->AppendL(entry);
00284
00285
00286 TTaskInfo taskInfo;
00287
00288 taskInfo.iName = KTransientSchedule;
00289
00290 const TInt tId = 0;
00291 taskInfo.iTaskId = tId;
00292
00293
00294 const TInt numberOfRepeats = 1;
00295 taskInfo.iRepeat = numberOfRepeats;
00296
00297
00298
00299
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
00308 User::LeaveIfError(iScheduler.ScheduleTask(taskInfo, *data, ref, *cSchEntryInfoArray));
00309
00310
00311 offset = 19;
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
00320 User::LeaveIfError(iScheduler.EditSchedule(ref.iHandle, *cSchEntryInfoArray));
00321
00322 CleanupStack::PopAndDestroy(2);
00323
00324
00325 TBool exists;
00326 DoesScheduledItemExistL(ref, exists);
00327 if(!exists)
00328 {
00329 User::Leave(KErrNotFound);
00330 }
00331
00332 iConsole->Printf(KTransientWait);
00333 const TInt twentySecs = 20000000;
00334 User::After(twentySecs);
00335
00336 iConsole->Printf(KDone);
00337
00338
00339
00340 DoesScheduledItemExistL(ref, exists);
00341 if(exists)
00342 {
00343 User::Leave(KErrGeneral);
00344 }
00345 }
00346
00353 void CTaskSchedule::DoesScheduledItemExistL(TSchedulerItemRef &aRef, TBool& aExists)
00354
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
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();
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
00410
00411 if(aRef.iHandle == (*CSchItemRefArray)[i].iHandle)
00412 {
00413
00414 iConsole->Printf(KDeleteAllTasks);
00415 User::LeaveIfError(iScheduler.DeleteTask(aRef.iHandle));
00416
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
00427
00428 iConsole->Printf(KOtherTask);
00429 }
00430 }
00431 CleanupStack::PopAndDestroy();
00432
00433 }
00434
00435 void MainL()
00436 {
00437
00438 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
00439 CleanupStack::PushL(scheduler);
00440
00441
00442 CActiveScheduler::Install( scheduler );
00443 CTaskSchedule* app = CTaskSchedule::NewLC();
00444
00445
00446 app->ConnectAndRegisterL();
00447
00448
00449 app->PersistentScheduleL();
00450
00451
00452 app->CreateTransientScheduleL();
00453
00454 CleanupStack::PopAndDestroy(2);
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 }