package mil.navy.nps.testing; /** BehaviorStreamBufferUDPExample This is an example of using BehaviorStreamBufferUDP. (Duh.) Both reading and writing are shown. BehaviorStreamBufferUDP is a class used to do a variety of operations. It: -Reads PDUs from the wire -Promotes the PDU datagram packets to full-fledged Java objects -Returns a Vector of PDU objects when asked The read operation operates in its own thread. This means the read operation is decoupled from any other operations that may be going on. The BehaviorStreamBufferUDP needs some configuration. If it is reading from a unicast socket, it requires the port number (eg, 3111.) Note that the port number can be used by only one process on the machine; you can't have two instances of BehaviorStreamBufferUDP listening on the same port, and you can't have two completely different processes listening on the same port. If BehaviorStreamBufferUDP is listening on multicast, it requires two pieces of information: the port number and the multicast address, eg 3111 and 224.2.244.141. You can pass these in as hardwired paramters to the BehaviorStreamBufferUDP constructor, or you can get fancy and read command line parameters and pass those in. Since this is supposed to be a minimal example, the code uses constants ("static final") in the code. This prevents us from having a bunch of simple but voluminous code hanging around to parse command line parameters, which would just confuse things. */ import mil.navy.nps.*; import mil.navy.nps.dis.*; // DIS library import java.net.*; // for some networking things import java.util.*; // for vectors, etc class BehaviorStreamBufferUDPExample extends Object { // Constants. Change these to reflect the port and/or multicast // address you want to listen to. If you want to listen to a // unicast address, set MULTICAST to false and set the port number // to what you want. The multicast address will be ignored in that // case. static final String MULTICAST_ADDRESS = new String("224.2.244.141"); static final int PORT = 3111; static final boolean MULTICAST = true; // unicast destination to send to and unicast port on dest. machine static final String DESTINATION_ADDRESS = new String("devo.cs.nps.navy.mil"); static final int DESTINATION_PORT = 8007; // The network monitor and the thread that reads from the wire. BehaviorStreamBufferUDP BehaviorStreamBufferUDP; // The network monitor Thread networkReadThread; // the thread the network monitor uses to read from the wire public BehaviorStreamBufferUDPExample() { // Instantiate an instance of BehaviorStreamBufferUDP, either multicast // or unicast, depending on what's set up in the constants section // above. if(MULTICAST) BehaviorStreamBufferUDP = new BehaviorStreamBufferUDP(MULTICAST_ADDRESS, PORT); else BehaviorStreamBufferUDP = new BehaviorStreamBufferUDP(PORT); // The thread runs the network monitor and begins reading. networkReadThread = new Thread(BehaviorStreamBufferUDP); // BehaviorStreamBufferUDP.setThread(networkReadThread); networkReadThread.start(); return; } public void readExample() { // Shows an example of reading from the BehaviorStreamBufferUDP. The // BehaviorStreamBufferUDP has, at this point, been instantiated and // the read thread has kicked off; this was done in the constructor. int idx; // index variable int idx2; // another loop variable Vector receivedPdus; // Vector of PDUs received from the wire // Loop through several times, getting PDUs from the wire. If this // were a real program, we'd probably be looping infinitely. for(idx = 0; idx < 100; idx++) { receivedPdus = BehaviorStreamBufferUDP.receivedPdus(); // get stored up PDUs System.out.println("Recieved " + receivedPdus.size() + " in iteration " + idx + "; PDU types are:"); // Go through the vector, examining each PDU in the vector and // printing out the PDU type. for(idx2 = 0; idx2 < receivedPdus.size(); idx2++) { // Vectors hold Objects; we need to cast to get it to the // correct type, so we'll have access to all the methods // of ProtocolDataUnit. ProtocolDataUnit aPDU = (ProtocolDataUnit)receivedPdus.elementAt(idx2); System.out.println(" PDU Type: " + aPDU.getPduType()); } // end of loop through received PDUs } // end of loop through several iterations return; } // end of readExample public void writeExample() { // Loop through several times, sending out a PDU every now and // then. This sends out on a unicast socket. EntityStatePdu espdu = new EntityStatePdu(); // instance of a PDU int idx = 0; double newVRMLTimestamp = 0.0, oldVRMLTimestamp = 0.0; while(true) { espdu.makeTimestampCurrent(); // not yet implemented? BehaviorStreamBufferUDP.sendPdu(espdu, MULTICAST_ADDRESS, PORT); //System.out.println("sent PDU number " + idx); // So we don't spit these out at some obscene rate, go to sleep // for n milliseconds between sends. try { Thread.sleep(2000); // go to sleep } catch(InterruptedException interruptedException) { throw new RuntimeException("Exceptional sleep "); } } // end of loop } // end of write example public static void main(String args[]) { // The main operation, just used as an entry point. BehaviorStreamBufferUDPExample example = new BehaviorStreamBufferUDPExample(); //example.readExample(); example.writeExample(); return; } } // end of class BehaviorStreamBufferUDPExample