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

improved method interfaces, javadoc

parent 77bdae30
No related branches found
No related tags found
No related merge requests found
...@@ -29,14 +29,18 @@ import java.util.logging.Logger; ...@@ -29,14 +29,18 @@ import java.util.logging.Logger;
*/ */
public class DisThreadedNetworkInterface public class DisThreadedNetworkInterface
{ {
/** Default value */ /** Default multicast group address <code>229.1.2.3</code> for send and receive connections.
public static String DEFAULT_MULTICAST_ADDRESS = "225.4.5.6"; * @see <a href="https://en.wikipedia.org/wiki/Multicast_address">https://en.wikipedia.org/wiki/Multicast_address</a> */
public static String DEFAULT_MULTICAST_ADDRESS = "229.1.2.3";
/** Default value */ /** Default socket port <code>3000</code>, matches Wireshark DIS capture default
* @see <a href="https://en.wikipedia.org/wiki/Port_(computer_networking)">https://en.wikipedia.org/wiki/Port_(computer_networking)</a> */
public static int DEFAULT_DIS_PORT = 3000; public static int DEFAULT_DIS_PORT = 3000;
private static final String TRACE_PREFIX = "[" + DisThreadedNetworkInterface.class.getName() + "] "; private static final String TRACE_PREFIX = "[" + DisThreadedNetworkInterface.class.getName() + "] ";
private boolean verbose = true; private boolean verbose = true;
private boolean verboseReceipt = true;
private boolean verboseSending = true;
private boolean verboseIncludesTimestamp = false; private boolean verboseIncludesTimestamp = false;
/** /**
...@@ -113,7 +117,7 @@ public class DisThreadedNetworkInterface ...@@ -113,7 +117,7 @@ public class DisThreadedNetworkInterface
private DatagramSocket datagramSocket = null; private DatagramSocket datagramSocket = null;
/** /**
* Default constructor using default port and multicast address * Object constructor using default multicast address and port
*/ */
public DisThreadedNetworkInterface() public DisThreadedNetworkInterface()
{ {
...@@ -121,7 +125,7 @@ public class DisThreadedNetworkInterface ...@@ -121,7 +125,7 @@ public class DisThreadedNetworkInterface
} }
/** /**
* Constructor * Object constructor using specified multicast address and port
* @param multicastGroup the multicast group address to utilize * @param multicastGroup the multicast group address to utilize
* @param port the multicast port to utilize * @param port the multicast port to utilize
*/ */
...@@ -223,20 +227,38 @@ public class DisThreadedNetworkInterface ...@@ -223,20 +227,38 @@ public class DisThreadedNetworkInterface
sleep(100l); // TODO needed? sleep(100l); // TODO needed?
} }
/** /** Deprecated, replaced by getPort()
* Get current port value
* @return current port value * @return current port value
*/ */
@Deprecated
public int getDisPort() public int getDisPort()
{
return getPort();
}
/** Get network port used, multicast or unicast.
* @see <a href="https://en.wikipedia.org/wiki/Port_(computer_networking)">https://en.wikipedia.org/wiki/Port_(computer_networking)</a>
* @return current port value
*/
public int getPort()
{ {
return disPort; return disPort;
} }
/** /**
* Get current multicast address value * Deprecated, replaced by getAddress()
* @return current multicast address value * @return current multicast address value
*/ */
@Deprecated
public String getMulticastGroup() public String getMulticastGroup()
{
return getAddress();
}
/**
* Get current multicast (or unicast) address value
* @return current multicast address value
*/
public String getAddress()
{ {
return multicastAddress; return multicastAddress;
} }
...@@ -291,7 +313,7 @@ public class DisThreadedNetworkInterface ...@@ -291,7 +313,7 @@ public class DisThreadedNetworkInterface
try try
{ {
// The initial value of the SO_BROADCAST socket option is FALSE // The initial value of the SO_BROADCAST socket option is FALSE
datagramSocket = new MulticastSocket(getDisPort()); datagramSocket = new MulticastSocket(getPort());
((MulticastSocket) datagramSocket).joinGroup(inetSocket, networkInterface); ((MulticastSocket) datagramSocket).joinGroup(inetSocket, networkInterface);
} }
catch (IOException ex) catch (IOException ex)
...@@ -303,7 +325,7 @@ public class DisThreadedNetworkInterface ...@@ -303,7 +325,7 @@ public class DisThreadedNetworkInterface
private Runnable receiveThread = () -> { private Runnable receiveThread = () -> {
int counter = 0; int pduReceiptCounter = 0;
// The capacity could go up to MAX_DIS_PDU_SIZE, but this should be good for now // The capacity could go up to MAX_DIS_PDU_SIZE, but this should be good for now
// The raw listeners will strip off any extra padding and process what is // The raw listeners will strip off any extra padding and process what is
...@@ -328,11 +350,11 @@ public class DisThreadedNetworkInterface ...@@ -328,11 +350,11 @@ public class DisThreadedNetworkInterface
if (pdu != null) if (pdu != null)
{ {
counter++; // TODO experimental, add to generator as a commented-out diagnostic; consider adding diagnostic mode pduReceiptCounter++; // TODO experimental, add to generator as a commented-out diagnostic; consider adding diagnostic mode
if (isVerbose()) if (hasVerboseOutput() && hasVerboseReceipt())
{ {
String message = TRACE_PREFIX + counter + ". received " + pdu.getPduType().toString(); String message = TRACE_PREFIX + "[receipt " + pduReceiptCounter + "] " + pdu.getPduType().toString();
if (isVerboseIncludesTimestamp()) if (hasVerboseOutputIncludesTimestamp())
message += " (timestamp " + DisTime.timeStampToString(pdu.getTimestamp()); message += " (timestamp " + DisTime.timeStampToString(pdu.getTimestamp());
message +=", size " + pdu.getMarshalledSize() + " bytes)"; message +=", size " + pdu.getMarshalledSize() + " bytes)";
System.out.println(message); System.out.println(message);
...@@ -369,6 +391,8 @@ public class DisThreadedNetworkInterface ...@@ -369,6 +391,8 @@ public class DisThreadedNetworkInterface
Pdu pdu; Pdu pdu;
int pduSentCounter = 0;
// The capacity could go up to MAX_DIS_PDU_SIZE, but this should be good for now // The capacity could go up to MAX_DIS_PDU_SIZE, but this should be good for now
ByteArrayOutputStream baos = new ByteArrayOutputStream(MAX_TRANSMISSION_UNIT_SIZE); ByteArrayOutputStream baos = new ByteArrayOutputStream(MAX_TRANSMISSION_UNIT_SIZE);
DataOutputStream dos = new DataOutputStream(baos); DataOutputStream dos = new DataOutputStream(baos);
...@@ -387,7 +411,17 @@ public class DisThreadedNetworkInterface ...@@ -387,7 +411,17 @@ public class DisThreadedNetworkInterface
pdu.marshal(dos); pdu.marshal(dos);
packet.setData(baos.toByteArray()); packet.setData(baos.toByteArray());
datagramSocket.send(packet); datagramSocket.send(packet);
pduSentCounter++; // TODO experimental, add to generator as a commented-out diagnostic; consider adding diagnostic mode
if (hasVerboseOutput() && hasVerboseSending())
{
String message = TRACE_PREFIX + "[sending " + pduSentCounter + "] " + pdu.getPduType().toString();
if (hasVerboseOutputIncludesTimestamp())
message += " (timestamp " + DisTime.timeStampToString(pdu.getTimestamp());
message +=", size " + pdu.getMarshalledSize() + " bytes)";
System.out.println(message);
System.out.flush();
}
dos.flush(); // immediately force pdu write dos.flush(); // immediately force pdu write
baos.reset(); baos.reset();
} }
...@@ -399,7 +433,8 @@ public class DisThreadedNetworkInterface ...@@ -399,7 +433,8 @@ public class DisThreadedNetworkInterface
} }
try { try {
dos.close(); dos.close();
} catch (IOException e) {} }
catch (IOException e) {} // shutting down, no need to report exception
}; };
private void toListeners(Pdu pdu) { private void toListeners(Pdu pdu) {
...@@ -430,8 +465,16 @@ public class DisThreadedNetworkInterface ...@@ -430,8 +465,16 @@ public class DisThreadedNetworkInterface
rawListeners.forEach(lis->lis.incomingPdu(bl)); rawListeners.forEach(lis->lis.incomingPdu(bl));
} }
/** Terminate the instance */ /** Method renamed as <code>close()</code>.
*/
@Deprecated
public void kill() public void kill()
{
close();
}
/** Terminate the instance after completion of pending send/receive activity. */
public void close()
{ {
killed = true; // set loop sentinel for threads killed = true; // set loop sentinel for threads
} }
...@@ -484,30 +527,80 @@ public class DisThreadedNetworkInterface ...@@ -484,30 +527,80 @@ public class DisThreadedNetworkInterface
} }
/** /**
* @return the verbose * Set whether or not trace statements are provided when packets are sent or received.
* @param newValue the verbose status to set. Also resets verboseReceipt and verboseSending to match.
* @see verboseReceipt
* @see verboseSending
*/ */
public boolean isVerbose() public void setVerbose(boolean newValue)
{
this.verbose = newValue;
verboseReceipt = verbose;
verboseSending = verbose;
}
/**
* Whether or not trace statements are provided when packets are sent or received.
* @return the verbose status
* @see verboseReceipt
* @see verboseSending
*/
public boolean hasVerboseOutput()
{ {
return verbose; return verbose;
} }
/** /**
* @param verbose the verbose to set * Set whether or not trace statements are provided when packets are received.
* @param newValue the verboseReceipt status to set
* @see verbose
* @see verboseSending
*/
public void setVerboseReceipt(boolean newValue)
{
this.verboseReceipt = newValue;
verbose = (verboseReceipt || verboseSending);
}
/**
* Whether or not trace statements are provided when packets are received.
* @return the verboseReceipt status
*/
public boolean hasVerboseReceipt()
{
return verboseReceipt;
}
/**
* Set whether or not trace statements are provided when packets are sent.
* @param newValue the verboseSending status to set
* @see verbose
* @see verboseReceipt
*/
public void setVerboseSending(boolean newValue)
{
this.verboseSending = newValue;
verbose = (verboseReceipt || verboseSending);
}
/**
* Whether or not trace statements are provided when packets are sent.
* @return the verboseSending status
*/ */
public void setVerbose(boolean verbose) public boolean hasVerboseSending()
{ {
this.verbose = verbose; return verboseSending;
} }
/** /**
* Whether or not trace statements include timestamp values.
* @return the verboseIncludesTimestamp value * @return the verboseIncludesTimestamp value
*/ */
public boolean isVerboseIncludesTimestamp() public boolean hasVerboseOutputIncludesTimestamp()
{ {
return verboseIncludesTimestamp; return verboseIncludesTimestamp;
} }
/** /**
* Set whether or not trace statements include timestamp values.
* @param verboseIncludesTimestamp the value to set * @param verboseIncludesTimestamp the value to set
*/ */
public void setVerboseIncludesTimestamp(boolean verboseIncludesTimestamp) public void setVerboseIncludesTimestamp(boolean verboseIncludesTimestamp)
...@@ -516,10 +609,12 @@ public class DisThreadedNetworkInterface ...@@ -516,10 +609,12 @@ public class DisThreadedNetworkInterface
} }
/** /**
* @param disPort the disPort value to set /** Set network port used, multicast or unicast.
* @see <a href="https://en.wikipedia.org/wiki/Port_(computer_networking)">https://en.wikipedia.org/wiki/Port_(computer_networking)</a>
* @param newPortValue the disPort value to set
*/ */
public void setDisPort(int disPort) public void setPort(int newPortValue)
{ {
this.disPort = disPort; this.disPort = newPortValue;
} }
} }
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