# examples/Base/MessageQueueExample/src/CCollector.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 takes user input and sends it to a different
    00015 // process called inverter.exe via InverterInQ message Queue.
    00016 //
    00017
    00018
    00019
    00024 #include "CCollector.h"
    00025 _LIT(KTitle,"InputMessage");
    00026 _LIT(KWelcome," Welcome to Message Queue Example application for the global queues\n");
    00027 _LIT(KEnterWords, "\n Enter some words and press enter key to send to inverter or Press Esc to exit.\n");
    00028 _LIT(KGlobalInverterInQ, "InverterInQ");
    00029 _LIT(KGlobalInverterOutQ, "InverterOutQ");
    00030 const TInt KNumberOfMsgs = 10;
    00031
    00035 CCollector::CCollector(TInt aPriority):CActive( aPriority )
    00036         {
    00037         CActiveScheduler::Add(this);
    00038         }
    00039
    00040 CCollector::~CCollector()
    00041         {
    00042         Cancel();
    00043         }
    00044
    00045 void CCollector::DoCancel()
    00046         {
    00047         }
    00048
    00054 void CCollector::ConstructL()
    00055         {
    00056         iConsole = Console::NewL(KTitle, TSize(KConsFullScreen, KConsFullScreen));
    00057         iConsole->Printf(KWelcome);
    00058         iConsole->Printf(KEnterWords);
    00059
    00060         //Create global message queue , which is used as input to the inverter and output to the collector.
    00061         User::LeaveIfError(iInverterInQ.CreateGlobal(KGlobalInverterInQ, KNumberOfMsgs, EOwnerProcess));
    00062
    00063         //Create global message queue , which is used as output to the inverter and input to the collector.
    00064         User::LeaveIfError(iInverterOutQ.CreateGlobal(KGlobalInverterOutQ, KNumberOfMsgs, EOwnerProcess));
    00065
    00066         //Start Inverter process.
    00067         _LIT(KProcessName, "inverter.exe");
    00068         User::LeaveIfError(process.Create(KProcessName, KNullDesC));
    00069         process.Resume();
    00070
    00071         iRcvInverterInQ= CMsgQActive::NewL();
    00072         }
    00073
    00074 CCollector* CCollector::NewL(TInt aPriority)
    00075         {
    00076         CCollector* self=new(ELeave)CCollector(aPriority);
    00077         CleanupStack::PushL(self);
    00078         self->ConstructL();
    00079         CleanupStack::Pop(self);
    00080         return self;
    00081         }
    00082
    00086 void CCollector::RequestFunction()
    00087         {
    00088         iConsole->Read(iStatus);
    00089         SetActive();
    00090         }
    00091
    00095 void CCollector::RunL()
    00096         {
    00097         //Store the words input by the user in a buffer.
    00098         _LIT(KChar,"%c");
    00099         iConsole->Printf(KChar,(TUint8)(iConsole->KeyCode()));
    00100         iBuf.Append(iConsole->KeyCode());
    00101         ProcessKeyPress(TChar(iConsole->KeyCode()));
    00102         }
    00103
    00108 void CCollector::ProcessKeyPress(TChar aChar)
    00109     {
    00110     if (aChar == EKeyEscape)
    00111         {
    00112         this->Stop();
    00113         }
    00114     else if(aChar == EKeyEnter)
    00115         {
    00116         _LIT(KNextLine , "\n");
    00117         iConsole->Printf(KNextLine);
    00118         this->SendMsgInQ(iBuf);
    00119         iBuf.Zero();
    00120         this->RequestFunction();
    00121         }
    00122     else
    00123         {
    00124         this->RequestFunction();
    00125         }
    00126     }
    00127
    00131 void CCollector::Stop()
    00132         {
    00133     TBuf<1> escape;
    00134     escape.Append(EKeyEscape);
    00135     //Sends stop message to the inverter.
    00136     this->SendMsgInQ(escape);
    00137
    00138     //Cancel any outstanding request.
    00139     iRcvInverterInQ->Cancel();
    00140     delete iRcvInverterInQ;
    00141     delete iConsole;
    00142     CActiveScheduler::Stop();
    00143     escape.Zero();
    00144         }
    00145
    00150 void CCollector::StartRecieving()
    00151         {
    00152         iRcvInverterInQ->StartRecieving();
    00153         }
    00154
    00158 void CCollector::SendMsgInQ(TBuf<100> buf)
    00159         {
    00160         TInt ret = iInverterInQ.OpenGlobal(KGlobalInverterInQ);
    00161         if (ret == KErrNone)
    00162         {
    00163         iInverterInQ.SendBlocking(buf);
    00164         }
    00165         }
    00166
    00167 LOCAL_C void DoStartL()
    00168         {
    00169         // Create active scheduler (to run active objects)
    00170         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
    00171         CleanupStack::PushL(scheduler);
    00172         CActiveScheduler::Install(scheduler);
    00173
    00174         CCollector* iCollect= CCollector::NewL();
    00175         CleanupStack::PushL(iCollect);
    00176
    00177          //Start getting the user input.
    00178         iCollect->RequestFunction();
    00179
    00180         //Recieve data from the inverter in another console.
    00181         iCollect->StartRecieving();
    00182         CActiveScheduler::Start();
    00183
    00184         CleanupStack::PopAndDestroy(iCollect);
    00185         CleanupStack::PopAndDestroy(scheduler);
    00186         }
    00187
    00188 GLDEF_C TInt E32Main()
    00189         {
    00190         __UHEAP_MARK;
    00191         CTrapCleanup* cleanup = CTrapCleanup::New();
    00192         if(cleanup == NULL)
    00193                 {
    00194                 return KErrNoMemory;
    00195                 }
    00196         TRAPD(mainError, DoStartL());
    00197         if(mainError != KErrNone)
    00198                 {
    00199                 _LIT(KUserPanic,"Failed to complete");
    00200                 User::Panic(KUserPanic, mainError);
    00201                 }
    00202                 delete cleanup;
    00203                 __UHEAP_MARKEND;
    00204                 return KErrNone;
    00205         }
    00206