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 chatserver;
016:
017: import java.net.*;
018: import java.io.*;
019: import java.util.*;
020: import org.quickserver.net.AppException;
021:
022: /**
023: * Makes Client that tests the ChatServer for large load
024: * @author Akshathkumar Shetty
025: */
026: class TestClientThread extends Thread {
027: private Socket socket;
028: private BufferedReader in;
029: private PrintWriter out;
030: public static int counter = 0;
031: private int id = counter++;
032: private static int threadcount = 0;
033: private Random random = new Random();
034:
035: public static int threadCount() {
036: return threadcount;
037: }
038:
039: public TestClientThread(InetAddress addr, int port) {
040: threadcount++;
041: try {
042: socket = new Socket(addr, port);
043: } catch (IOException e) {
044: System.err.println("Socket failed : " + e);
045: return;
046: }
047:
048: try {
049: in = new BufferedReader(new InputStreamReader(socket
050: .getInputStream()));
051: // Enable auto-flush:
052: out = new PrintWriter(new BufferedWriter(
053: new OutputStreamWriter(socket.getOutputStream())),
054: false);
055: start();
056: } catch (IOException e) {
057: System.out.println("Socket Error : " + e);
058: try {
059: socket.close();
060: } catch (IOException e2) {
061: System.err.println("Socket not closed");
062: }
063: }
064: }
065:
066: public void run() {
067: //System.out.println("Client " + id+" ");
068: try {
069: Thread.sleep(200);
070:
071: String str;
072: //to skip the banner
073: str = in.readLine();
074: if (str == null)
075: throw new IOException("Lost connection!");
076: in.readLine();
077: in.readLine();
078:
079: Thread.sleep(500);
080:
081: //auth
082: in.readLine();
083: out.println("User-" + id);
084: out.flush();
085: in.readLine();
086: out.println("User-" + id);
087: out.flush();
088: in.readLine();
089: out.println("home");
090: out.flush();
091: in.readLine(); //Auth Ok
092:
093: //skip help
094: for (int i = 0; i < 7; i++) {
095: in.readLine();
096: }
097:
098: do {
099: str = in.readLine();
100: if (str == null)
101: throw new IOException("Lost connection!");
102: if (str.startsWith("{system.help}") == false
103: && str.startsWith("{user.info:") == false) {
104: System.out.println("[" + id + "] Got: " + str);
105: }
106: if (str.endsWith("quit")) {
107: socket.close();
108: socket = null;
109: } else if (str.indexOf("sendBack ") != -1) {
110: out.print(str
111: .substring(str.indexOf("sendBack ") + 9));
112: out.print("\r\n");
113: out.flush();
114: }
115: } while (socket != null);
116:
117: } catch (IOException e) {
118: System.err.println("[" + id + "] IO Exception : " + e);
119: } catch (Exception e) {
120: System.err.println("[" + id + "] Exception : " + e);
121: } finally {
122: // Always close it:
123: try {
124: if (socket != null)
125: socket.close();
126: } catch (IOException e) {
127: System.err.println("Socket not closed");
128: }
129: threadcount--; // Ending this thread
130: }
131: }
132: }
133:
134: public class TestChatServer {
135: public static int MAX_THREADS = 100;
136: private static Random random = new Random();
137:
138: //parsm: port ip maxClient startid
139: public static void main(String[] args) throws IOException,
140: InterruptedException {
141: int port = 7412;
142: InetAddress addr = InetAddress.getByName(null);
143:
144: if (args.length > 0) {
145: try {
146: port = Integer.parseInt(args[0]);
147: } catch (NumberFormatException nfe) {
148: }
149: }
150:
151: if (args.length >= 2) {
152: addr = InetAddress.getByName(args[1]);
153: }
154:
155: if (args.length >= 3) {
156: try {
157: MAX_THREADS = Integer.parseInt(args[2]);
158: } catch (NumberFormatException nfe) {
159: }
160: }
161:
162: if (args.length >= 4) {
163: try {
164: TestClientThread.counter = Integer.parseInt(args[3]);
165: } catch (NumberFormatException nfe) {
166: }
167: }
168:
169: do {
170: if (TestClientThread.threadCount() < MAX_THREADS)
171: new TestClientThread(addr, port);
172: else
173: break;
174: Thread.currentThread().sleep(getSleepTime());
175: } while (true);
176: System.out
177: .println("All " + MAX_THREADS + " clients are ready!");
178: }
179:
180: private static int getSleepTime() {
181: return 200;
182: }
183: }
|