001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package echoserver;
016:
017: import java.net.*;
018: import java.io.*;
019: import java.util.*;
020: import org.quickserver.net.AppException;
021:
022: // Client that tests the EchoServer for all valid requests
023:
024: class MyTestClientThread extends Thread {
025: private Socket socket;
026: private BufferedReader in;
027: private PrintWriter out;
028: private static int counter = 0;
029: private int id = counter++;
030: private static int threadcount = 0;
031: private Random random = new Random();
032:
033: public static int clientJunkCount = 100;
034: public static int clientJunkSleep = 60 * 3; //sec
035:
036: String send[] = { "UserPASS", "UserPASS", "Hello", "World", "Hi",
037: "Last" };
038: String expectedFL[] = { "Bye ;-)" };
039: String expected[] = { "User Name :", "Password :", "Auth OK",
040: "Echo : Hello", "Echo : World", "Echo : Hi", "Echo : Last" };
041:
042: private String getJunk() {
043: StringBuffer sb = new StringBuffer();
044: int l = random.nextInt(1000);
045: char c = ' ';
046: for (int i = 0; i < l; i++) {
047: c = (char) (33 + random.nextInt(92));
048: sb.append(c);
049: }
050: return sb.toString();
051: }
052:
053: private boolean isDead = false;
054:
055: public boolean getIsDead() {
056: return isDead;
057: }
058:
059: public static int threadCount() {
060: return threadcount;
061: }
062:
063: private int getSleepTime() {
064: return (random.nextInt(20) + 5) * 300
065: - (random.nextInt(10) + 6) * 100;
066: }
067:
068: public MyTestClientThread(InetAddress addr, int port) {
069: threadcount++;
070: try {
071: socket = new Socket(addr, port);
072: } catch (IOException e) {
073: System.err.println("Socket failed : " + e);
074: return;
075: }
076:
077: try {
078: in = new BufferedReader(new InputStreamReader(socket
079: .getInputStream()));
080: // Enable auto-flush:
081: out = new PrintWriter(new BufferedWriter(
082: new OutputStreamWriter(socket.getOutputStream())),
083: true);
084: start();
085: } catch (IOException e) {
086: System.out.println("Socket Error [id#" + id + "]: " + e);
087: try {
088: socket.close();
089: } catch (IOException e2) {
090: System.err.println("Socket not closed [id#" + id + "]");
091: }
092: }
093: }
094:
095: public void astertEqual(String got, String with) {
096: //System.out.println("Got : "+got+",with : "+with);
097: if (got == null || got.equals(with) == false)
098: throw new RuntimeException("Test Exception [id#" + id
099: + "] : Expected : ->" + with + "<- Got->" + got
100: + "<-");
101: }
102:
103: public void run() {
104: System.out.println("Client " + id + " ");
105: try {
106: String str;
107: //to skip the banner
108: in.readLine();
109: Thread.yield();
110: in.readLine();
111: if (timeToDie()) {
112: Thread.yield();
113: socket.close();
114: return;
115: }
116: in.readLine();
117: in.readLine();
118: Thread.yield();
119: if (timeToDie()) {
120: Thread.yield();
121: socket.close();
122: return;
123: }
124: in.readLine();
125:
126: //username & password
127: for (int i = 0; i < 2; i++) {
128: str = in.readLine();
129: astertEqual(str, expected[i]);
130: if (timeToDie()) {
131: Thread.yield();
132: socket.close();
133: return;
134: }
135: this .sleep(500);
136: out.println(send[i]);
137: }
138:
139: Thread.yield();
140: str = in.readLine();
141: astertEqual(str, expected[2]);
142: Thread.yield();
143: if (timeToDie()) {
144: socket.close();
145: return;
146: }
147:
148: for (int i = 2; i < send.length; i++) {
149: if (timeToDie()) {
150: socket.close();
151: return;
152: }
153: out.println(send[i]);
154: str = in.readLine();
155: astertEqual(str, expected[i + 1]);
156: this .sleep(getSleepTime());
157: }
158:
159: String junk = null;
160: for (int k = 0; k < clientJunkCount; k++) {
161: junk = getJunk();
162: out.println(junk);
163: str = in.readLine();
164: astertEqual(str, "Echo : " + junk);
165: this .sleep(1000 * clientJunkSleep);
166: }
167:
168: Thread.yield();
169: out.println("Quit");
170:
171: if (timeToDie()) {
172: socket.close();
173: return;
174: }
175: Thread.yield();
176:
177: str = in.readLine();
178: astertEqual(str, expectedFL[0]);
179: // wait to close
180: try {
181: this .sleep(500);
182: } catch (InterruptedException e) {
183: System.err.println("Interrupted : " + e);
184: }
185: } catch (IOException e) {
186: System.err.println("IO Exception [id#" + id + "]: " + e);
187: } catch (InterruptedException e) {
188: System.err.println("Interrupted [id#" + id + "]: " + e);
189: } finally {
190: // Always close it:
191: try {
192: if (socket != null)
193: socket.close();
194: } catch (IOException e) {
195: System.err.println("Socket not closed [id#" + id + "]");
196: }
197: threadcount--; // Ending this thread
198: isDead = true;
199: }
200: }
201:
202: private boolean timeToDie() {
203: int i = random.nextInt(1000);
204: if (i % 25 == 23 || i % 95 == 48) {
205: return true;
206: }
207: return false;
208: }
209: }
210:
211: public class TestEchoServer {
212: public static int MAX_THREADS = 100;
213: private static Random random = new Random();
214:
215: public static void main(String[] args) throws IOException,
216: InterruptedException {
217: int port = 4123;
218: InetAddress addr = InetAddress.getByName(null);
219:
220: if (args.length > 0) {
221: try {
222: port = Integer.parseInt(args[0]);
223: } catch (NumberFormatException nfe) {
224: }
225: }
226:
227: if (args.length >= 2) {
228: addr = InetAddress.getByName(args[1]);
229: }
230:
231: if (args.length >= 3) {
232: try {
233: MAX_THREADS = Integer.parseInt(args[2]);
234: } catch (NumberFormatException nfe) {
235: }
236: }
237:
238: if (args.length >= 4) {
239: try {
240: MyTestClientThread.clientJunkCount = Integer
241: .parseInt(args[3]);
242: } catch (NumberFormatException nfe) {
243: }
244: }
245:
246: if (args.length >= 5) {
247: try {
248: MyTestClientThread.clientJunkSleep = Integer
249: .parseInt(args[4]);
250: } catch (NumberFormatException nfe) {
251: }
252: }
253:
254: new MyTestClientThread(addr, port);
255: while (true) {
256: Thread.currentThread().sleep(getSleepTime());
257: Thread.yield();
258: if (MyTestClientThread.threadCount() < MAX_THREADS)
259: new MyTestClientThread(addr, port);
260: }
261: }
262:
263: private static int getSleepTime() {
264: return random.nextInt(100) + (random.nextInt(20) + 1) * 200
265: - random.nextInt(50);
266: }
267: }
|