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

multiple cleanups, add ability to set address/port

parent 248347ba
No related branches found
No related tags found
No related merge requests found
...@@ -26,6 +26,9 @@ import java.util.logging.Logger; ...@@ -26,6 +26,9 @@ import java.util.logging.Logger;
*/ */
public class DisThreadedNetworkInterface public class DisThreadedNetworkInterface
{ {
public static String DEFAULT_MULTICAST_ADDRESS = "225.4.5.6";
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;
...@@ -63,48 +66,45 @@ public class DisThreadedNetworkInterface ...@@ -63,48 +66,45 @@ public class DisThreadedNetworkInterface
/************ Begin class ***************/ /************ Begin class ***************/
public static int DEFAULT_DIS_PORT = 3000; /** MTU 8192: TODO this has actually been superseded by a larger buffer size, but good enough for now */
public static String DEFAULT_MULTICAST_ADDRESS = "225.4.5.6";
/** 8192: This has actually been superseded by a larger buffer size, but good enough for now */
public static final int MAX_DIS_PDU_SIZE = 8192; public static final int MAX_DIS_PDU_SIZE = 8192;
/** 1500: size of an ethernet frame, common value to avoid packet segmentation */ /** MTU 1500: size of an Ethernet frame, common value to avoid packet segmentation */
public static final int MAX_TRANSMISSION_UNIT_SIZE = 1500; public static final int MAX_TRANSMISSION_UNIT_SIZE = 1500;
private int disPort; private int disPort;
private String mcastGroup; private String multicastAddress;
private boolean killed = false; private boolean killed = false;
private InetAddress maddr; private InetAddress inetAddress;
private InetSocketAddress group; private InetSocketAddress inetSocket;
private NetworkInterface ni; private NetworkInterface networkInterface;
private DatagramSocket socket = null; private DatagramSocket socket = null;
/** /**
* Default constructor using default port 3000 and multicast address 225.4.5.6 * Default constructor using default port 3000 and multicast address 225.4.5.6
*/ */
public DisThreadedNetworkInterface() public DisThreadedNetworkInterface()
{ {
this(DEFAULT_DIS_PORT, DEFAULT_MULTICAST_ADDRESS); this(DEFAULT_MULTICAST_ADDRESS, DEFAULT_DIS_PORT);
} }
/** /**
* * Constructor
* @param multicastGroup the multicast group address to utilize
* @param port the multicast port to utilize * @param port the multicast port to utilize
* @param mcastgroup the multicast group address to utilize
*/ */
public DisThreadedNetworkInterface(int port, String mcastgroup) public DisThreadedNetworkInterface(String multicastGroup, int port)
{ {
disPort = port; disPort = port;
mcastGroup = mcastgroup; multicastAddress = multicastGroup;
try { try {
maddr = InetAddress.getByName(mcastGroup); inetAddress = InetAddress.getByName(multicastAddress);
} catch (UnknownHostException ex) { } catch (UnknownHostException ex) {
Logger.getLogger(DisThreadedNetworkInterface.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(DisThreadedNetworkInterface.class.getName()).log(Level.SEVERE, null, ex);
} }
group = new InetSocketAddress(maddr, disPort); inetSocket = new InetSocketAddress(inetAddress, getDisPort());
ni = findIpv4Interface(); networkInterface = findIpv4Interface();
init(); init();
} }
...@@ -182,7 +182,7 @@ public class DisThreadedNetworkInterface ...@@ -182,7 +182,7 @@ public class DisThreadedNetworkInterface
public String getMcastGroup() public String getMcastGroup()
{ {
return mcastGroup; return multicastAddress;
} }
/** /**
...@@ -231,8 +231,8 @@ public class DisThreadedNetworkInterface ...@@ -231,8 +231,8 @@ 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
socket = new MulticastSocket(disPort); socket = new MulticastSocket(getDisPort());
((MulticastSocket)socket).joinGroup(group, ni); ((MulticastSocket)socket).joinGroup(inetSocket, networkInterface);
while (!killed) { while (!killed) {
...@@ -263,7 +263,7 @@ public class DisThreadedNetworkInterface ...@@ -263,7 +263,7 @@ public class DisThreadedNetworkInterface
finally { finally {
if (socket != null && !socket.isClosed()) { if (socket != null && !socket.isClosed()) {
try { try {
((MulticastSocket)socket).leaveGroup(group, ni); ((MulticastSocket)socket).leaveGroup(inetSocket, networkInterface);
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(DisThreadedNetworkInterface.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(DisThreadedNetworkInterface.class.getName()).log(Level.SEVERE, null, ex);
} }
...@@ -283,7 +283,7 @@ public class DisThreadedNetworkInterface ...@@ -283,7 +283,7 @@ public class DisThreadedNetworkInterface
// 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);
DatagramPacket packet = new DatagramPacket(baos.toByteArray(), baos.size(), group); DatagramPacket packet = new DatagramPacket(baos.toByteArray(), baos.size(), inetSocket);
while (!killed) { // keep trying on error while (!killed) { // keep trying on error
try { try {
...@@ -396,4 +396,12 @@ public class DisThreadedNetworkInterface ...@@ -396,4 +396,12 @@ public class DisThreadedNetworkInterface
{ {
this.verbose = verbose; this.verbose = verbose;
} }
/**
* @param disPort the disPort value to set
*/
public void setDisPort(int disPort)
{
this.disPort = disPort;
}
} }
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