diff --git a/src/helma/util/WebBroadcaster.java b/src/helma/util/WebBroadcaster.java deleted file mode 100644 index 8fe891d4..00000000 --- a/src/helma/util/WebBroadcaster.java +++ /dev/null @@ -1,421 +0,0 @@ -/* - * Helma License Notice - * - * The contents of this file are subject to the Helma License - * Version 2.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://adele.helma.org/download/helma/license.txt - * - * Copyright 1998-2003 Helma Software. All Rights Reserved. - * - * $RCSfile$ - * $Author$ - * $Revision$ - * $Date$ - */ - -package helma.util; - -import java.io.*; -import java.net.*; -import java.util.*; - -/** - * A utility hack to do "html web broadcasts". - */ -public class WebBroadcaster implements Runnable { - static String lastResult = ""; - static String reloadJS = "\r\n
\r\n"; - static String scrollJS = "\r\n\r\n\r\n"; - private Vector connections; - private ServerSocket serverSocket; - private Thread listener; - private boolean paranoid; - private Vector accept; - private Vector deny; - long time; - int last; - - /** - * Creates a Web server at the specified port number. - */ - public WebBroadcaster(int port) throws IOException { - super(); - connections = new Vector(); - accept = new Vector(); - deny = new Vector(); - - // make a new server socket with extra large queue size - this.serverSocket = new ServerSocket(port, 2000); - listener = new Thread(this); - listener.start(); - } - - /** - * - */ - public static void main(String[] args) { - System.out.println("Usage: java helma.util.WebBroadcaster [port]"); - - int p = 8080; - - if (args.length > 0) { - try { - p = Integer.parseInt(args[0]); - } catch (NumberFormatException nfx) { - System.out.println("Error parsing port number: " + args[0]); - } - } - - try { - WebBroadcaster server = new WebBroadcaster(p); - - // webserver.setParanoid (false); - // webserver.acceptClient ("192.168.*.*"); - System.out.println("started web broadcast server on port " + p); - } catch (IOException x) { - System.out.println("Error creating web broadcast server: " + x); - } - } - - /** - * - * - * @param message ... - */ - public void broadcast(String message) { - long start = System.currentTimeMillis(); - int l = connections.size(); - - synchronized (this) { - if (l != last) { - System.out.println("broadcasting to " + l + " clients in " + time + - " millis."); - last = l; - } - } - - for (int i = l - 1; i >= 0; i--) { - try { - Connection c = (Connection) connections.elementAt(i); - - c.send(message); - } catch (Exception ignore) { - } - } - - time = System.currentTimeMillis() - start; - } - - /** - * Switch client filtering on/off. - * @see acceptClient(java.lang.String) - * @see denyClient(java.lang.String) - */ - public void setParanoid(boolean p) { - paranoid = p; - } - - /** - * Add an IP address to the list of accepted clients. The parameter can contain '*' as wildcard - * character, e.g. "192.168.*.*". You must call setParanoid(true) in order for this to have any - * effect. - * - * @see denyClient(java.lang.String) - * @see setParanoid(boolean) - */ - public void acceptClient(String address) throws IllegalArgumentException { - try { - AddressMatcher m = new AddressMatcher(address); - - accept.addElement(m); - } catch (Exception x) { - throw new IllegalArgumentException("\"" + address + - "\" does not represent a valid IP address"); - } - } - - /** - * Add an IP address to the list of denied clients. The parameter can contain '*' as wildcard - * character, e.g. "192.168.*.*". You must call setParanoid(true) in order for this to have any - * effect. - * - * @see acceptClient(java.lang.String) - * @see setParanoid(boolean) - */ - public void denyClient(String address) throws IllegalArgumentException { - try { - AddressMatcher m = new AddressMatcher(address); - - deny.addElement(m); - } catch (Exception x) { - throw new IllegalArgumentException("\"" + address + - "\" does not represent a valid IP address"); - } - } - - private boolean checkSocket(Socket s) { - int l = deny.size(); - byte[] address = s.getInetAddress().getAddress(); - - for (int i = 0; i < l; i++) { - AddressMatcher match = (AddressMatcher) deny.elementAt(i); - - if (match.matches(address)) { - return false; - } - } - - l = accept.size(); - - for (int i = 0; i < l; i++) { - AddressMatcher match = (AddressMatcher) accept.elementAt(i); - - if (match.matches(address)) { - return true; - } - } - - return false; - } - - /** - * Listens for client requests until stopped. - */ - public void run() { - Thread current = Thread.currentThread(); - - try { - while (listener == current) { - Socket socket = serverSocket.accept(); - - try { - // if (!paranoid || checkSocket (socket)) - new Connection(socket); - - // else - // socket.close (); - } catch (Exception x) { - System.out.println("Error in listener: " + x); - } - } - } catch (Exception exception) { - System.err.println("Error accepting Web connections (" + exception + ")."); - } finally { - try { - serverSocket.close(); - serverSocket = null; - } catch (IOException ignore) { - } - } - } - - class Connection implements Runnable { - private Socket socket; - private InputStream input; - private BufferedWriter output; - private Thread responder; - - public Connection(Socket socket) throws IOException { - super(); - this.socket = socket; - socket.setSoTimeout(30000); - input = new BufferedInputStream(socket.getInputStream()); - output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); - responder = new Thread(this); - responder.start(); - } - - /* public void run () { - Thread current = Thread.currentThread (); - if (current == cleaner) { - cleanup (); - } else if (current == responder) { - respond (); - } - } */ - public void run() { - boolean newConnection = false; - - try { - DataInputStream reader = new DataInputStream(input); - boolean keepalive = false; - int cycle = 0; - - // implement keep-alive connections - do { - // if (cycle > 0) System.out.println ("Reusing connection: "+cycle); - String line = reader.readLine(); - - if (line == null) { - throw new IOException("connection reset"); - } - - int contentLength = 0; - StringTokenizer tokens = new StringTokenizer(line); - String method = tokens.nextToken(); - String uri = tokens.nextToken(); - String httpversion = tokens.nextToken(); - - keepalive = "HTTP/1.1".equals(httpversion); - - do { - // System.out.println (line); - line = reader.readLine(); - - if (line != null) { - line = line.toLowerCase(); - - if (line.startsWith("content-length:")) { - contentLength = Integer.parseInt(line.substring(15).trim()); - } - - if (line.startsWith("connection:")) { - keepalive = line.indexOf("keep-alive") > -1; - } - } - } while ((line != null) && !line.equals("")); - - // System.out.println (""); - if ("GET".equals(method)) { - output.write(httpversion + " 200 OK\r\n"); - output.write("Server: helma.WebBroadcast\r\n"); - output.write("Content-Type: text/html\r\n"); - newConnection = uri.startsWith("/NEW"); - - if (!newConnection) { - output.write("Content-Length: 5\r\n"); - - if (keepalive) { - output.write("Connection: keep-alive\r\n"); - } - - output.write("\r\n"); - output.write("done."); - output.flush(); - cycle += 1; - - if (uri.startsWith("/MSG")) { - broadcast(uri + "