examples/Base/IPC/pubsub/subscribepe.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: Demonstrates the pure event pattern of subscribing to an integer property  
00028 */
00029 
00030 
00031 
00036 #include "subscribepe.h"
00037 
00038 LOCAL_D CConsoleBase* console;
00039 
00043 CIntPropertyWatch::CIntPropertyWatch():CActive(EPriority)
00044         {
00045         }
00046 
00051 void CIntPropertyWatch::ConstructL(CConsoleBase* aConsole)
00052         {
00053         User::LeaveIfError(iProperty.Attach(KMyPropertyCat,KMyPropertyName));
00054         iConsole = aConsole;
00055         CActiveScheduler::Add(this);
00056         }
00057 
00063 CIntPropertyWatch* CIntPropertyWatch::NewL(CConsoleBase* aConsole)
00064         {
00065         CIntPropertyWatch* self = new (ELeave) CIntPropertyWatch;
00066         CleanupStack::PushL(self);
00067         self->ConstructL(aConsole);
00068         CleanupStack::Pop(self);
00069         return self;
00070         }
00071 
00075 CIntPropertyWatch::~CIntPropertyWatch()
00076         {
00077         iProperty.Close();
00078         Cancel();
00079         }
00080 
00084 void CIntPropertyWatch::DoCancel()
00085         {
00086         iProperty.Cancel();
00087         }
00088 
00093 void CIntPropertyWatch::RunL()
00094         {
00095         // Receive a notification when a value is published
00096         // The RProperty::Get() function call can be omitted as the published value is of no importance in the pure event pattern
00097         TInt value;
00098         TInt err = iProperty.Get(value);
00099         if (err == KErrNotFound)
00100                 {
00101                 // No need to re-subscribe to the property as it has been deleted by the publisher
00102                 CActiveScheduler::Stop();
00103                 }
00104         else if (err == KErrNone)
00105                 {
00106                 // Print the value of the property
00107                 PrintProperty(value);
00108                 // Re-subscribe to the property
00109                 IssueRequest();
00110                 }
00111         else
00112                 {
00113                 // Leave the function in case of any other error
00114                 CActiveScheduler::Stop();
00115                 User::Leave(err);
00116                 }
00117         }
00118 
00123 void CIntPropertyWatch::PrintProperty(TInt aValue)
00124         {
00125         iConsole->Printf(KTxtValChange,aValue);
00126         }
00127 
00132 void CIntPropertyWatch::WatchL()
00133         {
00134         TInt value;
00135         // Checks if the property exists
00136         TInt res = iProperty.Get(value);
00137         if (res == KErrNotFound)
00138                 {
00139                 // Property not defined
00140                 User::Leave(res);
00141                 }
00142         // Prints the value of the property
00143         // This is only to illustrate that the value of the property is set by the publisher
00144         // This value has no significance as the pure event distribution pattern of publish and subscribe is being followed
00145         PrintProperty(value);
00146         IssueRequest();
00147         }
00148 
00152 void CIntPropertyWatch::IssueRequest()
00153         {
00154         iProperty.Subscribe(iStatus);
00155         SetActive();
00156         }
00157 
00158 void DoExampleL()
00159         {
00160         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
00161         CleanupStack::PushL(scheduler);
00162         CActiveScheduler::Install(scheduler);
00163 
00164         console->Printf(KTxtPESub);
00165 
00166         // Create the integer property watch active object
00167         CIntPropertyWatch* obj = CIntPropertyWatch::NewL(console);
00168         CleanupStack::PushL(obj);
00169 
00170         // Subscribe to the property and start the scheduler
00171         obj->WatchL();
00172         CActiveScheduler::Start();
00173 
00174         CleanupStack::PopAndDestroy(obj);
00175         CleanupStack::PopAndDestroy(scheduler);
00176         }
00177 
00178 GLDEF_C TInt E32Main()
00179         {
00180         __UHEAP_MARK;
00181         CTrapCleanup* cleanup = CTrapCleanup::New();
00182 
00183         TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
00184         if (createError)
00185                 return createError;
00186 
00187         TRAPD(mainError, DoExampleL());
00188         if (mainError)
00189                 console->Printf(KTextFailed, mainError);
00190         console->Printf(KTextPressAnyKey);
00191         console->Getch();
00192 
00193         delete console;
00194         delete cleanup;
00195         __UHEAP_MARKEND;
00196         return KErrNone;
00197         }

Generated by  doxygen 1.6.2