examples/Base/IPC/pubsub/publishspec.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 publishing pattern of 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         TInt bufLength = aBuf.Length();
00048         console->Printf(KTxtPublish);
00049 
00050         // Publish the property and handle the value returned appropriately
00051         // Ignore the KErrNotFound error
00052         TInt err = aProperty.Set(aBuf);
00053         switch(err)
00054                 {
00055                 // No error, property is defined
00056                 case KErrNone:
00057                         {
00058                         // Print array elements
00059                         console->Printf(KTxtArray);
00060                         for(TInt ix = 0; ix < bufLength; ix++)
00061                                 {
00062                                 console->Printf(KTxtArrayElement,aBuf[ix]);
00063                                 }
00064                         console->Printf(KTxtNewLine);
00065                         }
00066                         break;
00067                 // Property is not defined, ignore this error
00068                 case KErrNotFound:
00069                         {
00070                         console->Printf(KTxtNotFound);
00071                         }
00072                         break;
00073                 // Leave if any other errors are returned
00074                 default:
00075                         {
00076                         User::LeaveIfError(err);
00077                         }
00078                 }
00079         }
00080 
00081 void DoExampleL()
00082         {
00083         console->Printf(KTxtSpecPublish);
00084 
00085         RProperty myProperty;
00086         console->Printf(KTxtCreateHandle);
00087         // Create a handle to the property
00088         User::LeaveIfError(myProperty.Attach(KMyPropertyCat,KMyPropertyName,EOwnerThread));
00089 
00090         // Byte-Array to be published
00091         TUint16 array[KArraySize] = {10,20,30,40};
00092 
00093         // Descriptor that the array contents are copied into
00094         TBuf16<KBufSize> buf;
00095 
00096         // Copy array contents into the descriptor
00097         buf.Copy(&array[0],sizeof(array));
00098         buf.SetLength(KArraySize);
00099 
00100         // Publish the property
00101         PublishL(myProperty,buf);
00102 
00103         // Change the array contents
00104         array[0] = 0x00ab;
00105         array[1] = 0x00bc;
00106         array[2] = 0x00cd;
00107         array[3] = 0x00de;
00108         array[4] = 0x00ef;
00109         array[5] = 0x00fa;
00110 
00111         // Copy array contents into the descriptor
00112         buf.Copy(&array[0],sizeof(array));
00113         buf.SetLength(KArraySize);
00114         console->Printf(KTxtEnter);
00115 
00116         // Wait for a key press
00117         while(console->Getch()!= EKeyEnter)
00118                 {
00119                 console->Printf(KTxtEnter);
00120                 }
00121 
00122         // Publish the property
00123         PublishL(myProperty,buf);
00124 
00125         // Indicate the publisher will not publish any more values
00126         buf.Copy(KStop);
00127         console->Printf(KTxtEnter);
00128 
00129         // Wait for a key press
00130         while(console->Getch()!= EKeyEnter)
00131                 {
00132                 console->Printf(KTxtEnter);
00133                 }
00134 
00135         // Publish the property
00136         PublishL(myProperty,buf);
00137 
00138         // Close the handle
00139         myProperty.Close();
00140         }
00141 
00142 GLDEF_C TInt E32Main()
00143         {
00144         __UHEAP_MARK;
00145         CTrapCleanup* cleanup = CTrapCleanup::New();
00146 
00147         TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
00148         if (createError)
00149                 return createError;
00150 
00151         TRAPD(mainError, DoExampleL());
00152         if (mainError)
00153                 console->Printf(KTextFailed, mainError);
00154         console->Printf(KTextPressAnyKey);
00155         console->Getch();
00156 
00157         delete console;
00158         delete cleanup;
00159         __UHEAP_MARKEND;
00160         return KErrNone;
00161         }

Generated by  doxygen 1.6.2