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

variable rename for clarity

parent 1bd1833e
No related branches found
No related tags found
No related merge requests found
...@@ -45,7 +45,7 @@ public class UnicastUdpSender ...@@ -45,7 +45,7 @@ public class UnicastUdpSender
DatagramSocket udpSocket = null; DatagramSocket udpSocket = null;
DataOutputStream dos = null; DataOutputStream dos = null;
int packetID = 0; // counter variable to send in packet int packetID = 0; // counter variable to send in packet
float value = -1.0f; // unreachable value is good sentinel to ensure expected changes occur float countdown = -1.0f; // unreachable value is good sentinel to ensure expected changes occur
String message = MY_NAME + " says Hello MV3500"; // no really String message = MY_NAME + " says Hello MV3500"; // no really
String padding = new String(); String padding = new String();
...@@ -85,29 +85,29 @@ public class UnicastUdpSender ...@@ -85,29 +85,29 @@ public class UnicastUdpSender
for (int index = 1; index <= TOTAL_PACKETS_TO_SEND; index++) // avoid infinite send loops in code, they can be hard to kill! for (int index = 1; index <= TOTAL_PACKETS_TO_SEND; index++) // avoid infinite send loops in code, they can be hard to kill!
{ {
packetID++; // increment counter, prefer using explicit value to index packetID++; // increment counter, prefer using explicit value to index
value = TOTAL_PACKETS_TO_SEND - packetID; // countdown countdown = TOTAL_PACKETS_TO_SEND - packetID; // countdown from goal total
boolean isPacketIdEvenParity = ((packetID % 2) == 0); // % is modulo operator; result 0 is even parity, 1 is odd parity boolean isPacketIdEvenParity = ((packetID % 2) == 0); // % is modulo operator; result 0 is even parity, 1 is odd parity
// values of interest follow. order and types of what was sent must match what you are reading! // values of interest follow. order and types of what was sent must match what you are reading!
dos.writeInt (packetID); dos.writeInt (packetID);
dos.writeFloat (value); dos.writeFloat (countdown);
dos.writeUTF (message); // string value with guaranteed encoding, matches UTF-8 is 8 bit dos.writeUTF (message); // string value with guaranteed encoding, matches UTF-8 is 8 bit
dos.writeBoolean(isPacketIdEvenParity); dos.writeBoolean(isPacketIdEvenParity);
dos.flush(); // sends DataOutputStream to ByteArrayOutputStream dos.flush(); // sends DataOutputStream to ByteArrayOutputStream, clearing the buffer
byteArray = baos.toByteArray(); // OK so go get the flushed result... byteArray = baos.toByteArray(); // OK so go get the flushed result...
datagramPacket.setData(byteArray); // and put it in the packet... datagramPacket.setData(byteArray); // and put it in the packet...
udpSocket.send(datagramPacket); // and send it away. boom gone, nonblocking. udpSocket.send(datagramPacket); // and send it away. boom gone, nonblocking.
// System.out.println("udpSocket output port=" + udpSocket.getLocalPort()); // diagnostic tells what port was chosen by system // System.out.println("udpSocket output port=" + udpSocket.getLocalPort()); // diagnostic tells what port was chosen by system
if (isPacketIdEvenParity) if (isPacketIdEvenParity)
padding = " "; padding = " "; // single blank character as a string of length 1
else padding = ""; else padding = ""; // empty string
Thread.sleep(100); // Send packets at rate of one per second Thread.sleep(100); // Send packets at rate of one per second
System.out.println("[" + UnicastUdpSender.class.getName() + "] " + MY_NAME + " " + sourceAddress + System.out.println("[" + UnicastUdpSender.class.getName() + "] " + MY_NAME + " " + sourceAddress +
" port=" + UDP_PORT + " port=" + UDP_PORT +
" has sent values (" + packetID + "," + value + ",\"" + message + "\"," + isPacketIdEvenParity + " has sent values (" + packetID + "," + countdown + ",\"" + message + "\"," + isPacketIdEvenParity +
")" + padding + " as packet #" + index + " of " + TOTAL_PACKETS_TO_SEND); ")" + padding + " as packet #" + index + " of " + TOTAL_PACKETS_TO_SEND);
baos.reset(); // clear the output stream after sending baos.reset(); // clear the output stream after sending
} // end for loop } // end for loop
......
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