examples/Base/IPC/pubsub/subscribestd.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 // Demonstrates the standard state pattern of subscribing to a byte-array property
00015 //
00016 
00017 
00018 
00023 #include "subscribe.h"
00024 LOCAL_D CConsoleBase* console;
00025 
00029 CArrayPropertyWatch::CArrayPropertyWatch():CActive(EPriority)
00030         {
00031         }
00032 
00037 void CArrayPropertyWatch::ConstructL(CConsoleBase* aConsole)
00038         {
00039         User::LeaveIfError(iProperty.Attach(KMyPropertyCat,KMyPropertyName));
00040         iConsole = aConsole;
00041         CActiveScheduler::Add(this);
00042         }
00043 
00049 CArrayPropertyWatch* CArrayPropertyWatch::NewL(CConsoleBase* aConsole)
00050         {
00051         CArrayPropertyWatch* self = new (ELeave) CArrayPropertyWatch;
00052         CleanupStack::PushL(self);
00053         self->ConstructL(aConsole);
00054         CleanupStack::Pop(self);
00055         return self;
00056         }
00057 
00061 CArrayPropertyWatch::~CArrayPropertyWatch()
00062         {
00063         iProperty.Close();
00064         Cancel();
00065         }
00066 
00070 void CArrayPropertyWatch::DoCancel()
00071         {
00072         iProperty.Cancel();
00073         }
00074 
00079 void CArrayPropertyWatch::RunL()
00080         {
00081         // Get the value of the property
00082         TBuf16<KArraySize> buf;
00083         TInt err = iProperty.Get(buf);
00084         if(err == KErrNotFound)
00085                 {
00086                 // Leave the function if the property is not defined
00087                 CActiveScheduler::Stop();
00088                 User::Leave(err);
00089                 }
00090         else
00091                 {
00092                 if(buf == KStop)
00093                         {
00094                         CActiveScheduler::Stop();
00095                         }
00096                 else
00097                         {
00098                         // Re-subscribe to the property
00099                         WatchL();
00100                         }
00101                 }
00102         }
00103 
00108 void CArrayPropertyWatch::PrintProperty(TDes16& aBuf)
00109         {
00110         iConsole->Printf(KTxtArray);
00111         TInt bufLength = aBuf.Length();
00112         for(TInt ix = 0; ix < bufLength;ix++)
00113                 {
00114                 iConsole->Printf(KTxtArrayElement,aBuf[ix]);
00115                 }
00116         iConsole->Printf(KTxtNewLine);
00117         }
00118 
00122 void CArrayPropertyWatch::WatchL()
00123         {
00124         // Get the value of the property
00125         TBuf16<KArraySize> buf;
00126         TInt err = iProperty.Get(buf);
00127         if(err == KErrNotFound)
00128                 {
00129                 // Leave if the property is not found
00130                 User::Leave(err);
00131                 }
00132         PrintProperty(buf);
00133         // Subscribe to the property
00134         iProperty.Subscribe(iStatus);
00135         // Issue an outstanding request
00136         SetActive();
00137         }
00138 
00139 void DoExampleL()
00140         {
00141         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
00142         CleanupStack::PushL(scheduler);
00143         CActiveScheduler::Install(scheduler);
00144 
00145         console->Printf(KTxtSpecSt);
00146 
00147         // Create the byte-array property watch active object
00148         CArrayPropertyWatch* obj = CArrayPropertyWatch::NewL(console);
00149         CleanupStack::PushL(obj);
00150 
00151         // Subscribe to the property and start the scheduler
00152         obj->WatchL();
00153         CActiveScheduler::Start();
00154 
00155         CleanupStack::PopAndDestroy(obj);
00156         CleanupStack::PopAndDestroy(scheduler);
00157         }
00158 
00159 GLDEF_C TInt E32Main()
00160         {
00161         __UHEAP_MARK;
00162         CTrapCleanup* cleanup = CTrapCleanup::New();
00163 
00164         TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
00165         if (createError)
00166                 return createError;
00167 
00168         TRAPD(mainError, DoExampleL());
00169         if (mainError)
00170                 console->Printf(KTextFailed, mainError);
00171         console->Printf(KTextPressAnyKey);
00172         console->Getch();
00173 
00174         delete console;
00175         delete cleanup;
00176         __UHEAP_MARKEND;
00177         return KErrNone;
00178         }

Generated by  doxygen 1.6.2