|
1 /* |
|
2 * Copyright (c) 2005 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: CXcapCacheServer |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 // INCLUDE FILES |
|
22 #include <e32std.h> |
|
23 #include <e32math.h> |
|
24 #include <flogger.h> |
|
25 #include <centralrepository.h> |
|
26 #include <XdmEngineDomainCRKeys.h> |
|
27 #include "XcapCacheIndex.h" |
|
28 #include "XcapCacheServer.h" |
|
29 #include "XcapCacheSession.h" |
|
30 #include "XcapCacheIndexAdmin.h" |
|
31 #include "XcapCacheServerMain.h" |
|
32 |
|
33 GLDEF_C void PanicServer( TXcapCacheServerPanic aPanic ) |
|
34 { |
|
35 _LIT( KCacheServerPanic, "XcapCacheServer"); |
|
36 User::Panic( KCacheServerPanic, aPanic ); |
|
37 } |
|
38 |
|
39 // ---------------------------------------------------------- |
|
40 // CXcapCacheServer::New |
|
41 // |
|
42 // ---------------------------------------------------------- |
|
43 // |
|
44 CServer2* CXcapCacheServer::NewLC() |
|
45 { |
|
46 CXcapCacheServer* self = new CXcapCacheServer( EPriorityHigh ); |
|
47 CleanupStack::PushL( self ); |
|
48 self->ConstructL(); |
|
49 return self; |
|
50 } |
|
51 |
|
52 // ---------------------------------------------------------- |
|
53 // CXcapCacheServer::New |
|
54 // |
|
55 // ---------------------------------------------------------- |
|
56 // |
|
57 void CXcapCacheServer::ConstructL() |
|
58 { |
|
59 ReadMaxCacheSizeL(); |
|
60 User::LeaveIfError( iFileSession.Connect() ); |
|
61 StartL( KXcapCacheServerLib ); |
|
62 iShutdownTimer.ConstructL(); |
|
63 iShutdownTimer.Start(); |
|
64 } |
|
65 |
|
66 // ---------------------------------------------------------- |
|
67 // CXcapCacheServer::NewSessionL |
|
68 // |
|
69 // ---------------------------------------------------------- |
|
70 // |
|
71 CSession2* CXcapCacheServer::NewSessionL( const TVersion& aVersion, |
|
72 const RMessage2& /*aMessage*/ ) const |
|
73 { |
|
74 // Check version number of API against our known version number. |
|
75 TVersion thisVersion( KCacheServerMajorVersionNumber, |
|
76 KCacheServerMinorVersionNumber, |
|
77 KCacheServerBuildVersionNumber ); |
|
78 if( !User::QueryVersionSupported( thisVersion, aVersion ) ) |
|
79 User::Leave( KErrNotSupported ); |
|
80 return CXcapCacheSession::NewL( ( CXcapCacheServer* )this ); |
|
81 } |
|
82 |
|
83 // ---------------------------------------------------------- |
|
84 // CXcapCacheServer::CXcapCacheServer |
|
85 // |
|
86 // ---------------------------------------------------------- |
|
87 // |
|
88 CXcapCacheServer::~CXcapCacheServer() |
|
89 { |
|
90 iFileSession.Close(); |
|
91 } |
|
92 |
|
93 // ---------------------------------------------------------- |
|
94 // CXcapCacheServer::ReadMaxCacheSizeL |
|
95 // |
|
96 // ---------------------------------------------------------- |
|
97 // |
|
98 void CXcapCacheServer::ReadMaxCacheSizeL() |
|
99 { |
|
100 #ifdef _DEBUG |
|
101 CXcapCacheServer::WriteToLog( _L8( "CXcapCacheServer::ReadMaxCacheSizeL()" ) ); |
|
102 #endif |
|
103 CRepository* repository = CRepository::NewL( KCRUIDXDMEngineXcapLocal ); |
|
104 CleanupStack::PushL( repository ); |
|
105 TInt error = repository->Get( KXDMXcapCacheSize, iMaxCacheSize ); |
|
106 //If something went wrong, just use 30 KB as a default value |
|
107 if( error != KErrNone ) |
|
108 { |
|
109 #ifdef _DEBUG |
|
110 CXcapCacheServer::WriteToLog( _L8( "Reading from CenRep failed: %d => Default to 30 KB" ), error ); |
|
111 #endif |
|
112 iMaxCacheSize = 30000; |
|
113 } |
|
114 else |
|
115 { |
|
116 #ifdef _DEBUG |
|
117 CXcapCacheServer::WriteToLog( _L8( " Max cache size is %d KB" ), iMaxCacheSize ); |
|
118 #endif |
|
119 iMaxCacheSize = iMaxCacheSize * 1000; |
|
120 } |
|
121 CleanupStack::PopAndDestroy(); //repository |
|
122 } |
|
123 |
|
124 // ---------------------------------------------------------- |
|
125 // CXcapCacheServer::CXcapCacheServer |
|
126 // |
|
127 // ---------------------------------------------------------- |
|
128 // |
|
129 CXcapCacheServer::CXcapCacheServer( TInt aPriority ) : CServer2( aPriority ) |
|
130 { |
|
131 } |
|
132 |
|
133 // ---------------------------------------------------------- |
|
134 // CXcapCacheServer::AddSession |
|
135 // |
|
136 // ---------------------------------------------------------- |
|
137 // |
|
138 void CXcapCacheServer::AddSession() |
|
139 { |
|
140 #ifdef _DEBUG |
|
141 CXcapCacheServer::WriteToLog( _L8( "CXcapCacheServer::AddSession()" ) ); |
|
142 #endif |
|
143 ++iSessionCount; |
|
144 iShutdownTimer.Cancel(); |
|
145 } |
|
146 |
|
147 // ---------------------------------------------------------- |
|
148 // CXcapCacheServer::DropSession |
|
149 // |
|
150 // ---------------------------------------------------------- |
|
151 // |
|
152 void CXcapCacheServer::DropSession() |
|
153 { |
|
154 #ifdef _DEBUG |
|
155 CXcapCacheServer::WriteToLog( _L8( "CXcapCacheServer::DropSession()" ) ); |
|
156 #endif |
|
157 if( --iSessionCount == 0 ) |
|
158 CActiveScheduler::Stop(); |
|
159 } |
|
160 |
|
161 // ---------------------------------------------------------- |
|
162 // CXcapCacheServer::ConvertDesc |
|
163 // |
|
164 // ---------------------------------------------------------- |
|
165 // |
|
166 TInt CXcapCacheServer::ConvertDesc( const TDesC8& aNumberDesc ) |
|
167 { |
|
168 TInt ret = 0; |
|
169 TLex8 lex( aNumberDesc ); |
|
170 TInt error = lex.Val( ret ); |
|
171 return error == KErrNone ? ret : error; |
|
172 } |
|
173 |
|
174 // --------------------------------------------------------- |
|
175 // CXcapCacheServer::DateL |
|
176 // |
|
177 // --------------------------------------------------------- |
|
178 // |
|
179 HBufC* CXcapCacheServer::DateL() |
|
180 { |
|
181 TTime time; |
|
182 TBuf<KDateMaxSize> dateBuffer; |
|
183 dateBuffer.SetLength( 0 ); |
|
184 time.HomeTime(); |
|
185 time.FormatL( dateBuffer, KCacheDateFormat ); |
|
186 HBufC* heapDate = dateBuffer.AllocL(); |
|
187 return heapDate; |
|
188 } |
|
189 |
|
190 // --------------------------------------------------------- |
|
191 // CXcapCacheServer::DateTimeL |
|
192 // |
|
193 // --------------------------------------------------------- |
|
194 // |
|
195 HBufC* CXcapCacheServer::DateTimeL() |
|
196 { |
|
197 TTime time; |
|
198 TBuf<KDateMaxSize> dateBuffer; |
|
199 time.HomeTime(); |
|
200 time.FormatL( dateBuffer, KDateFormatFileName ); |
|
201 TBuf<KDateMaxSize> timeBuffer; |
|
202 time.HomeTime(); |
|
203 time.FormatL( timeBuffer, KTimeFormatFileName ); |
|
204 TBuf<KDateTimeMaxSize> buffer; |
|
205 buffer.SetLength( 0 ); |
|
206 buffer.Copy( dateBuffer ); |
|
207 buffer.Append( timeBuffer ); |
|
208 HBufC* ret = HBufC::NewL( buffer.Length() ); |
|
209 ret->Des().Copy( buffer ); |
|
210 return ret; |
|
211 } |
|
212 |
|
213 // --------------------------------------------------------- |
|
214 // CXcapCacheServer::DateTimeL |
|
215 // |
|
216 // --------------------------------------------------------- |
|
217 // |
|
218 HBufC* CXcapCacheServer::DateTimeL( const TTime& aTime ) |
|
219 { |
|
220 TBuf<KDateTimeMaxSize> dateTimeBuffer; |
|
221 aTime.FormatL( dateTimeBuffer, KDateTimeFormat ); |
|
222 HBufC* heapDateTime = dateTimeBuffer.AllocL(); |
|
223 return heapDateTime; |
|
224 } |
|
225 |
|
226 // ---------------------------------------------------------- |
|
227 // CXcapCacheServer::MaxCacheSize |
|
228 // |
|
229 // ---------------------------------------------------------- |
|
230 // |
|
231 TInt CXcapCacheServer::MaxCacheSize() const |
|
232 { |
|
233 return iMaxCacheSize; |
|
234 } |
|
235 |
|
236 // --------------------------------------------------------- |
|
237 // CXcapCacheServer::CacheSize |
|
238 // |
|
239 // --------------------------------------------------------- |
|
240 // |
|
241 TInt CXcapCacheServer::CacheSize( TInt& aEntryCount ) |
|
242 { |
|
243 #ifdef _DEBUG |
|
244 CXcapCacheServer::WriteToLog( _L8( "CXcapCacheServer::CacheSize()" ) ); |
|
245 #endif |
|
246 TInt total = 0; |
|
247 TInt entryCount = 0; |
|
248 CDir* directory = NULL; |
|
249 User::LeaveIfError( iFileSession.GetDir( KCacheServerRoot, KEntryAttNormal, |
|
250 ESortNone, directory ) ); |
|
251 CleanupStack::PushL( directory ); |
|
252 entryCount = directory->Count(); |
|
253 for( TInt i = 0;i < entryCount;i++ ) |
|
254 total = total + ( *directory )[i].iSize; |
|
255 #ifdef _DEBUG |
|
256 CXcapCacheServer::WriteToLog( _L8( " Entries: %d " ), entryCount ); |
|
257 CXcapCacheServer::WriteToLog( _L8( " Total size: %d bytes" ), total ); |
|
258 #endif |
|
259 CleanupStack::PopAndDestroy(); |
|
260 aEntryCount = entryCount; |
|
261 return total; |
|
262 } |
|
263 |
|
264 // --------------------------------------------------------- |
|
265 // CXcapCacheServer::RandomStringL |
|
266 // |
|
267 // --------------------------------------------------------- |
|
268 // |
|
269 HBufC* CXcapCacheServer::RandomStringL() |
|
270 { |
|
271 const TInt charCount( sizeof( KRandomStringCharArray ) / sizeof( TInt ) ); |
|
272 TBuf<KRandStringLength> buffer; |
|
273 buffer.Zero(); |
|
274 for( TInt i = 0; i < KRandStringLength;i++ ) |
|
275 { |
|
276 TInt index = Math::Random() % charCount; |
|
277 buffer.Append( ( TChar )KRandomStringCharArray[index] ); |
|
278 } |
|
279 HBufC* randomHeapBuffer = buffer.AllocL(); |
|
280 return randomHeapBuffer; |
|
281 } |
|
282 |
|
283 // --------------------------------------------------------- |
|
284 // CXcapCacheServer::TimeL |
|
285 // |
|
286 // --------------------------------------------------------- |
|
287 // |
|
288 HBufC* CXcapCacheServer::TimeL() |
|
289 { |
|
290 TTime time; |
|
291 TBuf<KDateMaxSize> timeBuffer; |
|
292 timeBuffer.SetLength( 0 ); |
|
293 time.HomeTime(); |
|
294 time.FormatL( timeBuffer, KCacheTimeFormat ); |
|
295 HBufC* heapTime = timeBuffer.AllocL(); |
|
296 return heapTime; |
|
297 } |
|
298 |
|
299 // --------------------------------------------------------- |
|
300 // CXcapCacheServer::DescriptorCast |
|
301 // |
|
302 // --------------------------------------------------------- |
|
303 // |
|
304 TPtr8 CXcapCacheServer::DescriptorCast( const TDesC8& aConstData ) |
|
305 { |
|
306 TInt length = aConstData.Length(); |
|
307 TPtr8 constCast( CONST_CAST( TUint8*, aConstData.Ptr() ), length, length ); |
|
308 constCast.TrimAll(); |
|
309 return constCast; |
|
310 } |
|
311 |
|
312 // ---------------------------------------------------- |
|
313 // CXcapCacheServer::FileSession |
|
314 // |
|
315 // ---------------------------------------------------- |
|
316 // |
|
317 RFs& CXcapCacheServer::FileSession() |
|
318 { |
|
319 return FsSession; |
|
320 } |
|
321 |
|
322 // ---------------------------------------------------- |
|
323 // CXcapCacheServer::FileSession |
|
324 // |
|
325 // ---------------------------------------------------- |
|
326 // |
|
327 CXcapCacheIndex* CXcapCacheServer::Index() |
|
328 { |
|
329 return CacheIndex; |
|
330 } |
|
331 |
|
332 // ---------------------------------------------------- |
|
333 // CXcapCacheServer::FileSession |
|
334 // |
|
335 // ---------------------------------------------------- |
|
336 // |
|
337 CXcapCacheIndexAdmin* CXcapCacheServer::IndexAdmin() |
|
338 { |
|
339 return CacheIndexAdmin; |
|
340 } |
|
341 |
|
342 // ---------------------------------------------------- |
|
343 // CXcapCacheServer::StartThreadL |
|
344 // |
|
345 // ---------------------------------------------------- |
|
346 // |
|
347 EXPORT_C TInt CXcapCacheServer::StartThreadL() |
|
348 { |
|
349 User::LeaveIfError( User::RenameThread( KXcapCacheServerName ) ); |
|
350 CActiveScheduler* scheduler = new ( ELeave ) CActiveScheduler; |
|
351 CleanupStack::PushL( scheduler ); |
|
352 CActiveScheduler::Install( scheduler ); |
|
353 User::LeaveIfError( FsSession.Connect() ); |
|
354 CleanupClosePushL( FsSession ); |
|
355 CXcapCacheServer::NewLC(); |
|
356 CacheIndex = CXcapCacheIndex::NewL(); |
|
357 CleanupStack::PushL( CacheIndex ); |
|
358 CacheIndexAdmin = CXcapCacheIndexAdmin::NewL(); |
|
359 CleanupStack::PushL( CacheIndexAdmin ); |
|
360 RProcess::Rendezvous( KErrNone ); |
|
361 #ifdef _DEBUG |
|
362 CXcapCacheServer::WriteToLog( _L8( "XcapCacheServer fully running" ) ); |
|
363 #endif |
|
364 CActiveScheduler::Start(); |
|
365 #ifdef _DEBUG |
|
366 CXcapCacheServer::WriteToLog( _L8( "XcapCacheServer closing..." ) ); |
|
367 #endif |
|
368 CleanupStack::PopAndDestroy( 5 ); //CacheIndexAdmin, CacheIndex, server, FsSession, scheduler |
|
369 return KErrNone; |
|
370 } |
|
371 |
|
372 // ---------------------------------------------------- |
|
373 // CXcapCacheServer::WriteToLog |
|
374 // |
|
375 // ---------------------------------------------------- |
|
376 // |
|
377 EXPORT_C void CXcapCacheServer::WriteToLog( TRefByValue<const TDesC8> aFmt,... ) |
|
378 { |
|
379 VA_LIST list; |
|
380 VA_START( list, aFmt ); |
|
381 TBuf8<KServerLogBufferMaxSize> buf; |
|
382 buf.FormatList( aFmt, list ); |
|
383 RFileLogger::Write( KCacheServerLogDir, KCacheServerLogFile, EFileLoggingModeAppend, buf ); |
|
384 RDebug::RawPrint( buf ); |
|
385 } |
|
386 |
|
387 |