1 /* |
|
2 * Copyright (c) 2007 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: Metadata Harvester server's metadata collector component |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 // INCLUDE FILES |
|
25 #include <upnpstring.h> |
|
26 #include <upnpavcontrolpoint.h> |
|
27 #include <upnperrors.h> // HTTP error codes |
|
28 #include <hash.h> |
|
29 #include "cmsearchresponsehash.h" |
|
30 #include "mdhmetadatacollector.h" |
|
31 #include "msdebug.h" |
|
32 #include "mdhmediaservercontainer.h" |
|
33 |
|
34 // CONSTANTS |
|
35 |
|
36 const TInt KSearchTimeout = 120000000; |
|
37 const TInt KMaxSearchRetries = 3; |
|
38 const TInt KSearchCountLimit = 1; |
|
39 |
|
40 // --------------------------------------------------------------------------- |
|
41 // Two-phased constructor |
|
42 // --------------------------------------------------------------------------- |
|
43 CCmMdhMetadataCollector* CCmMdhMetadataCollector::NewL( |
|
44 CCmMdhMediaserverContainer& aMediaserverContainer, |
|
45 CUpnpAVControlPoint& aControlPoint, |
|
46 TInt aSearchChunkSize, |
|
47 TInt aAddGranularity ) |
|
48 { |
|
49 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::NewL")); |
|
50 |
|
51 CCmMdhMetadataCollector* self = new (ELeave) CCmMdhMetadataCollector ( |
|
52 aMediaserverContainer, aControlPoint, |
|
53 aSearchChunkSize, aAddGranularity ); |
|
54 CleanupStack::PushL( self ); |
|
55 self->ConstructL(); |
|
56 CleanupStack::Pop( self ); |
|
57 return self; |
|
58 } |
|
59 |
|
60 // --------------------------------------------------------------------------- |
|
61 // Constructor |
|
62 // --------------------------------------------------------------------------- |
|
63 CCmMdhMetadataCollector::CCmMdhMetadataCollector( |
|
64 CCmMdhMediaserverContainer& aMediaserverContainer, |
|
65 CUpnpAVControlPoint& aAvControlPoint, |
|
66 TInt aSearchChunkSize, |
|
67 TInt aAddGranularity ) : |
|
68 iMediaserverContainer( aMediaserverContainer ), |
|
69 iAvControlPoint ( aAvControlPoint ), |
|
70 iSearchChunkSize( aSearchChunkSize ), |
|
71 iAddGranularity( aAddGranularity ) |
|
72 |
|
73 { |
|
74 iCdsSync = NULL; |
|
75 iXmlToParse = NULL; |
|
76 iUuid = NULL; |
|
77 iTimer = NULL; |
|
78 iSearchRetryCount = 0; |
|
79 iSessionId = 0; |
|
80 iTempStartIndex = -1; |
|
81 } |
|
82 |
|
83 |
|
84 // --------------------------------------------------------------------------- |
|
85 // 2nd phase constructor |
|
86 // --------------------------------------------------------------------------- |
|
87 void CCmMdhMetadataCollector::ConstructL() |
|
88 { |
|
89 iHashGenerator = CSHA1::NewL(); |
|
90 } |
|
91 |
|
92 // --------------------------------------------------------------------------- |
|
93 // Destructor |
|
94 // --------------------------------------------------------------------------- |
|
95 CCmMdhMetadataCollector::~CCmMdhMetadataCollector() |
|
96 { |
|
97 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::\ |
|
98 ~CCmMdhMetadataCollector")); |
|
99 delete iCdsSync; |
|
100 delete iXmlToParse; |
|
101 delete iTimer; |
|
102 iSourceDataArray.ResetAndDestroy(); |
|
103 delete iHashGenerator; |
|
104 delete iUuid; |
|
105 iHashValues.ResetAndDestroy(); |
|
106 } |
|
107 |
|
108 // --------------------------------------------------------------------------- |
|
109 // Harvests a media server |
|
110 // --------------------------------------------------------------------------- |
|
111 void CCmMdhMetadataCollector::DoHarvestMediaserverL() |
|
112 { |
|
113 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::DoHarvestMediaserver")); |
|
114 |
|
115 // iUuid must be set before calling this method. |
|
116 if ( !iUuid ) |
|
117 { |
|
118 User::Leave( KErrNotReady ); |
|
119 } |
|
120 |
|
121 #ifdef _DEBUG |
|
122 |
|
123 HBufC* uuid = UpnpString::ToUnicodeL( *iUuid ); |
|
124 TRACE( Print(_L("[CmMdh Server]\t search request, uuid: %S, \ |
|
125 searchIndex %d, chunk size %d"), |
|
126 uuid, iSearchIndex, iSearchChunkSize )); |
|
127 delete uuid; |
|
128 uuid = NULL; |
|
129 iDebugTime.HomeTime(); |
|
130 |
|
131 #endif |
|
132 |
|
133 TInt err = iAvControlPoint.CdsSearchActionL( *iUuid, |
|
134 KRootContainerId, |
|
135 KSearchCriteria, |
|
136 KMdhSearchFilter, |
|
137 iSearchIndex, |
|
138 iSearchChunkSize, |
|
139 KSortCriteria ); |
|
140 TRACE( Print( _L("[CmMdh Server]\t search request returned %d"), err)); |
|
141 if ( err > 0 ) |
|
142 { |
|
143 iSessionId = err; |
|
144 } |
|
145 LOG( _L("[CmMdh Server]\t creating CPeriodic \ |
|
146 timer for search timeout..." ) ); |
|
147 iTimer = CPeriodic::NewL(EPriorityHigh); |
|
148 iTimer->Start(KSearchTimeout, 0, TCallBack(TimeoutTimerCompletedL, this)); |
|
149 |
|
150 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::\ |
|
151 DoHarvestMediaserver end")); |
|
152 |
|
153 } |
|
154 |
|
155 // --------------------------------------------------------------------------- |
|
156 // Harvests a media server |
|
157 // --------------------------------------------------------------------------- |
|
158 void CCmMdhMetadataCollector::HarvestMediaserverL( TDesC8& aUuid ) |
|
159 { |
|
160 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::HarvestMediaserver")); |
|
161 iSearchIndex = 0; |
|
162 delete iUuid; iUuid = NULL; |
|
163 iUuid = aUuid.AllocL(); |
|
164 iItemsToParse = 0; |
|
165 |
|
166 #ifdef _DEBUG |
|
167 |
|
168 HBufC* uuid = UpnpString::ToUnicodeL( aUuid ); |
|
169 |
|
170 TRACE( Print( _L("[CmMdh Server]\t starting \ |
|
171 to harvest, uuid: %S"), uuid)); |
|
172 delete uuid; |
|
173 |
|
174 #endif |
|
175 |
|
176 if ( !iCdsSync ) |
|
177 { |
|
178 iCdsSync = CCdsSync::NewL(); |
|
179 } |
|
180 iResultIncomplete = ETrue; |
|
181 iCanceled = EFalse; |
|
182 iSearchRetryCount = 0; |
|
183 iSourceDataArray.ResetAndDestroy(); |
|
184 iHashValues.ResetAndDestroy(); |
|
185 |
|
186 iMediaServerId = iMediaserverContainer.IdForMediaServerL( aUuid ); |
|
187 iMediaserverContainer.HashValuesForMediaServerL( iMediaServerId, iHashValues ); |
|
188 |
|
189 iCdsSync->InitL( iSourceDataArray, |
|
190 iMediaServerId, |
|
191 *this, |
|
192 iAddGranularity ); |
|
193 |
|
194 DoHarvestMediaserverL(); |
|
195 |
|
196 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::\ |
|
197 HarvestMediaserver end")); |
|
198 } |
|
199 |
|
200 // --------------------------------------------------------------------------- |
|
201 // Cancels harvest |
|
202 // --------------------------------------------------------------------------- |
|
203 void CCmMdhMetadataCollector::CancelHarvest() |
|
204 { |
|
205 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::CancelHarvestL")); |
|
206 if( iCdsSync ) |
|
207 { |
|
208 // cancel parsing and mde operations |
|
209 TRAP_IGNORE( iCdsSync->ResetL() ); |
|
210 } |
|
211 if ( iTimer ) |
|
212 { |
|
213 iTimer->Cancel(); |
|
214 delete iTimer; iTimer = NULL; |
|
215 } |
|
216 |
|
217 // Remove old hash values |
|
218 TRAP_IGNORE(iMediaserverContainer.DeleteOldHashValuesL( |
|
219 iMediaServerId, |
|
220 iTempStartIndex ) ); |
|
221 |
|
222 iTempStartIndex = -1; |
|
223 iSourceDataArray.ResetAndDestroy(); |
|
224 iCanceled = ETrue; |
|
225 } |
|
226 |
|
227 |
|
228 // --------------------------------------------------------------------------- |
|
229 // Static timer callback function |
|
230 // --------------------------------------------------------------------------- |
|
231 TInt CCmMdhMetadataCollector::TimeoutTimerCompletedL(TAny* aInstance) |
|
232 { |
|
233 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::\ |
|
234 TimeoutTimerCompletedL")); |
|
235 return static_cast<CCmMdhMetadataCollector*> |
|
236 ( aInstance )->DoTimeoutTimerCompletedL(); |
|
237 } |
|
238 |
|
239 // --------------------------------------------------------------------------- |
|
240 // Timer callback function |
|
241 // --------------------------------------------------------------------------- |
|
242 TInt CCmMdhMetadataCollector::DoTimeoutTimerCompletedL() |
|
243 { |
|
244 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::\ |
|
245 DoTimeoutTimerCompletedL")); |
|
246 |
|
247 delete iTimer; iTimer = NULL; |
|
248 |
|
249 iMediaserverContainer.HarvestCompleteL( KErrTimedOut ); |
|
250 |
|
251 return KErrNone; |
|
252 } |
|
253 |
|
254 |
|
255 |
|
256 // --------------------------------------------------------------------------- |
|
257 // Observer callback for Content Directory Search function. |
|
258 // --------------------------------------------------------------------------- |
|
259 void CCmMdhMetadataCollector::CdsSearchResponse( TInt aSessionId, |
|
260 TInt aErr, |
|
261 const TDesC8& aResult, |
|
262 TInt aReturned, |
|
263 TInt aMatches ) |
|
264 { |
|
265 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::CdsSearchResponse")); |
|
266 TRACE( Print( _L("[CmMdh Server]\t session %d err %d \ |
|
267 returned:%d matches %d"), |
|
268 aSessionId, aErr, aReturned, aMatches) ); |
|
269 |
|
270 |
|
271 TRAPD( err, HandleSearchResponseL( aSessionId, |
|
272 aErr, |
|
273 aResult, |
|
274 aReturned, |
|
275 aMatches ) ); |
|
276 |
|
277 if ( err ) |
|
278 { |
|
279 TRACE( Print( _L("[CmMdh Server]\t HandleSearchResponse leaved with \ |
|
280 error %d"), err ) ); |
|
281 } |
|
282 } |
|
283 |
|
284 // --------------------------------------------------------------------------- |
|
285 // Handles search response. |
|
286 // --------------------------------------------------------------------------- |
|
287 void CCmMdhMetadataCollector::HandleSearchResponseL( TInt aSessionId, |
|
288 TInt aErr, |
|
289 const TDesC8& aResult, |
|
290 TInt aReturned, |
|
291 TInt aMatches ) |
|
292 { |
|
293 LOG(_L( |
|
294 "[CmMdh Server]\t CCmMdhMetadataCollector::HandleSearchResponseL") ); |
|
295 |
|
296 if ( iSessionId != aSessionId ) |
|
297 { |
|
298 LOG( _L("[CmMdh Server]\t session id mismatch, returning..")); |
|
299 return; |
|
300 } |
|
301 else |
|
302 { |
|
303 iSessionId = 0; |
|
304 } |
|
305 |
|
306 #ifdef _DEBUG |
|
307 TTime tempTime; |
|
308 tempTime.HomeTime(); |
|
309 |
|
310 TRACE( Print( _L("[CmMdh Server]\t search request took %ld microsec"), |
|
311 tempTime.MicroSecondsFrom( iDebugTime ).Int64() )); |
|
312 #endif |
|
313 |
|
314 if ( iTimer ) |
|
315 { |
|
316 iTimer->Cancel(); |
|
317 delete iTimer; |
|
318 iTimer = NULL; |
|
319 } |
|
320 |
|
321 if ( iXmlToParse ) |
|
322 { |
|
323 LOG( _L("[CmMdh Server]\t deleting old xml buffer..")); |
|
324 |
|
325 delete iXmlToParse; |
|
326 iXmlToParse = NULL; |
|
327 } |
|
328 |
|
329 if ( iCanceled ) |
|
330 { |
|
331 LOG(_L("[CmMdh Server]\t harvest canceled, returning..")); |
|
332 return; |
|
333 } |
|
334 |
|
335 if ( aErr && aErr != EHttpOk ) |
|
336 { |
|
337 if ( aErr == EActionFailed && |
|
338 iSearchRetryCount++ < KMaxSearchRetries ) |
|
339 // 501 = EActionFailed, retrying |
|
340 { |
|
341 TRACE( Print( _L("[CmMdh Server]\t retrying search, retry #%d"), |
|
342 iSearchRetryCount )); |
|
343 DoHarvestMediaserverL(); |
|
344 } |
|
345 else |
|
346 { |
|
347 LOG(_L("[CmMdh Server]\t Unrecoverable response \ |
|
348 error, aborting.. ")); |
|
349 delete iTimer; iTimer = NULL; |
|
350 iMediaserverContainer.HarvestCompleteL( aErr ); |
|
351 } |
|
352 return; |
|
353 } |
|
354 |
|
355 // Check the hash value (or should it be checked in the cdssync side...?) |
|
356 iHashGenerator->Reset(); |
|
357 iHashGenerator->Update( aResult ); |
|
358 TPtrC8 hashValue = iHashGenerator->Final(); |
|
359 |
|
360 TBool foundMatch( EFalse ); |
|
361 for ( TInt index(0); index < iHashValues.Count() && !foundMatch; index++ ) |
|
362 { |
|
363 CCmSearchResponseHash* hashInstance = iHashValues[index]; |
|
364 if ( hashInstance->StartIndex() == iSearchIndex |
|
365 && hashInstance->ItemCount() == aReturned |
|
366 && hashInstance->Hash() == hashValue ) |
|
367 { |
|
368 LOG( _L("[CmMdh Server]\t FOUND HASH MATCH!!")); |
|
369 foundMatch = ETrue; |
|
370 // Update last matching search chuck to cds sync component |
|
371 iCdsSync->SetSearchIndex( iSearchIndex + aReturned ); |
|
372 } |
|
373 } |
|
374 |
|
375 if ( !foundMatch ) |
|
376 { |
|
377 // Before updating hash values, remove all changed hash values |
|
378 iMediaserverContainer.DeleteOldHashValuesL( iMediaServerId, |
|
379 iSearchIndex ); |
|
380 |
|
381 iMediaserverContainer.AddHashValueForResponseL( iMediaServerId, |
|
382 iSearchIndex, |
|
383 aReturned, |
|
384 hashValue ); |
|
385 |
|
386 // Save information about hash value in database in order to delete it |
|
387 iTempStartIndex = iSearchIndex; |
|
388 |
|
389 TRACE( Print( _L("[CmMdh Server]\t HASH= Index:%d\tCount:%d\tHash:%S"), |
|
390 iSearchIndex, aReturned, &hashValue )); |
|
391 |
|
392 iSearchIndex += aReturned; |
|
393 iResultIncomplete = iSearchIndex < aMatches; |
|
394 iSearchRetryCount = 0; |
|
395 iItemsToParse = aReturned; |
|
396 HBufC8* xmlToParse = aResult.AllocL(); |
|
397 iSourceDataArray.Append( xmlToParse ); |
|
398 TRACE( Print( _L("[CmMdh Server]\t source data array has %d items"), |
|
399 iSourceDataArray.Count() )); |
|
400 |
|
401 iCdsSync->NotifySourceDataAddedL( !iResultIncomplete ); |
|
402 |
|
403 if ( iResultIncomplete && iCdsSync->ChunkCount() < KSearchCountLimit ) |
|
404 { |
|
405 DoHarvestMediaserverL(); |
|
406 } |
|
407 } |
|
408 else // Found matching parsed search response |
|
409 { |
|
410 iSearchIndex += aReturned; |
|
411 iResultIncomplete = iSearchIndex < aMatches; |
|
412 iSearchRetryCount = 0; |
|
413 iItemsToParse = aReturned; |
|
414 // Send progress |
|
415 iMediaserverContainer.SendProgressInfo( aReturned ); |
|
416 |
|
417 if ( iResultIncomplete && iCdsSync->ChunkCount() < KSearchCountLimit ) |
|
418 { |
|
419 DoHarvestMediaserverL(); |
|
420 } |
|
421 else |
|
422 { |
|
423 iMediaserverContainer.HarvestCompleteL( KErrNone ); |
|
424 } |
|
425 } |
|
426 } |
|
427 |
|
428 |
|
429 // --------------------------------------------------------------------------- |
|
430 // Sends progress data |
|
431 // --------------------------------------------------------------------------- |
|
432 void CCmMdhMetadataCollector::ProgressL( TInt aItemCount ) |
|
433 { |
|
434 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::ProgressL")); |
|
435 |
|
436 iMediaserverContainer.SendProgressInfo( aItemCount ); |
|
437 |
|
438 } |
|
439 |
|
440 // --------------------------------------------------------------------------- |
|
441 // Cds Sync chunk completion callback function |
|
442 // --------------------------------------------------------------------------- |
|
443 void CCmMdhMetadataCollector::ChunkCompleteL() |
|
444 { |
|
445 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::ChunkCompleteL")); |
|
446 |
|
447 // to check if all of the items have been processed successfully |
|
448 // If it is not, the percentage need to be increased for those |
|
449 // items that are not processed successfully |
|
450 TInt processedItems = iCdsSync->ProcessedItemCount(); |
|
451 if ( iItemsToParse > processedItems ) |
|
452 { |
|
453 ProgressL( iItemsToParse - processedItems ); |
|
454 } |
|
455 if ( iResultIncomplete && iCdsSync->ChunkCount() < KSearchCountLimit ) |
|
456 { |
|
457 DoHarvestMediaserverL(); |
|
458 } |
|
459 } |
|
460 |
|
461 // --------------------------------------------------------------------------- |
|
462 // Cds Sync completion callback function |
|
463 // --------------------------------------------------------------------------- |
|
464 void CCmMdhMetadataCollector::SyncCompleteL() |
|
465 { |
|
466 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::SyncCompleteL")); |
|
467 if ( iCanceled ) |
|
468 { |
|
469 LOG(_L("[CmMdh Server]\t harvest canceled, returning..")); |
|
470 return; |
|
471 } |
|
472 |
|
473 iMediaserverContainer.HarvestCompleteL( KErrNone ); |
|
474 } |
|
475 |
|
476 // --------------------------------------------------------------------------- |
|
477 // Cds Sync error callback function |
|
478 // --------------------------------------------------------------------------- |
|
479 #ifdef _DEBUG |
|
480 void CCmMdhMetadataCollector::SyncErrorL( TInt aError ) |
|
481 #else // _DEBUG |
|
482 void CCmMdhMetadataCollector::SyncErrorL( TInt /*aError*/ ) |
|
483 #endif // _DEBUG |
|
484 { |
|
485 LOG(_L("[CmMdh Server]\t CCmMdhMetadataCollector::SyncErrorL")); |
|
486 TRACE( Print( _L("[CmMdh Server]\t error is %d"), aError )); |
|
487 } |
|
488 |
|
489 |
|
490 |
|