--- experimental/server/XMLReader/UDPReader.java 2000/11/17 12:16:39 1.2 +++ experimental/server/XMLReader/UDPReader.java 2000/11/21 10:27:24 1.9 @@ -3,46 +3,68 @@ 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); + } - // default UDP listening port. - int port = 4589; - final int packetSizeLimit = 8192; + public void receiveXML(String xml){ + UDPReaderThread t = new UDPReaderThread(); + t.run(xml); + } - // Allow the user to choose their own port number. - if (args.length == 1){ - try { - port = new Integer(args[0]).intValue(); - } - catch (Exception e){ - // If something went wrong, use the default again. - port = 4589; - } + public void run() { + + DatagramSocket socket = null; + try { + socket = new DatagramSocket(port); } + catch (BindException e){ + logger.write(this.toString(), Logger.SYSMSG, "Could not start the UDPReader thread on port "+port+" as this port was already in use."); + return; + } + catch (Exception e){ + logger.write(this.toString(), Logger.SYSMSG, "Could not start the UDPReader thread on port "+port+"."); + return; + } + + logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+port); - DatagramSocket socket = new DatagramSocket(port); - System.out.println("UDPReader ready and listening for UDP packets on port "+port); - + byte[] buf; + boolean running = true; while (running){ try { - byte[] buf = new byte[packetSizeLimit]; + buf = new byte[packetSizeLimit]; // receive request DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); - UDPReaderThread thread = new UDPReaderThread(packet); - thread.run(); + UDPReaderThread t = new UDPReaderThread(); + t.run(packet); } catch (IOException e) { - System.out.println("An exception occured in the UDPReader!"); - e.printStackTrace(); + logger.write(this.toString(), Logger.SYSMSG, "The UDPReader thread has been shut down as an exception occured: "+e); + return; } } socket.close(); } + + + Logger logger; + int port; + + final int packetSizeLimit = 8192; + }