|
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 // |
|
15 |
|
16 #include "cspoofserver.h" |
|
17 |
|
18 const TInt KListeningSocketQueueSize = 1; |
|
19 |
|
20 CSpoofServer::CSpoofServer(MImapTestEventHandler& aObserver) |
|
21 : CActive(EPriorityStandard), iObserver(aObserver) |
|
22 { |
|
23 iServerStatus=EIdle; |
|
24 CActiveScheduler::Add(this); |
|
25 } |
|
26 |
|
27 EXPORT_C CSpoofServer* CSpoofServer::NewL(MImapTestEventHandler& aObserver,const TDesC& aScriptFile) |
|
28 { |
|
29 CSpoofServer* self = NewLC(aObserver,aScriptFile); |
|
30 CleanupStack::Pop(self); |
|
31 return self; |
|
32 } |
|
33 |
|
34 EXPORT_C CSpoofServer* CSpoofServer::NewLC(MImapTestEventHandler& aObserver,const TDesC& aScriptFile) |
|
35 { |
|
36 CSpoofServer* self = new(ELeave) CSpoofServer(aObserver); |
|
37 CleanupStack::PushL(self); |
|
38 self->ConstructL(aScriptFile); |
|
39 return self; |
|
40 } |
|
41 |
|
42 CSpoofServer::~CSpoofServer() |
|
43 { |
|
44 Cancel(); |
|
45 iListeningSocket.Close(); |
|
46 iServiceSocket.Close(); |
|
47 iSocketServer.Close(); |
|
48 delete iScriptFileProcessor; |
|
49 } |
|
50 |
|
51 EXPORT_C TInt CSpoofServer::RunError(TInt aError) |
|
52 { |
|
53 iObserver.TestComplete(aError); |
|
54 return KErrNone; |
|
55 } |
|
56 |
|
57 void CSpoofServer::ConstructL(const TDesC& aScriptFile) |
|
58 { |
|
59 //connect to the socket server |
|
60 User::LeaveIfError(iSocketServer.Connect()); |
|
61 //create the script file processor |
|
62 iScriptFileProcessor = CScriptFileProcessor::NewL(aScriptFile); |
|
63 } |
|
64 |
|
65 void CSpoofServer::DoCancel() |
|
66 { |
|
67 iListeningSocket.CancelAll(); |
|
68 } |
|
69 |
|
70 EXPORT_C void CSpoofServer::StartL(const TInt aPortNumber) |
|
71 { |
|
72 iServerStatus = EWriting; |
|
73 //set up the listening socket |
|
74 User::LeaveIfError(iListeningSocket.Open(iSocketServer, KAfInet, KSockStream, KProtocolInetTcp)); |
|
75 //bind the listener |
|
76 TSockAddr address; |
|
77 address.SetPort(aPortNumber); |
|
78 iListeningSocket.Bind(address); |
|
79 //listen for incomming connections |
|
80 iListeningSocket.Listen(KListeningSocketQueueSize); |
|
81 //accept incomming connections and use iServiceSocket to communicate with it. |
|
82 User::LeaveIfError(iServiceSocket.Open(iSocketServer)); |
|
83 iListeningSocket.Accept(iServiceSocket,iStatus); |
|
84 SetActive(); |
|
85 } |
|
86 |
|
87 void CSpoofServer::IssueWrite() |
|
88 { |
|
89 iBuffer.Zero(); |
|
90 iBuffer.Append(iNextLine); |
|
91 _LIT(KEndOfLine,"\r\n"); |
|
92 iBuffer.Append(KEndOfLine); |
|
93 iServiceSocket.Write(iBuffer, iStatus); |
|
94 SetActive(); |
|
95 } |
|
96 |
|
97 void CSpoofServer::IssueRead() |
|
98 { |
|
99 iBuffer.Zero(); |
|
100 iServiceSocket.RecvOneOrMore(iBuffer, 0, iStatus,iReadLength); |
|
101 SetActive(); |
|
102 } |
|
103 |
|
104 void CSpoofServer::RunL() |
|
105 { |
|
106 User::LeaveIfError(iStatus.Int()); |
|
107 |
|
108 CScriptFileProcessor::TDataDirection direction; |
|
109 |
|
110 iBuffer.TrimAll(); |
|
111 if(iServerStatus==EReading) |
|
112 { |
|
113 if(iNextLine!=iBuffer) |
|
114 { |
|
115 User::Leave(KErrCorrupt); |
|
116 } |
|
117 } |
|
118 //read next line of script file and get its direction |
|
119 User::LeaveIfError(iScriptFileProcessor->ReadLine(iNextLine)); |
|
120 direction=iScriptFileProcessor->DataDirection(); |
|
121 |
|
122 //if we are sending then write the line, else read |
|
123 if(direction==CScriptFileProcessor::EFromServer) |
|
124 { |
|
125 iServerStatus=EWriting; |
|
126 IssueWrite(); |
|
127 } |
|
128 else if(direction==CScriptFileProcessor::EFromClient) |
|
129 { |
|
130 iServerStatus=EReading; |
|
131 IssueRead(); |
|
132 } |
|
133 } |
|
134 |
|
135 |
|
136 |
|
137 |