testexecmdw/tef/tef/pipslogger/src/server.cpp
branchRCL_3
changeset 3 9397a16b6eb8
parent 1 6edeef394eb7
equal deleted inserted replaced
1:6edeef394eb7 3:9397a16b6eb8
     1 /*
       
     2 * Copyright (c) 2005-2009 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:  
       
    15 * Main log server engine.
       
    16 * Process log requests from multiple clients simultaneously.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 /**
       
    23  @file server.cpp
       
    24 */
       
    25 #include "server.h"
       
    26 #include <unistd.h>
       
    27 #include <fcntl.h>
       
    28 
       
    29 CLogServer* CLogServer::NewL()
       
    30 /**
       
    31  * @return - Instance of the log server
       
    32  */
       
    33 	{
       
    34 	CLogServer * server = new (ELeave) CLogServer();
       
    35 	CleanupStack::PushL(server);
       
    36 	server->ConstructL();
       
    37 	// CServer base class call
       
    38 	server->StartL(KTestExecutePIPSLogServerName);
       
    39 	CleanupStack::Pop(server);
       
    40 	return server;
       
    41 	}
       
    42 
       
    43 void CLogServer::ConstructL()
       
    44 /**
       
    45  * Second phase construction
       
    46  */
       
    47 	{
       
    48 	}
       
    49 
       
    50 
       
    51 CLogServer::CLogServer() : CServer2(EPriorityStandard,ESharableSessions)
       
    52 /**
       
    53  * Constructor
       
    54  */
       
    55 	{
       
    56 	}
       
    57 
       
    58 CLogServer::~CLogServer()
       
    59 /**
       
    60  * Destructor
       
    61  */
       
    62 	{
       
    63 	// Close the array of control structures
       
    64 	LogControl().Close();
       
    65 	//Fs().Close();
       
    66 	}
       
    67 
       
    68 
       
    69 CSession2* CLogServer::NewSessionL(const TVersion& /*aVersion*/,const RMessage2& /*aMessage*/) const
       
    70 /**
       
    71  * @param RMessage - RMessage for the session open
       
    72  */
       
    73 	{
       
    74 	// Just create the session
       
    75 	CLogSession* session = new (ELeave) CLogSession();
       
    76 	return session;
       
    77 	}
       
    78 
       
    79 void CLogServer::ControlComplete(CLogSession& aControl)
       
    80 /**
       
    81  * @param aControl - Logfile control class reference
       
    82  *
       
    83  * Checks to see if this control session can be removed 
       
    84  */
       
    85 	{
       
    86 	if(aControl.SessionCount())
       
    87 		return;
       
    88 
       
    89 	// There are no subsessions mapped to the logfile control class and
       
    90 	// no data buffers on its write queue
       
    91 	// Loop through the server's control array and remove it then delete it
       
    92 	TInt i;
       
    93 	for(i=0;i<LogControl().Count();i++)
       
    94 		{
       
    95 		if(&aControl == LogControl()[i])
       
    96 			{
       
    97 			// Done
       
    98 			LogControl().Remove(i);
       
    99 			delete &aControl;
       
   100 			break;
       
   101 			}
       
   102 		}
       
   103 	// If it's the last one then exit the server
       
   104 	if(!LogControl().Count())
       
   105 		CActiveScheduler::Stop();
       
   106 	}
       
   107 
       
   108 
       
   109 ///////
       
   110 
       
   111 CLogSession::CLogSession()
       
   112 :iFileDes(-1)
       
   113 /**
       
   114  * Constructor
       
   115  */
       
   116 	{
       
   117 	}
       
   118 
       
   119 CLogSession::~CLogSession()
       
   120 /**
       
   121  * Destructor
       
   122  */
       
   123 	{
       
   124 	// Check for null
       
   125 	// Session close without a createlog call leaves iControl null
       
   126 	// A logfile control structure can have multiple server sessions
       
   127 	// decrement its server session count
       
   128 	RemoveSession();
       
   129 	CLogServer* p=(CLogServer*) Server();
       
   130 	// Shuts Down the server if this is the last open session
       
   131 	p->ControlComplete(*this);
       
   132 	}
       
   133 
       
   134 void CLogSession::ServiceL(const RMessage2& aMessage)
       
   135 /**
       
   136  * @param aMessage - Function and data for the session
       
   137  */
       
   138 	{
       
   139 	switch(aMessage.Function())
       
   140 		{
       
   141 //		case RTestExecutePIPSLogServ::ECreateLog :
       
   142 //			{}
       
   143 		// One of the API write calls
       
   144 		case RTestExecutePIPSLogServ::EWriteLog :
       
   145 			{
       
   146 			// Data can be any size
       
   147 			// Get the length from second argument
       
   148 			TInt bufferLength = aMessage.Int1();
       
   149 			// Get a heap buffer of the right size
       
   150 			HBufC8* buffer = HBufC8::NewLC(bufferLength);
       
   151 			TPtr8 ptr(buffer->Des());
       
   152 			// read the data
       
   153 			aMessage.ReadL(0,ptr);			
       
   154 			// Get a buffer control class contructed with the heap data
       
   155 			// takes ownership
       
   156 			//CLogBuffer* logBuffer = new (ELeave) CLogBuffer(*buffer);
       
   157 			CleanupStack::Pop(buffer);
       
   158 			//get the name of the pipe we want to dump the logs to
       
   159 			TInt pipeNameLen = aMessage.Int3();
       
   160 			// Get a heap buffer of the right size
       
   161 			HBufC8* pipebuffer = HBufC8::NewLC(pipeNameLen);
       
   162 			TPtr8 pipePtr(pipebuffer->Des());
       
   163 			// read the data
       
   164 			aMessage.ReadL(2,pipePtr);			
       
   165 			// Get a buffer control class contructed with the heap data
       
   166 			// takes ownership
       
   167 			CleanupStack::Pop(pipebuffer);
       
   168 			
       
   169 			if((iFileDes == -1)  || (!iPipeName ) || (iPipeName->CompareF(pipePtr))  )
       
   170 				{
       
   171 				iPipeName = new TPtr8(pipePtr) ; 
       
   172 				//iPipeName.Set(pipePtr) ;  
       
   173 				
       
   174 				char arr[KMaxPath];
       
   175 				int var;
       
   176 				for (var = 0; var < pipePtr.Length(); ++var) {
       
   177 					arr[var]=pipePtr[var];
       
   178 				}
       
   179 				arr[var]='\0';
       
   180 				
       
   181 				iFileDes = open( arr, O_WRONLY);
       
   182 				}
       
   183 			
       
   184 			const char *printsomething = (const char *)ptr.Ptr() ;
       
   185 			// incases when the pipe name given was empty / incorrect
       
   186 			// or unavailable for some reason, the open is expected
       
   187 			// to return -1, and then we do nothing
       
   188 			if(iFileDes != -1 ) 
       
   189 				{
       
   190 				write(iFileDes , printsomething, bufferLength) ; 
       
   191 				aMessage.Complete(KErrNone);
       
   192 				}
       
   193 			else
       
   194 				{
       
   195 				aMessage.Complete(KErrNotFound);
       
   196 				}
       
   197 			// apparently closing the pipe is an issue with named pipes
       
   198 			// so let be for now
       
   199 			//close(aFileDes); 
       
   200 			}
       
   201 			break;
       
   202 
       
   203 		default:
       
   204 			break;
       
   205 		}
       
   206 	}
       
   207