|
1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <e32svr.h> |
|
17 |
|
18 #include "TestFrameworkServer.h" |
|
19 |
|
20 #include "TestFrameworkServer.inl" |
|
21 |
|
22 #include <testframeworkipc.h> |
|
23 |
|
24 |
|
25 /** |
|
26 * |
|
27 * Initiate server exit when the timer expires |
|
28 * |
|
29 * @xxxx |
|
30 * |
|
31 */ |
|
32 void CTestFrameworkServerShutdown::RunL() |
|
33 { |
|
34 CActiveScheduler::Stop(); |
|
35 } |
|
36 |
|
37 /** |
|
38 * |
|
39 * Test Framework server static constructor |
|
40 * NOTE : only one instance of the server process should run |
|
41 * during any Test Framework run; all client sessions |
|
42 * will use this one instance. |
|
43 * |
|
44 * @xxxx |
|
45 * |
|
46 */ |
|
47 CMmfIpcServer* CTestFrameworkServer::NewL() |
|
48 { |
|
49 CTestFrameworkServer* s = new(ELeave) CTestFrameworkServer; |
|
50 CleanupStack::PushL(s); |
|
51 s->ConstructL(); |
|
52 CleanupStack::Pop(); |
|
53 return s; |
|
54 } |
|
55 |
|
56 /** |
|
57 * |
|
58 * Test Framework server first-phase constructor |
|
59 * |
|
60 * @xxxx |
|
61 * |
|
62 */ |
|
63 CTestFrameworkServer::CTestFrameworkServer() |
|
64 :CMmfIpcServer(0, ESharableSessions) |
|
65 { |
|
66 } |
|
67 |
|
68 // set up so that iConsole is NULL. A call to OpenLogL will initialise the console |
|
69 // and file (if any) |
|
70 |
|
71 /** |
|
72 * |
|
73 * Test Framework server second-phase constructor |
|
74 * Starts the server; does not initialise the log |
|
75 * (this must be done independently with a call to OpenLogL) |
|
76 * |
|
77 * @xxxx |
|
78 * |
|
79 */ |
|
80 void CTestFrameworkServer::ConstructL() |
|
81 { |
|
82 StartL(KTestFrameworkServerName); |
|
83 //Ensure that the server will exit even if the first client fails to connect |
|
84 iShutdown.ConstructL(); |
|
85 iShutdown.Start(); |
|
86 |
|
87 iLogMode = 0; // initial value : not running any log |
|
88 iConsole = NULL; |
|
89 iFileLogger = NULL; |
|
90 } |
|
91 |
|
92 /** |
|
93 * |
|
94 * Test Framework server destructor |
|
95 * |
|
96 * @xxxx |
|
97 * |
|
98 */ |
|
99 CTestFrameworkServer::~CTestFrameworkServer() |
|
100 { |
|
101 CloseLog(); |
|
102 } |
|
103 |
|
104 /** |
|
105 * |
|
106 * Create new server session. |
|
107 * |
|
108 * @param "const TVersion &aVersion" |
|
109 * Server version (required) |
|
110 * |
|
111 * @return "CSharableSession*" |
|
112 * The created server session. |
|
113 * |
|
114 * @xxxx |
|
115 * |
|
116 */ |
|
117 CMmfIpcSession* CTestFrameworkServer::NewSessionL(const TVersion &aVersion) const |
|
118 { |
|
119 TVersion ver(KTestFrameworkServerMajorVersionNumber, |
|
120 KTestFrameworkServerMinorVersionNumber, |
|
121 KTestFrameworkServerBuildVersionNumber); |
|
122 if (!User::QueryVersionSupported(ver, aVersion)) |
|
123 User::Leave(KErrNotSupported); |
|
124 // make new session |
|
125 return new(ELeave) CTestFrameworkServerSession(); |
|
126 } |
|
127 |
|
128 /** |
|
129 * |
|
130 * Add a server session. |
|
131 * |
|
132 * @xxxx |
|
133 * |
|
134 */ |
|
135 void CTestFrameworkServer::AddSession() |
|
136 { |
|
137 ++iSessionCount; |
|
138 iShutdown.Cancel(); |
|
139 } |
|
140 |
|
141 /** |
|
142 * |
|
143 * Drop a server session. |
|
144 * |
|
145 * @xxxx |
|
146 * |
|
147 */ |
|
148 void CTestFrameworkServer::DropSession() |
|
149 { |
|
150 if (--iSessionCount <= 0) |
|
151 iShutdown.Start(); |
|
152 } |
|
153 |
|
154 /** |
|
155 * |
|
156 * Add an input window (at request from client). |
|
157 * |
|
158 * @param "CTestFrameworkServerSession* aOwner" |
|
159 * Window owning server session |
|
160 * |
|
161 * @xxxx |
|
162 * |
|
163 */ |
|
164 void CTestFrameworkServer::AddInputWindowL(CTestFrameworkServerSession* aOwner) |
|
165 { |
|
166 if (iInputWindow.HasOwner()) |
|
167 User::Leave(KErrAlreadyExists); |
|
168 // else |
|
169 iInputWindow.SetOwner(aOwner); |
|
170 TRect rect; |
|
171 rect.iTl = TPoint(0,0); |
|
172 rect.iBr = TPoint(KConsFullScreen, KConsFullScreen); |
|
173 iInputWindow.SetWinRectAndNotifyOwner(rect); |
|
174 } |
|
175 |
|
176 /** |
|
177 * |
|
178 * Remove an input window (at request from client). |
|
179 * |
|
180 * @param "CTestFrameworkServerSession* aOwner" |
|
181 * Window owning server session |
|
182 * |
|
183 * @xxxx |
|
184 * |
|
185 */ |
|
186 void CTestFrameworkServer::RemoveWindow(CTestFrameworkServerSession* aOwner) |
|
187 { |
|
188 if (aOwner == iInputWindow.Owner()) |
|
189 { |
|
190 iInputWindow.SetOwner(NULL); |
|
191 } |
|
192 } |
|
193 |
|
194 /** |
|
195 * |
|
196 * Open a log. |
|
197 * |
|
198 * @param "const TDesC& aLogName" |
|
199 * The log name |
|
200 * |
|
201 * @param "TInt aLogMode" |
|
202 * The log mode (as bitmask of TTestFrameworkLogMode) |
|
203 * |
|
204 * @xxxx |
|
205 * |
|
206 */ |
|
207 void CTestFrameworkServer::OpenLogL(const TDesC& aLogName, TInt aLogMode) |
|
208 { |
|
209 // NB we need to check if a console is already open - if so, we do NOT |
|
210 // create another one. Ditto with file / port. |
|
211 |
|
212 if(aLogMode & ELogToConsole) |
|
213 { |
|
214 if(!iConsole) |
|
215 { |
|
216 iConsole = CServerConsole::NewL(aLogName); |
|
217 |
|
218 CConsoleBase* theConsole = iConsole->Console(); |
|
219 theConsole->Printf(_L("%S : Server log starting\n"), &aLogName); |
|
220 |
|
221 iLogMode |= ELogToConsole; |
|
222 |
|
223 if (aLogMode & ELogConsoleFull) |
|
224 iLogMode |= ELogConsoleFull; |
|
225 } |
|
226 } |
|
227 |
|
228 // NB relative paths will not work with TParse (there is no file server open). |
|
229 // Exception is a bare filename (with no path) : this will be found in root of C: |
|
230 |
|
231 // NOTE! We have no mechanism to notify this error. The console will display |
|
232 // and then exit. The log file cannot be opened. |
|
233 |
|
234 // TO BE ENHANCED - if console is made active, then we can pause |
|
235 |
|
236 if(aLogMode & ELogToFile) |
|
237 { |
|
238 if(!iFileLogger) |
|
239 { |
|
240 TRAPD(err, iFileLogger = CFileLogger::NewL()); |
|
241 if(err != KErrNone) |
|
242 { |
|
243 // if we can't create a logger, we panic |
|
244 User::Panic(_L("TestFrameworkServer"), 1); |
|
245 } |
|
246 |
|
247 _LIT(KLogPath, "C:\\Logs\\TestResults"); |
|
248 _LIT(KDefault, "C:\\.htm"); |
|
249 |
|
250 TParse parseLogName; |
|
251 parseLogName.Set(aLogName, NULL, NULL); |
|
252 |
|
253 TFileName logFilePath; |
|
254 logFilePath = KLogPath; |
|
255 |
|
256 if(parseLogName.PathPresent()) |
|
257 logFilePath.Append(parseLogName.Path()); |
|
258 else |
|
259 logFilePath.Append(_L("\\")); |
|
260 |
|
261 |
|
262 // overwrite extension if supplied with .htm |
|
263 TParse logFileFullName; |
|
264 TInt returnCode = logFileFullName.Set(KDefault, &logFilePath, &aLogName); |
|
265 if (returnCode == KErrNone) |
|
266 { |
|
267 TInt ret = iFileLogger->Connect(); |
|
268 if (ret == KErrNone) |
|
269 { |
|
270 iFileLogger->CreateLog(logFilePath, logFileFullName.NameAndExt()); |
|
271 iLogMode |= ELogToFile; |
|
272 } |
|
273 } |
|
274 } |
|
275 } |
|
276 |
|
277 if(aLogMode & ELogToPort) |
|
278 { |
|
279 // RDebug::Print will deal with the serial port, we don't do anything special here |
|
280 iLogMode |= ELogToPort; |
|
281 } |
|
282 } |
|
283 |
|
284 /** |
|
285 * |
|
286 * Write to the log. |
|
287 * |
|
288 * @param "const TDesC& aMsg" |
|
289 * The message string to write |
|
290 * |
|
291 * @param "TInt aLogMode" |
|
292 * The log mode (as bitmask of TTestFrameworkLogMode) |
|
293 * |
|
294 * @xxxx |
|
295 * |
|
296 */ |
|
297 void CTestFrameworkServer::WriteLog(const TDesC& aMsg, TInt aLogMode) |
|
298 { |
|
299 if(aLogMode & ELogToConsole) |
|
300 { |
|
301 if(iConsole) |
|
302 { |
|
303 CConsoleBase* theConsole = iConsole->Console(); |
|
304 theConsole->Printf(aMsg); |
|
305 theConsole->Printf(_L("\n")); // add newline |
|
306 } |
|
307 } |
|
308 |
|
309 if(aLogMode & ELogToFile) |
|
310 { |
|
311 if(iFileLogger) |
|
312 { |
|
313 iFileLogger->WriteLog(aMsg); |
|
314 } |
|
315 } |
|
316 |
|
317 if(aLogMode & ELogToPort) |
|
318 { |
|
319 RDebug::Print(aMsg); |
|
320 } |
|
321 } |
|
322 |
|
323 /** |
|
324 * |
|
325 * Close the log. |
|
326 * |
|
327 * @xxxx |
|
328 * |
|
329 */ |
|
330 void CTestFrameworkServer::CloseLog() |
|
331 { |
|
332 if (iFileLogger != NULL) |
|
333 iFileLogger->CloseLog(); |
|
334 delete(iFileLogger); |
|
335 iFileLogger = NULL; |
|
336 delete(iConsole); |
|
337 iConsole = NULL; |
|
338 iLogMode = 0; |
|
339 } |
|
340 |
|
341 /** |
|
342 * |
|
343 * Get the log status. |
|
344 * |
|
345 * @return "TInt" |
|
346 * The log status (as bitmask of TTestFrameworkLogMode) |
|
347 * |
|
348 * @xxxx |
|
349 * |
|
350 */ |
|
351 TInt CTestFrameworkServer::LogStatus() const |
|
352 { |
|
353 return iLogMode; |
|
354 } |
|
355 |
|
356 |
|
357 /** |
|
358 * |
|
359 * Default window constructor (no owner) |
|
360 * |
|
361 * @xxxx |
|
362 * |
|
363 */ |
|
364 TWindow::TWindow() |
|
365 { |
|
366 iOwner = NULL; |
|
367 } |
|
368 |
|
369 /** |
|
370 * |
|
371 * Default window constructor |
|
372 * |
|
373 * @param "CTestFrameworkServerSession* aOwner" |
|
374 * The window owner |
|
375 * @xxxx |
|
376 * |
|
377 */ |
|
378 TWindow::TWindow(CTestFrameworkServerSession* aOwner) |
|
379 { |
|
380 iOwner = aOwner; |
|
381 } |
|
382 |
|
383 /** |
|
384 * |
|
385 * Set the window owner |
|
386 * |
|
387 * @param "CTestFrameworkServerSession* aOwner" |
|
388 * The window owner |
|
389 * @xxxx |
|
390 * |
|
391 */ |
|
392 void TWindow::SetOwner(CTestFrameworkServerSession* aOwner) |
|
393 { |
|
394 iOwner = aOwner; |
|
395 } |
|
396 |
|
397 /** |
|
398 * |
|
399 * Set the window rectangle, and notify owner |
|
400 * |
|
401 * @param "const TRect& aWinRect" |
|
402 * The window rectangle |
|
403 * @xxxx |
|
404 * |
|
405 */ |
|
406 void TWindow::SetWinRectAndNotifyOwner(const TRect& aWinRect) |
|
407 { |
|
408 if (HasOwner()) |
|
409 iOwner->NotifyWindowChanged(aWinRect); |
|
410 } |
|
411 |
|
412 /** |
|
413 * |
|
414 * Does this window have an owner? |
|
415 * |
|
416 * @return "TBool" |
|
417 * ETrue if window has an owner |
|
418 * @xxxx |
|
419 * |
|
420 */ |
|
421 TBool TWindow::HasOwner() |
|
422 { |
|
423 if (iOwner) |
|
424 return ETrue; |
|
425 return EFalse; |
|
426 } |
|
427 |
|
428 /** |
|
429 * |
|
430 * Server session first-phase constructor |
|
431 * |
|
432 * @xxxx |
|
433 * |
|
434 */ |
|
435 CTestFrameworkServerSession::CTestFrameworkServerSession() |
|
436 { |
|
437 } |
|
438 |
|
439 /** |
|
440 * |
|
441 * Create a server session. |
|
442 * |
|
443 * @param "aServer" |
|
444 * The server to add this session to |
|
445 * |
|
446 * @xxxx |
|
447 * |
|
448 */ |
|
449 void CTestFrameworkServerSession::CreateL(const CMmfIpcServer& aServer) |
|
450 { |
|
451 CMmfIpcSession::CreateL(aServer); // does not leave |
|
452 //Add session to server first. If anything leaves, it will be removed by the destructor |
|
453 iServer = STATIC_CAST(CTestFrameworkServer*, (CONST_CAST(CMmfIpcServer*, &aServer))); |
|
454 iServer->AddSession(); |
|
455 ConstructL(); |
|
456 } |
|
457 |
|
458 /** |
|
459 * |
|
460 * Server session second-phase constructor |
|
461 * |
|
462 * @xxxx |
|
463 * |
|
464 */ |
|
465 void CTestFrameworkServerSession::ConstructL() |
|
466 { |
|
467 } |
|
468 |
|
469 /** |
|
470 * |
|
471 * Server session destructor |
|
472 * |
|
473 * @xxxx |
|
474 * |
|
475 */ |
|
476 CTestFrameworkServerSession::~CTestFrameworkServerSession() |
|
477 { |
|
478 iServer->RemoveWindow(this); |
|
479 iServer->DropSession(); |
|
480 } |
|
481 |
|
482 /** |
|
483 * |
|
484 * Server session service function |
|
485 * |
|
486 * @param "aMessage" |
|
487 * The message to be serviced. |
|
488 * |
|
489 * @xxxx |
|
490 * |
|
491 */ |
|
492 void CTestFrameworkServerSession::ServiceL(const RMmfIpcMessage& aMessage) |
|
493 { |
|
494 switch (aMessage.Function()) |
|
495 { |
|
496 case ECreateInputWindow: |
|
497 SetOwnCopyOfWindowMessageL(aMessage); |
|
498 iServer->AddInputWindowL(this); |
|
499 break; |
|
500 case ENotifyIfWindowChange: |
|
501 SetOwnCopyOfWindowMessageL(aMessage); |
|
502 break; |
|
503 case ECancelNotifyIfWindowChange: |
|
504 CompleteOwnCopyOfWindowMessage(KErrCancel); |
|
505 aMessage.Complete(KErrNone); |
|
506 break; |
|
507 |
|
508 // logging messages |
|
509 |
|
510 case EOpenLog: |
|
511 { |
|
512 TBuf<KMaxLogFilenameLength> msgBuf; |
|
513 |
|
514 TInt r = MmfMessageUtilX::Read(aMessage, 0, msgBuf); |
|
515 if (r != KErrNone) |
|
516 RunError(aMessage, r); |
|
517 else |
|
518 { |
|
519 iServer->OpenLogL(msgBuf, aMessage.Int1()); |
|
520 aMessage.Complete(KErrNone); |
|
521 } |
|
522 } |
|
523 break; |
|
524 |
|
525 case EWriteLog: |
|
526 { |
|
527 TBuf<KMaxLogLineLength> msgBuf; |
|
528 |
|
529 TInt r = MmfMessageUtilX::Read(aMessage, 0, msgBuf); |
|
530 |
|
531 if (r != KErrNone) |
|
532 RunError(aMessage, r); |
|
533 else |
|
534 { |
|
535 iServer->WriteLog(msgBuf, aMessage.Int1()); |
|
536 aMessage.Complete(KErrNone); |
|
537 } |
|
538 } |
|
539 break; |
|
540 |
|
541 case ECloseLog: |
|
542 iServer->CloseLog(); |
|
543 aMessage.Complete(KErrNone); |
|
544 break; |
|
545 |
|
546 case ELogStatus: |
|
547 { |
|
548 TPckgBuf<TInt> countPckg(iServer->LogStatus()); |
|
549 TInt r = MmfMessageUtilX::Write(aMessage, 0, countPckg); |
|
550 if (r != KErrNone) |
|
551 RunError(aMessage, r); |
|
552 else |
|
553 aMessage.Complete(KErrNone); |
|
554 } |
|
555 break; |
|
556 |
|
557 default: |
|
558 // ? should panic here |
|
559 break; |
|
560 } |
|
561 } |
|
562 |
|
563 /** |
|
564 * |
|
565 * Error handler for server session. |
|
566 * Completes the pending message using the error code |
|
567 * |
|
568 * @param "TInt aError" |
|
569 * The error code |
|
570 * |
|
571 * @return "TInt" |
|
572 * The error code (always KErrNone) |
|
573 * |
|
574 * @xxxx |
|
575 * |
|
576 */ |
|
577 TInt CTestFrameworkServerSession::RunError(const RMmfIpcMessage& aMessage, TInt aError) |
|
578 { |
|
579 aMessage.Complete(aError); |
|
580 return KErrNone; |
|
581 } |
|
582 |
|
583 /** |
|
584 * |
|
585 * Set own copy of message. |
|
586 * Helper function to enable window change notification. |
|
587 * |
|
588 * @param "aMessage" |
|
589 * The message to be serviced. |
|
590 * |
|
591 * @xxxx |
|
592 * |
|
593 */ |
|
594 void CTestFrameworkServerSession::SetOwnCopyOfWindowMessageL(const RMmfIpcMessage& aMessage) |
|
595 { |
|
596 if (iCanCompleteWindowMessage) |
|
597 User::Leave(KErrAlreadyExists); |
|
598 //else |
|
599 iWindowMessage = aMessage; |
|
600 iCanCompleteWindowMessage = ETrue; |
|
601 |
|
602 if (iNeedToNotifyClientOfWindowSizeChange) |
|
603 { |
|
604 TInt err = MmfMessageUtil::Write(iWindowMessage, 0, iWinRectBuf); |
|
605 CompleteOwnCopyOfWindowMessage(err); |
|
606 iNeedToNotifyClientOfWindowSizeChange = EFalse; |
|
607 } |
|
608 } |
|
609 |
|
610 /** |
|
611 * |
|
612 * Complete own copy of message. |
|
613 * Helper function to enable window change notification. |
|
614 * |
|
615 * @param "TInt aReason" |
|
616 * The message return code. |
|
617 * |
|
618 * @xxxx |
|
619 * |
|
620 */ |
|
621 void CTestFrameworkServerSession::CompleteOwnCopyOfWindowMessage(TInt aReason) |
|
622 { |
|
623 if (iCanCompleteWindowMessage) |
|
624 iWindowMessage.Complete(aReason); |
|
625 iCanCompleteWindowMessage = EFalse; |
|
626 } |
|
627 |
|
628 /** |
|
629 * |
|
630 * Window change notification. |
|
631 * |
|
632 * @param "const TRect& aWinRect" |
|
633 * The window rectangle. |
|
634 * |
|
635 * @xxxx |
|
636 * |
|
637 */ |
|
638 void CTestFrameworkServerSession::NotifyWindowChanged(const TRect& aWinRect) |
|
639 { |
|
640 iWinRectBuf() = aWinRect; |
|
641 |
|
642 if (iCanCompleteWindowMessage) |
|
643 { |
|
644 TInt err = MmfMessageUtilX::Write(iWindowMessage, 0, iWinRectBuf); |
|
645 CompleteOwnCopyOfWindowMessage(err); |
|
646 } |
|
647 else |
|
648 { |
|
649 iNeedToNotifyClientOfWindowSizeChange = ETrue; |
|
650 } |
|
651 } |
|
652 |
|
653 // EXE/DLL initialisation |
|
654 |
|
655 // NOTE! None of this should be built when compiling for Unit Testing |
|
656 #if !defined (__TSU_TESTFRAMEWORK__) |
|
657 |
|
658 |
|
659 /** |
|
660 * |
|
661 * Perform all server initialisation, in particular creation of the |
|
662 * scheduler and server; then run the scheduler |
|
663 * |
|
664 * |
|
665 * @xxxx |
|
666 * |
|
667 */ |
|
668 static void RunServerL() |
|
669 { |
|
670 // create and install the active scheduler we need |
|
671 CActiveScheduler* s = new(ELeave) CActiveScheduler; |
|
672 CleanupStack::PushL(s); |
|
673 CActiveScheduler::Install(s); |
|
674 // |
|
675 // create the server (leave it on the cleanup stack) |
|
676 CleanupStack::PushL(CTestFrameworkServer::NewL()); |
|
677 // |
|
678 // Initialisation complete, now signal the client |
|
679 RProcess::Rendezvous(KErrNone); |
|
680 |
|
681 // |
|
682 // Ready to run |
|
683 CActiveScheduler::Start(); |
|
684 // |
|
685 // Cleanup the server and scheduler |
|
686 CleanupStack::PopAndDestroy(2); |
|
687 } |
|
688 |
|
689 /** |
|
690 * |
|
691 * Main entry point for the server thread |
|
692 * |
|
693 * @param "TTestFrameworkServerStart& aStart" |
|
694 * The server starter. |
|
695 * |
|
696 * @xxxx |
|
697 * |
|
698 */ |
|
699 static TInt RunServer() |
|
700 { |
|
701 __UHEAP_MARK; |
|
702 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
703 TInt r = KErrNoMemory; |
|
704 if (cleanup) |
|
705 { |
|
706 TRAP(r, RunServerL()); |
|
707 delete cleanup; |
|
708 } |
|
709 // |
|
710 __UHEAP_MARKEND; |
|
711 return r; |
|
712 } |
|
713 |
|
714 |
|
715 GLDEF_C TInt E32Main() |
|
716 |
|
717 // Server process entry-point |
|
718 // Recover the startup parameters and run the server |
|
719 |
|
720 { |
|
721 TInt r = RunServer(); |
|
722 return r; |
|
723 } |
|
724 |
|
725 |
|
726 #endif // __TSU_TESTFRAMEWORK__ |