|
1 // Copyright (c) 2007-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 /** |
|
17 @file |
|
18 */ |
|
19 |
|
20 #include <e32debug.h> |
|
21 |
|
22 #include "amastartsrv.h" |
|
23 |
|
24 |
|
25 |
|
26 /** |
|
27 * Used to create a new CAmaStartServer |
|
28 * @return A pointer to the CAmaStartServer |
|
29 * @leave One of the system-wide error codes if construction fails. |
|
30 */ |
|
31 EXPORT_C CAmaStartServer* CAmaStartServer::NewLC() |
|
32 { |
|
33 DEBUGPRINT1A( ">CAmaStartServer::NewLC" ); |
|
34 CAmaStartServer* self = new(ELeave) CAmaStartServer(); |
|
35 CleanupStack::PushL( self ); |
|
36 self->ConstructL(); |
|
37 DEBUGPRINT1A( "CAmaStartServer::NewLC>" ); |
|
38 return self; |
|
39 } |
|
40 |
|
41 |
|
42 |
|
43 /** |
|
44 * Static function used to create and start CAmaStartServer |
|
45 * |
|
46 * @return KErrAlreadyExists if the server is already running |
|
47 */ |
|
48 EXPORT_C TInt CAmaStartServer::StartAmaStartSrv() |
|
49 { |
|
50 DEBUGPRINT1A( ">CAmaStartServer::StartAmaStartSrv" ); |
|
51 |
|
52 const TInt KAmaStartSrvMinHeapSize = 0x2000; |
|
53 const TInt KAmaStartSrvMaxHeapSize = 10 * KAmaStartSrvMinHeapSize; |
|
54 RThread srvThread; |
|
55 |
|
56 TInt err = srvThread.Create( KAmaStartSrvName, &CAmaStartServer::AmaStartSrvThreadFn, |
|
57 KDefaultStackSize, KAmaStartSrvMinHeapSize, KAmaStartSrvMaxHeapSize, |
|
58 NULL, EOwnerProcess ); |
|
59 |
|
60 if( KErrNone == err ) |
|
61 { |
|
62 TRequestStatus trs; |
|
63 srvThread.Rendezvous( trs ); |
|
64 if ( trs != KRequestPending) |
|
65 { |
|
66 srvThread.Kill(trs.Int()); |
|
67 srvThread.Close(); |
|
68 DEBUGPRINT1A( "CAmaStartServer::StartAmaStartSrv> (Not Request pending)" ); |
|
69 return trs.Int(); |
|
70 } |
|
71 srvThread.Resume(); |
|
72 User::WaitForRequest( trs ); |
|
73 srvThread.Close(); |
|
74 err = trs.Int(); |
|
75 } |
|
76 |
|
77 DEBUGPRINT1A( "CAmaStartServer::StartAmaStartSrv>" ); |
|
78 return err; |
|
79 } |
|
80 |
|
81 |
|
82 |
|
83 CAmaStartServer::~CAmaStartServer() |
|
84 { |
|
85 DEBUGPRINT1A( ">CAmaStartServer::~CAmaStartServer" ); |
|
86 TInt i = 0; |
|
87 TInt count = iSessionInfoArray.Count(); |
|
88 for(; i < count; i++ ) |
|
89 { |
|
90 delete iSessionInfoArray[ i ].iStarter; |
|
91 iSessionInfoArray[ i ].iStarter = NULL; |
|
92 } |
|
93 iSessionInfoArray.Close(); |
|
94 DEBUGPRINT1A( "CAmaStartServer::~CAmaStartServer>" ); |
|
95 } |
|
96 |
|
97 |
|
98 |
|
99 void CAmaStartServer::StartDscL( const TUid& aDscId, const RMessage2& aMessage, const TInt aSessionIndex ) |
|
100 { |
|
101 DEBUGPRINT1A( ">CAmaStartServer::StartDscL" ); |
|
102 |
|
103 if( !__IN_RANGE(aSessionIndex, iSessionInfoArray.Count()) ) |
|
104 { |
|
105 User::Leave( KErrArgument ); |
|
106 } |
|
107 iSessionInfoArray[ aSessionIndex ].iStarter = CAmaStartAsync::NewL( aMessage ); |
|
108 iSessionInfoArray[ aSessionIndex ].iMessagePtr = aMessage; |
|
109 iSessionInfoArray[ aSessionIndex ].iStarter->StartL( aDscId ); |
|
110 |
|
111 DEBUGPRINT1A( "CAmaStartServer::StartDscL>" ); |
|
112 } |
|
113 |
|
114 |
|
115 |
|
116 /** |
|
117 * Locate the CommandListExecutor associated with the session calling CommandListCancel. |
|
118 * Delete the Command list (which cancels its AO) then complete the client's RMessage with KErrCancel. |
|
119 */ |
|
120 void CAmaStartServer::StartDscCancel( const TInt aSessionIndex ) |
|
121 { |
|
122 DEBUGPRINT1A( ">CAmaStartServer::StartDscCancel" ); |
|
123 __ASSERT_DEBUG( __IN_RANGE( aSessionIndex, iSessionInfoArray.Count()), User::Panic( KAmaStartSrvBadIdx, KAmaStartSrvArrayIndexInvalid ) ); |
|
124 |
|
125 iSessionInfoArray[ aSessionIndex ].iStarter->Cancel(); |
|
126 |
|
127 DEBUGPRINT1A( "CAmaStartServer::StartDscCancel>" ); |
|
128 } |
|
129 |
|
130 |
|
131 |
|
132 /** |
|
133 * Iterate through iSessionInfoArray to find an unused array element |
|
134 * If found, use it. Otherwise, Append() a new TAmaStartSessionInfo. |
|
135 * Called during CSession construction. |
|
136 */ |
|
137 void CAmaStartServer::RegisterSessionL( TInt& aSessionIndex ) |
|
138 { |
|
139 DEBUGPRINT1A( ">CAmaStartServer::RegisterSessionL" ); |
|
140 |
|
141 TInt i = 0; |
|
142 TInt count = iSessionInfoArray.Count(); |
|
143 TBool slotFound = EFalse; |
|
144 |
|
145 for(; i < count; i++ ) |
|
146 { |
|
147 if( iSessionInfoArray[ i ].iInUse ) |
|
148 { |
|
149 continue; |
|
150 } |
|
151 else |
|
152 { |
|
153 iSessionInfoArray[ i ].iInUse = ETrue; |
|
154 iSessionInfoArray[ i ].iStarter = NULL; |
|
155 aSessionIndex = i; |
|
156 slotFound = ETrue; |
|
157 iSessionCount++; |
|
158 break; |
|
159 } |
|
160 } |
|
161 |
|
162 if( !slotFound ) |
|
163 { |
|
164 TAmaStartSessionInfo sessionInfo; |
|
165 sessionInfo.iStarter = NULL; |
|
166 sessionInfo.iInUse = ETrue; |
|
167 iSessionInfoArray.AppendL( sessionInfo ); |
|
168 aSessionIndex = iSessionCount++; |
|
169 } |
|
170 |
|
171 DEBUGPRINT2A( "Registered in slot %d", aSessionIndex ); |
|
172 DEBUGPRINT1A( "CAmaStartServer::RegisterSessionL>" ); |
|
173 } |
|
174 |
|
175 |
|
176 |
|
177 /** |
|
178 * Called from CSession destructor. |
|
179 */ |
|
180 void CAmaStartServer::DeregisterSession( const TInt& aSessionIndex ) |
|
181 { |
|
182 DEBUGPRINT1A( ">CAmaStartServer::DeregisterSession" ); |
|
183 __ASSERT_DEBUG( __IN_RANGE(aSessionIndex, iSessionInfoArray.Count()), User::Panic(KAmaStartSrvBadIdx, KAmaStartSrvArrayIndexInvalid) ); |
|
184 |
|
185 if( iSessionInfoArray[ aSessionIndex ].iStarter ) |
|
186 { |
|
187 delete iSessionInfoArray[ aSessionIndex ].iStarter; |
|
188 iSessionInfoArray[ aSessionIndex ].iStarter = NULL; |
|
189 } |
|
190 iSessionInfoArray[ aSessionIndex ].iInUse = EFalse; |
|
191 iSessionCount--; |
|
192 |
|
193 DEBUGPRINT2A( "De-registered slot %d", aSessionIndex ); |
|
194 DEBUGPRINT1A( "CAmaStartServer::DeregisterSession>" ); |
|
195 } |
|
196 |
|
197 |
|
198 |
|
199 /** |
|
200 * Used to create a new server-side session. |
|
201 * @return A pointer to the new instance of CSession2. |
|
202 * @leave KErrNotSupported if versions are incompatible. |
|
203 */ |
|
204 EXPORT_C CSession2* CAmaStartServer::NewSessionL( const TVersion& aVersion, const RMessage2& /*aMessage*/ ) const |
|
205 { |
|
206 DEBUGPRINT1A( ">CAmaStartServer::NewSessionL" ); |
|
207 CAmaStartServer& mutatedSelf = MUTABLE_CAST( CAmaStartServer&, *this ); |
|
208 if( !User::QueryVersionSupported(iVersion, aVersion) ) |
|
209 { |
|
210 User::Leave( KErrNotSupported ); |
|
211 } |
|
212 DEBUGPRINT1A( "CAmaStartServer::NewSessionL>" ); |
|
213 return CAmaStartSession::NewL( mutatedSelf ); |
|
214 } |
|
215 |
|
216 |
|
217 |
|
218 CAmaStartServer::CAmaStartServer() |
|
219 : CServer2( EPriorityHigh, EUnsharableSessions ), |
|
220 iVersion( KAmaStartSrvVersionMajor, KAmaStartSrvVersionMinor, KAmaStartSrvVersionBuild ) |
|
221 { |
|
222 DEBUGPRINT1A( ">CAmaStartServer::CAmaStartServer> (Empty constructor)" ); |
|
223 } |
|
224 |
|
225 |
|
226 |
|
227 void CAmaStartServer::ConstructL() |
|
228 { |
|
229 DEBUGPRINT1A( ">CAmaStartServer::ConstructL" ); |
|
230 StartL( KAmaStartSrvName ); |
|
231 // The starting thread is signalled in the thread function. |
|
232 DEBUGPRINT1A( "CAmaStartServer::ConstructL>" ); |
|
233 } |
|
234 |
|
235 |
|
236 // Static functions |
|
237 |
|
238 TInt CAmaStartServer::AmaStartSrvThreadFn( TAny* /*aAny*/ ) |
|
239 { |
|
240 DEBUGPRINT1A( ">CAmaStartServer::AmaStartSrvThreadFn" ); |
|
241 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
242 TRAPD( err, AmaStartSrvThreadRunL() ); |
|
243 delete cleanup; |
|
244 DEBUGPRINT1A( "CAmaStartServer::AmaStartSrvThreadFn>" ); |
|
245 return err; |
|
246 } |
|
247 |
|
248 |
|
249 |
|
250 void CAmaStartServer::AmaStartSrvThreadRunL() |
|
251 { |
|
252 DEBUGPRINT1A( ">CAmaStartServer::AmaStartSrvThreadRunL" ); |
|
253 CActiveScheduler* sched = new(ELeave) CActiveScheduler(); |
|
254 CleanupStack::PushL( sched ); |
|
255 CActiveScheduler::Install( sched ); |
|
256 CAmaStartServer* amaStartSrv = CAmaStartServer::NewLC(); |
|
257 RThread::Rendezvous( KErrNone ); |
|
258 CActiveScheduler::Start(); |
|
259 |
|
260 CleanupStack::PopAndDestroy( 2, sched ); |
|
261 DEBUGPRINT1A( "CAmaStartServer::AmaStartSrvThreadRunL>" ); |
|
262 } //lint !e529 not subsequently referenced (amaStartSrv) |