plugins/consoles/consoleproxy/src/server.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // server.cpp
       
     2 // 
       
     3 // Copyright (c) 2009 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <consoleproxy.h>
       
    14 #include "server.h"
       
    15 
       
    16 _LIT(KPronxyConsolePanic, "proxyconsole");
       
    17 	
       
    18 void Panic(TConsoleProxyPanic aReason)
       
    19 	{
       
    20 	User::Panic(KPronxyConsolePanic, aReason);
       
    21 	}
       
    22 
       
    23 
       
    24 void ServerThreadL(TServerParams* aParams)
       
    25 	{
       
    26 	CActiveScheduler* as = new(ELeave)CActiveScheduler;
       
    27 	CleanupStack::PushL(as);
       
    28 	CActiveScheduler::Install(as);
       
    29 	
       
    30 	CConsoleProxyServer* server = (aParams->iServerNewL)(aParams->iServerParams);
       
    31 	CleanupStack::PushL(server);
       
    32 	
       
    33 	aParams->iServer = server->Server();
       
    34 	RThread::Rendezvous(KErrNone);
       
    35 	
       
    36 	CActiveScheduler::Start();
       
    37 	
       
    38 	CleanupStack::PopAndDestroy(2, as);
       
    39 	}
       
    40 		
       
    41 
       
    42 TInt ServerThreadFunction(TAny* aArgs)
       
    43 	{
       
    44 	__UHEAP_MARK;
       
    45 	User::SetCritical(User::ENotCritical);
       
    46 	
       
    47 	TServerParams* params = (TServerParams*)aArgs;
       
    48 	
       
    49 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
    50 	if (!cleanup)
       
    51 		{
       
    52 		return KErrNoMemory;
       
    53 		}
       
    54 		
       
    55 	TRAPD(err, ServerThreadL(params));
       
    56 		
       
    57 	delete cleanup;
       
    58 	__UHEAP_MARKEND;
       
    59 	return err;
       
    60 	}
       
    61 		
       
    62 //______________________________________________________________________________
       
    63 //						CConsoleProxyServer
       
    64 CConsoleProxyServer* CConsoleProxyServer::NewL(TAny* aParams)
       
    65 	{
       
    66 	TConsoleProxyServerNewLParams* params = (TConsoleProxyServerNewLParams*)aParams;
       
    67 	return NewL(params->iName, params->iAoPriority, params->iConsoleCreate);
       
    68 	}
       
    69 	
       
    70 EXPORT_C CConsoleProxyServer* CConsoleProxyServer::NewL(const TDesC& aName, TInt aAoPriority, TConsoleCreateFunction aConsoleCreate)
       
    71 	{
       
    72 	CConsoleProxyServer* self = new(ELeave)CConsoleProxyServer(aConsoleCreate, aAoPriority);
       
    73 	CleanupStack::PushL(self);
       
    74 	self->ConstructL(aName);
       
    75 	CleanupStack::Pop(self);
       
    76 	return self;
       
    77 	}
       
    78 	
       
    79 EXPORT_C void CConsoleProxyServer::ConstructL(const TDesC& aName)
       
    80 	{
       
    81 	StartL(aName);
       
    82 	iShutdownTimer = CShutdownTimer::NewL(*this);
       
    83 	iShutdownTimer->Start();
       
    84 	}
       
    85 
       
    86 EXPORT_C CConsoleProxyServer::CConsoleProxyServer(TConsoleCreateFunction aConsoleCreate, TInt aAoPriority)
       
    87 	: CServer2(aAoPriority, CServer2::ESharableSessions)
       
    88 	, iConsoleCreate(aConsoleCreate)
       
    89 	{
       
    90 	}
       
    91 
       
    92 EXPORT_C CConsoleProxyServer::~CConsoleProxyServer()
       
    93 	{
       
    94 	delete iShutdownTimer;
       
    95 	iShutdownTimer = NULL; // Can be referenced by session destructors which run after this, as part of ~CServer2, which will call ~CConsoleProxySession which can call DropSession...
       
    96 	}
       
    97 	
       
    98 EXPORT_C void CConsoleProxyServer::ShutdownTimerExpired()
       
    99 	{
       
   100 	CActiveScheduler::Stop();
       
   101 	}
       
   102 
       
   103 EXPORT_C CSession2* CConsoleProxyServer::NewSessionL(const TVersion&,const RMessage2&) const
       
   104 	{
       
   105 	return new(ELeave)CConsoleProxySession(iConsoleCreate);
       
   106 	}
       
   107 
       
   108 void CConsoleProxyServer::AddSession()
       
   109 	{
       
   110 	iShutdownTimer->Cancel();
       
   111 	++iSessionCount;
       
   112 	}
       
   113 
       
   114 void CConsoleProxyServer::DropSession()
       
   115 	{
       
   116 	--iSessionCount;
       
   117 	if ((iSessionCount==0) && iShutdownTimer && (!iShutdownTimer->IsActive()))
       
   118 		{
       
   119 		iShutdownTimer->Start();
       
   120 		}
       
   121 	}
       
   122 
       
   123 //______________________________________________________________________________
       
   124 //						CConsoleProxySession
       
   125 EXPORT_C CConsoleProxySession::CConsoleProxySession(TConsoleCreateFunction aConsoleCreate)
       
   126 	: iConsoleCreate(aConsoleCreate)
       
   127 	{
       
   128 	}
       
   129 	
       
   130 EXPORT_C CConsoleProxySession::CConsoleProxySession(MProxiedConsole* aConsole)
       
   131 	: iConsole(aConsole)
       
   132 	{
       
   133 	iConsole->Open();
       
   134 	}
       
   135 
       
   136 EXPORT_C CConsoleProxySession::~CConsoleProxySession()
       
   137 	{
       
   138 	Server()->DropSession();
       
   139 	if (!iReadMessage.IsNull())
       
   140 		{
       
   141 		iReadMessage.Complete(KErrDisconnected);
       
   142 		if (iConsole) iConsole->ReadCancel();
       
   143 		}
       
   144 	if (iConsole) iConsole->Close();
       
   145 	}
       
   146 
       
   147 EXPORT_C void CConsoleProxySession::CreateL()
       
   148 	{
       
   149 	Server()->AddSession();
       
   150 	}
       
   151 
       
   152 EXPORT_C void CConsoleProxySession::ServiceL(const RMessage2& aMessage)
       
   153 	{
       
   154 	if (!iConsole && (aMessage.Function() != RConsoleProxy::ECreate))
       
   155 		{
       
   156 		User::Leave(KErrNotReady);
       
   157 		}
       
   158 	RBuf buf;
       
   159 	CleanupClosePushL(buf);
       
   160 	TBool complete = ETrue;
       
   161 	switch (aMessage.Function())
       
   162 		{
       
   163 	case RConsoleProxy::ECreate:
       
   164 		CreateL(aMessage);
       
   165 		break;
       
   166 	case RConsoleProxy::ERead:
       
   167 		if (!iReadMessage.IsNull()) User::Leave(KErrAlreadyExists);
       
   168 		iReadType = EReadBasic;
       
   169 		iReadMessage = aMessage;
       
   170 		iConsole->Read(*this);
       
   171 		complete = EFalse;
       
   172 		break;
       
   173 	case RConsoleProxy::EReadKey:
       
   174 		if (!iReadMessage.IsNull()) User::Leave(KErrAlreadyExists);
       
   175 		iReadType = EReadKeys;
       
   176 		iReadMessage = aMessage;
       
   177 		iConsole->Read(*this);
       
   178 		complete = EFalse;
       
   179 		break;
       
   180 	case RConsoleProxy::EReadCancel:
       
   181 		if (!iReadMessage.IsNull())
       
   182 			{
       
   183 			iReadMessage.Complete(KErrCancel);
       
   184 			iConsole->ReadCancel();
       
   185 			}
       
   186 		break;
       
   187 	case RConsoleProxy::EWrite:
       
   188 		buf.CreateL(aMessage.GetDesLengthL(0));
       
   189 		aMessage.ReadL(0, buf);
       
   190 		iConsole->Console()->Write(buf);
       
   191 		break;
       
   192 	case RConsoleProxy::EGetCursorPos:
       
   193 		{
       
   194 		TPckgBuf<TPoint> pos;
       
   195 		pos() = iConsole->Console()->CursorPos();
       
   196 		aMessage.WriteL(0, pos);
       
   197 		break;
       
   198 		}
       
   199 	case RConsoleProxy::ESetCursorPosAbs:
       
   200 		{
       
   201 		TPoint pos(aMessage.Int0(), aMessage.Int1());
       
   202 		iConsole->Console()->SetCursorPosAbs(pos);
       
   203 		break;
       
   204 		}
       
   205 	case RConsoleProxy::ESetCursorPosRel:
       
   206 		{
       
   207 		TPoint pos(aMessage.Int0(), aMessage.Int1());
       
   208 		iConsole->Console()->SetCursorPosRel(pos);
       
   209 		break;
       
   210 		}
       
   211 	case RConsoleProxy::ESetCursorHeight:
       
   212 		iConsole->Console()->SetCursorHeight(aMessage.Int0());
       
   213 		break;
       
   214 	case RConsoleProxy::ESetTitle:
       
   215 		buf.CreateL(aMessage.GetDesLengthL(0));
       
   216 		aMessage.ReadL(0, buf);
       
   217 		iConsole->Console()->SetTitle(buf);
       
   218 		break;
       
   219 	case RConsoleProxy::EClearScreen:
       
   220 		iConsole->Console()->ClearScreen();
       
   221 		break;
       
   222 	case RConsoleProxy::EClearToEndOfLine:
       
   223 		iConsole->Console()->ClearToEndOfLine();
       
   224 		break;
       
   225 	case RConsoleProxy::EGetScreenSize:
       
   226 		{
       
   227 		TPckgBuf<TSize> size;
       
   228 		if (aMessage.GetDesLengthL(0)!=size.Length()) User::Leave(KErrArgument);
       
   229 		size() = iConsole->Console()->ScreenSize();
       
   230 		aMessage.WriteL(0, size);
       
   231 		break;
       
   232 		}
       
   233 	case RConsoleProxy::EGetKeyCode:
       
   234 		{
       
   235 		TPckg<TKeyCode> kc(iKeyCode);
       
   236 		if (aMessage.GetDesLengthL(0)!=kc.Length()) User::Leave(KErrArgument);
       
   237 		aMessage.WriteL(0, kc);
       
   238 		break;
       
   239 		}
       
   240 	case RConsoleProxy::EGetKeyModifiers:
       
   241 		{
       
   242 		TPckg<TUint> mod(iKeyModifiers);
       
   243 		if (aMessage.GetDesLengthL(0)!=mod.Length()) User::Leave(KErrArgument);
       
   244 		aMessage.WriteL(0, mod);
       
   245 		break;
       
   246 		}
       
   247 	case RConsoleProxy::ESetAttributes:
       
   248 		{
       
   249 		if (!iConsole) User::Leave(KErrNotReady);
       
   250 		ConsoleAttributes::TAttributes attributes((TUint)aMessage.Int0(), (ConsoleAttributes::TColor)aMessage.Int1(), (ConsoleAttributes::TColor)aMessage.Int2());
       
   251 		TInt err = ConsoleAttributes::Set(iConsole->Console(), attributes);
       
   252 		aMessage.Complete(err);
       
   253 		complete = EFalse;
       
   254 		break;
       
   255 		}
       
   256 	case RConsoleProxy::EIsConstructed:
       
   257 		{
       
   258 		if (aMessage.GetDesLengthL(0)!=sizeof(TBool)) User::Leave(KErrArgument);
       
   259 		if (!iConsole) User::Leave(KErrNotReady);
       
   260 		if (!LazyConsole::IsLazy(iConsole->Console())) User::Leave(KErrExtensionNotSupported);
       
   261 		TPckgBuf<TBool> constructed = LazyConsole::IsConstructed(iConsole->Console());
       
   262 		aMessage.WriteL(0, constructed);
       
   263 		complete = ETrue;
       
   264 		break;
       
   265 		}
       
   266 	default:
       
   267 		aMessage.Complete(KErrNotSupported);
       
   268 		}
       
   269 	CleanupStack::PopAndDestroy(&buf);
       
   270 	if (complete)
       
   271 		{
       
   272 		aMessage.Complete(KErrNone);
       
   273 		}
       
   274 	}
       
   275 	
       
   276 EXPORT_C void CConsoleProxySession::ReadComplete(TInt aStatus)
       
   277 	{
       
   278 	iKeyCode = iConsole->Console()->KeyCode();
       
   279 	iKeyModifiers = iConsole->Console()->KeyModifiers();
       
   280 	if (!iReadMessage.IsNull())
       
   281 		{
       
   282 		TInt err = aStatus;
       
   283 		if (iReadType == EReadKeys)
       
   284 			{
       
   285 			if (err == KErrNone)
       
   286 				{
       
   287 				err = iReadMessage.Write(0, TPckg<TKeyCode>(iKeyCode));
       
   288 				}
       
   289 			if (err == KErrNone)
       
   290 				{
       
   291 				err = iReadMessage.Write(1, TPckg<TUint>(iKeyModifiers));
       
   292 				}	
       
   293 			}
       
   294 		
       
   295 		iReadMessage.Complete(err);
       
   296 		}
       
   297 	}
       
   298 	
       
   299 EXPORT_C void CConsoleProxySession::DoCreateL(const TDesC& aTitle, const TSize& aSize)
       
   300 	{
       
   301 	__ASSERT_ALWAYS(!iConsole, Panic(EConsoleAlreadyCreated));
       
   302 	MProxiedConsole* cons = InstantiateConsoleL();
       
   303 	__ASSERT_ALWAYS(cons, Panic(ENoConsoleInstatiated));
       
   304 	CleanupClosePushL(*cons);
       
   305 	TName procName = RProcess().Name(); // econseik sets the process name to the console title...
       
   306 	TInt err = cons->Console()->Create(aTitle, aSize);
       
   307 	User::RenameProcess(procName.Left(procName.Locate('['))); // ...so restore it just in case
       
   308 	User::LeaveIfError(err);
       
   309 	
       
   310 	ConsoleCreatedL(cons);
       
   311 	
       
   312 	iConsole = cons;
       
   313 	CleanupStack::Pop(cons);
       
   314 	}
       
   315 	
       
   316 EXPORT_C void CConsoleProxySession::ConsoleCreatedL(MProxiedConsole*)
       
   317 	{
       
   318 	}
       
   319 
       
   320 void CConsoleProxySession::CreateL(const RMessage2& aMessage)
       
   321 	{
       
   322 	if (iConsole) User::Leave(KErrAlreadyExists);
       
   323 	RBuf title;
       
   324 	title.CreateL(aMessage.GetDesLengthL(0));
       
   325 	CleanupClosePushL(title);
       
   326 	aMessage.ReadL(0, title);
       
   327 	
       
   328 	TSize size(aMessage.Int1(), aMessage.Int2());
       
   329 	
       
   330 	DoCreateL(title, size);
       
   331 	
       
   332 	CleanupStack::PopAndDestroy(&title);
       
   333 	}
       
   334 
       
   335 EXPORT_C MProxiedConsole* CConsoleProxySession::InstantiateConsoleL()
       
   336 	{
       
   337 	return CConsoleWrapper::NewL(iConsoleCreate);
       
   338 	}
       
   339 
       
   340 
       
   341 //______________________________________________________________________________
       
   342 //						MProxiedConsole
       
   343 EXPORT_C MProxiedConsole* MProxiedConsole::DefaultL(CConsoleBase* aConsole)
       
   344 	{
       
   345 	return CConsoleWrapper::NewL(aConsole);
       
   346 	}
       
   347 //______________________________________________________________________________
       
   348 //						CConsoleWrapper
       
   349 CConsoleWrapper* CConsoleWrapper::NewL(TConsoleCreateFunction aConsoleCreate)
       
   350 	{
       
   351 	CConsoleWrapper* self = new(ELeave)CConsoleWrapper;
       
   352 	CleanupStack::PushL(self);
       
   353 	self->ConstructL(aConsoleCreate);
       
   354 	CleanupStack::Pop(self);
       
   355 	return self;
       
   356 	}
       
   357 	
       
   358 CConsoleWrapper* CConsoleWrapper::NewL(CConsoleBase* aConsole)
       
   359 	{
       
   360 	CConsoleWrapper* self = new(ELeave)CConsoleWrapper;
       
   361 	self->iConsole = aConsole;
       
   362 	return self;	
       
   363 	}
       
   364 	
       
   365 CConsoleWrapper::~CConsoleWrapper()
       
   366 	{
       
   367 	Cancel();
       
   368 	delete iConsole;
       
   369 	}
       
   370 
       
   371 void CConsoleWrapper::Open()
       
   372 	{
       
   373 	++iRefCount;
       
   374 	}
       
   375 
       
   376 void CConsoleWrapper::Close()
       
   377 	{
       
   378 	--iRefCount;
       
   379 	if (!iRefCount)
       
   380 		{
       
   381 		delete this;
       
   382 		}
       
   383 	}
       
   384 
       
   385 CConsoleBase* CConsoleWrapper::Console()
       
   386 	{
       
   387 	return iConsole;
       
   388 	}
       
   389 
       
   390 void CConsoleWrapper::Read(CConsoleProxySession& aSession)
       
   391 	{
       
   392 	if (!IsActive())
       
   393 		{
       
   394 		iReader = &aSession;
       
   395 		iConsole->Read(iStatus);
       
   396 		SetActive();
       
   397 		}
       
   398 	}
       
   399 
       
   400 void CConsoleWrapper::ReadCancel()
       
   401 	{
       
   402 	Cancel();
       
   403 	iReader = NULL;
       
   404 	}
       
   405 
       
   406 void CConsoleWrapper::RunL()
       
   407 	{
       
   408 	iReader->ReadComplete(iStatus.Int());
       
   409 	}
       
   410 
       
   411 void CConsoleWrapper::DoCancel()
       
   412 	{
       
   413 	iConsole->ReadCancel();
       
   414 	}
       
   415 
       
   416 void CConsoleWrapper::ConstructL(TConsoleCreateFunction aConsoleCreate)
       
   417 	{
       
   418 	iConsole = aConsoleCreate();
       
   419 	User::LeaveIfNull(iConsole);
       
   420 	}
       
   421 
       
   422 CConsoleWrapper::CConsoleWrapper()
       
   423 	: CActive(CActive::EPriorityStandard)
       
   424 	, iRefCount(1)
       
   425 	{
       
   426 	CActiveScheduler::Add(this);
       
   427 	}
       
   428 
       
   429 
       
   430 //______________________________________________________________________________
       
   431 //						CShutdownTimer
       
   432 CShutdownTimer* CShutdownTimer::NewL(CConsoleProxyServer& aServer)
       
   433 	{
       
   434 	CShutdownTimer* self = new(ELeave)CShutdownTimer(aServer);
       
   435 	CleanupStack::PushL(self);
       
   436 	self->ConstructL();
       
   437 	CleanupStack::Pop(self);
       
   438 	return self;
       
   439 	}
       
   440 	
       
   441 CShutdownTimer::CShutdownTimer(CConsoleProxyServer& aServer)
       
   442 	: CTimer(CActive::EPriorityLow), iServer(aServer)
       
   443 	{
       
   444 	CActiveScheduler::Add(this);
       
   445 	}
       
   446 	
       
   447 void CShutdownTimer::Start()
       
   448 	{
       
   449 	After(KServerShutdownTimer);
       
   450 	}
       
   451 
       
   452 void CShutdownTimer::RunL()
       
   453 	{
       
   454 	iServer.ShutdownTimerExpired();
       
   455 	}
       
   456