63
|
1 |
/*
|
|
2 |
* Copyright (c) 2007 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: CCA client's connecting class.
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
#include <e32std.h>
|
|
19 |
#include <s32mem.h>
|
|
20 |
#include <aknappui.h>
|
|
21 |
|
|
22 |
#include "ccaclientheaders.h"
|
|
23 |
|
|
24 |
/// Unnamed namespace for local definitions
|
|
25 |
namespace {
|
|
26 |
|
|
27 |
const TInt KCCAStreamBufferSize = 256;
|
|
28 |
const TInt KCCAConnectionMaxRetriesForLaunchKErrInUse = 5;
|
|
29 |
const TInt KCCAConnectionRelaunchDelay = 0.2 * 1000000;// 0.2 sec
|
|
30 |
|
|
31 |
#ifdef _DEBUG
|
|
32 |
enum TPanicCode
|
|
33 |
{
|
|
34 |
EPanicPreCond_DoLaunchL = 1,
|
|
35 |
EPanicPreCond_DoLaunchL2,
|
|
36 |
EPanicPreCond_DoLaunchL3
|
|
37 |
};
|
|
38 |
|
|
39 |
void Panic(TPanicCode aReason)
|
|
40 |
{
|
|
41 |
_LIT(KPanicText, "CCCAConnection");
|
|
42 |
User::Panic(KPanicText, aReason);
|
|
43 |
}
|
|
44 |
#endif // _DEBUG
|
|
45 |
|
|
46 |
} /// namespace
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
// ================= MEMBER FUNCTIONS =======================
|
|
51 |
//
|
|
52 |
|
|
53 |
// ----------------------------------------------------------
|
|
54 |
// CCCAConnection::NewL
|
|
55 |
// ----------------------------------------------------------
|
|
56 |
//
|
|
57 |
CCCAConnection* CCCAConnection::NewL()
|
|
58 |
{
|
|
59 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "CCCAConnection::NewL"));
|
|
60 |
|
|
61 |
CCCAConnection* self = new (ELeave) CCCAConnection();
|
|
62 |
CleanupStack::PushL( self );
|
|
63 |
self->ConstructL();
|
|
64 |
CleanupStack::Pop( self );
|
|
65 |
return self;
|
|
66 |
}
|
|
67 |
|
|
68 |
// ----------------------------------------------------------
|
|
69 |
// CCCAConnection::CCCAConnection
|
|
70 |
// ----------------------------------------------------------
|
|
71 |
//
|
|
72 |
CCCAConnection::CCCAConnection() :
|
|
73 |
CActive( EPriorityStandard ),
|
|
74 |
iAppStatus( EAppClosed ),
|
|
75 |
iMsgStreamPtr( NULL, 0 )
|
|
76 |
{
|
|
77 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "CCCAConnection::CCCAConnection"));
|
|
78 |
CActiveScheduler::Add( this );
|
|
79 |
}
|
|
80 |
|
|
81 |
// ----------------------------------------------------------
|
|
82 |
// CCCAConnection::ConstructL
|
|
83 |
// ----------------------------------------------------------
|
|
84 |
//
|
|
85 |
void CCCAConnection::ConstructL()
|
|
86 |
{
|
|
87 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "CCCAConnection::ConstructL"));
|
|
88 |
iSchedulerWait = new ( ELeave ) CActiveSchedulerWait();
|
|
89 |
}
|
|
90 |
|
|
91 |
// ----------------------------------------------------------
|
|
92 |
// CCCAConnection::~CCCAConnection
|
|
93 |
// ----------------------------------------------------------
|
|
94 |
//
|
|
95 |
CCCAConnection::~CCCAConnection()
|
|
96 |
{
|
|
97 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "CCCAConnection::~CCCAConnection"));
|
|
98 |
|
|
99 |
Cancel();
|
|
100 |
delete iSchedulerWait;
|
|
101 |
delete iMsgStream;
|
|
102 |
DisconnectServerApplication();
|
|
103 |
CloseParameter();
|
|
104 |
}
|
|
105 |
|
|
106 |
// ----------------------------------------------------------
|
|
107 |
// CCCAConnection::Close
|
|
108 |
// ----------------------------------------------------------
|
|
109 |
//
|
|
110 |
void CCCAConnection::Close()
|
|
111 |
{
|
|
112 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "CCCAConnection::Close"));
|
|
113 |
delete this;
|
|
114 |
}
|
|
115 |
|
|
116 |
// --------------------------------------------------------------------------
|
|
117 |
// CCCAConnection::RunL
|
|
118 |
// --------------------------------------------------------------------------
|
|
119 |
//
|
|
120 |
void CCCAConnection::RunL()
|
|
121 |
{
|
|
122 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "CCCAConnection::RunL: iStatus: %d"), iStatus.Int());
|
|
123 |
if ( iSchedulerWait->IsStarted() )
|
|
124 |
{
|
|
125 |
iSchedulerWait->AsyncStop();
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
// --------------------------------------------------------------------------
|
|
130 |
// CCCAConnection::DoCancel
|
|
131 |
// --------------------------------------------------------------------------
|
|
132 |
//
|
|
133 |
void CCCAConnection::DoCancel()
|
|
134 |
{
|
|
135 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "CCCAConnection::DoCancel"));
|
|
136 |
if ( iSchedulerWait->IsStarted() )
|
|
137 |
{
|
|
138 |
iSchedulerWait->AsyncStop();
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
// ----------------------------------------------------------
|
|
143 |
// CCCAConnection::LaunchApplicationL
|
|
144 |
// ----------------------------------------------------------
|
|
145 |
//
|
|
146 |
void CCCAConnection::LaunchApplicationL( MCCAParameter& aParameter )
|
|
147 |
{
|
|
148 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "->CCCAConnection::LaunchApplicationL"));
|
|
149 |
|
|
150 |
//Only one active connection per time
|
|
151 |
__ASSERT_ALWAYS( iAppStatus == EAppClosed, User::Leave( KErrAlreadyExists ));
|
|
152 |
|
|
153 |
iAppStatus = EAppLaunching;
|
|
154 |
|
|
155 |
//PERFORMANCE LOGGING: 1. Starting AppServer (StartPoint)
|
|
156 |
WriteToPerfLog();
|
|
157 |
WriteToPerfLaunchLog(_L(" CCA launched"));
|
|
158 |
|
|
159 |
// Need to trap, because consumer freezes if ConnectL function leaves.
|
|
160 |
// Reason for the freeze is that connection stays open.
|
|
161 |
TRAPD( error, ConnectServerApplicationL() );
|
|
162 |
if ( KErrNone != error )
|
|
163 |
{
|
|
164 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "::LaunchApplicationL: Error connect: %d"), error);
|
|
165 |
DisconnectServerApplication();
|
|
166 |
User::Leave( error );
|
|
167 |
}
|
|
168 |
|
|
169 |
DoLaunchL( aParameter );
|
|
170 |
SetActive();
|
|
171 |
iSchedulerWait->Start();
|
|
172 |
|
|
173 |
error = iStatus.Int();
|
|
174 |
if ( KErrNone != error )
|
|
175 |
{
|
|
176 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "::LaunchApplicationL: Error launch: %d"), error);
|
|
177 |
DisconnectServerApplication();
|
|
178 |
User::Leave( error );
|
|
179 |
}
|
|
180 |
|
|
181 |
iAppStatus = EAppRunning;
|
|
182 |
|
|
183 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "<-CCCAConnection::LaunchApplicationL"));
|
|
184 |
}
|
|
185 |
|
|
186 |
// ----------------------------------------------------------
|
|
187 |
// CCCAConnection::LaunchAppL
|
|
188 |
// ----------------------------------------------------------
|
|
189 |
//
|
|
190 |
void CCCAConnection::LaunchAppL(
|
|
191 |
MCCAParameter& aParameter,
|
|
192 |
MCCAObserver* aObserver )
|
|
193 |
{
|
|
194 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "->CCCAConnection::LaunchAppL"));
|
|
195 |
|
|
196 |
if (iAppStatus == EAppClosed)
|
|
197 |
{
|
|
198 |
LaunchApplicationL( aParameter );
|
|
199 |
|
|
200 |
CloseParameter();
|
|
201 |
iParameter = static_cast<CCCAParameter*>( &aParameter );
|
|
202 |
iObserver = aObserver;
|
|
203 |
}
|
|
204 |
|
|
205 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "<-CCCAConnection::LaunchAppL"));
|
|
206 |
}
|
|
207 |
|
|
208 |
// --------------------------------------------------------------------------
|
|
209 |
// CCCAConnection::CloseAppL
|
|
210 |
// --------------------------------------------------------------------------
|
|
211 |
//
|
|
212 |
void CCCAConnection::CloseAppL()
|
|
213 |
{
|
|
214 |
if ( !iClosePending ) //To protect from duplicate close attempts
|
|
215 |
{
|
|
216 |
iAppService.DoCloseApplication();
|
|
217 |
iClosePending = ETrue;
|
|
218 |
}
|
|
219 |
}
|
|
220 |
|
|
221 |
// --------------------------------------------------------------------------
|
|
222 |
// CCCAConnection::HandleServerAppExit
|
|
223 |
// --------------------------------------------------------------------------
|
|
224 |
//
|
|
225 |
void CCCAConnection::HandleServerAppExit( TInt aReason )
|
|
226 |
{
|
|
227 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "->CCCAConnection::HandleServerAppExit"));
|
|
228 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "::HandleServerAppExit: aReason: %d"), aReason);
|
|
229 |
|
|
230 |
DisconnectServerApplication();
|
|
231 |
|
|
232 |
TInt flags(0);
|
|
233 |
if(iParameter)
|
|
234 |
{
|
|
235 |
flags = iParameter->ConnectionFlag();
|
|
236 |
}
|
|
237 |
|
|
238 |
if ( iObserver )
|
|
239 |
{
|
|
240 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "::HandleServerAppExit - informing observer about exit"));
|
|
241 |
CloseParameter();
|
|
242 |
TRAP_IGNORE( iObserver->CCASimpleNotifyL( MCCAObserver::EExitEvent, aReason ));
|
|
243 |
iObserver = NULL;
|
|
244 |
}
|
|
245 |
|
|
246 |
iClosePending = EFalse;
|
|
247 |
|
|
248 |
//Kill us if not soft exit requested
|
|
249 |
if(!(flags & MCCAParameter::ESoftExit))
|
|
250 |
{
|
|
251 |
//Kill this process, i.e. the application running ccaclient
|
|
252 |
// Following call ends application if aReason == EAknCmdExit (and does nothing else)
|
|
253 |
MAknServerAppExitObserver::HandleServerAppExit( aReason );
|
|
254 |
}
|
|
255 |
|
|
256 |
|
|
257 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "<-CCCAConnection::HandleServerAppExit"));
|
|
258 |
}
|
|
259 |
|
|
260 |
// --------------------------------------------------------------------------
|
|
261 |
// CCCAConnection::DisconnectServerApplication
|
|
262 |
// --------------------------------------------------------------------------
|
|
263 |
//
|
|
264 |
void CCCAConnection::DisconnectServerApplication()
|
|
265 |
{
|
|
266 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "->CCCAConnection::DisconnectServerApplication"));
|
|
267 |
|
|
268 |
delete iMonitor;
|
|
269 |
iMonitor = NULL;
|
|
270 |
iAppService.Close();
|
|
271 |
iAppStatus = EAppClosed;
|
|
272 |
|
|
273 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "<-CCCAConnection::DisconnectServerApplication"));
|
|
274 |
}
|
|
275 |
|
|
276 |
// --------------------------------------------------------------------------
|
|
277 |
// CCCAConnection::ConnectServerApplicationL
|
|
278 |
// --------------------------------------------------------------------------
|
|
279 |
//
|
|
280 |
void CCCAConnection::ConnectServerApplicationL()
|
|
281 |
{
|
|
282 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "->CCCAConnection::ConnectServerApplicationL"));
|
|
283 |
|
|
284 |
/*
|
|
285 |
* In cases when the CCApplication is running on background and connection
|
|
286 |
* is closed and reopened instantly, the opening of connection returns
|
|
287 |
* sometimes KErrInUse. One solution could be to arrange Rendezvous-signals
|
|
288 |
* between processes, but to keep implementation somewhat simple, launch is
|
|
289 |
* now just tried again couple of times with small delay between.
|
|
290 |
*/
|
|
291 |
TInt error = KErrInUse;
|
|
292 |
for ( TInt i = 0; i < KCCAConnectionMaxRetriesForLaunchKErrInUse &&
|
|
293 |
(KErrNotFound == error || KErrInUse == error); i++ )
|
|
294 |
{
|
|
295 |
TRAP( error, iAppService.DoConnectL());
|
|
296 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "::ConnectServerApplicationL: error: %d i: %d"), error, i );
|
|
297 |
if ( KErrInUse == error &&
|
|
298 |
i < KCCAConnectionMaxRetriesForLaunchKErrInUse - 1 )
|
|
299 |
{
|
|
300 |
User::After( KCCAConnectionRelaunchDelay );
|
|
301 |
}
|
|
302 |
}
|
|
303 |
User::LeaveIfError( error );
|
|
304 |
|
|
305 |
delete iMonitor;
|
|
306 |
iMonitor = NULL;
|
|
307 |
iMonitor = CApaServerAppExitMonitor::NewL(
|
|
308 |
iAppService, *this, CActive::EPriorityStandard );
|
|
309 |
|
|
310 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "<-CCCAConnection::ConnectServerApplicationL"));
|
|
311 |
}
|
|
312 |
|
|
313 |
// --------------------------------------------------------------------------
|
|
314 |
// CCCAConnection::DoLaunchL
|
|
315 |
// --------------------------------------------------------------------------
|
|
316 |
//
|
|
317 |
void CCCAConnection::DoLaunchL( MCCAParameter& aParameter )
|
|
318 |
{
|
|
319 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "->CCCAConnection::DoLaunchL"));
|
|
320 |
__ASSERT_DEBUG( &aParameter, Panic(EPanicPreCond_DoLaunchL) );
|
|
321 |
|
|
322 |
CCCAParameter& param = STATIC_CAST(CCCAParameter&, aParameter);
|
|
323 |
CBufSeg* buf1 = CBufSeg::NewL( KCCAStreamBufferSize );
|
|
324 |
CleanupStack::PushL( buf1 );
|
|
325 |
|
|
326 |
RBufWriteStream writeStream( *buf1 );
|
|
327 |
CleanupClosePushL( writeStream );
|
|
328 |
param.ExternalizeL( writeStream );
|
|
329 |
writeStream.CommitL();
|
|
330 |
|
|
331 |
delete iMsgStream;
|
|
332 |
iMsgStream = NULL;
|
|
333 |
iMsgStreamPtr.Set( NULL, 0, 0 );
|
|
334 |
|
|
335 |
const TInt neededSize = buf1->Size();
|
|
336 |
iMsgStream = HBufC8::NewL( neededSize );
|
|
337 |
iMsgStreamPtr.Set( iMsgStream->Des() );
|
|
338 |
buf1->Read( 0, iMsgStreamPtr, neededSize );
|
|
339 |
CleanupStack::PopAndDestroy(2); //buf1, writeStream
|
|
340 |
|
|
341 |
__ASSERT_DEBUG( 0 < iMsgStreamPtr.Size(), Panic(EPanicPreCond_DoLaunchL3) );
|
|
342 |
|
|
343 |
iStatus = KRequestPending;
|
|
344 |
iAppService.DoLaunch( iStatus, iMsgStreamPtr );
|
|
345 |
|
|
346 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "<-CCCAConnection::DoLaunchL"));
|
|
347 |
}
|
|
348 |
|
|
349 |
// ----------------------------------------------------------
|
|
350 |
// CCCAConnection::CloseParameter
|
|
351 |
// ----------------------------------------------------------
|
|
352 |
//
|
|
353 |
void CCCAConnection::CloseParameter( )
|
|
354 |
{
|
|
355 |
CCA_DP(KCCAClientLoggerFile, CCA_L( "CCCAConnection::CloseParameter"));
|
|
356 |
if ( iParameter )
|
|
357 |
{
|
|
358 |
iParameter->Close();
|
|
359 |
iParameter = NULL;
|
|
360 |
}
|
|
361 |
}
|
|
362 |
|
|
363 |
// End of file
|