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

Generated by  doxygen 1.6.2