email/pop3andsmtpmtm/imapservermtm/test/src/ImapSpoofServer.cpp
changeset 25 84d9eb65b26f
equal deleted inserted replaced
23:238255e8b033 25:84d9eb65b26f
       
     1 // Copyright (c) 2005-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 // imapserver.cpp
       
    15 // 
       
    16 //
       
    17  
       
    18 #include "imapspoofserver.h"
       
    19 
       
    20 //
       
    21 // CImapSpoofServer
       
    22 //
       
    23 
       
    24 const TInt KImapPortNumber = 143;
       
    25 
       
    26 CImapSpoofServer::CImapSpoofServer() : CActive(EPriorityStandard)
       
    27 	{
       
    28 	iServerStatus=EIdle;
       
    29 	}
       
    30 
       
    31 EXPORT_C CImapSpoofServer* CImapSpoofServer::NewL(MImapTestEventHandler* aOwner,const TDesC& aScriptFile)
       
    32 	{
       
    33 	CImapSpoofServer* self = NewLC(aOwner,aScriptFile);
       
    34 	CleanupStack::Pop(self);
       
    35 	return self;
       
    36 	}
       
    37 
       
    38 EXPORT_C CImapSpoofServer* CImapSpoofServer::NewLC(MImapTestEventHandler* aOwner,const TDesC& aScriptFile)
       
    39 	{
       
    40 	CImapSpoofServer* self = new(ELeave) CImapSpoofServer;
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL(aOwner,aScriptFile);
       
    43 	return self;
       
    44 	}
       
    45 	
       
    46 CImapSpoofServer::~CImapSpoofServer()
       
    47 	{
       
    48 	Cancel();	
       
    49 	iListeningSocket.Close();
       
    50 	iServiceSocket.Close();
       
    51 	iSocketServer.Close();
       
    52 	delete iScriptFileProcessor;
       
    53 	}
       
    54 
       
    55 EXPORT_C TInt CImapSpoofServer::RunError(TInt aError)
       
    56 	{
       
    57 	iOwner->HandleEvent(aError);
       
    58 	return KErrNone;
       
    59 	}
       
    60 
       
    61 void CImapSpoofServer::ConstructL(MImapTestEventHandler* aOwner,const TDesC& aScriptFile)
       
    62 	{
       
    63 	CActiveScheduler::Add(this); 
       
    64 	iOwner=aOwner;
       
    65 	//connect to the socket server
       
    66 	User::LeaveIfError(iSocketServer.Connect());
       
    67 	//create the script file processor
       
    68 	iScriptFileProcessor = CScriptFileProcessor::NewL(aScriptFile);	
       
    69 	}
       
    70 
       
    71 void CImapSpoofServer::DoCancel()
       
    72 	{
       
    73 	iListeningSocket.CancelAccept();
       
    74 	}
       
    75 	
       
    76 void CImapSpoofServer::StartL()
       
    77 	{
       
    78 	iServerStatus = EWriting;
       
    79 	//set up the listening socket
       
    80 	User::LeaveIfError(iListeningSocket.Open(iSocketServer, KAfInet, KSockStream, KProtocolInetTcp));
       
    81 	//bind the listener to any address from port 143
       
    82 	TSockAddr address;
       
    83 	address.SetPort(KImapPortNumber);
       
    84 	iListeningSocket.Bind(address);
       
    85 	//listen for incomming connections
       
    86 	iListeningSocket.Listen(KListeningSocketQueueSize);
       
    87 	//accept incomming connections and use iServiceSocket to communicate with it.
       
    88 	User::LeaveIfError(iServiceSocket.Open(iSocketServer));
       
    89 	iListeningSocket.Accept(iServiceSocket,iStatus);
       
    90 	SetActive();	
       
    91 	}
       
    92 
       
    93 	
       
    94 void CImapSpoofServer::IssueWrite()
       
    95 	{			
       
    96     iBuffer.Zero();
       
    97   	iBuffer.Append(iNextLine);
       
    98 	iBuffer.Append(_L("\r\n"));
       
    99 	iServiceSocket.Write(iBuffer, iStatus);
       
   100 	SetActive();
       
   101 	}
       
   102 
       
   103 void CImapSpoofServer::IssueRead()
       
   104 	{
       
   105     iBuffer.Zero();
       
   106   	iServiceSocket.RecvOneOrMore(iBuffer, 0, iStatus,iReadLength);
       
   107   	SetActive();
       
   108 	}
       
   109  
       
   110 void CImapSpoofServer::RunL()
       
   111 	{	
       
   112 	
       
   113 	CScriptFileProcessor::TDataDirection direction;
       
   114 
       
   115 	if (iStatus == KErrNone)
       
   116 		{		
       
   117 		iBuffer.TrimAll();
       
   118 		if(iServerStatus==EReading)
       
   119 			{
       
   120 			if(iNextLine!=iBuffer)
       
   121 				{
       
   122 				iOwner->HandleEvent(KErrCorrupt);		
       
   123 				}		
       
   124 			}
       
   125 		//read next line of script file
       
   126 		//if weve reached the end of the file then inform the owning object
       
   127 		TInt err = iScriptFileProcessor->ReadLine(iNextLine);
       
   128 		
       
   129 		if(err==KErrEof)
       
   130 			{
       
   131 			iOwner->HandleEvent(KErrEof);	
       
   132 			}
       
   133 		else if(err==KErrNone)
       
   134 			{
       
   135 			direction=iScriptFileProcessor->DataDirection();
       
   136 		
       
   137     		//if we are sending then write the line, else read 
       
   138     		if(direction==CScriptFileProcessor::EFromServer)
       
   139     			{
       
   140     			iServerStatus=EWriting;	
       
   141     			IssueWrite();
       
   142     			}
       
   143     		else if(direction==CScriptFileProcessor::EFromClient)
       
   144     			{
       
   145     			iServerStatus=EReading;
       
   146     			IssueRead();
       
   147     			}    	
       
   148 			}
       
   149 		else
       
   150 			{
       
   151 			iOwner->HandleEvent(err);	
       
   152 			}
       
   153 		}	
       
   154 	else
       
   155 		{
       
   156 		iOwner->HandleEvent(iStatus.Int());		
       
   157 		}
       
   158 		
       
   159 	}
       
   160