00001 /* 00002 Copyright (c) 2000-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: 00028 Show asynchronous programming (without active objects) 00029 Example shows how a wait loop can be used to identify how a 00030 request completed and service its completion 00031 */ 00032 00033 00034 #include "CommonFramework.h" 00035 #include <e32math.h> 00036 00037 // 00038 // utility functions 00039 // 00040 00041 LOCAL_D TInt64 smallRandSeed; 00042 00043 LOCAL_C TInt smallRand() 00044 { 00045 // produce small random numbers in range 0..9 00046 TInt bigResult=Math::Rand(smallRandSeed);// result uses full 32-bit range 00047 return bigResult % 10; // return result mod 10 00048 } 00049 00050 LOCAL_C void sleep(TInt aTenths) 00051 { 00052 // sleep for an interval measured in tenths of a second 00053 User::After(aTenths*100000); // just let the User function do it for us 00054 } 00055 00056 00057 // Do the example 00058 LOCAL_C void doExampleL() 00059 { 00060 // create and initialize heartbeat timer 00061 RTimer heartbeat; // heartbeat timer 00062 TRequestStatus heartbeatStatus; // request status associated with it 00063 heartbeat.CreateLocal(); // always created for this thread 00064 00065 // issue first heartbeat request 00066 heartbeat.After(heartbeatStatus,1000000); // request completion 00067 // after 1 second 00068 TInt heartbeatTick=0; // counts heartbeat ticks 00069 00070 // wait loop 00071 for (;;) 00072 { 00073 // wait for any request 00074 User::WaitForAnyRequest(); 00075 // find out which request completed, and handle it 00076 if (heartbeatStatus!=KRequestPending) 00077 { 00078 // heartbeat completed so service request 00079 _LIT(KMsgServicing,"Servicing heartbeat tick %d ...\n"); 00080 console->Printf(KMsgServicing,heartbeatTick); 00081 // take some time over it 00082 sleep(smallRand()); 00083 _LIT(KMsgServiced,"... heartbeat tick %d serviced\n"); 00084 console->Printf(KMsgServiced,heartbeatTick); 00085 // test whether processing should finish 00086 if (heartbeatTick >= 10) 00087 { 00088 // 10 heart-beats: processing finished 00089 _LIT(KMsgFinishing,"Finishing\n"); 00090 console->Printf(KMsgFinishing); 00091 // finish wait loop 00092 break; 00093 } 00094 // re-issue request 00095 heartbeatTick++; // increment tick 00096 // counter 00097 heartbeat.After(heartbeatStatus,1000000); // request completion 00098 // after another second 00099 } 00100 else 00101 { 00102 // stray signal 00103 _LIT(KMsgStraySignal,"Stray signal\n"); 00104 User::Panic(KMsgStraySignal, 1); // panic! 00105 } 00106 } 00107 00108 // close timer 00109 heartbeat.Close(); // close timer 00110 }