examples/Base/MessageQueueExample/Inverter/src/CInverter.cpp

Go to the documentation of this file.
00001 // Copyright (c) 2008-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 // The following class describes the Inverter class which recieves input 
00015 // through InverterInq and sends it to InverterOutQ after inverting the recieved words.
00016 // It implements a periodic timer which opens the InverterInq every 10 seconds.
00017 //
00018 
00019 
00020 
00025 #include "CInverter.h"
00026 #include "e32keys.h"     // for EKeyEscape.
00027 
00028 //Periodic timer interval of 10 seconds.
00029 const TInt KPeriodicTimerInterval10Sec(10000000);
00030 
00031 //Error code to indicate Stop command.
00032 const TInt KErrStop = -100; 
00033 
00037 CInverter::~CInverter()
00038         {
00039     if(iTimer)
00040         {
00041         // Calling Cancel without checking if the timer is active is safe
00042         iTimer->Cancel();
00043         }
00044     delete iTimer;
00045         iInMsgQ.Close();
00046         iOutMsgQ.Close();
00047         }
00048 
00049 CInverter* CInverter::NewL()
00050         {
00051         CInverter* self=new(ELeave)CInverter();
00052         CleanupStack::PushL(self);
00053         self->ConstructL();
00054         CleanupStack::Pop(self);
00055         return self;
00056         }
00060 void CInverter::ConstructL()
00061     {
00062     // Initialize the periodic timer.
00063     iTimer = CPeriodic::NewL(CActive::EPriorityIdle);
00064     // Start the periodic timer, when ever the time elapsed
00065     // the StaticWake() will get called.
00066 
00067     iTimer->Start(KPeriodicTimerInterval10Sec, KPeriodicTimerInterval10Sec,TCallBack(StaticWake, this));
00068     
00069     }
00070 
00075 TInt CInverter::StaticWake(TAny* aAny)
00076         {
00077         CInverter* self = static_cast<CInverter*>( aAny );
00078         TInt ret = self->RecieveMsg();
00079         if(ret!= KErrStop) //KErrStop indicates that system has to be stopped and no more messages to be sent.
00080         self->SendMsg();
00081         return KErrNone; // Return value ignored by CPeriodic
00082         }
00083 
00090 TInt CInverter::RecieveMsg()
00091     {
00092 
00093     //Opens the message queue input to the inverter.
00094     _LIT(KGlobalInverterInQ, "InverterInQ");
00095     TInt ret = iInMsgQ.OpenGlobal(KGlobalInverterInQ); 
00096         
00097         if (ret == KErrNone)
00098         {
00099         //Recieve the message coming from the first process.
00100         iMsgQData.Zero();
00101         iInMsgQ.ReceiveBlocking(iMsgQData);
00102         
00103         //If the recieved data indicates stop command, stop the system.
00104         if(iMsgQData[0]==EKeyEscape)
00105                         {
00106                         this->Stop();
00107                         return KErrStop;
00108                         }
00109         }
00110         return KErrNone;
00111     }
00112 
00116 void CInverter::SendMsg()
00117         {
00118         //Opens the global message Queue named InverterOutQ.
00119         _LIT(KGlobalInverterOutQ, "InverterOutQ");
00120         TInt ret = iOutMsgQ.OpenGlobal(KGlobalInverterOutQ); 
00121         
00122         //Sends the inverted words to the opened message queue.
00123         if (ret == KErrNone)
00124         {
00125         DoInvert();
00126         iOutMsgQ.SendBlocking(idestMsg);
00127         }
00128         }
00129 
00133 void CInverter::DoInvert()
00134 {
00135 idestMsg.Zero();
00136 TInt i=0; 
00137 while(iMsgQData[i]!= '\r')
00138 {
00139         while((iMsgQData[i]!= ' ')&&(iMsgQData[i]!= '\r'))
00140         {
00141         iwords.Append(iMsgQData[i]);
00142         i++; 
00143         }
00144         ReverseWord(iwords);
00145         idestMsg.Append(itmpWord); 
00146         idestMsg.Append(' ');
00147         if(iMsgQData[i]== '\r')
00148         {
00149         i--;
00150         }
00151         i++;
00152         iwords.Zero();
00153     }
00154 }
00155 
00159 void CInverter::ReverseWord(TBuf<20> buf)
00160         {
00161         itmpWord.Zero();
00162         TInt size = (buf.Size())/2;
00163         for(int i=1; i<= size; i++)
00164                 {
00165                 itmpWord.Append(buf[size -i]);
00166                 }
00167         }
00168 
00173 void CInverter::Stop()
00174         {
00175         iTimer->Cancel();
00176         CActiveScheduler::Stop();
00177         }
00178 
00179 LOCAL_C void DoStartL()
00180         {
00181 
00182         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
00183         CleanupStack::PushL(scheduler);
00184         CActiveScheduler::Install(scheduler);   
00185         
00186         CInverter* invert = CInverter::NewL();
00187         CleanupStack::PushL(invert);
00188         
00189         //Start the scheduler for the periodic timer
00190         CActiveScheduler::Start();
00191         
00192         CleanupStack::PopAndDestroy(invert);
00193         CleanupStack::PopAndDestroy(scheduler);
00194         }
00195 
00196 GLDEF_C TInt E32Main()
00197         {
00198         __UHEAP_MARK;
00199         CTrapCleanup* cleanup = CTrapCleanup::New();
00200         if(cleanup == NULL)
00201                 {
00202                 return KErrNoMemory;
00203                 }
00204         
00205 
00206         TRAPD(mainError, DoStartL());
00207         if(mainError != KErrNone)
00208                 {
00209                 _LIT(KUserPanic,"Failed to complete");  
00210                 User::Panic(KUserPanic, mainError);
00211                 }
00212 
00213                 delete cleanup;
00214                 __UHEAP_MARKEND;
00215                 return KErrNone;
00216         }

Generated by  doxygen 1.6.2