examples/Base/MessageQueueExample/src/CCollector.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: 
00028 The following class takes user input and sends it to a different 
00029 process called inverter.exe via InverterInQ message Queue. 
00030 */
00031 
00032 
00033 
00034 
00039 #include "CCollector.h"
00040 _LIT(KTitle,"InputMessage");
00041 _LIT(KWelcome," Welcome to Message Queue Example application for the global queues\n");
00042 _LIT(KEnterWords, "\n Enter some words and press enter key to send to inverter or Press Esc to exit.\n");
00043 _LIT(KGlobalInverterInQ, "InverterInQ");
00044 _LIT(KGlobalInverterOutQ, "InverterOutQ");
00045 const TInt KNumberOfMsgs = 10;
00046 
00050 CCollector::CCollector(TInt aPriority):CActive( aPriority )
00051         {       
00052         CActiveScheduler::Add(this);
00053         }
00054 
00055 CCollector::~CCollector()
00056         {
00057         Cancel();
00058         }
00059 
00060 void CCollector::DoCancel()
00061         {
00062         }
00063 
00069 void CCollector::ConstructL()
00070         {
00071         iConsole = Console::NewL(KTitle, TSize(KConsFullScreen, KConsFullScreen));
00072         iConsole->Printf(KWelcome);
00073         iConsole->Printf(KEnterWords);
00074         
00075         //Create global message queue , which is used as input to the inverter and output to the collector.
00076         User::LeaveIfError(iInverterInQ.CreateGlobal(KGlobalInverterInQ, KNumberOfMsgs, EOwnerProcess));
00077                 
00078         //Create global message queue , which is used as output to the inverter and input to the collector.
00079         User::LeaveIfError(iInverterOutQ.CreateGlobal(KGlobalInverterOutQ, KNumberOfMsgs, EOwnerProcess));
00080         
00081         //Start Inverter process.
00082         _LIT(KProcessName, "inverter.exe");
00083         User::LeaveIfError(process.Create(KProcessName, KNullDesC));
00084         process.Resume();
00085         
00086         iRcvInverterInQ= CMsgQActive::NewL(); 
00087         }
00088 
00089 CCollector* CCollector::NewL(TInt aPriority)
00090         {
00091         CCollector* self=new(ELeave)CCollector(aPriority);
00092         CleanupStack::PushL(self);
00093         self->ConstructL();
00094         CleanupStack::Pop(self);
00095         return self;
00096         }
00097 
00101 void CCollector::RequestFunction()
00102         {
00103         iConsole->Read(iStatus);
00104         SetActive();
00105         }
00106 
00110 void CCollector::RunL()
00111         {
00112         //Store the words input by the user in a buffer.
00113         _LIT(KChar,"%c");
00114         iConsole->Printf(KChar,(TUint8)(iConsole->KeyCode()));
00115         iBuf.Append(iConsole->KeyCode());       
00116         ProcessKeyPress(TChar(iConsole->KeyCode()));
00117         }
00118 
00123 void CCollector::ProcessKeyPress(TChar aChar)
00124     {    
00125     if (aChar == EKeyEscape)
00126         {
00127         this->Stop();
00128         }
00129     else if(aChar == EKeyEnter)
00130         {
00131         _LIT(KNextLine , "\n");
00132         iConsole->Printf(KNextLine);
00133         this->SendMsgInQ(iBuf); 
00134         iBuf.Zero();
00135         this->RequestFunction();        
00136         }
00137     else
00138         {
00139         this->RequestFunction();  
00140         }       
00141     }
00142 
00146 void CCollector::Stop()
00147         {
00148     TBuf<1> escape;
00149     escape.Append(EKeyEscape); 
00150     //Sends stop message to the inverter.
00151     this->SendMsgInQ(escape); 
00152     
00153     //Cancel any outstanding request.
00154     iRcvInverterInQ->Cancel();
00155     delete iRcvInverterInQ;
00156     delete iConsole;
00157     CActiveScheduler::Stop();   
00158     escape.Zero();
00159         }
00160 
00165 void CCollector::StartRecieving()
00166         {
00167         iRcvInverterInQ->StartRecieving();
00168         }
00169 
00173 void CCollector::SendMsgInQ(TBuf<100> buf)
00174         {
00175         TInt ret = iInverterInQ.OpenGlobal(KGlobalInverterInQ); 
00176         if (ret == KErrNone)
00177         {
00178         iInverterInQ.SendBlocking(buf);
00179         }
00180         }
00181 
00182 LOCAL_C void DoStartL()
00183         {
00184         // Create active scheduler (to run active objects)
00185         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
00186         CleanupStack::PushL(scheduler);
00187         CActiveScheduler::Install(scheduler);
00188 
00189         CCollector* iCollect= CCollector::NewL();
00190         CleanupStack::PushL(iCollect);
00191         
00192          //Start getting the user input.
00193         iCollect->RequestFunction();
00194         
00195         //Recieve data from the inverter in another console.
00196         iCollect->StartRecieving();
00197         CActiveScheduler::Start();
00198         
00199         CleanupStack::PopAndDestroy(iCollect);
00200         CleanupStack::PopAndDestroy(scheduler); 
00201         }
00202 
00203 GLDEF_C TInt E32Main()
00204         {
00205         __UHEAP_MARK;
00206         CTrapCleanup* cleanup = CTrapCleanup::New();
00207         if(cleanup == NULL)
00208                 {
00209                 return KErrNoMemory;
00210                 }
00211         TRAPD(mainError, DoStartL());
00212         if(mainError != KErrNone)
00213                 {
00214                 _LIT(KUserPanic,"Failed to complete");  
00215                 User::Panic(KUserPanic, mainError);
00216                 }
00217                 delete cleanup;
00218                 __UHEAP_MARKEND;
00219                 return KErrNone;
00220         }
00221 

Generated by  doxygen 1.6.2