KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echoserver > TestEchoServer


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package echoserver;
16
17 import java.net.*;
18 import java.io.*;
19 import java.util.*;
20 import org.quickserver.net.AppException;
21
22 // Client that tests the EchoServer for all valid requests
23

24 class MyTestClientThread extends Thread JavaDoc {
25     private Socket socket;
26     private BufferedReader in;
27     private PrintWriter out;
28     private static int counter = 0;
29     private int id = counter++;
30     private static int threadcount = 0;
31     private Random random = new Random();
32
33     public static int clientJunkCount = 100;
34     public static int clientJunkSleep = 60*3; //sec
35

36     String JavaDoc send[]={"UserPASS","UserPASS","Hello","World","Hi","Last"};
37     String JavaDoc expectedFL[]={"Bye ;-)"};
38     String JavaDoc expected[]={"User Name :","Password :","Auth OK",
39       "Echo : Hello","Echo : World","Echo : Hi","Echo : Last"};
40
41     private String JavaDoc getJunk() {
42         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
43         int l = random.nextInt(1000);
44         char c = ' ';
45         for(int i=0;i<l;i++) {
46             c = (char) (33+random.nextInt(92));
47             sb.append(c);
48         }
49         return sb.toString();
50     }
51
52     private boolean isDead = false;
53
54     public boolean getIsDead() {
55         return isDead;
56     }
57
58     public static int threadCount() {
59         return threadcount;
60     }
61
62     private int getSleepTime() {
63         return (random.nextInt(20)+5)*300-(random.nextInt(10)+6)*100;
64     }
65
66     public MyTestClientThread(InetAddress addr,int port) {
67         threadcount++;
68         try {
69             socket = new Socket(addr,port);
70         } catch(IOException e) {
71             System.err.println("Socket failed : "+e);
72             return;
73         }
74
75         try {
76             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
77             // Enable auto-flush:
78
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
79             start();
80         } catch(IOException e) {
81             System.out.println("Socket Error [id#"+id+"]: "+e);
82             try {
83                 socket.close();
84             } catch(IOException e2) {
85                 System.err.println("Socket not closed [id#"+id+"]");
86             }
87         }
88     }
89
90     public void astertEqual(String JavaDoc got,String JavaDoc with) {
91         //System.out.println("Got : "+got+",with : "+with);
92
if(got==null || got.equals(with)==false)
93             throw new RuntimeException JavaDoc("Test Exception [id#"+id+"] : Expected : ->"+with+"<- Got->"+got+"<-");
94     }
95
96     public void run() {
97         System.out.println("Client " + id+" ");
98         try {
99             String JavaDoc str;
100             //to skip the banner
101
in.readLine();
102             Thread.yield();
103             in.readLine();
104             if(timeToDie()) {
105                 Thread.yield();
106                 socket.close(); return;
107             }
108             in.readLine();
109             in.readLine();
110             Thread.yield();
111             if(timeToDie()) {
112                 Thread.yield();
113                 socket.close(); return;
114             }
115             in.readLine();
116
117             //username & password
118
for(int i = 0; i < 2; i++) {
119                 str = in.readLine();
120                 astertEqual(str,expected[i]);
121                 if(timeToDie()) {
122                     Thread.yield();
123                     socket.close(); return;
124                 }
125                 this.sleep(500);
126                 out.println(send[i]);
127             }
128             
129             Thread.yield();
130             str = in.readLine();
131             astertEqual(str,expected[2]);
132             Thread.yield();
133             if(timeToDie()) {
134                 socket.close(); return;
135             }
136
137             for(int i = 2; i < send.length; i++) {
138                 if(timeToDie()) {
139                     socket.close(); return;
140                 }
141                 out.println(send[i]);
142                 str = in.readLine();
143                 astertEqual(str,expected[i+1]);
144                 this.sleep(getSleepTime());
145             }
146
147             String JavaDoc junk = null;
148             for(int k=0;k<clientJunkCount;k++) {
149                 junk = getJunk();
150                 out.println(junk);
151                 str = in.readLine();
152                 astertEqual(str,"Echo : "+junk);
153                 this.sleep(1000*clientJunkSleep);
154             }
155
156             Thread.yield();
157             out.println("Quit");
158
159             if(timeToDie()) {
160                 socket.close(); return;
161             }
162             Thread.yield();
163
164             str = in.readLine();
165             astertEqual(str,expectedFL[0]);
166             // wait to close
167
try {
168                 this.sleep(500);
169             } catch(InterruptedException JavaDoc e) {
170                 System.err.println("Interrupted : "+e);
171             }
172         } catch(IOException e) {
173             System.err.println("IO Exception [id#"+id+"]: "+e);
174         } catch(InterruptedException JavaDoc e) {
175             System.err.println("Interrupted [id#"+id+"]: "+e);
176         } finally {
177             // Always close it:
178
try {
179                 if(socket!=null) socket.close();
180             } catch(IOException e) {
181                 System.err.println("Socket not closed [id#"+id+"]");
182             }
183             threadcount--; // Ending this thread
184
isDead = true;
185         }
186     }
187
188     private boolean timeToDie() {
189         int i = random.nextInt(1000);
190         if(i%25==23 || i%95==48) {
191             return true;
192         }
193         return false;
194     }
195 }
196
197
198 public class TestEchoServer {
199     public static int MAX_THREADS = 100;
200     private static Random random = new Random();
201
202     public static void main(String JavaDoc[] args)
203             throws IOException, InterruptedException JavaDoc {
204         int port=4123;
205         InetAddress addr = InetAddress.getByName(null);
206
207         if(args.length > 0) {
208             try {
209                 port = Integer.parseInt(args[0]);
210             } catch(NumberFormatException JavaDoc nfe) {}
211         }
212         
213         if(args.length>=2) {
214             addr = InetAddress.getByName(args[1]);
215         }
216
217         if(args.length>=3) {
218             try {
219                 MAX_THREADS = Integer.parseInt(args[2]);
220             } catch(NumberFormatException JavaDoc nfe) {}
221         }
222
223         if(args.length>=4) {
224             try {
225                 MyTestClientThread.clientJunkCount = Integer.parseInt(args[3]);
226             } catch(NumberFormatException JavaDoc nfe) {}
227         }
228
229         if(args.length>=5) {
230             try {
231                 MyTestClientThread.clientJunkSleep = Integer.parseInt(args[4]);
232             } catch(NumberFormatException JavaDoc nfe) {}
233         }
234         
235
236         new MyTestClientThread(addr,port);
237         while(true) {
238             Thread.currentThread().sleep(getSleepTime());
239             Thread.yield();
240             if(MyTestClientThread.threadCount() < MAX_THREADS)
241                 new MyTestClientThread(addr,port);
242         }
243     }
244
245     private static int getSleepTime() {
246       return random.nextInt(100)+(random.nextInt(20)+1)*200-random.nextInt(50);
247     }
248 }
249
Popular Tags