examples/Base/IPC/pubsub/subscribepe.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 pure event pattern of subscribing to an integer property
00015 //
00016 
00017 
00018 
00023 #include "subscribepe.h"
00024 
00025 LOCAL_D CConsoleBase* console;
00026 
00030 CIntPropertyWatch::CIntPropertyWatch():CActive(EPriority)
00031         {
00032         }
00033 
00038 void CIntPropertyWatch::ConstructL(CConsoleBase* aConsole)
00039         {
00040         User::LeaveIfError(iProperty.Attach(KMyPropertyCat,KMyPropertyName));
00041         iConsole = aConsole;
00042         CActiveScheduler::Add(this);
00043         }
00044 
00050 CIntPropertyWatch* CIntPropertyWatch::NewL(CConsoleBase* aConsole)
00051         {
00052         CIntPropertyWatch* self = new (ELeave) CIntPropertyWatch;
00053         CleanupStack::PushL(self);
00054         self->ConstructL(aConsole);
00055         CleanupStack::Pop(self);
00056         return self;
00057         }
00058 
00062 CIntPropertyWatch::~CIntPropertyWatch()
00063         {
00064         iProperty.Close();
00065         Cancel();
00066         }
00067 
00071 void CIntPropertyWatch::DoCancel()
00072         {
00073         iProperty.Cancel();
00074         }
00075 
00080 void CIntPropertyWatch::RunL()
00081         {
00082         // Receive a notification when a value is published
00083         // The RProperty::Get() function call can be omitted as the published value is of no importance in the pure event pattern
00084         TInt value;
00085         TInt err = iProperty.Get(value);
00086         if (err == KErrNotFound)
00087                 {
00088                 // No need to re-subscribe to the property as it has been deleted by the publisher
00089                 CActiveScheduler::Stop();
00090                 }
00091         else if (err == KErrNone)
00092                 {
00093                 // Print the value of the property
00094                 PrintProperty(value);
00095                 // Re-subscribe to the property
00096                 IssueRequest();
00097                 }
00098         else
00099                 {
00100                 // Leave the function in case of any other error
00101                 CActiveScheduler::Stop();
00102                 User::Leave(err);
00103                 }
00104         }
00105 
00110 void CIntPropertyWatch::PrintProperty(TInt aValue)
00111         {
00112         iConsole->Printf(KTxtValChange,aValue);
00113         }
00114 
00119 void CIntPropertyWatch::WatchL()
00120         {
00121         TInt value;
00122         // Checks if the property exists
00123         TInt res = iProperty.Get(value);
00124         if (res == KErrNotFound)
00125                 {
00126                 // Property not defined
00127                 User::Leave(res);
00128                 }
00129         // Prints the value of the property
00130         // This is only to illustrate that the value of the property is set by the publisher
00131         // This value has no significance as the pure event distribution pattern of publish and subscribe is being followed
00132         PrintProperty(value);
00133         IssueRequest();
00134         }
00135 
00139 void CIntPropertyWatch::IssueRequest()
00140         {
00141         iProperty.Subscribe(iStatus);
00142         SetActive();
00143         }
00144 
00145 void DoExampleL()
00146         {
00147         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
00148         CleanupStack::PushL(scheduler);
00149         CActiveScheduler::Install(scheduler);
00150 
00151         console->Printf(KTxtPESub);
00152 
00153         // Create the integer property watch active object
00154         CIntPropertyWatch* obj = CIntPropertyWatch::NewL(console);
00155         CleanupStack::PushL(obj);
00156 
00157         // Subscribe to the property and start the scheduler
00158         obj->WatchL();
00159         CActiveScheduler::Start();
00160 
00161         CleanupStack::PopAndDestroy(obj);
00162         CleanupStack::PopAndDestroy(scheduler);
00163         }
00164 
00165 GLDEF_C TInt E32Main()
00166         {
00167         __UHEAP_MARK;
00168         CTrapCleanup* cleanup = CTrapCleanup::New();
00169 
00170         TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
00171         if (createError)
00172                 return createError;
00173 
00174         TRAPD(mainError, DoExampleL());
00175         if (mainError)
00176                 console->Printf(KTextFailed, mainError);
00177         console->Printf(KTextPressAnyKey);
00178         console->Getch();
00179 
00180         delete console;
00181         delete cleanup;
00182         __UHEAP_MARKEND;
00183         return KErrNone;
00184         }

Generated by  doxygen 1.6.2