01: // Discard.java
02: // $Id: Discard.java,v 1.5 2000/08/16 21:38:02 ylafon Exp $
03: // (c) COPYRIGHT MIT and INRIA, 1996.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.www.mux.handlers;
07:
08: import java.io.IOException;
09: import java.io.InputStream;
10: import java.io.OutputStream;
11: import java.io.PrintStream;
12:
13: import org.w3c.www.mux.MuxProtocolHandler;
14: import org.w3c.www.mux.MuxSession;
15:
16: /**
17: * The <em>discard</em> protocol handler.
18: */
19:
20: public class Discard extends Thread implements MuxProtocolHandler {
21: /**
22: * Debug flag.
23: */
24: private static final boolean debug = true;
25: /**
26: * Trace flag (will emit a line per discarded packet)
27: */
28: private static final boolean trace = false;
29: InputStream in = null;
30: OutputStream out = null;
31: MuxSession session = null;
32:
33: /**
34: * Run the <em>discard</em> protocol, can't be easier !
35: */
36:
37: public void run() {
38: byte buffer[] = new byte[1024];
39: int got = -1;
40: try {
41: long tstart = System.currentTimeMillis();
42: int recv = 0;
43: int nread = 0;
44: while ((got = in.read(buffer, 0, buffer.length)) > 0) {
45: recv += got;
46: nread++;
47: if (trace)
48: System.out.println("discard: " + nread + " " + got
49: + " bytes.");
50: }
51: long tend = System.currentTimeMillis();
52: if (debug) {
53: System.out.println("discard: recv=" + recv + ", reads="
54: + nread + ", bytes/read=" + (recv / nread)
55: + ", bytes/sec="
56: + (recv / ((tend - tstart) / 1000)) + ".");
57: }
58: session.shutdown();
59: } catch (Exception ex) {
60: ex.printStackTrace();
61: }
62: }
63:
64: /**
65: * Initialize the <em>echo</em> protocol on that session.
66: * @param session The session to use to speak that protocol.
67: */
68:
69: public void initialize(MuxSession session) throws IOException {
70: this .in = session.getInputStream();
71: this .out = session.getOutputStream();
72: this .session = session;
73: start();
74: }
75:
76: /**
77: * Default public constructor.
78: */
79:
80: public Discard() {
81: super ();
82: setName("discard");
83: }
84:
85: }
|