001: // MuxTest.java
002: // $Id: MuxTest.java,v 1.6 2000/08/16 21:38:02 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
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: import org.w3c.www.mux.SampleMuxHandler;
020:
021: class DiscardServer extends Thread {
022: ServerSocket socket = null;
023: MuxStreamHandler handler = null;
024: int port = -1;
025:
026: public void run() {
027: try {
028: while (true) {
029: System.out.println("DiscardServer@localhost:" + port
030: + " accept.");
031: Socket client = socket.accept();
032: new MuxStream(true, handler, client);
033: }
034: } catch (Exception ex) {
035: System.out.println("DiscardServer: fatal error !");
036: ex.printStackTrace();
037: }
038: }
039:
040: public DiscardServer(int port) throws IOException {
041: this .handler = SampleMuxHandler.getStreamHandler();
042: this .socket = new ServerSocket(port);
043: this .port = port;
044: setName("DiscardServer");
045: this .start();
046: }
047:
048: }
049:
050: class MuxTestClient extends Thread {
051: static Socket socket = null;
052: static MuxStream stream = null;
053:
054: int count = 0;
055: int bufsize = 0;
056: byte buffer[] = null;
057: int total = 0;
058: InputStream in = null;
059: OutputStream out = null;
060:
061: public static void makeClient(String host, int port, int bufsize,
062: int count) throws IOException {
063: if (socket == null) {
064: socket = new Socket(host, port);
065: stream = new MuxStream(false, null, socket);
066: }
067: new MuxTestClient(stream.connect(9), bufsize, count);
068: }
069:
070: public void run() {
071: System.out.println(this + " bs=" + bufsize + ", cn=" + count);
072: try {
073: long start = System.currentTimeMillis();
074: while (--count >= 0)
075: out.write(buffer, 0, buffer.length);
076: out.close();
077: out.flush();
078: long end = System.currentTimeMillis();
079: System.out.println(this + ": emited " + total + " bytes "
080: + " in " + (end - start) + " ms.");
081: } catch (Exception ex) {
082: System.out.println(this + ": failed");
083: ex.printStackTrace();
084: }
085: }
086:
087: MuxTestClient(MuxSession session, int bufsize, int count)
088: throws IOException {
089: this .in = session.getInputStream();
090: this .out = session.getOutputStream();
091: this .bufsize = bufsize;
092: this .count = count;
093: this .buffer = new byte[bufsize];
094: this .total = bufsize * count;
095: setName("client-" + session.getIdentifier());
096: start();
097: }
098:
099: }
100:
101: public class MuxTest {
102:
103: public static void usage() {
104: System.out.println("-h host -p port -t bufsize count>*");
105: System.out.println("-s (server) -p port");
106: }
107:
108: public static void main(String args[]) {
109: boolean server = false;
110: String host = null;
111: int port = -1;
112:
113: for (int i = 0; i < args.length; i++) {
114: if (args[i].equals("-s")) {
115: server = true;
116: } else if (args[i].equals("-h")) {
117: host = args[++i];
118: } else if (args[i].equals("-p")) {
119: try {
120: port = Integer.parseInt(args[++i]);
121: } catch (Exception ex) {
122: usage();
123: }
124: } else if (args[i].equals("-t")) {
125: if ((port < 0) || (host == null))
126: usage();
127: try {
128: int bufsize = Integer.parseInt(args[++i]);
129: int count = Integer.parseInt(args[++i]);
130: MuxTestClient
131: .makeClient(host, port, bufsize, count);
132: } catch (Exception ex) {
133: ex.printStackTrace();
134: }
135: }
136: }
137: if (server) {
138: try {
139: new DiscardServer(port);
140: } catch (Exception ex) {
141: ex.printStackTrace();
142: }
143: }
144: }
145:
146: }
|