messagingfw/msgtest/testutils/MsgTestUtilServer/src/MessagingTestUtilityServer.cpp
changeset 62 db3f5fa34ec7
parent 0 8e480a14352b
equal deleted inserted replaced
60:9f5ae1728557 62:db3f5fa34ec7
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // MsgTestUtilsServer.cpp
       
    15 // MessagingTestUtilityServer implementation
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <e32std.h>
       
    20 #include "MessagingTestUtilityServer.h"
       
    21 #include "messagingtestutility.h"
       
    22 
       
    23 //DoDeleteMessageStore headers:
       
    24 #include "TestUtilityServerObserver.h"
       
    25 
       
    26 //DoCopyFile headers:
       
    27 #include <bautils.h>
       
    28 
       
    29 
       
    30 inline CShutdown::CShutdown()
       
    31 	:CTimer(-1)
       
    32 	{CActiveScheduler::Add(this);}
       
    33 inline void CShutdown::ConstructL()
       
    34 	{CTimer::ConstructL();}
       
    35 inline void CShutdown::Start()
       
    36 	{After(KMyShutdownDelay);}
       
    37 
       
    38 inline CMessagingTestUtilityServer::CMessagingTestUtilityServer()
       
    39 	:CServer2(0,ESharableSessions)
       
    40 	{}
       
    41 
       
    42 inline CMessagingTestUtilitySession::CMessagingTestUtilitySession()
       
    43 	{}
       
    44 inline CMessagingTestUtilityServer& CMessagingTestUtilitySession::Server()
       
    45 	{return *static_cast<CMessagingTestUtilityServer*>(const_cast<CServer2*>(CSession2::Server()));}
       
    46 
       
    47 
       
    48 void PanicClient(const RMessage2& aMessage,TTestPanic aPanic)
       
    49 //
       
    50 // RMessage::Panic() also completes the message. This is:
       
    51 // (a) important for efficient cleanup within the kernel
       
    52 // (b) a problem if the message is completed a second time
       
    53 //
       
    54 	{
       
    55 	_LIT(KPanic,"MessagingTestUtilityServer");
       
    56  	aMessage.Panic(KPanic,aPanic);
       
    57 	}
       
    58 
       
    59 void CMessagingTestUtilitySession::CreateL()
       
    60 //
       
    61 // 2nd phase construct for sessions - called by the CServer framework
       
    62 //
       
    63 	{
       
    64 	Server().AddSession();
       
    65 	
       
    66 	User::LeaveIfError(iFs.Connect());
       
    67 	}
       
    68 
       
    69 CMessagingTestUtilitySession::~CMessagingTestUtilitySession()
       
    70 	{
       
    71 	Server().DropSession();
       
    72 	
       
    73 	iFs.Close();
       
    74 	delete iDir;
       
    75 	}
       
    76 
       
    77 
       
    78 void CMessagingTestUtilitySession::ServiceL(const RMessage2& aMessage)
       
    79 //
       
    80 // Entry point for when a new message is received
       
    81 //
       
    82 	{
       
    83 	TInt result = KErrNone;
       
    84 
       
    85 	switch (aMessage.Function())
       
    86 		{
       
    87 	case EDeleteMessageStore:
       
    88 		result = DoDeleteMessageStoreL(aMessage);
       
    89 		break;
       
    90 	case EKillProcess:
       
    91 		result = DoKillProcessL(aMessage);
       
    92 		break;
       
    93 	case ECopyFile:
       
    94 		result = DoCopyFileL(aMessage);
       
    95 		break;
       
    96 	case EDeleteFile:
       
    97 		result = DoDeleteFileL(aMessage);
       
    98 		break;
       
    99 	case EMkDir:
       
   100 		result = DoMkDirL(aMessage);
       
   101 		break;
       
   102 	case ERmDir:
       
   103 		result = DoRmDirL(aMessage);
       
   104 		break;
       
   105 	case EGetDir:
       
   106 		result = DoGetDirL(aMessage);
       
   107 		break;
       
   108 	case EGetDirCount:
       
   109 		result = DoGetDirCountL(aMessage);
       
   110 		break;
       
   111 	case EGetDirEntry:
       
   112 		result = DoGetDirEntryL(aMessage);
       
   113 		break;
       
   114 	case EFileExists:
       
   115 		result = DoFileExistsL(aMessage);
       
   116 		break;
       
   117 	default:
       
   118 		PanicClient(aMessage,ETestPanicIllegalFunction);
       
   119 		break;
       
   120 		}
       
   121 	aMessage.Complete(result);
       
   122 	}
       
   123 
       
   124 void CMessagingTestUtilitySession::ServiceError(const RMessage2& aMessage,TInt aError)
       
   125 //
       
   126 // Handle an error from CMySession::ServiceL()
       
   127 // A bad descriptor error implies a badly programmed client, so panic it;
       
   128 // otherwise use the default handling (report the error to the client)
       
   129 //
       
   130 	{
       
   131 	if (aError==KErrBadDescriptor)
       
   132 		PanicClient(aMessage,ETestPanicBadDescriptor);
       
   133 	CSession2::ServiceError(aMessage,aError);
       
   134 	}
       
   135 
       
   136 void CShutdown::RunL()
       
   137 //
       
   138 // Initiate server exit when the timer expires
       
   139 //
       
   140 	{
       
   141 	CActiveScheduler::Stop();
       
   142 	}
       
   143 
       
   144 CServer2* CMessagingTestUtilityServer::NewLC()
       
   145 	{
       
   146 	CMessagingTestUtilityServer* self=new(ELeave) CMessagingTestUtilityServer;
       
   147 	CleanupStack::PushL(self);
       
   148 	self->ConstructL();
       
   149 	return self;
       
   150 	}
       
   151 
       
   152 void CMessagingTestUtilityServer::ConstructL()
       
   153 //
       
   154 // 2nd phase construction - ensure the timer and server objects are running
       
   155 //
       
   156 	{
       
   157 	StartL(KTestServerName);
       
   158 	iShutdown.ConstructL();
       
   159 	// ensure that the server still exits even if the 1st client fails to connect
       
   160 	iShutdown.Start();
       
   161 	}
       
   162 
       
   163 
       
   164 CSession2* CMessagingTestUtilityServer::NewSessionL(const TVersion&,const RMessage2&) const
       
   165 //
       
   166 // Create a new client session. This should really check the version number.
       
   167 //
       
   168 	{
       
   169 	return new(ELeave) CMessagingTestUtilitySession();
       
   170 	}
       
   171 
       
   172 void CMessagingTestUtilityServer::AddSession()
       
   173 //
       
   174 // A new session is being created
       
   175 // Cancel the shutdown timer if it was running
       
   176 //
       
   177 	{
       
   178 	++iSessionCount;
       
   179 	iShutdown.Cancel();
       
   180 	}
       
   181 
       
   182 void CMessagingTestUtilityServer::DropSession()
       
   183 //
       
   184 // A session is being destroyed
       
   185 // Start the shutdown timer if it is the last session.
       
   186 //
       
   187 	{
       
   188 	if (--iSessionCount==0)
       
   189 		iShutdown.Start();
       
   190 	}
       
   191 	
       
   192 //
       
   193 //Functions to service calls to the Messaging Test Utility Server:
       
   194 
       
   195 TInt CMessagingTestUtilitySession::DoDeleteMessageStoreL(const RMessage2& aMessage)
       
   196 	{
       
   197 	TPckgBuf<TMsvId> msvIdBuf;
       
   198 	aMessage.ReadL(0,msvIdBuf);
       
   199 	TMsvId msvId;	
       
   200 	msvId = msvIdBuf();
       
   201 	
       
   202 	CTestUtilityServerObserver* ob = CTestUtilityServerObserver::NewLC();
       
   203 	
       
   204 	CMsvSession* session = CMsvSession::OpenSyncL(*ob);
       
   205 	CleanupStack::PushL(session);
       
   206 
       
   207 	CMsvEntry* cEntry = CMsvEntry::NewL(*session, msvId, TMsvSelectionOrdering());
       
   208 	CleanupStack::PushL(cEntry);
       
   209 
       
   210 	TInt ret = KErrNone;
       
   211 /*
       
   212 	if (cEntry->HasStoreL())
       
   213 		{
       
   214 		CMsvStore* store = cEntry->EditStoreL();
       
   215 		store->DeleteL();
       
   216 //		test(cEntry->HasStoreL()==EFalse);
       
   217 		}
       
   218 	else
       
   219 		{
       
   220 		ret = KErrInUse;
       
   221 		}
       
   222 */		
       
   223 	CMsvStore* store = cEntry->EditStoreL();
       
   224 	store->DeleteL();
       
   225 	
       
   226 	delete store;
       
   227 
       
   228 	CleanupStack::PopAndDestroy(3); //cEntry, session, ob.
       
   229 	
       
   230 	return ret;
       
   231 	}
       
   232 	
       
   233 TInt CMessagingTestUtilitySession::DoKillProcessL(const RMessage2& aMessage)
       
   234 	{
       
   235 	TPckgBuf<TProcessId> procIdBuf;
       
   236 	aMessage.ReadL(0,procIdBuf);
       
   237 	TProcessId procId;	
       
   238 	procId = procIdBuf();
       
   239 	
       
   240 	RProcess process;
       
   241 	
       
   242 	TInt ret;
       
   243 	
       
   244 	ret = process.Open(procId);
       
   245 	
       
   246 	if (ret != KErrNone)
       
   247 		return ret;
       
   248 	
       
   249 	if (User::ProcessCritical() != User::ENotCritical)
       
   250 		ret = KErrPermissionDenied;
       
   251 	else
       
   252 		process.Kill(KErrNone);
       
   253 	
       
   254 	process.Close();
       
   255 	
       
   256 	return ret;
       
   257 	}
       
   258 	
       
   259 TInt CMessagingTestUtilitySession::DoCopyFileL(const RMessage2& aMessage)
       
   260 	{
       
   261 	TBuf<255> srcPath, destPath;
       
   262 	aMessage.ReadL(0, srcPath);
       
   263 	aMessage.ReadL(1, destPath);
       
   264 	
       
   265 	TInt ret = BaflUtils::CopyFile(iFs, srcPath, destPath);
       
   266 	
       
   267 	return ret;
       
   268 	}
       
   269 	
       
   270 TInt CMessagingTestUtilitySession::DoDeleteFileL(const RMessage2& aMessage)
       
   271 	{
       
   272 	TBuf<255> filePath;
       
   273 	aMessage.ReadL(0, filePath);
       
   274 		
       
   275 	TInt ret = iFs.Delete(filePath);
       
   276 		
       
   277 	return ret;
       
   278 	}
       
   279 	
       
   280 TInt CMessagingTestUtilitySession::DoMkDirL(const RMessage2& aMessage)
       
   281 	{
       
   282 	TBuf<255> dirPath;
       
   283 	aMessage.ReadL(0, dirPath);
       
   284 		
       
   285 	TInt ret = iFs.MkDirAll(dirPath);
       
   286 		
       
   287 	return ret;
       
   288 	}
       
   289 
       
   290 TInt CMessagingTestUtilitySession::DoRmDirL(const RMessage2& aMessage)
       
   291 	{
       
   292 	TBuf<255> dirPath;
       
   293 	aMessage.ReadL(0, dirPath);
       
   294 		
       
   295 	TInt ret = iFs.RmDir(dirPath);
       
   296 		
       
   297 	return ret;
       
   298 	}
       
   299 
       
   300 TInt CMessagingTestUtilitySession::DoGetDirL(const RMessage2& aMessage)
       
   301 	{
       
   302 	TBuf<255> dirPath;
       
   303 	aMessage.ReadL(0, dirPath);
       
   304 	
       
   305 	TPckgBuf<TUint> entryAttMaskBuf;
       
   306 	aMessage.ReadL(1,entryAttMaskBuf);
       
   307 	TUint entryAttMask = entryAttMaskBuf();
       
   308 
       
   309 	TPckgBuf<TUint> entrySortKeyBuf;
       
   310 	aMessage.ReadL(2,entrySortKeyBuf);
       
   311 	TUint entrySortKey = entrySortKeyBuf();
       
   312 	
       
   313 	delete iDir; //clean up first.
       
   314 	
       
   315 	// Hold the CDir* in the Session object.
       
   316 	// Used by calls to DoGetDirCount() and DoGetDirEntry().	
       
   317 	TInt ret = iFs.GetDir(dirPath, entryAttMask, entrySortKey, iDir);
       
   318 	
       
   319 	return ret;
       
   320 	}
       
   321 
       
   322 TInt CMessagingTestUtilitySession::DoGetDirCountL(const RMessage2& aMessage)
       
   323 	{
       
   324 	TUint count =0;
       
   325 	
       
   326 	if(iDir != 0)
       
   327 	{
       
   328 		count = iDir->Count();
       
   329 		
       
   330 		TPckgBuf<TUint> countBuf(count);
       
   331 		aMessage.WriteL(0, countBuf);
       
   332 		return KErrNone;
       
   333 	}
       
   334 	
       
   335 	return KErrNotFound;
       
   336 	}
       
   337 
       
   338 TInt CMessagingTestUtilitySession::DoGetDirEntryL(const RMessage2& aMessage)
       
   339 	{
       
   340 	TPckgBuf<TInt> indexBuf;
       
   341 	aMessage.ReadL(0,indexBuf);
       
   342 	TInt index = indexBuf();
       
   343 	
       
   344 	TEntry entry;
       
   345 	
       
   346 	if(iDir != 0)
       
   347 	{
       
   348 		entry = iDir->operator[](index);
       
   349 
       
   350 		TPckgBuf<TEntry> entryBuf(entry);		
       
   351 		aMessage.WriteL(1, entryBuf);
       
   352 		return KErrNone;
       
   353 	}
       
   354 	
       
   355 	return KErrNotFound;	
       
   356 	}
       
   357 	
       
   358 TBool CMessagingTestUtilitySession::DoFileExistsL(const RMessage2& aMessage)
       
   359 	{
       
   360 	TBuf<255> filePath;
       
   361 	aMessage.ReadL(0, filePath);
       
   362 	
       
   363 	TBool ret = BaflUtils::FileExists(iFs, filePath);
       
   364 		
       
   365 	return ret;
       
   366 	}
       
   367 	
       
   368 //EOF