--- experimental/server/XMLReader/UDPReader.java 2000/11/17 11:23:19 1.1 +++ experimental/server/XMLReader/UDPReader.java 2000/11/21 09:15:06 1.6 @@ -3,27 +3,64 @@ import java.net.*; import java.util.*; // This class contains the main method to be run by -// the filter children. It harvests UDP traffic +// the filter children. It harvests UDP traffic. // // -public class UDPReader { +public class UDPReader extends Thread{ - public static void main(String[] args) throws IOException { + // It is normal to use this constructor in preference + // to any other in this class. + public UDPReader(int port, Logger logger){ + this.logger = logger; + this.port = port; + } + + public UDPReader(Logger logger){ + this(4589, logger); + } - DatagramSocket socket = new DatagramSocket(4589); + public void run() { + DatagramSocket socket = null; try { - byte[] buf = new byte[1024]; - // receive request - DatagramPacket packet = new DatagramPacket(buf, buf.length); - socket.receive(packet); - String dataIn = new String(packet.getData()); - System.out.println("Received: " + dataIn); + socket = new DatagramSocket(port); } - catch (IOException e) { - System.out.println("An exception occured!"); - e.printStackTrace(); + catch (BindException e){ + System.out.println("Some other process is already listening on port "+port+"."); + System.out.println("Please specify another port number on the command line."); + return; } + catch (Exception e){ + System.out.println("An exception occured while creating the DatagramSocket."); + return; + } + + System.out.println("UDPReader ready and listening for UDP packets on port "+port); + + byte[] buf; + UDPReaderThread t = new UDPReaderThread(); + + boolean running = true; + while (running){ + try { + buf = new byte[packetSizeLimit]; + // receive request + DatagramPacket packet = new DatagramPacket(buf, buf.length); + socket.receive(packet); + t.run(packet); + } + catch (IOException e) { + System.out.println("An exception occured in the UDPReader!"); + e.printStackTrace(); + } + } socket.close(); } + + + Logger logger; + int port; + + final int packetSizeLimit = 8192; + }