001: // MuxEcho.java
002: // $Id: MuxEcho.java,v 1.6 2000/08/16 21:38:02 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.mux.tests;
007:
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.io.OutputStream;
011: import java.io.PrintStream;
012:
013: import java.net.ServerSocket;
014: import java.net.Socket;
015:
016: import org.w3c.www.mux.MuxSession;
017: import org.w3c.www.mux.MuxStream;
018: import org.w3c.www.mux.MuxStreamHandler;
019:
020: class EchoServerStreamHandler implements MuxStreamHandler {
021:
022: public boolean acceptSession(MuxStream stream, int sessid,
023: int protid) {
024: System.out.println("EchoServerStreamHandler: accept " + sessid
025: + " for protocol " + protid);
026: return true;
027: }
028:
029: public void notifySession(MuxSession session) {
030: try {
031: new EchoServerHandler(session);
032: } catch (Exception ex) {
033: System.out.println("EchoServerStreamHandler: error !");
034: ex.printStackTrace();
035: }
036: }
037:
038: }
039:
040: // EchoServerHandler is the one handling the "echo" protocol
041: // One instance runs per MuxSession on a single MuxStream.
042:
043: class EchoServerHandler extends Thread {
044: MuxSession ses = null;
045: InputStream in = null;
046: OutputStream out = null;
047:
048: public void run() {
049: byte buffer[] = new byte[1024];
050: int got = -1;
051: System.out.println(this + ": now running !");
052: try {
053: while ((got = in.read(buffer, 0, buffer.length)) >= 0) {
054: System.out.println("< " + this + " ["
055: + new String(buffer, 0, 0, got) + "]");
056: out.write(buffer, 0, got);
057: out.flush();
058: }
059: } catch (Exception ex) {
060: System.out.println(this + ": fatal error, aborting.");
061: ex.printStackTrace();
062: }
063: }
064:
065: public EchoServerHandler(MuxSession ses) throws IOException {
066: this .ses = ses;
067: this .in = ses.getInputStream();
068: this .out = ses.getOutputStream();
069: setName("Echo@" + ses.getIdentifier());
070: start();
071: }
072:
073: }
074:
075: // The EchoServer is the one accepting new incomming connections:
076:
077: class EchoServer extends Thread {
078: ServerSocket socket = null;
079: EchoServerStreamHandler handler = null;
080: int port = -1;
081:
082: public void run() {
083: try {
084: while (true) {
085: System.out.println("EchoServer@localhost:" + port
086: + ": accept.");
087: Socket client = socket.accept();
088: new MuxStream(true, handler, client);
089: }
090: } catch (Exception ex) {
091: System.out.println("EchoServer: fatal error !");
092: ex.printStackTrace();
093: }
094: }
095:
096: EchoServer(int port) throws IOException {
097: this .handler = new EchoServerStreamHandler();
098: this .socket = new ServerSocket(port);
099: this .port = port;
100: this .start();
101: }
102: }
103:
104: class EchoClient extends Thread {
105: static Socket socket = null;
106: static MuxStream stream = null;
107: String msg = null;
108: InputStream in = null;
109: OutputStream out = null;
110: MuxSession ses = null;
111: int id = -1;
112: int repeat = -1;
113:
114: public void run() {
115: byte buffer[] = new byte[1024];
116: int msgid = 0;
117: try {
118: while ((repeat < 0) || (repeat != 0)) {
119: String m = "[" + id + "]" + (msgid++) + "/" + msg;
120: m.getBytes(0, m.length(), buffer, 0);
121: out.write(buffer, 0, m.length());
122: out.flush();
123: int got = in.read(buffer, 0, buffer.length);
124: System.out.println("> " + this + " ["
125: + new String(buffer, 0, 0, got) + "]");
126: if (repeat > 0)
127: repeat--;
128: }
129: out.close();
130: } catch (Exception ex) {
131: System.out.println(this + ": failed.");
132: ex.printStackTrace();
133: }
134: }
135:
136: public static void makeClients(String host, int port, int count,
137: int repeat, String msg) throws IOException {
138: // Init if needed:
139: if (socket == null) {
140: socket = new Socket(host, port);
141: stream = new MuxStream(false, null, socket);
142: }
143: // Start as many echo sessions as needed:
144: while (--count >= 0)
145: new EchoClient(stream, count, repeat, msg);
146: }
147:
148: EchoClient(MuxStream stream, int id, int repeat, String msg)
149: throws IOException {
150: this .ses = stream.connect(7);
151: this .in = ses.getInputStream();
152: this .out = ses.getOutputStream();
153: this .msg = msg;
154: this .id = id;
155: this .repeat = repeat;
156: setName("client/" + id);
157: start();
158: }
159: }
160:
161: public class MuxEcho {
162:
163: public static void usage() {
164: System.out.println("-s (server) -p port");
165: System.out
166: .println("-c (client) -m msg -h server -p port -n count -r repeat");
167: }
168:
169: public static void main(String args[]) {
170: boolean server = false;
171: String host = null;
172: String msg = null;
173: int port = 5001;
174: int count = 2;
175: int repeat = 0;
176:
177: for (int i = 0; i < args.length; i++) {
178: if (args[i].equals("-s")) {
179: server = true;
180: } else if (args[i].equals("-c")) {
181: server = false;
182: } else if (args[i].equals("-m")) {
183: msg = args[++i];
184: } else if (args[i].equals("-h")) {
185: host = args[++i];
186: } else if (args[i].equals("-p")) {
187: try {
188: port = Integer.parseInt(args[++i]);
189: } catch (Exception ex) {
190: usage();
191: }
192: } else if (args[i].equals("-n")) {
193: try {
194: count = Integer.parseInt(args[++i]);
195: } catch (Exception ex) {
196: usage();
197: }
198: } else if (args[i].equals("-r")) {
199: try {
200: repeat = Integer.parseInt(args[++i]);
201: } catch (Exception ex) {
202: usage();
203: }
204: } else {
205: usage();
206: }
207: }
208: try {
209: if (server) {
210: new EchoServer(port);
211: } else {
212: EchoClient.makeClients(host, port, count, repeat, msg);
213: }
214: } catch (Exception ex) {
215: ex.printStackTrace();
216: }
217:
218: }
219:
220: }
|