00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "CommonFramework.h"
00025
00026
00027
00028
00029 _LIT(KTxtNewLine,"\n");
00030
00031
00032
00033 enum
00034 {
00035 EPanicAlreadyActive=1000,
00036 };
00037
00038
00040
00041
00042
00044 class CTimedMessenger : public CTimer
00045 {
00046 public:
00047
00048 CTimedMessenger();
00049
00050 ~CTimedMessenger();
00051
00052 public:
00053
00054 static CTimedMessenger* NewLC(const TDesC& aGreeting,
00055 TInt aTicksRequested,
00056 TInt aTicksInterval
00057 );
00058 static CTimedMessenger* NewL(const TDesC& aGreeting,
00059 TInt aTicksRequested,
00060 TInt aTicksInterval
00061 );
00062
00063 public:
00064
00065 void ConstructL(const TDesC& aGreeting,
00066 TInt aTicksRequested,
00067 TInt aTicksInterval
00068 );
00069
00070
00071 void IssueRequest();
00072
00073
00074
00075
00076 void DoCancel();
00077
00078
00079
00080
00081 void RunL();
00082
00083 public:
00084
00085 TBufC<20> iGreeting;
00086 TInt iTicksRequested;
00087
00088 TInt iTicksInterval;
00089 TInt iTicksDone;
00090 };
00091
00092
00094
00095
00096
00098 class CActiveConsole;
00099 class CExampleScheduler : public CActiveScheduler
00100 {
00101 public:
00102 void Error (TInt aError) const;
00103 void WaitForAnyRequest();
00104 void SetActiveObject(CActiveConsole* aActiveConsole);
00105 private:
00106
00107 CActiveConsole* iActiveConsole;
00108 };
00109
00110
00112
00113
00114
00115
00116
00118 class CActiveConsole : public CActive
00119 {
00120 public:
00121
00122 CActiveConsole(CConsoleBase* aConsole);
00123 void ConstructL();
00124
00125
00126 ~CActiveConsole();
00127
00128
00129 void RequestCharacter();
00130
00131
00132
00133
00134 void DoCancel();
00135
00136
00137
00138
00139 void RunL();
00140
00141
00142
00143 virtual void ProcessKeyPress(TChar aChar) = 0;
00144
00145 protected:
00146
00147 CConsoleBase* iConsole;
00148 };
00149
00150
00152
00153
00154
00155
00156
00157
00158
00160 class CWriteKeyProcessor : public CActiveConsole
00161 {
00162 public:
00163
00164 CWriteKeyProcessor(CConsoleBase* aConsole);
00165
00166 public:
00167
00168 static CWriteKeyProcessor *NewLC (CConsoleBase* aConsole);
00169 static CWriteKeyProcessor *NewL(CConsoleBase* aConsole);
00170
00171
00172 void ProcessKeyPress(TChar aChar);
00173 };
00174
00175
00177
00178
00179
00181 CTimedMessenger::CTimedMessenger()
00182 : CTimer(CActive::EPriorityStandard)
00183
00184 {};
00185
00186 CTimedMessenger* CTimedMessenger::NewLC(const TDesC& aGreeting,
00187 TInt aTicksRequested,
00188 TInt aTicksInterval
00189 )
00190 {
00191 CTimedMessenger* self=new (ELeave) CTimedMessenger;
00192 CleanupStack::PushL(self);
00193 self->ConstructL(aGreeting,aTicksRequested,aTicksInterval);
00194 return self;
00195 }
00196
00197 CTimedMessenger* CTimedMessenger::NewL(const TDesC& aGreeting,
00198 TInt aTicksRequested,
00199 TInt aTicksInterval
00200 )
00201 {
00202 CTimedMessenger* self = NewLC(aGreeting,aTicksRequested,aTicksInterval);
00203 CleanupStack::Pop();
00204 return self;
00205 }
00206
00207 void CTimedMessenger::ConstructL(const TDesC& aGreeting,
00208 TInt aTicksRequested,
00209 TInt aTicksInterval
00210 )
00211 {
00212
00213 CTimer::ConstructL();
00214
00215 iGreeting = aGreeting;
00216 iTicksRequested = aTicksRequested;
00217 iTicksInterval = aTicksInterval;
00218
00219 CActiveScheduler::Add(this);
00220 }
00221
00222
00223 CTimedMessenger::~CTimedMessenger()
00224 {
00225
00226 Cancel();
00227 }
00228
00229 void CTimedMessenger::DoCancel()
00230 {
00231
00232 CTimer::DoCancel();
00233
00234 iTicksDone = 0;
00235
00236 _LIT(KTxtOutMessReqCancelled,"Outstanding Messenger request cancelled\n");
00237 console->Printf(KTxtOutMessReqCancelled);
00238 }
00239
00240 void CTimedMessenger::IssueRequest()
00241 {
00242
00243 _LIT(KTxtIsAlreadyActive,"Is already Active");
00244 __ASSERT_ALWAYS(!IsActive(),User::Panic(KTxtIsAlreadyActive,EPanicAlreadyActive));
00245
00246 CTimer::After( iTicksInterval*1000000);
00247 }
00248
00249 void CTimedMessenger::RunL()
00250 {
00251
00252
00253 iTicksDone++;
00254
00255 _LIT(KFormat1,"%S \n");
00256 console->Printf(KFormat1,&iGreeting);
00257
00258 if (iTicksDone < iTicksRequested)
00259 {
00260 IssueRequest();
00261 }
00262 else
00263 {
00264 _LIT(KTxtMessengerFinished,"Messenger finished \n");
00265 console->Printf(KTxtMessengerFinished);
00266
00267 iTicksDone=0;
00268
00269 CActiveScheduler::Stop();
00270 }
00271 }
00272
00273
00275
00276
00277
00279 void CExampleScheduler::Error(TInt aError) const
00280 {
00281 _LIT(KTxtSchedulerError,"CExampleScheduler - error");
00282 User::Panic(KTxtSchedulerError,aError);
00283 }
00284
00285
00286 void CExampleScheduler::WaitForAnyRequest()
00287 {
00288 if (!(iActiveConsole->IsActive()))
00289 iActiveConsole->RequestCharacter();
00290 CActiveScheduler::WaitForAnyRequest();
00291 }
00292
00293 void CExampleScheduler::SetActiveObject(CActiveConsole* aActiveConsole)
00294 {
00295 iActiveConsole = aActiveConsole;
00296 }
00297
00298
00300
00301
00302
00304 CActiveConsole::CActiveConsole( CConsoleBase* aConsole)
00305 : CActive(CActive::EPriorityUserInput)
00306
00307 {
00308 iConsole = aConsole;
00309 }
00310
00311 void CActiveConsole::ConstructL()
00312 {
00313
00314 CActiveScheduler::Add(this);
00315 }
00316
00317 CActiveConsole::~CActiveConsole()
00318 {
00319
00320 Cancel();
00321 }
00322
00323 void CActiveConsole::DoCancel()
00324 {
00325 iConsole->ReadCancel();
00326 }
00327
00328 void CActiveConsole::RunL()
00329 {
00330
00331 ProcessKeyPress(TChar(iConsole->KeyCode()));
00332 }
00333
00334 void CActiveConsole::RequestCharacter()
00335 {
00336
00337
00338 iConsole->Read(iStatus);
00339 SetActive();
00340 }
00341
00342
00344
00345
00346
00348 CWriteKeyProcessor::CWriteKeyProcessor(CConsoleBase* aConsole)
00349 : CActiveConsole(aConsole)
00350
00351 {};
00352
00353 CWriteKeyProcessor* CWriteKeyProcessor::NewLC(CConsoleBase* aConsole)
00354 {
00355 CWriteKeyProcessor* self=new (ELeave) CWriteKeyProcessor(aConsole);
00356 CleanupStack::PushL(self);
00357 self->ConstructL();
00358 return self;
00359 }
00360
00361 CWriteKeyProcessor* CWriteKeyProcessor::NewL(CConsoleBase* aConsole)
00362 {
00363 CWriteKeyProcessor* self=NewLC(aConsole);
00364 CleanupStack::Pop();
00365 return self;
00366 }
00367
00368 void CWriteKeyProcessor::ProcessKeyPress(TChar aChar)
00369 {
00370
00371 if (aChar == EKeyEscape)
00372 {
00373 iConsole->Printf(KTxtNewLine);
00374 CActiveScheduler::Stop();
00375 return;
00376 }
00377
00378
00379
00380
00381 if (aChar == EKeyEnter)
00382 iConsole->Printf(KTxtNewLine);
00383 else
00384 {
00385 _LIT(KFormat2,"%c");
00386 _LIT(KFormat3,"%d");
00387 if (aChar.IsAlphaDigit()|| aChar.IsSpace())
00388
00389 iConsole->Printf(KFormat2,TUint(aChar));
00390 else
00391 iConsole->Printf(KFormat3,TUint(aChar));
00392 }
00393
00394
00395 RequestCharacter();
00396 }
00397
00398
00400
00401
00402
00403
00404
00405
00406
00407
00408
00410 class CMessageKeyProcessor : public CActiveConsole
00411 {
00412 public:
00413
00414 CMessageKeyProcessor(CConsoleBase* aConsole, CTimedMessenger* iMessenger);
00415 void ConstructL();
00416
00417 public:
00418
00419 static CMessageKeyProcessor* NewLC(CConsoleBase* aConsole,
00420 CTimedMessenger* iMessenger
00421 );
00422 static CMessageKeyProcessor* NewL(CConsoleBase* aConsole,
00423 CTimedMessenger* iMessenger
00424 );
00425
00426 public:
00427
00428 void ProcessKeyPress(TChar aChar);
00429
00430 private:
00431
00432 CTimedMessenger* iMessenger;
00433 };
00434
00436
00437
00438
00440 CMessageKeyProcessor::CMessageKeyProcessor(CConsoleBase* aConsole,
00441 CTimedMessenger* aMessenger
00442 )
00443 : CActiveConsole(aConsole)
00444
00445 {
00446 iMessenger = aMessenger;
00447 }
00448
00449 CMessageKeyProcessor* CMessageKeyProcessor::NewLC(CConsoleBase* aConsole,
00450 CTimedMessenger* aMessenger
00451 )
00452 {
00453 CMessageKeyProcessor* self=new (ELeave) CMessageKeyProcessor(aConsole,
00454 aMessenger
00455 );
00456 CleanupStack::PushL(self);
00457 self->ConstructL();
00458 return self;
00459 }
00460
00461 CMessageKeyProcessor* CMessageKeyProcessor::NewL(CConsoleBase* aConsole,
00462 CTimedMessenger* aMessenger
00463 )
00464 {
00465 CMessageKeyProcessor* self = NewLC(aConsole, aMessenger);
00466 CleanupStack::Pop();
00467 return self;
00468 }
00469
00470 void CMessageKeyProcessor::ConstructL()
00471 {
00472
00473 CActiveScheduler::Add(this);
00474
00475 ((CExampleScheduler*)(CActiveScheduler::Current()))->SetActiveObject(this);
00476 }
00477
00478 void CMessageKeyProcessor::ProcessKeyPress(TChar aChar)
00479 {
00480
00481
00482
00483 if (aChar == EKeyEscape)
00484 {
00485 iMessenger->Cancel();
00486 CActiveScheduler::Stop();
00487 return;
00488 }
00489
00490
00491
00492
00493
00494 if (aChar == 'm' || aChar == 'M')
00495 {
00496 _LIT(KTxtStartingMess,"Starting Messenger.... \n");
00497 iConsole->Printf(KTxtStartingMess);
00498 iMessenger->Cancel();
00499 iMessenger ->IssueRequest();
00500 }
00501
00502
00503
00504 if (aChar == 'c' || aChar == 'C')
00505 iMessenger->Cancel();
00506 }
00507
00508
00510
00511
00512
00514 LOCAL_C void doExampleL()
00515 {
00516
00517 CExampleScheduler* exampleScheduler = new (ELeave) CExampleScheduler;
00518
00519
00520 CleanupStack::PushL(exampleScheduler);
00521
00522
00523 CActiveScheduler::Install(exampleScheduler);
00524
00525
00526
00527 _LIT(KTxtGoodMorning,"Good Morning!");
00528 CTimedMessenger* messenger = CTimedMessenger::NewLC(KTxtGoodMorning, 3, 2);
00529
00530
00531 _LIT(KTxtMain1,"A single CKeyMessengerProcessor active object ");
00532 _LIT(KTxtMain2,"Active scheduler renews request.\n");
00533 _LIT(KTxtMain3,"Press 'm' to activate messenger; Press 'c' to cancel it.\n");
00534 _LIT(KTxtMain4,"Press ESC to end.\n\n");
00535 console->Printf(KTxtMain1);
00536 console->Printf(KTxtMain2);
00537 console->Printf(KTxtMain3);
00538 console->Printf(KTxtMain4);
00539 CMessageKeyProcessor::NewLC(console,messenger);
00540
00541
00542 CActiveScheduler::Start();
00543
00544
00545
00546
00547
00548 CleanupStack::PopAndDestroy(3);
00549 }
00550
00551
00552