|
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 #include <e32svr.h> |
|
18 |
|
19 #include <stfeventsystemerrorcodes.h> |
|
20 #include "eventsystemserver.h" |
|
21 #include "eventsystemsession.h" |
|
22 |
|
23 |
|
24 /** |
|
25 First phase construction |
|
26 */ |
|
27 CEventSystemServer* CEventSystemServer::NewL(CActive::TPriority aActiveObjectPriority) |
|
28 { |
|
29 //RDebug::Print(_L("STF [ESS]: CEventSystemServer::NewL")); |
|
30 CEventSystemServer* self = NewLC(aActiveObjectPriority); |
|
31 CleanupStack::Pop(); |
|
32 return self; |
|
33 } |
|
34 |
|
35 /** |
|
36 First phase construction |
|
37 */ |
|
38 CEventSystemServer* CEventSystemServer::NewLC(CActive::TPriority aActiveObjectPriority) |
|
39 { |
|
40 //RDebug::Print(_L("STF [ESS]: CEventSystemServer::NewLC")); |
|
41 CEventSystemServer* self = new (ELeave) CEventSystemServer(aActiveObjectPriority); |
|
42 CleanupStack::PushL(self); |
|
43 self->ConstructL(); |
|
44 return self; |
|
45 } |
|
46 |
|
47 /** |
|
48 Creates and returns a new object container using the server's object container index. |
|
49 This is a service that is used by a session. |
|
50 */ |
|
51 CObjectCon* CEventSystemServer::NewContainerL() |
|
52 { |
|
53 //RDebug::Print(_L("STF [ESS]: CEventSystemServer::NewContainerL")); |
|
54 return iContainerIndex->CreateL(); |
|
55 } |
|
56 |
|
57 /** |
|
58 Removes session object from container. |
|
59 */ |
|
60 void CEventSystemServer::RemoveContainer(CObjectCon *aObj) |
|
61 { |
|
62 //RDebug::Print(_L("STF [ESS]: CEventSystemServer::RemoveContainer aObj=[%x]"), aObj); |
|
63 iContainerIndex->Remove(aObj); |
|
64 } |
|
65 |
|
66 |
|
67 /** |
|
68 Constructor takes the server priority value. |
|
69 |
|
70 The server is an active object, and the priority value is the priority |
|
71 of this active object. |
|
72 |
|
73 It passes the priority value to the base class in the Ctor list. |
|
74 By default, the session is not sharable, which is what we want here |
|
75 so no second parameter is passed to the CServer2 constructor. |
|
76 */ |
|
77 CEventSystemServer::CEventSystemServer(CActive::TPriority aActiveObjectPriority) |
|
78 : CServer2(aActiveObjectPriority) |
|
79 { |
|
80 //RDebug::Print(_L("STF [ESS]: CEventSystemServer::CEventSystemServer")); |
|
81 } |
|
82 |
|
83 |
|
84 /** |
|
85 Second-phase constructor - creates the object container index. |
|
86 */ |
|
87 void CEventSystemServer::ConstructL() |
|
88 { |
|
89 //RDebug::Print(_L("STF [ESS]: CEventSystemServer::ConstructL")); |
|
90 StartL(KEventSystemServName); |
|
91 iContainerIndex = CObjectConIx::NewL(); |
|
92 //RDebug::Print(_L("STF [ESS]: CEventSystemServer::ConstructL created iContainerIndex=[%x]"), iContainerIndex); |
|
93 iShutdown.ConstructL(); |
|
94 iShutdown.Start(); |
|
95 } |
|
96 |
|
97 |
|
98 /** |
|
99 Desctructor - deletes the object container index. |
|
100 */ |
|
101 CEventSystemServer::~CEventSystemServer() |
|
102 { |
|
103 //RDebug::Print(_L("STF [ESS]: CEventSystemServer::~CEventSystemServer iContainerIndex=[%x]"), iContainerIndex); |
|
104 delete iContainerIndex; |
|
105 iWaitingEvents.ResetAndDestroy(); |
|
106 iWaitingEvents.Close(); |
|
107 iStateEvents.ResetAndDestroy(); |
|
108 iStateEvents.Close(); |
|
109 } |
|
110 |
|
111 |
|
112 /** |
|
113 Creates a new session with the server. |
|
114 */ |
|
115 CSession2* CEventSystemServer::NewSessionL(const TVersion &aVersion, const RMessage2& /*aMessage*/) const |
|
116 { |
|
117 RDebug::Print(_L("STF [ESS]: CEventSystemServer::NewSessionL")); |
|
118 // Check that the version is OK |
|
119 TVersion version(KEventSystemServMajorVersionNumber, KEventSystemServMinorVersionNumber, KEventSystemServBuildVersionNumber); |
|
120 if(!User::QueryVersionSupported(version, aVersion)) |
|
121 { |
|
122 User::Leave(EEventSystemVersionNotSupported); |
|
123 } |
|
124 |
|
125 // Create the session. |
|
126 CSession2* session = new (ELeave) CEventSystemSession; |
|
127 |
|
128 // Return session |
|
129 return session; |
|
130 } |
|
131 |
|
132 /** |
|
133 Adds session. |
|
134 */ |
|
135 void CEventSystemServer::AddSession(void) |
|
136 { |
|
137 //RDebug::Print(_L("STF [ESS]: CEventSystemServer::AddSession")); |
|
138 iSessionCounter++; |
|
139 RDebug::Print(_L("STF [ESS]: CEventSystemServer::AddSession iSessionCounter increased to %d"), iSessionCounter); |
|
140 iShutdown.Cancel(); |
|
141 } |
|
142 |
|
143 /** |
|
144 Closes session and starts shutdown timer in case no more sessions are opened. |
|
145 */ |
|
146 void CEventSystemServer::RemoveSession(void) |
|
147 { |
|
148 //RDebug::Print(_L("STF [ESS]: CEventSystemServer::RemoveSession")); |
|
149 __ASSERT_ALWAYS((iSessionCounter > 0), PanicClass(EInvalidSessionCounter)); |
|
150 iSessionCounter--; |
|
151 RDebug::Print(_L("STF [ESS]: CEventSystemServer::RemoveSession iSessionCounter decreased to %d"), iSessionCounter); |
|
152 CheckAndStartShutdownTimer(); |
|
153 } |
|
154 |
|
155 /** |
|
156 Check condition and start shudown timer. |
|
157 */ |
|
158 void CEventSystemServer::CheckAndStartShutdownTimer(void) |
|
159 { |
|
160 RDebug::Print(_L("STF [ESS]: CEventSystemServer::CheckAndStartShutdownTimer counter=[%d] state count=[%d] waiting count=[%d]"), iSessionCounter, iStateEvents.Count(), iWaitingEvents.Count()); |
|
161 // Timer can be started if there is no connected session and there are no state events set |
|
162 if(iSessionCounter == 0 && iStateEvents.Count() == 0 && iWaitingEvents.Count() == 0) |
|
163 { |
|
164 RDebug::Print(_L("STF [ESS]: starting shutdown timer")); |
|
165 iShutdown.Start(); |
|
166 } |
|
167 } |
|
168 |
|
169 /** |
|
170 Panic class with given error code. |
|
171 */ |
|
172 void CEventSystemServer::PanicClass(const TInt aErrorCode) |
|
173 { |
|
174 RDebug::Print(_L("STF [ESS]: Panic class CEventSystemServer [%x] with code [%d]"), this, aErrorCode); |
|
175 _LIT(KMessage, "CEventSystemServer"); |
|
176 User::Panic(KMessage, aErrorCode); |
|
177 } |
|
178 |
|
179 /** |
|
180 Search for state event and give its index. |
|
181 */ |
|
182 TInt CEventSystemServer::SearchForState(const TDesC& aEventName) |
|
183 { |
|
184 TInt i; |
|
185 for(i = 0; i < iStateEvents.Count(); i++) |
|
186 { |
|
187 // Check if this event matches |
|
188 if(iStateEvents[i]->IsMatchingEvent(aEventName)) |
|
189 { |
|
190 return i; |
|
191 } |
|
192 } |
|
193 return KErrNotFound; |
|
194 } |
|
195 |
|
196 /** |
|
197 Search for waiting event, starting from given index. |
|
198 aIndex will show found position and method return true. |
|
199 */ |
|
200 TBool CEventSystemServer::SearchForWaiting(const TDesC& aEventName, const TInt aOwnerId, TInt& aIndex) |
|
201 { |
|
202 if(aIndex < 0) |
|
203 aIndex = 0; |
|
204 |
|
205 for(; aIndex < iWaitingEvents.Count(); aIndex++) |
|
206 { |
|
207 // Check if this event matches |
|
208 if(iWaitingEvents[aIndex]->IsMatchingEvent(aEventName, aOwnerId)) |
|
209 { |
|
210 return ETrue; |
|
211 } |
|
212 } |
|
213 return EFalse; |
|
214 } |
|
215 |
|
216 /** |
|
217 Search for waiting event, starting from given index. |
|
218 aIndex will show found position and method return true. |
|
219 */ |
|
220 TBool CEventSystemServer::SearchForWaiting(const TDesC& aEventName, TInt& aIndex) |
|
221 { |
|
222 if(aIndex < 0) |
|
223 aIndex = 0; |
|
224 |
|
225 for(; aIndex < iWaitingEvents.Count(); aIndex++) |
|
226 { |
|
227 // Check if this event matches |
|
228 if(iWaitingEvents[aIndex]->IsMatchingEvent(aEventName)) |
|
229 { |
|
230 return ETrue; |
|
231 } |
|
232 } |
|
233 |
|
234 return EFalse; |
|
235 } |
|
236 |
|
237 /** |
|
238 Constructor of shutdown class. |
|
239 */ |
|
240 inline CShutdown::CShutdown(): CTimer(-1) |
|
241 { |
|
242 //RDebug::Print(_L("STF [ESS]: CShutdown::CShutdown")); |
|
243 CActiveScheduler::Add(this); |
|
244 } |
|
245 |
|
246 /** |
|
247 2nd phase of object construction. |
|
248 */ |
|
249 inline void CShutdown::ConstructL() |
|
250 { |
|
251 //RDebug::Print(_L("STF [ESS]: CShutdown::ConstructL")); |
|
252 CTimer::ConstructL(); |
|
253 } |
|
254 |
|
255 /** |
|
256 Start shutdown timer. |
|
257 */ |
|
258 inline void CShutdown::Start() |
|
259 { |
|
260 RDebug::Print(_L("STF [ESS]: CShutdown::Start")); |
|
261 After(KShutdownDelay); |
|
262 } |
|
263 |
|
264 /** |
|
265 Shutdown server. |
|
266 */ |
|
267 void CShutdown::RunL() |
|
268 { |
|
269 RDebug::Print(_L("STF [ESS]: CShutdown::RunL")); |
|
270 CActiveScheduler::Stop(); |
|
271 } |
|
272 |
|
273 // EOF |