// ********************************************************************** // EXECUTIVE SUMMARY // Module Name: NIU.java // Description: Definition of the NIU Network Interface Unit applet // Author: Kent A. Watsen, http://www.mbay.net/~watsen // Author: Don McGregor, http://www.npsnet.org/~mcgredo // // revised 25 July 97 // // ********************************************************************** package mil.navy.nps.eaiDemoBoids; // ********************************************************************** // IMPORT // ********************************************************************** // Standard classes import java.awt.*; import java.util.*; import java.applet.*; import java.net.*; import java.io.*; // NPS library classes import mil.navy.nps.dis.*; // ********************************************************************** // CLASS // ********************************************************************** public class NIU extends Applet implements Runnable { public final static boolean DEBUG = true; private static final int MAX_DATAGRAM_SIZE = 1500; private int sendSocketNumber = -1; private DatagramSocket sendSocket = null; private InetAddress sendDestHost = null; private int sendDestSocket = -1; private boolean amSending = false; private int listenSocketNumber = -1; private DatagramSocket listenSocket = null; private Vector listenBuffer = null; private boolean amListening = false; int pduCount = 0; public String getName() { return "NIU"; } public void init() { // Retrieve passed values String tmpParam = null; // test and configure listener tmpParam = getParameter("listenSocketNumber"); if (tmpParam != null && tmpParam.length() != 0) { amListening = true; listenSocketNumber = (Integer.valueOf(tmpParam)).intValue(); System.out.println("NIU listening to socket number " + listenSocketNumber); System.out.println("NIU configured for listening"); } // test and configure sender tmpParam = getParameter("sendSocketNumber"); if (tmpParam != null && tmpParam.length() != 0) { sendSocketNumber = (Integer.valueOf(tmpParam)).intValue(); System.out.println("NIU sending through socket number " + sendSocketNumber); } tmpParam = getParameter("sendDestHost"); if (tmpParam != null && tmpParam.length() != 0) { try {sendDestHost = InetAddress.getByName(tmpParam); } catch (UnknownHostException e) { System.out.println("NIU: error finding sendDestHost address"); } System.out.println("NIU sending to host: " + tmpParam); } tmpParam = getParameter("sendDestSocket"); if (tmpParam != null && tmpParam.length() != 0) { sendDestSocket = (Integer.valueOf(tmpParam)).intValue(); System.out.println("NIU sending to socket # " + sendDestSocket + " on remote machine"); } if (sendSocketNumber != -1 && sendDestHost != null && sendDestSocket != -1) { amSending = true; if(DEBUG) System.out.println("NIU configured for sending"); } // initialize for sender if (amSending == true) { try { sendSocket = new DatagramSocket(sendSocketNumber); } catch (SocketException e) {System.out.println("NIU new sendSocket Error");} System.out.println("NIU sending socket opened"); // sendBuffer = new Vector(); } // initialize listener if (amListening == true) { try { listenSocket = new DatagramSocket(listenSocketNumber); } catch (SocketException e) {System.out.println("new listenSocket Error");} System.out.println("NIU listening socket opened"); listenBuffer = new Vector(); Thread runner = new Thread(this); runner.setPriority(Thread.MIN_PRIORITY); runner.start(); } System.out.println("NIU init method finished"); } public void run() { DatagramPacket datagramPacket = null; byte dataBuffer[] = null; while(true) { // create a new datagram object, then read it from the wire. dataBuffer = new byte[MAX_DATAGRAM_SIZE]; datagramPacket = new DatagramPacket(dataBuffer, dataBuffer.length); try { listenSocket.receive(datagramPacket); } catch (IOException e) { System.out.println("NIU listenSocket.receive Error"); } pduCount++; if ((pduCount % 100 == 0) && (DEBUG == true)) {System.out.println(pduCount + " PDUs received by NIU");} // we have to be careful about access to the datagram holding pen synchronized(listenBuffer) { listenBuffer.addElement(datagramPacket); } } } // Returns a Vector of all the PDUs received since we last asked. // Creates a new datagram buffer for the read thread, then takes the // old datagram buffer for its own nefarious purposes. public Vector getPDUs() { Vector locListenBuffer = null; // just a ptr to keep the old buffer from being GC'd. Vector pduBuffer = null; // vector to hold inflated PDU objects Vector newlistenBuffer = null; DatagramPacket datagram = null; ProtocolDataUnit pdu = null; if (!amListening) { System.out.println("NIU not initialized to listen to a socket"); return null; } // Try to make the changeover fast, with minimal locked time locListenBuffer = listenBuffer; newlistenBuffer = new Vector(); synchronized(listenBuffer) { listenBuffer = newlistenBuffer; } // now translate all the datagrams into PDUs. if((DEBUG) && (locListenBuffer.size() > 0)) System.out.println("NIU translating " + locListenBuffer.size() + " PDUs"); if(locListenBuffer.size() > 0) { setBackground(Color.green); } pduBuffer = new Vector(); for (int i=0; i 0) { setBackground(Color.lightGray); } locListenBuffer.removeAllElements(); // garbage collector will destroy vector return pduBuffer; } public void sendPDU(ProtocolDataUnit pdu) { ByteArrayOutputStream oStream = null; DataOutputStream dataOutputStream = null; DatagramPacket dgramPacket = null; DatagramSocket debuggingSocket = null; if (!amSending) { System.out.println("NIU not initialized to send to a socket"); return; } oStream = new ByteArrayOutputStream(); dataOutputStream = new DataOutputStream(oStream); if(DEBUG) System.out.print("NIU preparing to serialize a PDU.. "); pdu.serialize(dataOutputStream); if(DEBUG) System.out.println("serialized PDU"); if(DEBUG) { System.out.println("NIU byte array size: " + oStream.toByteArray().length + "stream sez size is " + oStream.size()); } dgramPacket = new DatagramPacket(oStream.toByteArray(), oStream.size(), sendDestHost, sendDestSocket); if (DEBUG) { System.out.println("NIU created PDU in datagram packet"); } setBackground(Color.red); if(DEBUG) System.out.println("NIU set background color to red"); try { /* try { debuggingSocket = new DatagramSocket(8888); } catch (SocketException e) {System.out.println("NIU new debugging socket Error");} System.out.println("debuggingSocket socket opened"); */ sendSocket.send(dgramPacket); } catch (IOException e) { System.out.println("NIU error sending packet"); } if(DEBUG) System.out.println("NIU made it past socket send, attempting to set bg color"); setBackground(Color.lightGray); if (DEBUG) { System.out.println("NIU datagram sent to socket"); } } }