examples/Base/IPC/pubsub/subscribestd.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 standard state 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         Cancel();
00078         }
00079 
00083 void CArrayPropertyWatch::DoCancel()
00084         {
00085         iProperty.Cancel();
00086         }
00087 
00092 void CArrayPropertyWatch::RunL()
00093         {
00094         // Get the value of the property
00095         TBuf16<KArraySize> buf;
00096         TInt err = iProperty.Get(buf);
00097         if(err == KErrNotFound)
00098                 {
00099                 // Leave the function if the property is not defined
00100                 CActiveScheduler::Stop();
00101                 User::Leave(err);
00102                 }
00103         else
00104                 {
00105                 if(buf == KStop)
00106                         {
00107                         CActiveScheduler::Stop();
00108                         }
00109                 else
00110                         {
00111                         // Re-subscribe to the property
00112                         WatchL();
00113                         }
00114                 }
00115         }
00116 
00121 void CArrayPropertyWatch::PrintProperty(TDes16& aBuf)
00122         {
00123         iConsole->Printf(KTxtArray);
00124         TInt bufLength = aBuf.Length();
00125         for(TInt ix = 0; ix < bufLength;ix++)
00126                 {
00127                 iConsole->Printf(KTxtArrayElement,aBuf[ix]);
00128                 }
00129         iConsole->Printf(KTxtNewLine);
00130         }
00131 
00135 void CArrayPropertyWatch::WatchL()
00136         {
00137         // Get the value of the property
00138         TBuf16<KArraySize> buf;
00139         TInt err = iProperty.Get(buf);
00140         if(err == KErrNotFound)
00141                 {
00142                 // Leave if the property is not found
00143                 User::Leave(err);
00144                 }
00145         PrintProperty(buf);
00146         // Subscribe to the property
00147         iProperty.Subscribe(iStatus);
00148         // Issue an outstanding request
00149         SetActive();
00150         }
00151 
00152 void DoExampleL()
00153         {
00154         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
00155         CleanupStack::PushL(scheduler);
00156         CActiveScheduler::Install(scheduler);
00157 
00158         console->Printf(KTxtSpecSt);
00159 
00160         // Create the byte-array property watch active object
00161         CArrayPropertyWatch* obj = CArrayPropertyWatch::NewL(console);
00162         CleanupStack::PushL(obj);
00163 
00164         // Subscribe to the property and start the scheduler
00165         obj->WatchL();
00166         CActiveScheduler::Start();
00167 
00168         CleanupStack::PopAndDestroy(obj);
00169         CleanupStack::PopAndDestroy(scheduler);
00170         }
00171 
00172 GLDEF_C TInt E32Main()
00173         {
00174         __UHEAP_MARK;
00175         CTrapCleanup* cleanup = CTrapCleanup::New();
00176 
00177         TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
00178         if (createError)
00179                 return createError;
00180 
00181         TRAPD(mainError, DoExampleL());
00182         if (mainError)
00183                 console->Printf(KTextFailed, mainError);
00184         console->Printf(KTextPressAnyKey);
00185         console->Getch();
00186 
00187         delete console;
00188         delete cleanup;
00189         __UHEAP_MARKEND;
00190         return KErrNone;
00191         }

Generated by  doxygen 1.6.2