|
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: CirWatcher for UDP |
|
15 * |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include <ecom/ecom.h> |
|
22 #include <f32file.h> |
|
23 #include <flogger.h> |
|
24 #include <msgconnmanagerapi.h> |
|
25 #include "UdpCirWatcher.h" |
|
26 #include "UdpSocketListener.h" |
|
27 #include "CirWatcherDefines.h" |
|
28 #include "ImpsIpCirWatcherAPI.h" |
|
29 |
|
30 // ================= MEMBER FUNCTIONS ======================= |
|
31 // |
|
32 |
|
33 // ---------------------------------------------------------- |
|
34 // CUdpCirWatcher::CUdpCirWatcher |
|
35 // |
|
36 // ---------------------------------------------------------- |
|
37 // |
|
38 CUdpCirWatcher::CUdpCirWatcher() : iReferenceCount( 1 ) |
|
39 { |
|
40 } |
|
41 |
|
42 // ---------------------------------------------------------- |
|
43 // CUdpCirWatcher::NewL |
|
44 // |
|
45 // ---------------------------------------------------------- |
|
46 // |
|
47 CUdpCirWatcher* CUdpCirWatcher::NewL() |
|
48 { |
|
49 CUdpCirWatcher* self = NULL; |
|
50 if ( Dll::Tls() == NULL ) |
|
51 { |
|
52 self = new ( ELeave ) CUdpCirWatcher(); |
|
53 CleanupStack::PushL( self ); |
|
54 self->ConstructL(); |
|
55 CleanupStack::Pop(); |
|
56 Dll::SetTls( self ); |
|
57 } |
|
58 else |
|
59 { |
|
60 self = static_cast<CUdpCirWatcher*>( Dll::Tls() ); |
|
61 self->IncreaseReferenceCount(); |
|
62 } |
|
63 return self; |
|
64 } |
|
65 |
|
66 // ---------------------------------------------------- |
|
67 // CUdpCirWatcher::Destroy |
|
68 // Destructor |
|
69 // ---------------------------------------------------- |
|
70 // |
|
71 void CUdpCirWatcher::Destroy() |
|
72 { |
|
73 DecreaseReferenceCount(); |
|
74 CImpsUdpCirWatcher::Destroy(); |
|
75 if ( ReferenceCount() == 0 ) |
|
76 { |
|
77 Dll::FreeTls(); |
|
78 delete this; |
|
79 } |
|
80 } |
|
81 |
|
82 // ---------------------------------------------------- |
|
83 // CUdpCirWatcher::DeleteSingleton |
|
84 // Destructor |
|
85 // ---------------------------------------------------- |
|
86 // |
|
87 void CUdpCirWatcher::DeleteSingleton() |
|
88 { |
|
89 CImpsUdpCirWatcher::DeleteSingleton(); |
|
90 Dll::FreeTls(); |
|
91 delete this; |
|
92 } |
|
93 |
|
94 // ---------------------------------------------------- |
|
95 // CUdpCirWatcher::Destroy |
|
96 // |
|
97 // ---------------------------------------------------- |
|
98 // |
|
99 void CUdpCirWatcher::StoreDestructorKeyL( const TUid aDestructorKey ) |
|
100 { |
|
101 User::LeaveIfError( iDestructorKeyArray.Append( aDestructorKey ) ); |
|
102 } |
|
103 |
|
104 // ---------------------------------------------------------- |
|
105 // CUdpCirWatcher::ConstructL |
|
106 // |
|
107 // ---------------------------------------------------------- |
|
108 // |
|
109 void CUdpCirWatcher::ConstructL() |
|
110 { |
|
111 #ifdef _DEBUG |
|
112 DeleteLogFileL(); |
|
113 CUdpCirWatcher::WriteToLog( _L8( "CUdpCirWatcher::ConstructL()" ) ); |
|
114 #endif |
|
115 User::LeaveIfError( iSocketSession.Connect() ); |
|
116 } |
|
117 |
|
118 // ---------------------------------------------------------- |
|
119 // CTcpCirWatcher::DeleteLogFileL |
|
120 // |
|
121 // ---------------------------------------------------------- |
|
122 // |
|
123 void CUdpCirWatcher::DeleteLogFileL() |
|
124 { |
|
125 RFs session; |
|
126 User::LeaveIfError( session.Connect() ); |
|
127 CFileMan* manager = CFileMan::NewL( session ); |
|
128 manager->Delete( _L( "C:\\Logs\\ImpsUdpWatcher\\*.*" ) ); |
|
129 session.Close(); |
|
130 delete manager; |
|
131 manager = NULL; |
|
132 } |
|
133 |
|
134 // ---------------------------------------------------- |
|
135 // CUdpCirWatcher::~CUdpCirWatcher |
|
136 // Destructor |
|
137 // ---------------------------------------------------- |
|
138 // |
|
139 CUdpCirWatcher::~CUdpCirWatcher() |
|
140 { |
|
141 #ifdef _DEBUG |
|
142 CUdpCirWatcher::WriteToLog( _L8( "CUdpCirWatcher::~CUdpCirWatcher()" ) ); |
|
143 #endif |
|
144 iListenerQueue.ResetAndDestroy(); |
|
145 iListenerQueue.Close(); |
|
146 iSocketSession.Close(); |
|
147 #ifdef _DEBUG |
|
148 CUdpCirWatcher::WriteToLog( _L8( " Destructor ends" ) ); |
|
149 #endif |
|
150 } |
|
151 |
|
152 // ---------------------------------------------------------- |
|
153 // CUdpCirWatcher::FindListener |
|
154 // |
|
155 // ---------------------------------------------------------- |
|
156 // |
|
157 TBool CUdpCirWatcher::FindListenerL( const TInt aServicePort, |
|
158 const MImpsCirReceiver& aCirReceiver ) |
|
159 { |
|
160 #ifdef _DEBUG |
|
161 CUdpCirWatcher::WriteToLog( _L8( "CUdpCirWatcher::FindListener()" ) ); |
|
162 #endif |
|
163 TBool found = EFalse; |
|
164 TInt count = iListenerQueue.Count(); |
|
165 CUdpSocketListener* listener = NULL; |
|
166 for ( TInt i = 0;!found && i < count;i++ ) |
|
167 { |
|
168 listener = iListenerQueue[i]; |
|
169 if ( listener->ServicePort() == aServicePort ) |
|
170 { |
|
171 #ifdef _DEBUG |
|
172 CUdpCirWatcher::WriteToLog( _L8( |
|
173 " Port %d already on watch, attach the new listener to it" ), aServicePort ); |
|
174 #endif |
|
175 found = ETrue; |
|
176 listener->AppendReceiverL( aCirReceiver ); |
|
177 } |
|
178 } |
|
179 return found; |
|
180 } |
|
181 |
|
182 // ---------------------------------------------------------- |
|
183 // CUdpCirWatcher::FindListener |
|
184 // |
|
185 // ---------------------------------------------------------- |
|
186 // |
|
187 CUdpSocketListener* CUdpCirWatcher::FindListener( TInt& aIndex, |
|
188 const TInt aServicePort ) const |
|
189 { |
|
190 #ifdef _DEBUG |
|
191 CUdpCirWatcher::WriteToLog( _L8( "CUdpCirWatcher::FindListener()" ) ); |
|
192 #endif |
|
193 TBool found = EFalse; |
|
194 TInt count = iListenerQueue.Count(); |
|
195 CUdpSocketListener* listener = NULL; |
|
196 for ( TInt i = 0;!found && i < count;i++ ) |
|
197 { |
|
198 listener = iListenerQueue[i]; |
|
199 if ( listener->ServicePort() == aServicePort ) |
|
200 { |
|
201 found = ETrue; |
|
202 aIndex = i; |
|
203 } |
|
204 } |
|
205 return listener; |
|
206 } |
|
207 |
|
208 // ---------------------------------------------------------- |
|
209 // CUdpCirWatcher::CreateListenerL |
|
210 // |
|
211 // ---------------------------------------------------------- |
|
212 // |
|
213 TInt CUdpCirWatcher::CreateListenerL( const TInt aServicePort, |
|
214 const MImpsCirReceiver& aCirReceiver ) |
|
215 { |
|
216 #ifdef _DEBUG |
|
217 CUdpCirWatcher::WriteToLog( _L8( "CUdpCirWatcher::CreateListenerL()" ) ); |
|
218 #endif |
|
219 CUdpSocketListener* listener = CUdpSocketListener::NewL( aServicePort, aCirReceiver ); |
|
220 CleanupStack::PushL( listener ); |
|
221 User::LeaveIfError( iListenerQueue.Append( listener ) ); |
|
222 CleanupStack::Pop(); //listener |
|
223 return listener->StartUdpSocketListen( iSocketSession ); |
|
224 } |
|
225 |
|
226 // ---------------------------------------------------------- |
|
227 // CUdpCirWatcher::RegisterCirReceiverL |
|
228 // |
|
229 // ---------------------------------------------------------- |
|
230 // |
|
231 void CUdpCirWatcher::RegisterCirReceiverL( const MImpsCirReceiver& aCirReceiver, |
|
232 const TInt aServicePort ) |
|
233 { |
|
234 #ifdef _DEBUG |
|
235 CUdpCirWatcher::WriteToLog( _L8( "CUdpCirWatcher::RegisterCirReceiverL() - Port: %d" ), aServicePort ); |
|
236 #endif |
|
237 TInt count = iListenerQueue.Count(); |
|
238 if ( count > 0 ) |
|
239 { |
|
240 if ( !FindListenerL( aServicePort, aCirReceiver ) ) |
|
241 User::LeaveIfError( CreateListenerL( aServicePort, aCirReceiver ) ); |
|
242 } |
|
243 else |
|
244 User::LeaveIfError( CreateListenerL( aServicePort, aCirReceiver ) ); |
|
245 } |
|
246 |
|
247 // ---------------------------------------------------------- |
|
248 // CUdpCirWatcher::UnregisterCirReceiverL |
|
249 // |
|
250 // ---------------------------------------------------------- |
|
251 // |
|
252 void CUdpCirWatcher::UnregisterCirReceiverL( const MImpsCirReceiver& aCirReceiver ) |
|
253 { |
|
254 #ifdef _DEBUG |
|
255 CUdpCirWatcher::WriteToLog( _L8( "CUdpCirWatcher::UnregisterCirReceiverL()" ) ); |
|
256 #endif |
|
257 TBool found = EFalse; |
|
258 TInt count = iListenerQueue.Count(); |
|
259 if ( count > 0 ) |
|
260 { |
|
261 CUdpSocketListener* listener = NULL; |
|
262 for ( TInt i = 0;!found && i < count;i++ ) |
|
263 { |
|
264 listener = iListenerQueue[i]; |
|
265 if ( listener->Contains( aCirReceiver ) >= 0 ) |
|
266 { |
|
267 found = ETrue; |
|
268 if ( !listener->RemoveReceiver( aCirReceiver ) ) |
|
269 { |
|
270 iListenerQueue.Remove( i ); |
|
271 delete listener; |
|
272 listener = NULL; |
|
273 } |
|
274 } |
|
275 } |
|
276 if ( !found ) |
|
277 User::Leave( KErrNotFound ); |
|
278 } |
|
279 } |
|
280 |
|
281 // ---------------------------------------------------------- |
|
282 // CUdpCirWatcher::Reset |
|
283 // |
|
284 // ---------------------------------------------------------- |
|
285 // |
|
286 void CUdpCirWatcher::Reset( const TInt aServicePort ) |
|
287 { |
|
288 #ifdef _DEBUG |
|
289 CUdpCirWatcher::WriteToLog( _L8( "CUdpCirWatcher::Reset() - Port: %d" ), aServicePort ); |
|
290 #endif |
|
291 CUdpSocketListener* listener = NULL; |
|
292 if ( aServicePort != KErrArgument ) |
|
293 { |
|
294 TInt index = KErrNotFound; |
|
295 listener = FindListener( index, aServicePort ); |
|
296 if ( listener != NULL && index >= 0 ) |
|
297 { |
|
298 iListenerQueue.Remove( index ); |
|
299 delete listener; |
|
300 listener = NULL; |
|
301 } |
|
302 } |
|
303 else |
|
304 { |
|
305 TInt count = iListenerQueue.Count(); |
|
306 for ( TInt i = 0;i < count;i++ ) |
|
307 { |
|
308 listener = iListenerQueue[i]; |
|
309 iListenerQueue.Remove( i ); |
|
310 delete listener; |
|
311 listener = NULL; |
|
312 } |
|
313 } |
|
314 } |
|
315 |
|
316 // --------------------------------------------------------- |
|
317 // CUdpCirWatcher::IncreaseReferenceCount |
|
318 // |
|
319 // --------------------------------------------------------- |
|
320 // |
|
321 void CUdpCirWatcher::IncreaseReferenceCount() |
|
322 { |
|
323 iReferenceCount++; |
|
324 } |
|
325 |
|
326 // --------------------------------------------------------- |
|
327 // CUdpCirWatcher::DecreaseReferenceCount |
|
328 // |
|
329 // --------------------------------------------------------- |
|
330 // |
|
331 void CUdpCirWatcher::DecreaseReferenceCount() |
|
332 { |
|
333 iReferenceCount--; |
|
334 } |
|
335 |
|
336 // ---------------------------------------------------------- |
|
337 // CUdpCirWatcher::ReferenceCount |
|
338 // |
|
339 // ---------------------------------------------------------- |
|
340 // |
|
341 const TInt CUdpCirWatcher::ReferenceCount() const |
|
342 { |
|
343 return iReferenceCount; |
|
344 } |
|
345 |
|
346 #ifdef _DEBUG |
|
347 |
|
348 // ---------------------------------------------------- |
|
349 // CCIRWatcher::WriteToLog |
|
350 // |
|
351 // ---------------------------------------------------- |
|
352 // |
|
353 void CUdpCirWatcher::WriteToLog( TRefByValue<const TDesC8> aFmt, ... ) |
|
354 { |
|
355 VA_LIST list; |
|
356 VA_START( list, aFmt ); |
|
357 TBuf8<KUdpLogBufferMaxSize> buf; |
|
358 buf.FormatList( aFmt, list ); |
|
359 RFileLogger::Write( KUdpLogDir, KUdpWatcherLogFile, EFileLoggingModeAppend, buf ); |
|
360 } |
|
361 |
|
362 #endif |
|
363 |
|
364 // End of File |
|
365 |