/* * Vorlesung "Vernetzte Systeme" WS 1999/2000, Prof. Dr. F. Mattern * ---------------------------------------------------------------- * ClientSendThread.java: * Reads lines from STDIN and dispatches up to individual * ClientSendThreads to send out text blocks of size . * * Required Classes: * - SyncPrintWriter: * Allows multiple threads to share a single output stream. */ import java.io.*; import java.net.*; public class ClientSendThread extends Thread { private String outputLine; private SyncPrintWriter out; private int timeout; private boolean debug = false; private boolean terminated = false; /* Constructor */ public ClientSendThread (SyncPrintWriter out, int timeout, boolean debug) { super ("ClientSendThread"); this.out = out; this.timeout = timeout; this.debug = debug; } /* what to send */ public void setoutput(String outputLine) { this.outputLine = outputLine; } /* set termination flag (this is checked in the run()-method) */ public void terminate() { terminated = true; interrupt(); /* wake this thread up */ } /* this method gets executed if someone calls ClientSendThread.start() */ public void run() { // make sure we only do this until someone says we're done while (!terminated) { try { /* send */ if (debug) System.err.println ("> "+ outputLine.length() + " bytes"); out.println(outputLine); /* go to sleep */ Thread.sleep(timeout); } catch (InterruptedException e) { /* some ack arrived? let's wake up and check */ } } } }