|
1 // Copyright (c) 2006-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 "sysmondemo.h" |
|
17 #include "e32cons.h" |
|
18 #include "e32cmn.h" |
|
19 #include "bacline.h" |
|
20 #include "restartsys.h" |
|
21 |
|
22 _LIT(KPOKE, "poke") ; |
|
23 _LIT(KKILL, "kill") ; |
|
24 _LIT(KDONE, "done") ; |
|
25 _LIT(KRESTART, "restart") ; |
|
26 |
|
27 |
|
28 static TInt StartServer() |
|
29 // |
|
30 // Start the server process. Simultaneous launching |
|
31 // of two such processes should be detected when the second one attempts to |
|
32 // create the server object, failing with KErrAlreadyExists. |
|
33 // |
|
34 { |
|
35 /* we shall not load the server if it is not there*/ |
|
36 return -1 ; |
|
37 } |
|
38 |
|
39 |
|
40 TInt RDemoCli::Open() |
|
41 { |
|
42 TInt retry=2; |
|
43 for (;;) |
|
44 { |
|
45 TInt r=CreateSession(KDemoServerName,TVersion(0,0,0),1); |
|
46 if (r!=KErrNotFound && r!=KErrServerTerminated) |
|
47 return r; |
|
48 if (--retry==0) |
|
49 return r; |
|
50 r=StartServer(); |
|
51 if (r!=KErrNone && r!=KErrAlreadyExists) |
|
52 return r; |
|
53 } |
|
54 } |
|
55 |
|
56 void RDemoCli::Close() |
|
57 { |
|
58 RSessionBase::Close() ; |
|
59 } |
|
60 |
|
61 void RDemoCli::Poke(TTime &time) |
|
62 { |
|
63 // TPckgBuf<TTime*> buf(&time) ; |
|
64 // TIpcArgs ipcArgs(&buf) ; |
|
65 |
|
66 TPckg<TTime> buf(time) ; |
|
67 TIpcArgs ipcArgs(&buf) ; |
|
68 |
|
69 SendReceive(EServerPoke,ipcArgs) ; |
|
70 } |
|
71 |
|
72 void RDemoCli::Kill(TTime &time) |
|
73 { |
|
74 // TPckgBuf<TTime*> buf(&time) ; |
|
75 // TIpcArgs ipcArgs(&buf) ; |
|
76 |
|
77 TPckg<TTime> buf(time) ; |
|
78 TIpcArgs ipcArgs(&buf) ; |
|
79 |
|
80 SendReceive(EServerKill,ipcArgs) ; |
|
81 } |
|
82 |
|
83 void RDemoCli::Done(TTime &time) |
|
84 { |
|
85 // TPckgBuf<TTime*> buf(&time) ; |
|
86 // TIpcArgs ipcArgs(&buf) ; |
|
87 |
|
88 TPckg<TTime> buf(time) ; |
|
89 TIpcArgs ipcArgs(&buf) ; |
|
90 |
|
91 SendReceive(EServerDone,ipcArgs) ; |
|
92 } |
|
93 |
|
94 CConsoleBase *gConsole ; |
|
95 |
|
96 void TestDemoL() |
|
97 { |
|
98 RDemoCli demoCli ; |
|
99 |
|
100 TTime time ; |
|
101 gConsole = Console::NewL(_L("Demo"), TSize(KConsFullScreen, KConsFullScreen)) ; |
|
102 |
|
103 CleanupStack::PushL(gConsole) ; |
|
104 |
|
105 CCommandLineArguments* args = CCommandLineArguments::NewL() ; |
|
106 CleanupStack::PushL(args) ; |
|
107 |
|
108 TPtrC command ; |
|
109 command.Set(KNullDesC) ; |
|
110 |
|
111 if (args->Count() > 1) |
|
112 { |
|
113 command.Set(args->Arg(1)) ; |
|
114 } |
|
115 else |
|
116 { |
|
117 gConsole->Printf(_L("Usage: sysmondemocli [poke|kill|done|restart <mode>]\n")) ; |
|
118 } |
|
119 |
|
120 if (command.Compare(KRESTART) == 0) |
|
121 { |
|
122 |
|
123 TBuf<10> modeBuf = args->Arg(2) ; |
|
124 |
|
125 TPtr8 data = modeBuf.Collapse() ; |
|
126 |
|
127 TLex8 txt8 = TLex8(const_cast<TUint8*> (data.PtrZ())) ; |
|
128 |
|
129 TInt mode ; |
|
130 txt8.Val(mode) ; |
|
131 |
|
132 TInt err = RestartSys::RestartSystem(mode) ; /* restart in a different mode */ |
|
133 |
|
134 User::Leave(err) ; |
|
135 } |
|
136 |
|
137 |
|
138 if (demoCli.Open() != KErrNone) |
|
139 { |
|
140 gConsole->Printf(_L("The SysMonDemo server is not there - bye\n")) ; |
|
141 |
|
142 gConsole->Printf(_L("Press any key to continue...\n")) ; |
|
143 gConsole->Getch() ; |
|
144 |
|
145 User::Leave(KErrNone) ; |
|
146 } |
|
147 |
|
148 CleanupClosePushL(demoCli) ; |
|
149 |
|
150 if (command.Compare(KPOKE) == 0) |
|
151 { |
|
152 demoCli.Poke(time) ; |
|
153 gConsole->Printf(_L("The SysMonDemo server was last loaded at (local time):\n")) ; |
|
154 TDateTime dt = time.DateTime() ; |
|
155 gConsole->Printf(_L("Year %d Month %d Day %d Hour %d Min %d Sec %d\n"), dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), dt.Second()) ; |
|
156 } |
|
157 else if (command.Compare(KKILL) == 0) |
|
158 { |
|
159 demoCli.Kill(time) ; |
|
160 gConsole->Printf(_L("The SysMonDemo server has been killed.\n")) ; |
|
161 gConsole->Printf(_L("The time of death:\n")) ; |
|
162 TDateTime dt = time.DateTime() ; |
|
163 gConsole->Printf(_L("Year %d Month %d Day %d Hour %d Min %d Sec %d\n"), dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), dt.Second()) ; |
|
164 |
|
165 gConsole->Printf(_L("SysMon is going to reload SysMonDemo server soon.\n")) ; |
|
166 } |
|
167 else if (command.Compare(KDONE) == 0) |
|
168 { |
|
169 demoCli.Done(time) ; |
|
170 gConsole->Printf(_L("Monitoring has been cancelled for SysMonDemo\n")) ; |
|
171 gConsole->Printf(_L("The SysMonDemo server has been killed. But it won't be reloaded\n")) ; |
|
172 gConsole->Printf(_L("The time of death:\n")) ; |
|
173 TDateTime dt = time.DateTime() ; |
|
174 gConsole->Printf(_L("Year %d Month %d Day %d Hour %d Min %d Sec %d\n"), dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), dt.Second()) ; |
|
175 } |
|
176 |
|
177 |
|
178 // User::After(10000000) ; // let the console stay for a while |
|
179 |
|
180 gConsole->Printf(_L("Press any key to continue...\n")) ; |
|
181 gConsole->Getch() ; |
|
182 |
|
183 |
|
184 CleanupStack::PopAndDestroy(&demoCli) ; |
|
185 CleanupStack::PopAndDestroy(args) ; |
|
186 CleanupStack::PopAndDestroy(gConsole) ; |
|
187 |
|
188 |
|
189 } |
|
190 |
|
191 |
|
192 |
|
193 TInt E32Main() |
|
194 { |
|
195 __UHEAP_MARK; |
|
196 |
|
197 CTrapCleanup* cleanup=CTrapCleanup::New(); |
|
198 |
|
199 TInt err = KErrNone ; |
|
200 |
|
201 // for(;;) |
|
202 { |
|
203 TRAP(err, TestDemoL()); |
|
204 |
|
205 } |
|
206 |
|
207 |
|
208 delete cleanup; |
|
209 |
|
210 __UHEAP_MARKEND; |
|
211 |
|
212 return (err); |
|
213 } |
|
214 |
|
215 |