1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "loadgen_keypress.h" |
|
21 #include "loadgen_model.h" |
|
22 #include "loadgen.hrh" |
|
23 #include <loadgen.rsg> |
|
24 |
|
25 #include <e32math.h> |
|
26 |
|
27 _LIT(KThreadName, "KeyPress %d"); |
|
28 |
|
29 const TInt KDefaultStart = 50; |
|
30 const TInt KDefaultPeriod = 5000000; |
|
31 |
|
32 // ===================================== MEMBER FUNCTIONS ===================================== |
|
33 |
|
34 CKeyPress* CKeyPress::NewL(TKeyPressAttributes& aAttributes, TInt aReferenceNumber) |
|
35 { |
|
36 CKeyPress* self = new(ELeave) CKeyPress(aAttributes, aReferenceNumber); |
|
37 CleanupStack::PushL(self); |
|
38 self->ConstructL(); |
|
39 CleanupStack::Pop(self); |
|
40 return self; |
|
41 } |
|
42 |
|
43 // -------------------------------------------------------------------------------------------- |
|
44 |
|
45 CKeyPress::~CKeyPress() |
|
46 { |
|
47 Close(); |
|
48 } |
|
49 |
|
50 // -------------------------------------------------------------------------------------------- |
|
51 |
|
52 CKeyPress::CKeyPress(TKeyPressAttributes& aAttributes, TInt aReferenceNumber) : iAttributes(aAttributes) |
|
53 { |
|
54 iAttributes.iId = aReferenceNumber; |
|
55 } |
|
56 |
|
57 // -------------------------------------------------------------------------------------------- |
|
58 |
|
59 void CKeyPress::ConstructL() |
|
60 { |
|
61 CLoadBase::ConstructL(); |
|
62 |
|
63 iType = ELoadGenCmdNewLoadKeyPress; |
|
64 |
|
65 TBuf<64> threadName; |
|
66 threadName.Format(KThreadName, iAttributes.iId); |
|
67 |
|
68 // create a thread |
|
69 User::LeaveIfError(iThread.Create(threadName, ThreadFunction, KDefaultStackSize*2, KMinHeapSize, 1024*KMinHeapSize, (TAny*) &iAttributes )); |
|
70 |
|
71 // set priority of the thread |
|
72 SetPriority(); |
|
73 } |
|
74 |
|
75 // -------------------------------------------------------------------------------------------- |
|
76 |
|
77 TInt CKeyPress::ThreadFunction(TAny* aThreadArg) |
|
78 { |
|
79 CTrapCleanup* pC = CTrapCleanup::New(); |
|
80 CActiveScheduler* pS = new CActiveScheduler; |
|
81 CActiveScheduler::Install(pS); |
|
82 |
|
83 // start generating load, pass pointer to arguments |
|
84 GenerateLoad(*((TKeyPressAttributes*) aThreadArg)); |
|
85 |
|
86 delete pS; |
|
87 delete pC; |
|
88 |
|
89 return KErrNone; |
|
90 } |
|
91 |
|
92 // -------------------------------------------------------------------------------------------- |
|
93 |
|
94 void CKeyPress::GenerateLoad(TKeyPressAttributes& aAttributes) |
|
95 { |
|
96 CKeyPressManager* keyPressManager = NULL; |
|
97 TRAPD(err, keyPressManager = CKeyPressManager::NewL(aAttributes)); |
|
98 if (err == KErrNone) CActiveScheduler::Start(); |
|
99 delete keyPressManager; |
|
100 } |
|
101 |
|
102 // -------------------------------------------------------------------------------------------- |
|
103 |
|
104 void CKeyPress::Resume() |
|
105 { |
|
106 CLoadBase::Resume(); |
|
107 |
|
108 iThread.Resume(); |
|
109 } |
|
110 |
|
111 // -------------------------------------------------------------------------------------------- |
|
112 |
|
113 void CKeyPress::Suspend() |
|
114 { |
|
115 CLoadBase::Suspend(); |
|
116 |
|
117 iThread.Suspend(); |
|
118 } |
|
119 |
|
120 // -------------------------------------------------------------------------------------------- |
|
121 |
|
122 void CKeyPress::SetPriority() |
|
123 { |
|
124 CLoadBase::SetPriority(); |
|
125 |
|
126 iThread.SetPriority(CLoadGenModel::SettingItemToThreadPriority(iAttributes.iPriority)); |
|
127 } |
|
128 |
|
129 // -------------------------------------------------------------------------------------------- |
|
130 |
|
131 void CKeyPress::Close() |
|
132 { |
|
133 CLoadBase::Close(); |
|
134 |
|
135 if (iThread.ExitReason() == 0) // check if the thread is still alive |
|
136 { |
|
137 // signal the thread that it needs to close |
|
138 iThread.RequestComplete(iAttributes.iDeathStatus, KErrCancel); |
|
139 |
|
140 // wait the thread to die |
|
141 TRequestStatus waiter; |
|
142 iThread.Logon(waiter); |
|
143 User::WaitForRequest(waiter); |
|
144 iThread.Close(); |
|
145 } |
|
146 } |
|
147 |
|
148 // -------------------------------------------------------------------------------------------- |
|
149 |
|
150 TPtrC CKeyPress::Description() |
|
151 { |
|
152 TBuf<256> buf; |
|
153 TBuf<16> prioBuf; |
|
154 CLoadGenModel::SettingItemToThreadDescription(iAttributes.iPriority, prioBuf); |
|
155 |
|
156 _LIT(KKeyPressEntry, "[%d] KeyPress prio=%S heartbeat=%dms random=%d%%"); |
|
157 buf.Format(KKeyPressEntry, iAttributes.iId, &prioBuf, iAttributes.iHeartBeat, iAttributes.iRandomVariance); |
|
158 |
|
159 return TPtrC(buf); |
|
160 } |
|
161 |
|
162 // -------------------------------------------------------------------------------------------- |
|
163 // -------------------------------------------------------------------------------------------- |
|
164 |
|
165 CKeyPressManager* CKeyPressManager::NewL(TKeyPressAttributes& aAttributes) |
|
166 { |
|
167 CKeyPressManager* self = new(ELeave) CKeyPressManager(aAttributes); |
|
168 CleanupStack::PushL(self); |
|
169 self->ConstructL(); |
|
170 CleanupStack::Pop(); |
|
171 return self; |
|
172 } |
|
173 |
|
174 // -------------------------------------------------------------------------------------------- |
|
175 |
|
176 CKeyPressManager::CKeyPressManager(TKeyPressAttributes& aAttributes) : |
|
177 CActive(EPriorityStandard), iAttributes(aAttributes) |
|
178 { |
|
179 } |
|
180 |
|
181 // -------------------------------------------------------------------------------------------- |
|
182 |
|
183 CKeyPressManager::~CKeyPressManager() |
|
184 { |
|
185 Cancel(); |
|
186 |
|
187 iWsSession.Close(); |
|
188 } |
|
189 |
|
190 // -------------------------------------------------------------------------------------------- |
|
191 |
|
192 void CKeyPressManager::ConstructL() |
|
193 { |
|
194 CActiveScheduler::Add(this); |
|
195 |
|
196 // set the status as pending |
|
197 iStatus = KRequestPending; |
|
198 SetActive(); |
|
199 |
|
200 // set the death status pointer point to the request status of this ao |
|
201 iAttributes.iDeathStatus = &iStatus; |
|
202 |
|
203 // init |
|
204 User::LeaveIfError( iWsSession.Connect() ); |
|
205 |
|
206 // start timer |
|
207 iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard); |
|
208 iPeriodicTimer->Start(KDefaultStart, KDefaultPeriod, TCallBack(PeriodicTimerCallBack, this)); |
|
209 } |
|
210 |
|
211 // -------------------------------------------------------------------------------------------- |
|
212 |
|
213 void CKeyPressManager::RunL() |
|
214 { |
|
215 // request status has completed by the main thread meaning that we need to stop now |
|
216 CActiveScheduler::Stop(); |
|
217 } |
|
218 |
|
219 // -------------------------------------------------------------------------------------------- |
|
220 |
|
221 void CKeyPressManager::DoCancel() |
|
222 { |
|
223 } |
|
224 |
|
225 // -------------------------------------------------------------------------------------------- |
|
226 |
|
227 TInt CKeyPressManager::PeriodicTimerCallBack(TAny* aAny) |
|
228 { |
|
229 CKeyPressManager* self = static_cast<CKeyPressManager*>( aAny ); |
|
230 |
|
231 self->iPeriodicTimer->Cancel(); |
|
232 self->SimulateKeyEvent(); |
|
233 |
|
234 return KErrNone; |
|
235 } |
|
236 |
|
237 // -------------------------------------------------------------------------------------------- |
|
238 |
|
239 void CKeyPressManager::SimulateKeyEvent() |
|
240 { |
|
241 // generate a random key event from a to z |
|
242 TKeyEvent keyEvent; |
|
243 keyEvent.iCode = CLoadGenModel::RandomNumber('a','z'); |
|
244 keyEvent.iScanCode = keyEvent.iCode - 32; |
|
245 keyEvent.iModifiers = 0; |
|
246 keyEvent.iRepeats = 0; |
|
247 |
|
248 iWsSession.SimulateKeyEvent(keyEvent); |
|
249 iWsSession.Flush(); |
|
250 |
|
251 // call timer |
|
252 iPeriodicTimer->Start(CLoadGenModel::MilliSecondsToMicroSeconds(iAttributes.iHeartBeat, iAttributes.iRandomVariance), KDefaultPeriod, TCallBack(PeriodicTimerCallBack, this)); |
|
253 } |
|
254 |
|
255 // -------------------------------------------------------------------------------------------- |
|
256 |
|
257 // End of File |
|