KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chatserver > TestChatServer


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 chatserver;
16
17 import java.net.*;
18 import java.io.*;
19 import java.util.*;
20 import org.quickserver.net.AppException;
21
22 /**
23  * Makes Client that tests the ChatServer for large load
24  * @author Akshathkumar Shetty
25  */

26 class TestClientThread extends Thread JavaDoc {
27     private Socket socket;
28     private BufferedReader in;
29     private PrintWriter out;
30     public static int counter = 0;
31     private int id = counter++;
32     private static int threadcount = 0;
33     private Random random = new Random();
34
35     public static int threadCount() {
36         return threadcount;
37     }
38
39
40     public TestClientThread(InetAddress addr,int port) {
41         threadcount++;
42         try {
43             socket = new Socket(addr,port);
44         } catch(IOException e) {
45             System.err.println("Socket failed : "+e);
46             return;
47         }
48
49         try {
50             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
51             // Enable auto-flush:
52
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), false);
53             start();
54         } catch(IOException e) {
55             System.out.println("Socket Error : "+e);
56             try {
57                 socket.close();
58             } catch(IOException e2) {
59                 System.err.println("Socket not closed");
60             }
61         }
62     }
63
64     public void run() {
65         //System.out.println("Client " + id+" ");
66
try {
67             Thread.sleep(200);
68
69             String JavaDoc str;
70             //to skip the banner
71
str = in.readLine();
72             if(str==null) throw new IOException("Lost connection!");
73             in.readLine();
74             in.readLine();
75
76             Thread.sleep(500);
77
78             //auth
79
in.readLine();
80             out.println("User-"+id);out.flush();
81             in.readLine();
82             out.println("User-"+id);out.flush();
83             in.readLine();
84             out.println("home");out.flush();
85             in.readLine(); //Auth Ok
86

87
88             //skip help
89
for(int i = 0; i < 7; i++) {
90                 in.readLine();
91             }
92     
93             do {
94                 str = in.readLine();
95                 if(str==null)
96                     throw new IOException("Lost connection!");
97                 if(str.startsWith("{system.help}")==false &&
98                         str.startsWith("{user.info:")==false) {
99                     System.out.println("["+id+"] Got: "+str);
100                 }
101                 if(str.endsWith("quit")){
102                     socket.close();
103                     socket = null;
104                 } else if(str.indexOf("sendBack ")!=-1){
105                     out.print(str.substring(str.indexOf("sendBack ")+9));
106                     out.print("\r\n");
107                     out.flush();
108                 }
109             } while(socket!=null);
110
111         } catch(IOException e) {
112             System.err.println("["+id+"] IO Exception : "+e);
113         } catch(Exception JavaDoc e) {
114             System.err.println("["+id+"] Exception : "+e);
115         } finally {
116             // Always close it:
117
try {
118                 if(socket!=null) socket.close();
119             } catch(IOException e) {
120                 System.err.println("Socket not closed");
121             }
122             threadcount--; // Ending this thread
123
}
124     }
125 }
126
127 public class TestChatServer {
128     public static int MAX_THREADS = 100;
129     private static Random random = new Random();
130     
131     //parsm: port ip maxClient startid
132
public static void main(String JavaDoc[] args)
133             throws IOException, InterruptedException JavaDoc {
134         int port=7412;
135         InetAddress addr = InetAddress.getByName(null);
136
137         if(args.length > 0) {
138             try {
139                 port = Integer.parseInt(args[0]);
140             } catch(NumberFormatException JavaDoc nfe) {}
141         }
142         
143         if(args.length>=2) {
144             addr = InetAddress.getByName(args[1]);
145         }
146
147         if(args.length>=3) {
148             try {
149                 MAX_THREADS = Integer.parseInt(args[2]);
150             } catch(NumberFormatException JavaDoc nfe) {}
151         }
152
153         if(args.length>=4) {
154             try {
155                 TestClientThread.counter = Integer.parseInt(args[3]);
156             } catch(NumberFormatException JavaDoc nfe) {}
157         }
158
159         do {
160             if(TestClientThread.threadCount() < MAX_THREADS)
161                 new TestClientThread(addr,port);
162             else
163                 break;
164             Thread.currentThread().sleep(getSleepTime());
165         } while(true);
166         System.out.println("All "+MAX_THREADS+" clients are ready!");
167     }
168
169     private static int getSleepTime() {
170       return 200;
171     }
172 }
173
Popular Tags