Class DisThreadedNetworkInterface
java.lang.Object
edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface
This is a thread-safe, multicast DIS network interface class which greatly simplifies reading and writing of PDUs for applications.
This simplifies and consolidates all reading, writing and threading of DIS over the network.
Example
main()
self-test response shown in log file.- Since:
- Jul 29, 2019
- Author:
- Don Brutzman, brutzman@nps.edu, Mike Bailey, jmbailey@nps.edu
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclass
Stores data for further processingstatic interface
Pdu listener interface is an internal class.static interface
Raw pdu listener interface is an internal class. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Default multicast group address239.1.2.3
for sendPDU and receive connections.static final int
Default socket port3000
, matches Wireshark DIS capture defaultstatic final int
MTU 8192: TODO this has actually been superseded by a larger buffer size, but good enough for nowstatic final int
MTU 1500: size of an Ethernet frame, common value to avoid packet segmentation -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor using default multicast address and port, no descriptorDisThreadedNetworkInterface
(boolean defaultVerbose) Object constructor with verboseness initialization, using default multicast address and portDisThreadedNetworkInterface
(String newDescriptor) Object constructor with descriptor, using default multicast address and portDisThreadedNetworkInterface
(String address, int port) Object constructor using specified multicast address, port, descriptor, and verbosenessDisThreadedNetworkInterface
(String address, int port, String newDescriptor) Object constructor using specified multicast address and port, plus descriptor.DisThreadedNetworkInterface
(String address, int port, String newDescriptor, boolean defaultVerbose) Primary object constructor using specified multicast address and port, plus descriptor and verboseness. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(DisThreadedNetworkInterface.PduListener newListener) Add a listener to accept all pdu typesvoid
addListener
(DisThreadedNetworkInterface.PduListener newListener, DisPduType disPduType) Add a listener to accept only pdus of a given typevoid
addRawListener
(DisThreadedNetworkInterface.RawPduListener rawPduListener) Add a listener to accept pdus of all types in the form of a byte arrayfinal void
begin()
Can be used to restart DisThreadedNetworkInterface if closed.void
close()
Terminate the instance after completion of pending send/receive activity.static NetworkInterface
Find proper IPV4 interface on this computer for use with the djs addressGet current multicast (or unicast) network address for sendPDU and receive connections.Get simple descriptor (such as parent class name) for this network interface, used in trace statementsint
Deprecated.Deprecated.int
getPort()
Get network port used, multicast or unicast.boolean
Whether or not trace statements are provided when packets are sent or received.boolean
Whether or not trace statements include timestamp values.boolean
Whether or not trace statements are provided when packets are received.boolean
Whether or not trace statements are provided when packets are sent.void
kill()
Deprecated.void
killThread
(Thread threadToKill) Tell thread to stop.static void
Main method provides support for testing.void
removeListener
(DisThreadedNetworkInterface.PduListener priorListener) Remove previously added listenervoid
removeRawListener
(DisThreadedNetworkInterface.RawPduListener rawPduListener) Remove previously added raw listenervoid
Report whether sendingThread and receiveThread are alivevoid
selfTest()
Self test to check basic operation, invoked by main().void
Send the given pdu to the network using the IP address and port given to the constructor using a deep copy to avoid later modifications to the original pdu having any effect on already-buffered output pdu instances.void
setAddress
(String newAddress) Network address for sendPDU and receive connections.void
setDescriptor
(String newDescriptor) Set new simple descriptor (such as parent class name) for this network interface, used in trace statementsvoid
Tell sendingThread and receiveThread to stop.void
setPort
(int newPortValue) /** Set network port used, multicast or unicast.void
setVerbose
(boolean newValue) Set whether or not trace statements are provided when packets are sent or received, including verboseReceipt and verboseSending.void
setVerboseIncludesTimestamp
(boolean newVerboseIncludesTimestamp) Set whether or not trace statements include timestamp values.void
setVerboseReceipt
(boolean newValue) Set whether or not trace statements are provided when packets are received.void
setVerboseSending
(boolean newValue) Set whether or not trace statements are provided when packets are sent.
-
Field Details
-
MAX_DIS_PDU_SIZE
public static final int MAX_DIS_PDU_SIZEMTU 8192: TODO this has actually been superseded by a larger buffer size, but good enough for now- See Also:
-
MAX_TRANSMISSION_UNIT_SIZE
public static final int MAX_TRANSMISSION_UNIT_SIZEMTU 1500: size of an Ethernet frame, common value to avoid packet segmentation- See Also:
-
DEFAULT_DIS_PORT
public static final int DEFAULT_DIS_PORTDefault socket port3000
, matches Wireshark DIS capture default- See Also:
-
DEFAULT_DIS_ADDRESS
Default multicast group address239.1.2.3
for sendPDU and receive connections.- See Also:
-
-
Constructor Details
-
DisThreadedNetworkInterface
public DisThreadedNetworkInterface()Default constructor using default multicast address and port, no descriptor -
DisThreadedNetworkInterface
Object constructor with descriptor, using default multicast address and port- Parameters:
newDescriptor
- simple descriptor name for this interface
-
DisThreadedNetworkInterface
public DisThreadedNetworkInterface(boolean defaultVerbose) Object constructor with verboseness initialization, using default multicast address and port- Parameters:
defaultVerbose
- whether or not DisThreadedNetworkInterface is verbose
-
DisThreadedNetworkInterface
Object constructor using specified multicast address, port, descriptor, and verboseness- Parameters:
address
- the multicast group or unicast address to utilizeport
- the multicast port to utilize
-
DisThreadedNetworkInterface
Object constructor using specified multicast address and port, plus descriptor.- Parameters:
address
- the multicast group or unicast address to utilizeport
- the multicast port to utilizenewDescriptor
- simple descriptor name for this interface
-
DisThreadedNetworkInterface
public DisThreadedNetworkInterface(String address, int port, String newDescriptor, boolean defaultVerbose) Primary object constructor using specified multicast address and port, plus descriptor and verboseness.- Parameters:
address
- the multicast group or unicast address to utilizeport
- the multicast port to utilizenewDescriptor
- simple descriptor name for this interfacedefaultVerbose
- whether or not DisThreadedNetworkInterface is verbose
-
-
Method Details
-
addListener
Add a listener to accept only pdus of a given type- Parameters:
newListener
- listener instance implementing the RawPduListener interfacedisPduType
- Pdu type
-
addListener
Add a listener to accept all pdu types- Parameters:
newListener
- listener instance implementing the RawPduListener interface
-
removeListener
Remove previously added listener- Parameters:
priorListener
- listener instance implementing the RawPduListener interface
-
addRawListener
Add a listener to accept pdus of all types in the form of a byte array- Parameters:
rawPduListener
- listener instance implementing the RawPduListener interface
-
removeRawListener
Remove previously added raw listener- Parameters:
rawPduListener
- listener instance implementing the RawPduListener interface
-
getMulticastGroup
Deprecated.Method renamed asgetAddress()
so use that method instead.- Returns:
- current multicast address value
-
getAddress
Get current multicast (or unicast) network address for sendPDU and receive connections.- Returns:
- current multicast address value
- See Also:
-
setAddress
Network address for sendPDU and receive connections.- Parameters:
newAddress
- the new network address to set- See Also:
-
sendPDU
Send the given pdu to the network using the IP address and port given to the constructor using a deep copy to avoid later modifications to the original pdu having any effect on already-buffered output pdu instances.- Parameters:
pdu
- the pdu to sendPDU
-
begin
public final void begin()Can be used to restart DisThreadedNetworkInterface if closed. Create datagram socket if not already available; can also be invoked by either sender or receiver thread to ensure datagram socket is open. Method was originally named start(). -
kill
-
setKillSentinelAndInterrupts
public void setKillSentinelAndInterrupts()Tell sendingThread and receiveThread to stop. -
close
public void close()Terminate the instance after completion of pending send/receive activity. Synchronized to prevent interleaved invocation. -
killThread
Tell thread to stop.- Parameters:
threadToKill
- of interest
-
reportThreadStatus
public void reportThreadStatus()Report whether sendingThread and receiveThread are alive -
findIpv4Interface
Find proper IPV4 interface on this computer for use with the djs address- Returns:
- a network interface to use to join a multicast group
-
setVerbose
public void setVerbose(boolean newValue) Set whether or not trace statements are provided when packets are sent or received, including verboseReceipt and verboseSending.- Parameters:
newValue
- the verbose status to set. Also resets verboseReceipt and verboseSending to match.- See Also:
-
hasVerboseOutput
public boolean hasVerboseOutput()Whether or not trace statements are provided when packets are sent or received.- Returns:
- the verbose status
- See Also:
-
setVerboseReceipt
public void setVerboseReceipt(boolean newValue) Set whether or not trace statements are provided when packets are received.- Parameters:
newValue
- the verboseReceipt status to set- See Also:
-
hasVerboseReceipt
public boolean hasVerboseReceipt()Whether or not trace statements are provided when packets are received.- Returns:
- the verboseReceipt status
-
setVerboseSending
public void setVerboseSending(boolean newValue) Set whether or not trace statements are provided when packets are sent.- Parameters:
newValue
- the verboseSending status to set- See Also:
-
hasVerboseSending
public boolean hasVerboseSending()Whether or not trace statements are provided when packets are sent.- Returns:
- the verboseSending status
-
hasVerboseOutputIncludesTimestamp
public boolean hasVerboseOutputIncludesTimestamp()Whether or not trace statements include timestamp values.- Returns:
- the verboseIncludesTimestamp value
-
setVerboseIncludesTimestamp
public void setVerboseIncludesTimestamp(boolean newVerboseIncludesTimestamp) Set whether or not trace statements include timestamp values.- Parameters:
newVerboseIncludesTimestamp
- the value to set
-
getDisPort
Deprecated.Method renamed asgetPort()
so use that method instead.- Returns:
- current port value
-
getPort
public int getPort()Get network port used, multicast or unicast.- Returns:
- current port value
- See Also:
-
setPort
public void setPort(int newPortValue) /** Set network port used, multicast or unicast.- Parameters:
newPortValue
- the disPort value to set- See Also:
-
getDescriptor
Get simple descriptor (such as parent class name) for this network interface, used in trace statements- Returns:
- simple descriptor name
-
setDescriptor
Set new simple descriptor (such as parent class name) for this network interface, used in trace statements- Parameters:
newDescriptor
- simple descriptor name for this interface
-
selfTest
public void selfTest()Self test to check basic operation, invoked by main(). Warning, VPN can block receipt of PDUs! Also be careful when debugging since debug mode can interrupt otherwise-optimized threading operations. TODO: make this part of run-time network interface setup, reporting if sent PDUs are not received. See github issue 25. -
main
Main method provides support for testing.- Parameters:
args
- [address, port, descriptor] command-line arguments are an array of optional String parameters that are passed from execution environment during invocation- See Also:
-