00001 // Copyright (c) 2000-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 // Show asynchronous programming (without active objects) 00015 // Example introduces the "complications" involved in real-life 00016 // wait loops, which always deal with multiple asynchronous 00017 // service providers 00018 // 00019 00020 00021 00022 00023 #include "CommonFramework.h" 00024 #include <e32math.h> 00025 00026 // 00027 // Common literal text 00028 // 00029 00030 _LIT(KMsgQuickCancelled,"Quick service request canceled\n"); 00031 00032 // 00033 // utility functions 00034 // 00035 00036 LOCAL_D TInt64 smallRandSeed; 00037 00038 LOCAL_C TInt smallRand() 00039 { 00040 // produce small random numbers in range 0..9 00041 TInt bigResult=Math::Rand(smallRandSeed);// result uses full 32-bit range 00042 return bigResult % 10; // return result mod 10 00043 } 00044 00045 LOCAL_C void sleep(TInt aTenths) 00046 { 00047 // sleep for an interval measured in tenths of a second 00048 User::After(aTenths*100000); // just let the User function do it for us 00049 } 00050 00051 00052 // Do the example 00053 LOCAL_C void doExampleL() 00054 { 00055 // create and initialize heartbeat timer 00056 RTimer heartbeat; // Heartbeat timer. 00057 TRequestStatus heartbeatStatus; // Request status associated with it. 00058 heartbeat.CreateLocal(); // Always created for this thread. 00059 00060 // create stuff for secondary timer 00061 RTimer quickService; // A quick service. 00062 TRequestStatus quickServiceStatus; // Ccorresponding status. 00063 TBool quickServiceRequestIssued=EFalse; // Whether request issued. 00064 quickService.CreateLocal(); // Create timer. 00065 00066 // issue first heartbeat request 00067 heartbeat.After(heartbeatStatus,1000000); // Request completion 00068 // after 1 second. 00069 TInt heartbeatTick=0; // Ccounts heartbeat ticks. 00070 00071 // wait loop 00072 for (;;) 00073 { 00074 // Wait for any request 00075 User::WaitForAnyRequest(); 00076 // find out which request completed, and handle it 00077 if (heartbeatStatus!=KRequestPending) 00078 { 00079 // heartbeat completed so service request 00080 _LIT(KMsgServicing,"Servicing heartbeat tick %d ...\n"); 00081 console->Printf(KMsgServicing,heartbeatTick); 00082 // take some time over it 00083 sleep(smallRand()); 00084 00085 if (smallRand() < 5) 00086 { 00087 // issue a quick request 50% of the time 00088 // cancel any outstanding quick request 00089 if (quickServiceRequestIssued) 00090 { 00091 quickService.Cancel(); // cancel request 00092 User::WaitForRequest(quickServiceStatus); // wait 00093 quickServiceRequestIssued=EFalse; // indicate not issued 00094 console->Printf(KMsgQuickCancelled); 00095 } 00096 // issue new quick request 00097 quickService.After(quickServiceStatus,(smallRand()*2)*100000); 00098 // request an event after 0 .. 1.8 seconds 00099 quickServiceRequestIssued=ETrue; // indicate request issued 00100 _LIT(KMsgQuickIssued,"Quick service request issued\n"); 00101 console->Printf(KMsgQuickIssued); 00102 } 00103 00104 _LIT(KMsgServiced,"... heartbeat tick %d serviced\n"); 00105 console->Printf(KMsgServiced, heartbeatTick); 00106 // test whether processing should finish 00107 if (heartbeatTick >= 10) 00108 { 00109 // 10 heart-beats: processing finished 00110 _LIT(KMsgFinishing,"Finishing\n"); 00111 console->Printf(KMsgFinishing); 00112 00113 // cancel quick request, if outstanding 00114 if (quickServiceRequestIssued) 00115 { 00116 // only relevant if request issued 00117 quickService.Cancel(); // request early completion 00118 User::WaitForRequest(quickServiceStatus); // wait 00119 quickServiceRequestIssued=EFalse; 00120 // request no longer issued 00121 console->Printf(KMsgQuickCancelled); 00122 } 00123 00124 break; // finish wait loop 00125 } 00126 // re-issue request 00127 heartbeatTick++; // increment tick counter 00128 heartbeat.After(heartbeatStatus,1000000); // another second 00129 } 00130 00131 else if (quickServiceRequestIssued && quickServiceStatus!=KRequestPending) 00132 { // if quick service request issued, and completed, then service it 00133 _LIT(KMsgQuickCompleted,"Quick request completed\n"); 00134 console->Printf(KMsgQuickCompleted); 00135 quickServiceRequestIssued=EFalse; // indicate request not issued 00136 } 00137 00138 else 00139 { // stray signal 00140 _LIT(KMsgStraySignal,"Stray signal\n"); 00141 User::Panic(KMsgStraySignal, 1); // panic! 00142 } 00143 } 00144 00145 // close quick service 00146 quickService.Close(); // close 00147 00148 // close timer 00149 heartbeat.Close(); // close timer 00150 }
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.