Skip to content
Snippets Groups Projects
Commit a394deea authored by Brutzman, Don's avatar Brutzman, Don
Browse files

updates to support compatible chat client using Java console and keyboard to...

updates to support compatible chat client using Java console and keyboard to provide messages, using MulticastUdpChatKeyboardSender
parent b928df61
No related branches found
No related tags found
No related merge requests found
package UdpExamples; package UdpExamples;
import static UdpExamples.MulticastUdpChatKeyboardSender.LINE_TERMINATOR;
import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface; import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
...@@ -38,6 +39,8 @@ public class MulticastUdpReceiver ...@@ -38,6 +39,8 @@ public class MulticastUdpReceiver
private static final boolean INFINITE_READ_LOOP = true; private static final boolean INFINITE_READ_LOOP = true;
private static NetworkInterface ni; private static NetworkInterface ni;
public static final int BUFFER_LENGTH = 1500;
/** /**
* Program invocation, execution starts here * Program invocation, execution starts here
...@@ -47,7 +50,7 @@ public class MulticastUdpReceiver ...@@ -47,7 +50,7 @@ public class MulticastUdpReceiver
{ {
MulticastSocket multicastSocket = null; MulticastSocket multicastSocket = null;
InetSocketAddress group = null; InetSocketAddress group = null;
ByteBuffer buffer = ByteBuffer.allocate(1500); ByteBuffer buffer = ByteBuffer.allocate(BUFFER_LENGTH);
DatagramPacket packet = new DatagramPacket(buffer.array(), buffer.capacity()); DatagramPacket packet = new DatagramPacket(buffer.array(), buffer.capacity());
try { try {
...@@ -67,49 +70,75 @@ public class MulticastUdpReceiver ...@@ -67,49 +70,75 @@ public class MulticastUdpReceiver
group = new InetSocketAddress(multicastAddress, DESTINATION_PORT); group = new InetSocketAddress(multicastAddress, DESTINATION_PORT);
// Join group useful on receiving side // 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 // 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:"); System.out.println("Multicast packets received log:");
int index, count = 0; // initialize int messageCount = 0; // initialize
float firstFloat, secondFloat;
StringBuilder firstCharacters = new StringBuilder(); StringBuilder firstCharacters = new StringBuilder();
char nextChar; char nextChar;
while (true) { // true is always true, so this is an infinite loop that continues until interrupted while (true) // true is always true, so this is an infinite loop that continues until interrupted
multicastSocket.receive(packet); {
count++; // 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 while (nextChar != ';') // read and build string until terminator ; found
{ {
nextChar = buffer.getChar(); nextChar = buffer.getChar();
firstCharacters.append(nextChar); 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 + "\""); System.out.println("Received sentinel \"" + MulticastUdpSender.QUIT_SENTINEL + "\"");
if (!INFINITE_READ_LOOP) { if (!INFINITE_READ_LOOP) {
break; // exit out of reading loop break; // exit out of reading loop
} }
} }
index = buffer.getInt(); boolean foundNumbers = false;
firstFloat = buffer.getFloat(); String line = firstCharacters.toString();
secondFloat = buffer.getFloat(); 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(); buffer.clear();
if (count < 10) { if (messageCount < 10) {
System.out.print(" "); // prettier output formatting System.out.print(" "); // prettier output formatting for first nine lines
} }
System.out.print(count + ". \"" + firstCharacters + "\" "); // wrap string in quote marks System.out.println(messageCount + ". \"" + firstCharacters + "\" "); // wrap string in quote marks
System.out.println("index=" + index + ", firstFloat=" + firstFloat + " secondFloat=" + secondFloat); if (foundNumbers)
{
System.out.println("MulticastReceiver loop complete."); System.out.println(" messageIndex=" + messageIndex + ", firstFloat=" + firstFloat + " secondFloat=" + secondFloat);
firstCharacters.setLength(0); System.out.println(" MulticastUdpReceiver message receipt complete.");
}
firstCharacters.setLength(0); // reset
} }
} catch (IOException e) { } 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); System.err.println(e);
} finally // clean up after exit condition } finally // clean up after exit condition
{ {
...@@ -122,7 +151,7 @@ public class MulticastUdpReceiver ...@@ -122,7 +151,7 @@ public class MulticastUdpReceiver
multicastSocket.close(); multicastSocket.close();
} }
// socket/database/completion cleanup code can go here, if needed. // socket/database/completion cleanup code can go here, if needed.
System.out.println("MulticastReceiver finally block, complete."); System.out.println("MulticastUdpReceiver finally block, complete.");
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment