|
1 /* |
|
2 * Copyright (c) 2006-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: Harvester Client implementation* |
|
15 */ |
|
16 |
|
17 #include <e32property.h> |
|
18 |
|
19 #include "harvesterclient.h" |
|
20 #include "harvestercommon.h" |
|
21 #include "harvesterrequestqueue.h" |
|
22 #include "harvestereventobserverao.h" |
|
23 #include "harvesterlog.h" |
|
24 #include "harvesterclientao.h" |
|
25 #include "mdsutils.h" |
|
26 #include "harvesterrequestactive.h" |
|
27 #include "mdscommoninternal.h" |
|
28 |
|
29 /** @var Message slots */ |
|
30 const TInt KDefaultMessageSlots = -1; // Global pool |
|
31 |
|
32 /* Server name */ |
|
33 _LIT( KHarvesterServerName, "HarvesterServer" ); |
|
34 |
|
35 /* Harvester Server process location */ |
|
36 _LIT( KHarvesterServerExe, "harvesterserver.exe" ); |
|
37 |
|
38 // FUNCTION PROTOTYPES |
|
39 static TInt StartServer(); |
|
40 static TInt CreateServerProcess(); |
|
41 |
|
42 |
|
43 // ---------------------------------------------------------------------------------------- |
|
44 // RHarvesterClient |
|
45 // ---------------------------------------------------------------------------------------- |
|
46 // |
|
47 EXPORT_C RHarvesterClient::RHarvesterClient() : RSessionBase() |
|
48 { |
|
49 WRITELOG( "RHarvesterClient::RHarvesterClient() - Constructor" ); |
|
50 iHarvesterClientAO = NULL; |
|
51 iObserver = NULL; |
|
52 iHEO = NULL; |
|
53 iRequestQueue = NULL; |
|
54 } |
|
55 |
|
56 // ---------------------------------------------------------------------------------------- |
|
57 // Connect |
|
58 // ---------------------------------------------------------------------------------------- |
|
59 // |
|
60 EXPORT_C TInt RHarvesterClient::Connect() |
|
61 { |
|
62 WRITELOG( "RHarvesterClient::Connect()" ); |
|
63 |
|
64 RProperty property; |
|
65 const TInt error( property.Attach( KHarvesterPSShutdown, KShutdown, EOwnerThread ) ); |
|
66 TInt value = 0; |
|
67 property.Get( value ); |
|
68 property.Close(); |
|
69 |
|
70 if ( error != KErrNone || value > KErrNone ) |
|
71 { |
|
72 return KErrLocked; |
|
73 } |
|
74 |
|
75 if( iHarvesterClientAO ) |
|
76 { |
|
77 return KErrAlreadyExists; |
|
78 } |
|
79 |
|
80 TRAPD( err, iHarvesterClientAO = CHarvesterClientAO::NewL(*this) ); |
|
81 if ( err != KErrNone ) |
|
82 { |
|
83 WRITELOG( "RHarvesterClient::RHarvesterClient() - Couldn't create active object" ); |
|
84 return err; |
|
85 } |
|
86 |
|
87 // request processor |
|
88 TRAP( err, iRequestQueue = CHarvesterRequestQueue::NewL() ) |
|
89 { |
|
90 if ( err != KErrNone ) |
|
91 { |
|
92 WRITELOG( "RHarvesterClient::RHarvesterClient() - Couldn't create harvester request queue" ); |
|
93 delete iHarvesterClientAO; |
|
94 iHarvesterClientAO = NULL; |
|
95 return err; |
|
96 } |
|
97 } |
|
98 |
|
99 err = ::StartServer(); |
|
100 |
|
101 if ( err == KErrNone || err == KErrAlreadyExists ) |
|
102 { |
|
103 WRITELOG( "RHarvesterClient::Connect() - creating session" ); |
|
104 err = CreateSession( KHarvesterServerName, Version(), KDefaultMessageSlots ); |
|
105 } |
|
106 else |
|
107 { |
|
108 delete iHarvesterClientAO; |
|
109 iHarvesterClientAO = NULL; |
|
110 delete iRequestQueue; |
|
111 iRequestQueue = NULL; |
|
112 } |
|
113 |
|
114 #ifdef _DEBUG |
|
115 if ( err != KErrNone ) |
|
116 { |
|
117 WRITELOG( "RHarvesterClient::Connect() - Server is not running or could not be started" ); |
|
118 } |
|
119 else |
|
120 { |
|
121 WRITELOG1( "RHarvesterClient::Connect() - no errors: %d", err ); |
|
122 } |
|
123 WRITELOG( "RHarvesterClient::Connect() - end" ); |
|
124 #endif |
|
125 |
|
126 iHEO = NULL; |
|
127 |
|
128 return err; |
|
129 } |
|
130 |
|
131 // ---------------------------------------------------------------------------------------- |
|
132 // Pause |
|
133 // ---------------------------------------------------------------------------------------- |
|
134 // |
|
135 EXPORT_C TInt RHarvesterClient::Pause() |
|
136 { |
|
137 WRITELOG( "RHarvesterClient::Pause() - sending command EPauseHarvester" ); |
|
138 if( iHandle ) |
|
139 { |
|
140 return SendReceive( EPauseHarvester ); |
|
141 } |
|
142 return KErrDisconnected; |
|
143 } |
|
144 |
|
145 // ---------------------------------------------------------------------------------------- |
|
146 // Resume |
|
147 // ---------------------------------------------------------------------------------------- |
|
148 // |
|
149 EXPORT_C TInt RHarvesterClient::Resume() |
|
150 { |
|
151 WRITELOG( "RHarvesterClient::Resume() - sending command EResumeHarvester" ); |
|
152 if( iHandle ) |
|
153 { |
|
154 return SendReceive( EResumeHarvester ); |
|
155 } |
|
156 return KErrDisconnected; |
|
157 } |
|
158 |
|
159 // ---------------------------------------------------------------------------------------- |
|
160 // Close |
|
161 // ---------------------------------------------------------------------------------------- |
|
162 // |
|
163 EXPORT_C void RHarvesterClient::Close() |
|
164 { |
|
165 WRITELOG( "RHarvesterClient::Close()" ); |
|
166 |
|
167 // cancels Harvest Complete request if it exist at server |
|
168 UnregisterHarvestComplete(); |
|
169 |
|
170 WRITELOG( "RHarvesterClient::Close() - UnregisterHarvest done" ); |
|
171 |
|
172 iRequestQueue->ForceRequests(); |
|
173 |
|
174 delete iRequestQueue; |
|
175 iRequestQueue = NULL; |
|
176 |
|
177 delete iHarvesterClientAO; |
|
178 iHarvesterClientAO = NULL; |
|
179 |
|
180 delete iHEO; |
|
181 iHEO = NULL; |
|
182 |
|
183 WRITELOG( "RHarvesterClient::Close() - Closing session" ); |
|
184 |
|
185 RSessionBase::Close(); |
|
186 } |
|
187 |
|
188 // ---------------------------------------------------------------------------------------- |
|
189 // SetObserver |
|
190 // ---------------------------------------------------------------------------------------- |
|
191 // |
|
192 EXPORT_C void RHarvesterClient::SetObserver( MHarvestObserver* aObserver ) |
|
193 { |
|
194 WRITELOG( "RHarvesterClient::SetObserver()" ); |
|
195 |
|
196 if ( iHarvesterClientAO ) |
|
197 { |
|
198 iHarvesterClientAO->SetObserver( aObserver ); |
|
199 } |
|
200 iObserver = aObserver; |
|
201 } |
|
202 |
|
203 // ---------------------------------------------------------------------------------------- |
|
204 // RemoveObserver |
|
205 // ---------------------------------------------------------------------------------------- |
|
206 // |
|
207 EXPORT_C void RHarvesterClient::RemoveObserver( MHarvestObserver* aObserver ) |
|
208 { |
|
209 WRITELOG( "RHarvesterClient::RemoveObserver()" ); |
|
210 |
|
211 if ( iHarvesterClientAO ) |
|
212 { |
|
213 iHarvesterClientAO->RemoveObserver( aObserver ); |
|
214 } |
|
215 |
|
216 if ( aObserver == iObserver ) |
|
217 { |
|
218 if ( iObserver ) |
|
219 { |
|
220 WRITELOG( "CHarvesterClientAO::RemoveObserver() - deleting observer" ); |
|
221 iObserver = NULL; |
|
222 } |
|
223 } |
|
224 } |
|
225 |
|
226 // ---------------------------------------------------------------------------------------- |
|
227 // AddHarvesterEventObserver |
|
228 // ---------------------------------------------------------------------------------------- |
|
229 // |
|
230 EXPORT_C TInt RHarvesterClient::AddHarvesterEventObserver( |
|
231 MHarvesterEventObserver& aHarvesterEventObserver, |
|
232 TInt aHEObserverType, |
|
233 TInt aEventInterval ) |
|
234 { |
|
235 TInt err( 0 ); |
|
236 if( !iHEO ) |
|
237 { |
|
238 TRAP( err, iHEO = CHarvesterEventObserverAO::NewL( *this ) ); |
|
239 if ( err != KErrNone ) |
|
240 { |
|
241 WRITELOG( "RHarvesterClient::RHarvesterClient() - Couldn't create harvester event observer" ); |
|
242 return err; |
|
243 } |
|
244 } |
|
245 |
|
246 TRAP(err, iHEO->AddHarvesterEventObserverL( |
|
247 aHarvesterEventObserver, |
|
248 aHEObserverType, |
|
249 aEventInterval )); |
|
250 |
|
251 return err; |
|
252 } |
|
253 |
|
254 // ---------------------------------------------------------------------------------------- |
|
255 // RemoveObserver |
|
256 // ---------------------------------------------------------------------------------------- |
|
257 // |
|
258 EXPORT_C TInt RHarvesterClient::RemoveHarvesterEventObserver( MHarvesterEventObserver& aHarvesterEventObserver ) |
|
259 { |
|
260 TRAPD( err, iHEO->RemoveHarvesterEventObserverL( aHarvesterEventObserver ) ); |
|
261 |
|
262 return err; |
|
263 } |
|
264 |
|
265 // ---------------------------------------------------------------------------------------- |
|
266 // HarvestFile |
|
267 // ---------------------------------------------------------------------------------------- |
|
268 // |
|
269 EXPORT_C void RHarvesterClient::HarvestFile( const TDesC& aURI, RArray<TItemId>& aAlbumIds, TBool aAddLocation ) |
|
270 { |
|
271 WRITELOG1( "RHarvesterClient::HarvestFile() - file %S", &aURI ); |
|
272 |
|
273 HBufC8* paramBuf = NULL; |
|
274 TRAPD( err, paramBuf = SerializeArrayL( aAlbumIds ) ); |
|
275 if ( err ) |
|
276 { |
|
277 WRITELOG1( "RHarvesterClient::HarvestFile() - cannot create serialized array, error: %d", err ); |
|
278 if( iObserver ) |
|
279 { |
|
280 iObserver->HarvestingComplete( const_cast<TDesC&>(aURI), err ); |
|
281 } |
|
282 return; |
|
283 } |
|
284 |
|
285 CHarvesterRequestActive* harvestFileActive( NULL ); |
|
286 TRAP( err, harvestFileActive = CHarvesterRequestActive::NewL( *this, iObserver, (TInt)EHarvestFile, aURI, |
|
287 paramBuf, aAddLocation, iRequestQueue ) ); |
|
288 if( err ) |
|
289 { |
|
290 WRITELOG1( "RHarvesterClient::HarvestFile() - cannot create harvesting request, error: %d", err ); |
|
291 if( iObserver ) |
|
292 { |
|
293 iObserver->HarvestingComplete( const_cast<TDesC&>(aURI), err ); |
|
294 } |
|
295 return; |
|
296 } |
|
297 |
|
298 // send actually harvest request to server |
|
299 if( iHandle ) |
|
300 { |
|
301 TRAP( err, iRequestQueue->AddRequestL( harvestFileActive ) ); |
|
302 if( err && iObserver) |
|
303 { |
|
304 WRITELOG1( "RHarvesterClient::HarvestFile() - cannot not send harvest request to server, error: %d", KErrServerBusy ); |
|
305 iObserver->HarvestingComplete( const_cast<TDesC&>(aURI), KErrServerBusy ); |
|
306 delete harvestFileActive; |
|
307 } |
|
308 else if( err ) |
|
309 { |
|
310 WRITELOG1( "RHarvesterClient::HarvestFile() - cannot not send harvest request to server, error: %d", KErrServerBusy ); |
|
311 delete harvestFileActive; |
|
312 } |
|
313 else |
|
314 { |
|
315 WRITELOG( "RHarvesterClient::HarvestFile() - harvesting request added to queue" ); |
|
316 iRequestQueue->Process(); |
|
317 } |
|
318 } |
|
319 else if( iObserver ) |
|
320 { |
|
321 WRITELOG1( "RHarvesterClient::HarvestFile() - cannot not send harvest request to server, error: %d", KErrDisconnected ); |
|
322 iObserver->HarvestingComplete( const_cast<TDesC&>(aURI), KErrDisconnected ); |
|
323 delete harvestFileActive; |
|
324 } |
|
325 else |
|
326 { |
|
327 WRITELOG1( "RHarvesterClient::HarvestFile() - cannot not send harvest request to server, error: %d", KErrDisconnected ); |
|
328 delete harvestFileActive; |
|
329 } |
|
330 WRITELOG( "RHarvesterClient::HarvestFile() - end" ); |
|
331 } |
|
332 |
|
333 // ---------------------------------------------------------------------------------------- |
|
334 // HarvestFileWithUID |
|
335 // ---------------------------------------------------------------------------------------- |
|
336 // |
|
337 EXPORT_C void RHarvesterClient::HarvestFileWithUID( const TDesC& aURI, |
|
338 RArray<TItemId>& aAlbumIds, |
|
339 TBool aAddLocation, |
|
340 TUid /*aUid*/ ) |
|
341 { |
|
342 WRITELOG1( "RHarvesterClient::HarvestFileWithUID() - file %S", &aURI ); |
|
343 |
|
344 HBufC8* paramBuf = NULL; |
|
345 TRAPD( err, paramBuf = SerializeArrayL( aAlbumIds ) ); |
|
346 if ( err ) |
|
347 { |
|
348 WRITELOG1( "RHarvesterClient::HarvestFileWithUID() - cannot create serialized array, error: %d", err ); |
|
349 if( iObserver ) |
|
350 { |
|
351 iObserver->HarvestingComplete( const_cast<TDesC&>(aURI), err ); |
|
352 } |
|
353 return; |
|
354 } |
|
355 |
|
356 CHarvesterRequestActive* harvestFileActive( NULL ); |
|
357 TRAP( err, harvestFileActive = CHarvesterRequestActive::NewL( *this, iObserver, (TInt)EHarvestFile, aURI, |
|
358 paramBuf, aAddLocation, iRequestQueue ) ); |
|
359 if( err ) |
|
360 { |
|
361 WRITELOG1( "RHarvesterClient::HarvestFile() - cannot create harvesting request, error: %d", err ); |
|
362 if( iObserver ) |
|
363 { |
|
364 iObserver->HarvestingComplete( const_cast<TDesC&>(aURI), err ); |
|
365 } |
|
366 return; |
|
367 } |
|
368 |
|
369 // send actually harvest request to server |
|
370 if( iHandle ) |
|
371 { |
|
372 TRAP( err, iRequestQueue->AddRequestL( harvestFileActive ) ); |
|
373 if( err && iObserver) |
|
374 { |
|
375 WRITELOG1( "RHarvesterClient::HarvestFile() - cannot not send harvest request to server, error: %d", KErrServerBusy ); |
|
376 iObserver->HarvestingComplete( const_cast<TDesC&>(aURI), KErrServerBusy ); |
|
377 delete harvestFileActive; |
|
378 } |
|
379 else if( err ) |
|
380 { |
|
381 WRITELOG1( "RHarvesterClient::HarvestFile() - cannot not send harvest request to server, error: %d", KErrServerBusy ); |
|
382 delete harvestFileActive; |
|
383 } |
|
384 else |
|
385 { |
|
386 iRequestQueue->Process(); |
|
387 } |
|
388 } |
|
389 else if( iObserver ) |
|
390 { |
|
391 WRITELOG1( "RHarvesterClient::HarvestFile() - cannot not send harvest request to server, error: %d", KErrDisconnected ); |
|
392 iObserver->HarvestingComplete( const_cast<TDesC&>(aURI), KErrDisconnected ); |
|
393 delete harvestFileActive; |
|
394 } |
|
395 else |
|
396 { |
|
397 WRITELOG1( "RHarvesterClient::HarvestFile() - cannot not send harvest request to server, error: %d", KErrDisconnected ); |
|
398 delete harvestFileActive; |
|
399 } |
|
400 } |
|
401 |
|
402 // ---------------------------------------------------------------------------------------- |
|
403 // RegisterHarvestComplete |
|
404 // ---------------------------------------------------------------------------------------- |
|
405 // |
|
406 void RHarvesterClient::RegisterHarvestComplete(TDes& aURI, TRequestStatus& aStatus) |
|
407 { |
|
408 TIpcArgs ipcArgs( &aURI ); |
|
409 |
|
410 if( !iHandle ) |
|
411 { |
|
412 return; |
|
413 } |
|
414 SendReceive( ERegisterHarvestComplete, ipcArgs, aStatus); |
|
415 } |
|
416 |
|
417 |
|
418 // ---------------------------------------------------------------------------------------- |
|
419 // UnregisterHarvestComplete |
|
420 // ---------------------------------------------------------------------------------------- |
|
421 // |
|
422 void RHarvesterClient::UnregisterHarvestComplete() |
|
423 { |
|
424 if( !iHandle ) |
|
425 { |
|
426 return; |
|
427 } |
|
428 |
|
429 Send( EUnregisterHarvestComplete ); |
|
430 } |
|
431 |
|
432 // ---------------------------------------------------------------------------------------- |
|
433 // HarvestFile |
|
434 // ---------------------------------------------------------------------------------------- |
|
435 // |
|
436 void RHarvesterClient::HarvestFile( TInt& aService, TIpcArgs& aArgs, TRequestStatus& aStatus ) |
|
437 { |
|
438 // send to server harvesting complete observer |
|
439 iHarvesterClientAO->Active(); |
|
440 SendReceive( aService, aArgs, aStatus ); |
|
441 } |
|
442 |
|
443 // ---------------------------------------------------------------------------------------- |
|
444 // ForceHarvestFile |
|
445 // ---------------------------------------------------------------------------------------- |
|
446 // |
|
447 void RHarvesterClient::ForceHarvestFile( TInt& aService, TIpcArgs& aArgs ) |
|
448 { |
|
449 // send to server harvesting complete observer |
|
450 iHarvesterClientAO->Active(); |
|
451 SendReceive( aService, aArgs ); |
|
452 } |
|
453 |
|
454 // ---------------------------------------------------------------------------------------- |
|
455 // Version |
|
456 // ---------------------------------------------------------------------------------------- |
|
457 // |
|
458 TVersion RHarvesterClient::Version() const |
|
459 { |
|
460 WRITELOG( "RHarvesterClient::Version()" ); |
|
461 |
|
462 return TVersion( KHarvesterServerMajorVersion, KHarvesterServerMinorVersion, |
|
463 KHarvesterServerBuildVersion ); |
|
464 } |
|
465 |
|
466 // ---------------------------------------------------------------------------------------- |
|
467 // StartServer |
|
468 // ---------------------------------------------------------------------------------------- |
|
469 // |
|
470 static TInt StartServer() |
|
471 { |
|
472 WRITELOG( "StartServer() - begin" ); |
|
473 |
|
474 TFindServer findHarvesterServer( KHarvesterServerName ); |
|
475 TFullName name; |
|
476 |
|
477 TInt result = findHarvesterServer.Next( name ); |
|
478 if ( result == KErrNone ) |
|
479 { |
|
480 WRITELOG( "StartServer() - Server allready running" ); |
|
481 |
|
482 // Server already running |
|
483 return KErrNone; |
|
484 } |
|
485 else |
|
486 { |
|
487 WRITELOG1( "StartServer() error - error code: %d", result ); |
|
488 } |
|
489 |
|
490 result = CreateServerProcess(); |
|
491 if ( result != KErrNone ) |
|
492 { |
|
493 WRITELOG1( "StartServer() - creating process failed, error: %d", result ); |
|
494 } |
|
495 |
|
496 WRITELOG( "StartServer() - end" ); |
|
497 return result; |
|
498 } |
|
499 |
|
500 // ---------------------------------------------------------------------------------------- |
|
501 // CreateServerProcess |
|
502 // ---------------------------------------------------------------------------------------- |
|
503 // |
|
504 static TInt CreateServerProcess() |
|
505 { |
|
506 WRITELOG( "CreateServerProcess() - begin" ); |
|
507 RProcess server; |
|
508 TInt result = server.Create( KHarvesterServerExe, KNullDesC ); |
|
509 |
|
510 if( result == KErrAlreadyExists ) |
|
511 { |
|
512 return KErrNone; |
|
513 } |
|
514 |
|
515 if ( result != KErrNone ) |
|
516 { |
|
517 WRITELOG1( "CreateServerProcess() - failed to create server process, error: %d", result ); |
|
518 return result; |
|
519 } |
|
520 |
|
521 TRequestStatus stat( 0 ); |
|
522 server.Rendezvous( stat ); |
|
523 |
|
524 if ( stat != KRequestPending ) |
|
525 { |
|
526 server.Kill( 0 ); // abort startup |
|
527 } |
|
528 else |
|
529 { |
|
530 server.Resume(); // logon OK - start the server |
|
531 } |
|
532 |
|
533 User::WaitForRequest( stat ); // wait for start or death |
|
534 // we can't use the 'exit reason' if the server panicked as this |
|
535 // is the panic 'reason' and may be '0' wehich cannot be distinguished |
|
536 // from KErrNone |
|
537 result = ( server.ExitType() == EExitPanic ) ? KErrCommsBreak : stat.Int(); |
|
538 server.Close(); |
|
539 |
|
540 WRITELOG( "CreateServerProcess() - end" ); |
|
541 |
|
542 return result; |
|
543 } |
|
544 |
|
545 // End of file |
|
546 |