examples/Base/IPC/pubsub/subscribespec.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 speculative pattern of subscribing to a byte-array property  
00028 */
00029 
00030 
00031 
00036 #include "subscribe.h"
00037 LOCAL_D CConsoleBase* console;
00038 
00042 CArrayPropertyWatch::CArrayPropertyWatch():CActive(EPriority)
00043         {
00044         }
00045 
00050 void CArrayPropertyWatch::ConstructL(CConsoleBase* aConsole)
00051         {
00052         User::LeaveIfError(iProperty.Attach(KMyPropertyCat,KMyPropertyName));
00053         iConsole = aConsole;
00054         CActiveScheduler::Add(this);
00055         }
00056 
00062 CArrayPropertyWatch* CArrayPropertyWatch::NewL(CConsoleBase* aConsole)
00063         {
00064         CArrayPropertyWatch* self = new (ELeave) CArrayPropertyWatch;
00065         CleanupStack::PushL(self);
00066         self->ConstructL(aConsole);
00067         CleanupStack::Pop(self);
00068         return self;
00069         }
00070 
00074 CArrayPropertyWatch::~CArrayPropertyWatch()
00075         {
00076         iProperty.Close();
00077         // Delete the property as it is no longer required by the subscriber
00078         RProperty::Delete(KMyPropertyCat, KMyPropertyName);
00079         Cancel();
00080         }
00081 
00085 void CArrayPropertyWatch::DoCancel()
00086         {
00087         iProperty.Cancel();
00088         }
00089 
00094 void CArrayPropertyWatch::RunL()
00095         {
00096         // Get the value of the property
00097         TBuf16<KArraySize> buf;
00098         TInt err = iProperty.Get(buf);
00099         if(err == KErrNotFound)
00100                 {
00101                 // Leave the function if the property is not defined
00102                 CActiveScheduler::Stop();
00103                 User::Leave(err);
00104                 }
00105         else
00106                 {
00107                 // Print the value of the property
00108                 PrintProperty(buf);
00109                 if(buf == KStop)
00110                         {
00111                         CActiveScheduler::Stop();
00112                         }
00113                 else
00114                         {
00115                         // Re-subscribe to the property
00116                         iProperty.Subscribe(iStatus);
00117                         SetActive();
00118                         }
00119                 }
00120         }
00121 
00126 void CArrayPropertyWatch::PrintProperty(TDes16& aBuf)
00127         {
00128         iConsole->Printf(KTxtArray);
00129         TInt bufLength = aBuf.Length();
00130         for(TInt ix = 0; ix < bufLength;ix++)
00131                 {
00132                 iConsole->Printf(KTxtArrayElement,aBuf[ix]);
00133                 }
00134         iConsole->Printf(KTxtNewLine);
00135         }
00136 
00140 void CArrayPropertyWatch::DefinePropertyL()
00141         {
00142         iConsole->Printf(KTxtDefine);
00143         // Define the property
00144         TInt err = RProperty::Define(KMyPropertyCat, KMyPropertyName,RProperty::EByteArray,KAllowAllPolicy,KAllowAllPolicy,KArraySize);
00145         if(err == KErrAlreadyExists || err == KErrNone)
00146                 {
00147                 // Ignore the KErrAlreadyExists error code
00148                 TBuf16<KArraySize> buf;
00149                 // Returns the default value of the property
00150                 // Ignore this value
00151                 err = iProperty.Get(buf);
00152                 User::LeaveIfError(err);
00153                 // Subscribe to the property and maintain an outstanding request
00154                 iProperty.Subscribe(iStatus);
00155                 SetActive();
00156                 }
00157         else
00158                 {
00159                 // Leave in case of errors other than KErrAlreadyExists
00160                 User::Leave(err);
00161                 }
00162         }
00163 
00164 void DoExampleL()
00165         {
00166         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
00167         CleanupStack::PushL(scheduler);
00168         CActiveScheduler::Install(scheduler);
00169 
00170         console->Printf(KTxtSpecSub);
00171 
00172         // Create the byte-array property watch active object
00173         CArrayPropertyWatch* obj = CArrayPropertyWatch::NewL(console);
00174         CleanupStack::PushL(obj);
00175 
00176         // Subscribe to the property and start the scheduler
00177         obj->DefinePropertyL();
00178         CActiveScheduler::Start();
00179 
00180         CleanupStack::PopAndDestroy(obj);
00181         CleanupStack::PopAndDestroy(scheduler);
00182         }
00183 
00184 GLDEF_C TInt E32Main()
00185         {
00186         __UHEAP_MARK;
00187         CTrapCleanup* cleanup = CTrapCleanup::New();
00188 
00189         TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
00190         if (createError)
00191                 return createError;
00192 
00193         TRAPD(mainError, DoExampleL());
00194         if (mainError)
00195                 console->Printf(KTextFailed, mainError);
00196         console->Printf(KTextPressAnyKey);
00197         console->Getch();
00198 
00199         delete console;
00200         delete cleanup;
00201         __UHEAP_MARKEND;
00202         return KErrNone;
00203         }

Generated by  doxygen 1.6.2