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