diff --git a/examples/src/UdpExamples/MulticastUdpReceiver.java b/examples/src/UdpExamples/MulticastUdpReceiver.java index 86e3e6dfddd8446a9ef38aba0b8946600a5ee833..83b5c415e9c0f5ac468984ab26d3fc1fbbf0b81a 100644 --- a/examples/src/UdpExamples/MulticastUdpReceiver.java +++ b/examples/src/UdpExamples/MulticastUdpReceiver.java @@ -1,5 +1,6 @@ package UdpExamples; +import static UdpExamples.MulticastUdpChatKeyboardSender.LINE_TERMINATOR; import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface; import java.io.*; import java.net.*; @@ -38,6 +39,8 @@ public class MulticastUdpReceiver private static final boolean INFINITE_READ_LOOP = true; private static NetworkInterface ni; + + public static final int BUFFER_LENGTH = 1500; /** * Program invocation, execution starts here @@ -47,7 +50,7 @@ public class MulticastUdpReceiver { MulticastSocket multicastSocket = null; InetSocketAddress group = null; - ByteBuffer buffer = ByteBuffer.allocate(1500); + ByteBuffer buffer = ByteBuffer.allocate(BUFFER_LENGTH); DatagramPacket packet = new DatagramPacket(buffer.array(), buffer.capacity()); try { @@ -67,49 +70,75 @@ public class MulticastUdpReceiver group = new InetSocketAddress(multicastAddress, DESTINATION_PORT); // Join group useful on receiving side - multicastSocket.joinGroup(group, ni = DisThreadedNetworkInterface.findIpv4Interface()); + multicastSocket.joinGroup(group, ni = DisThreadedNetworkInterface.findIpv4Interface()); // utility function // You can join multiple groups here - System.out.println("Multicast address/port: " + multicastAddress.getHostAddress() + "/" + DESTINATION_PORT); + System.out.println("Multicast address/port: " + multicastAddress.getHostAddress() + "/" + DESTINATION_PORT + + " isBound=" + multicastSocket.isBound()); + // unnecessary, no server involved: + "isConnected=" + multicastSocket.isConnected() System.out.println("Multicast packets received log:"); - int index, count = 0; // initialize - float firstFloat, secondFloat; + int messageCount = 0; // initialize StringBuilder firstCharacters = new StringBuilder(); char nextChar; - while (true) { // true is always true, so this is an infinite loop that continues until interrupted - multicastSocket.receive(packet); - count++; + while (true) // true is always true, so this is an infinite loop that continues until interrupted + { + // initializations for this loop + int messageIndex = 0; + float firstFloat = 0.0f; + float secondFloat = 0.0f; + + multicastSocket.receive(packet); // this method call will block until a packet is received + messageCount++; - nextChar = ' '; + nextChar = ' '; // reinitialize while (nextChar != ';') // read and build string until terminator ; found { nextChar = buffer.getChar(); firstCharacters.append(nextChar); } - if (firstCharacters.toString().contains(MulticastUdpSender.QUIT_SENTINEL)) { + if (firstCharacters.toString().contains(MulticastUdpSender.QUIT_SENTINEL)) + { System.out.println("Received sentinel \"" + MulticastUdpSender.QUIT_SENTINEL + "\""); if (!INFINITE_READ_LOOP) { break; // exit out of reading loop } } - index = buffer.getInt(); - firstFloat = buffer.getFloat(); - secondFloat = buffer.getFloat(); + boolean foundNumbers = false; + String line = firstCharacters.toString(); + if (line.contains(LINE_TERMINATOR)) + { + foundNumbers = false; + firstCharacters.setLength(line.length() - LINE_TERMINATOR.length()); // clip EOL sentinel + } + else if (buffer.hasRemaining()) // likely always true since size is 1500 + { + messageIndex = buffer.getInt(); + firstFloat = buffer.getFloat(); + secondFloat = buffer.getFloat(); + // simplistic tests for numbers read from buffer + if ((messageIndex == 0) && (firstFloat == 0.0f) && (secondFloat == 0.0f)) + foundNumbers = false; + else if ((messageIndex >= 0) && (messageIndex <= 10000)) + foundNumbers = true; + } + else System.out.print("[trace] no numbers encountered in buffer"); buffer.clear(); - if (count < 10) { - System.out.print(" "); // prettier output formatting + if (messageCount < 10) { + System.out.print(" "); // prettier output formatting for first nine lines } - System.out.print(count + ". \"" + firstCharacters + "\" "); // wrap string in quote marks - System.out.println("index=" + index + ", firstFloat=" + firstFloat + " secondFloat=" + secondFloat); - - System.out.println("MulticastReceiver loop complete."); - firstCharacters.setLength(0); + System.out.println(messageCount + ". \"" + firstCharacters + "\" "); // wrap string in quote marks + if (foundNumbers) + { + System.out.println(" messageIndex=" + messageIndex + ", firstFloat=" + firstFloat + " secondFloat=" + secondFloat); + System.out.println(" MulticastUdpReceiver message receipt complete."); + } + firstCharacters.setLength(0); // reset } } catch (IOException e) { - System.err.println("Problem with MulticastReceiver, see exception trace:"); + System.err.println("Problem with MulticastUdpReceiver, see exception trace:"); System.err.println(e); } finally // clean up after exit condition { @@ -122,7 +151,7 @@ public class MulticastUdpReceiver multicastSocket.close(); } // socket/database/completion cleanup code can go here, if needed. - System.out.println("MulticastReceiver finally block, complete."); + System.out.println("MulticastUdpReceiver finally block, complete."); } } }