examples/Base/IPC/pubsub/publishstd.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 standard state pattern of publishing a byte-array property  
00028 */
00029 
00030 
00031 
00036 #include "publish.h"
00037 
00038 LOCAL_D CConsoleBase* console;
00039 
00045 void PublishL(RProperty& aProperty,TDes16& aBuf)
00046         {
00047         console->Printf(KTxtPublish);
00048 
00049         // Publish the property
00050         // Failure to publish values in standard state publishing pattern is treated as a serious error
00051         User::LeaveIfError(aProperty.Set(aBuf));
00052         console->Printf(KTxtArray);
00053 
00054         // Print the value of the byte-array
00055         TInt bufLength = aBuf.Length();
00056         for(TInt ix = 0; ix < bufLength; ix++)
00057                 {
00058                 console->Printf(KTxtArrayElement,aBuf[ix]);
00059                 }
00060         console->Printf(KTxtNewLine);
00061         }
00062 
00063 void DoExampleL()
00064         {
00065         console->Printf(KTxtStPublish);
00066         console->Printf(KTxtDefine);
00067 
00068         // Define and pre-allocate memory to the byte-array property
00069         RProperty::Define(KMyPropertyCat, KMyPropertyName,RProperty::EByteArray,KAllowAllPolicy,KAllowAllPolicy,KArraySize);
00070         RProperty myProperty;
00071         // Create a handle to the property
00072         User::LeaveIfError(myProperty.Attach(KMyPropertyCat,KMyPropertyName,EOwnerThread));
00073         console->Printf(KTxtCreateHandle);
00074 
00075         // Byte-Array property value to be published
00076         TUint16 array[KArraySize] = {10,20,30,40};
00077 
00078         // Descriptor that the array contents are copied into
00079         TBuf16<KBufSize> buf;
00080 
00081         // Copy array contents into the descriptor
00082         buf.Copy(&array[0],sizeof(array));
00083         buf.SetLength(KArraySize);
00084 
00085         // Publish the property
00086         PublishL(myProperty,buf);
00087 
00088         // Change the array contents
00089         array[0] = 0x00ab;
00090         array[1] = 0x00bc;
00091         array[2] = 0x00cd;
00092         array[3] = 0x00de;
00093         array[4] = 0x00ef;
00094         array[5] = 0x00fa;
00095 
00096         buf.Copy(&array[0],sizeof(array));
00097         // Copy array contents into the descriptor
00098         buf.SetLength(KArraySize);
00099 
00100         // Wait for a key press
00101         console->Printf(KTxtEnter);
00102         while(console->Getch()!= EKeyEnter)
00103                 {
00104                 console->Printf(KTxtEnter);
00105                 }
00106 
00107         // Publish the property
00108         PublishL(myProperty,buf);
00109 
00110         // Indicate the publisher will not publish any more values
00111         // The subscriber stops its subscription to the property when this value is published
00112         buf.Copy(KStop);
00113         console->Printf(KTxtEnter);
00114 
00115         // Wait for a key press
00116         while(console->Getch()!= EKeyEnter)
00117                 {
00118                 console->Printf(KTxtEnter);
00119                 }
00120 
00121         // Publish the property
00122         PublishL(myProperty,buf);
00123 
00124         // Close the handle to the property
00125 
00126         myProperty.Close();
00127 
00128         // Wait for a key press
00129         while(console->Getch()!= EKeyEnter)
00130                 {
00131                 console->Printf(KTxtEnter);
00132                 }
00133 
00134         // The publisher has finished so delete the property
00135         User::LeaveIfError(RProperty::Delete(KMyPropertyCat, KMyPropertyName));
00136         }
00137 
00138 GLDEF_C TInt E32Main()
00139         {
00140         __UHEAP_MARK;
00141         CTrapCleanup* cleanup = CTrapCleanup::New();
00142 
00143         TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
00144         if (createError)
00145                 return createError;
00146 
00147         TRAPD(mainError, DoExampleL());
00148         if (mainError)
00149                 console->Printf(KTextFailed, mainError);
00150         console->Printf(KTextPressAnyKey);
00151         console->Getch();
00152 
00153         delete console;
00154         delete cleanup;
00155         __UHEAP_MARKEND;
00156         return KErrNone;
00157         }

Generated by  doxygen 1.6.2