examples/Base/IPC/pubsub/subscribespec.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 speculative 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         // Delete the property as it is no longer required by the subscriber
00065         RProperty::Delete(KMyPropertyCat, KMyPropertyName);
00066         Cancel();
00067         }
00068 
00072 void CArrayPropertyWatch::DoCancel()
00073         {
00074         iProperty.Cancel();
00075         }
00076 
00081 void CArrayPropertyWatch::RunL()
00082         {
00083         // Get the value of the property
00084         TBuf16<KArraySize> buf;
00085         TInt err = iProperty.Get(buf);
00086         if(err == KErrNotFound)
00087                 {
00088                 // Leave the function if the property is not defined
00089                 CActiveScheduler::Stop();
00090                 User::Leave(err);
00091                 }
00092         else
00093                 {
00094                 // Print the value of the property
00095                 PrintProperty(buf);
00096                 if(buf == KStop)
00097                         {
00098                         CActiveScheduler::Stop();
00099                         }
00100                 else
00101                         {
00102                         // Re-subscribe to the property
00103                         iProperty.Subscribe(iStatus);
00104                         SetActive();
00105                         }
00106                 }
00107         }
00108 
00113 void CArrayPropertyWatch::PrintProperty(TDes16& aBuf)
00114         {
00115         iConsole->Printf(KTxtArray);
00116         TInt bufLength = aBuf.Length();
00117         for(TInt ix = 0; ix < bufLength;ix++)
00118                 {
00119                 iConsole->Printf(KTxtArrayElement,aBuf[ix]);
00120                 }
00121         iConsole->Printf(KTxtNewLine);
00122         }
00123 
00127 void CArrayPropertyWatch::DefinePropertyL()
00128         {
00129         iConsole->Printf(KTxtDefine);
00130         // Define the property
00131         TInt err = RProperty::Define(KMyPropertyCat, KMyPropertyName,RProperty::EByteArray,KAllowAllPolicy,KAllowAllPolicy,KArraySize);
00132         if(err == KErrAlreadyExists || err == KErrNone)
00133                 {
00134                 // Ignore the KErrAlreadyExists error code
00135                 TBuf16<KArraySize> buf;
00136                 // Returns the default value of the property
00137                 // Ignore this value
00138                 err = iProperty.Get(buf);
00139                 User::LeaveIfError(err);
00140                 // Subscribe to the property and maintain an outstanding request
00141                 iProperty.Subscribe(iStatus);
00142                 SetActive();
00143                 }
00144         else
00145                 {
00146                 // Leave in case of errors other than KErrAlreadyExists
00147                 User::Leave(err);
00148                 }
00149         }
00150 
00151 void DoExampleL()
00152         {
00153         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
00154         CleanupStack::PushL(scheduler);
00155         CActiveScheduler::Install(scheduler);
00156 
00157         console->Printf(KTxtSpecSub);
00158 
00159         // Create the byte-array property watch active object
00160         CArrayPropertyWatch* obj = CArrayPropertyWatch::NewL(console);
00161         CleanupStack::PushL(obj);
00162 
00163         // Subscribe to the property and start the scheduler
00164         obj->DefinePropertyL();
00165         CActiveScheduler::Start();
00166 
00167         CleanupStack::PopAndDestroy(obj);
00168         CleanupStack::PopAndDestroy(scheduler);
00169         }
00170 
00171 GLDEF_C TInt E32Main()
00172         {
00173         __UHEAP_MARK;
00174         CTrapCleanup* cleanup = CTrapCleanup::New();
00175 
00176         TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
00177         if (createError)
00178                 return createError;
00179 
00180         TRAPD(mainError, DoExampleL());
00181         if (mainError)
00182                 console->Printf(KTextFailed, mainError);
00183         console->Printf(KTextPressAnyKey);
00184         console->Getch();
00185 
00186         delete console;
00187         delete cleanup;
00188         __UHEAP_MARKEND;
00189         return KErrNone;
00190         }

Generated by  doxygen 1.6.2