1 /* |
|
2 * Copyright (c) 2010 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_netconn.h" |
|
21 #include "loadgen_utils.h" |
|
22 #include "loadgen.hrh" |
|
23 #include <loadgen.rsg> |
|
24 |
|
25 #include <e32math.h> |
|
26 #include <commdb.h> |
|
27 |
|
28 _LIT(KThreadName, "NetConn %d"); |
|
29 |
|
30 const TInt KDefaultStart = 50; |
|
31 const TInt KDefaultPeriod = 5000000; |
|
32 |
|
33 // ===================================== MEMBER FUNCTIONS ===================================== |
|
34 |
|
35 CNetConn* CNetConn::NewL(TNetConnAttributes& aAttributes, TInt aReferenceNumber) |
|
36 { |
|
37 CNetConn* self = new(ELeave) CNetConn(aAttributes, aReferenceNumber); |
|
38 CleanupStack::PushL(self); |
|
39 self->ConstructL(); |
|
40 CleanupStack::Pop(self); |
|
41 return self; |
|
42 } |
|
43 |
|
44 // -------------------------------------------------------------------------------------------- |
|
45 |
|
46 CNetConn::~CNetConn() |
|
47 { |
|
48 Close(); |
|
49 } |
|
50 |
|
51 // -------------------------------------------------------------------------------------------- |
|
52 |
|
53 CNetConn::CNetConn(TNetConnAttributes& aAttributes, TInt aReferenceNumber) : iAttributes(aAttributes) |
|
54 { |
|
55 iAttributes.iId = aReferenceNumber; |
|
56 } |
|
57 |
|
58 // -------------------------------------------------------------------------------------------- |
|
59 |
|
60 void CNetConn::ConstructL() |
|
61 { |
|
62 CLoadBase::ConstructL(); |
|
63 |
|
64 iType = ELoadGenCmdNewLoadNetConn; |
|
65 |
|
66 TBuf<64> threadName; |
|
67 threadName.Format(KThreadName, iAttributes.iId); |
|
68 |
|
69 // create a thread |
|
70 User::LeaveIfError(iThread.Create(threadName, ThreadFunction, KDefaultStackSize*2, 1024*KMinHeapSize, 16*1024*KMinHeapSize, (TAny*) &iAttributes )); |
|
71 |
|
72 // set priority of the thread |
|
73 SetPriority(); |
|
74 } |
|
75 |
|
76 // -------------------------------------------------------------------------------------------- |
|
77 |
|
78 TInt CNetConn::ThreadFunction(TAny* aThreadArg) |
|
79 { |
|
80 CTrapCleanup* pC = CTrapCleanup::New(); |
|
81 CActiveScheduler* pS = new CActiveScheduler; |
|
82 CActiveScheduler::Install(pS); |
|
83 |
|
84 // start generating load, pass pointer to arguments |
|
85 GenerateLoad(*((TNetConnAttributes*) aThreadArg)); |
|
86 |
|
87 delete pS; |
|
88 delete pC; |
|
89 |
|
90 return KErrNone; |
|
91 } |
|
92 |
|
93 // -------------------------------------------------------------------------------------------- |
|
94 |
|
95 void CNetConn::GenerateLoad(TNetConnAttributes& aAttributes) |
|
96 { |
|
97 CNetConnManager* netConnManager = NULL; |
|
98 TRAPD(err, netConnManager = CNetConnManager::NewL(aAttributes)); |
|
99 if (err == KErrNone) CActiveScheduler::Start(); |
|
100 delete netConnManager; |
|
101 } |
|
102 |
|
103 // -------------------------------------------------------------------------------------------- |
|
104 |
|
105 void CNetConn::Resume() |
|
106 { |
|
107 CLoadBase::Resume(); |
|
108 |
|
109 iThread.Resume(); |
|
110 } |
|
111 |
|
112 // -------------------------------------------------------------------------------------------- |
|
113 |
|
114 void CNetConn::Suspend() |
|
115 { |
|
116 CLoadBase::Suspend(); |
|
117 |
|
118 iThread.Suspend(); |
|
119 } |
|
120 |
|
121 // -------------------------------------------------------------------------------------------- |
|
122 |
|
123 void CNetConn::SetPriority() |
|
124 { |
|
125 CLoadBase::SetPriority(); |
|
126 |
|
127 iThread.SetPriority(CLoadGenUtils::SettingItemToThreadPriority(iAttributes.iPriority)); |
|
128 } |
|
129 |
|
130 // -------------------------------------------------------------------------------------------- |
|
131 |
|
132 void CNetConn::Close() |
|
133 { |
|
134 CLoadBase::Close(); |
|
135 |
|
136 if (iThread.ExitReason() == 0) // check if the thread is still alive |
|
137 { |
|
138 // signal the thread that it needs to close |
|
139 iThread.RequestComplete(iAttributes.iDeathStatus, KErrCancel); |
|
140 |
|
141 // wait the thread to die |
|
142 TRequestStatus waiter; |
|
143 iThread.Logon(waiter); |
|
144 User::WaitForRequest(waiter); |
|
145 iThread.Close(); |
|
146 } |
|
147 } |
|
148 |
|
149 // -------------------------------------------------------------------------------------------- |
|
150 |
|
151 TPtrC CNetConn::Description() |
|
152 { |
|
153 TBuf<256> buf; |
|
154 TBuf<16> prioBuf; |
|
155 CLoadGenUtils::SettingItemToThreadDescription(iAttributes.iPriority, prioBuf); |
|
156 |
|
157 _LIT(KNetConnEntry, "[%d] NetConn prio=%S dest=%S idle=%dms random=%d%%"); |
|
158 buf.Format(KNetConnEntry, iAttributes.iId, &prioBuf, &iAttributes.iDestination, iAttributes.iIdle, iAttributes.iRandomVariance); |
|
159 |
|
160 return TPtrC(buf); |
|
161 } |
|
162 |
|
163 // -------------------------------------------------------------------------------------------- |
|
164 // -------------------------------------------------------------------------------------------- |
|
165 |
|
166 CNetConnManager* CNetConnManager::NewL(TNetConnAttributes& aAttributes) |
|
167 { |
|
168 CNetConnManager* self = new(ELeave) CNetConnManager(aAttributes); |
|
169 CleanupStack::PushL(self); |
|
170 self->ConstructL(); |
|
171 CleanupStack::Pop(); |
|
172 return self; |
|
173 } |
|
174 |
|
175 // -------------------------------------------------------------------------------------------- |
|
176 |
|
177 CNetConnManager::CNetConnManager(TNetConnAttributes& aAttributes) : |
|
178 CActive(EPriorityStandard), iAttributes(aAttributes) |
|
179 { |
|
180 } |
|
181 |
|
182 // -------------------------------------------------------------------------------------------- |
|
183 |
|
184 CNetConnManager::~CNetConnManager() |
|
185 { |
|
186 Cancel(); |
|
187 |
|
188 iDownloadMgr.Close(); |
|
189 } |
|
190 |
|
191 // -------------------------------------------------------------------------------------------- |
|
192 |
|
193 void CNetConnManager::ConstructL() |
|
194 { |
|
195 CActiveScheduler::Add(this); |
|
196 |
|
197 // set the status as pending |
|
198 iStatus = KRequestPending; |
|
199 SetActive(); |
|
200 |
|
201 // set the death status pointer point to the request status of this ao |
|
202 iAttributes.iDeathStatus = &iStatus; |
|
203 |
|
204 // init |
|
205 TUid dlUid; |
|
206 dlUid.iUid = 0x00011100 + iAttributes.iId; // generate unique identifier instead of using the LoadGen uid for all instances |
|
207 iDownloadMgr.ConnectL(dlUid, *this, ETrue); |
|
208 |
|
209 // start timer |
|
210 iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard); |
|
211 iPeriodicTimer->Start(KDefaultStart, KDefaultPeriod, TCallBack(PeriodicTimerCallBack, this)); |
|
212 } |
|
213 |
|
214 // -------------------------------------------------------------------------------------------- |
|
215 |
|
216 void CNetConnManager::RunL() |
|
217 { |
|
218 // request status has completed by the main thread meaning that we need to stop now |
|
219 CActiveScheduler::Stop(); |
|
220 } |
|
221 |
|
222 // -------------------------------------------------------------------------------------------- |
|
223 |
|
224 void CNetConnManager::DoCancel() |
|
225 { |
|
226 } |
|
227 |
|
228 // -------------------------------------------------------------------------------------------- |
|
229 |
|
230 TInt CNetConnManager::PeriodicTimerCallBack(TAny* aAny) |
|
231 { |
|
232 CNetConnManager* self = static_cast<CNetConnManager*>( aAny ); |
|
233 |
|
234 self->iPeriodicTimer->Cancel(); |
|
235 TRAPD(err,self->StartDownloadL()); |
|
236 |
|
237 return err; |
|
238 } |
|
239 |
|
240 // -------------------------------------------------------------------------------------------- |
|
241 |
|
242 void CNetConnManager::StartDownloadL() |
|
243 { |
|
244 iDownloadMgr.SetIntAttribute(EDlMgrExitAction, EExitPause); |
|
245 iDownloadMgr.DeleteAll(); |
|
246 |
|
247 // create new download |
|
248 TBuf8<256> url; |
|
249 url.Copy(iAttributes.iDestination); |
|
250 |
|
251 RHttpDownload& download = iDownloadMgr.CreateDownloadL( url ); |
|
252 |
|
253 download.SetIntAttribute(EDlAttrAction, EDoNothing); // do nothing when download has finished |
|
254 download.SetBoolAttribute(EDlAttrHidden, ETrue); // download is hidden |
|
255 download.SetIntAttribute(EDlAttrRestartAction, ERestartForced); // force to download always ignoring cache |
|
256 |
|
257 // start the download |
|
258 download.Start(); |
|
259 } |
|
260 |
|
261 // -------------------------------------------------------------------------------------------- |
|
262 |
|
263 void CNetConnManager::HandleDMgrEventL(RHttpDownload& aDownload, THttpDownloadEvent aEvent) |
|
264 { |
|
265 if (aEvent.iProgressState == EHttpContentTypeReceived) |
|
266 { |
|
267 // need to start the download if already not started |
|
268 aDownload.Start(); |
|
269 } |
|
270 |
|
271 switch ( aEvent.iDownloadState ) |
|
272 { |
|
273 case EHttpDlPaused: |
|
274 case EHttpDlCompleted: |
|
275 case EHttpDlFailed: |
|
276 { |
|
277 // assume that the download has finished in this stage |
|
278 // delete download and restart |
|
279 aDownload.Delete(); |
|
280 iPeriodicTimer->Start(CLoadGenUtils::MilliSecondsToMicroSeconds(iAttributes.iIdle, iAttributes.iRandomVariance), KDefaultPeriod, TCallBack(PeriodicTimerCallBack, this)); |
|
281 break; |
|
282 } |
|
283 |
|
284 default: |
|
285 { |
|
286 break; |
|
287 } |
|
288 } |
|
289 } |
|
290 |
|
291 // -------------------------------------------------------------------------------------------- |
|
292 |
|
293 // End of File |
|