Class DisThreadedNetworkInterface

java.lang.Object
edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface

public class DisThreadedNetworkInterface extends Object
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:
  • Field Details

  • Constructor Details

    • DisThreadedNetworkInterface

      public DisThreadedNetworkInterface()
      Default constructor using default multicast address and port, no descriptor
    • DisThreadedNetworkInterface

      public DisThreadedNetworkInterface(String newDescriptor)
      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

      public DisThreadedNetworkInterface(String address, int port)
      Object constructor using specified multicast address, port, descriptor, and verboseness
      Parameters:
      address - the multicast group or unicast address to utilize
      port - the multicast port to utilize
    • DisThreadedNetworkInterface

      public DisThreadedNetworkInterface(String address, int port, String newDescriptor)
      Object constructor using specified multicast address and port, plus descriptor.
      Parameters:
      address - the multicast group or unicast address to utilize
      port - the multicast port to utilize
      newDescriptor - 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 utilize
      port - the multicast port to utilize
      newDescriptor - simple descriptor name for this interface
      defaultVerbose - whether or not DisThreadedNetworkInterface is verbose
  • Method Details

    • addListener

      public void addListener(DisThreadedNetworkInterface.PduListener newListener, DisPduType disPduType)
      Add a listener to accept only pdus of a given type
      Parameters:
      newListener - listener instance implementing the RawPduListener interface
      disPduType - Pdu type
    • addListener

      public void addListener(DisThreadedNetworkInterface.PduListener newListener)
      Add a listener to accept all pdu types
      Parameters:
      newListener - listener instance implementing the RawPduListener interface
    • removeListener

      public void removeListener(DisThreadedNetworkInterface.PduListener priorListener)
      Remove previously added listener
      Parameters:
      priorListener - listener instance implementing the RawPduListener interface
    • addRawListener

      public void addRawListener(DisThreadedNetworkInterface.RawPduListener rawPduListener)
      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

      public void removeRawListener(DisThreadedNetworkInterface.RawPduListener rawPduListener)
      Remove previously added raw listener
      Parameters:
      rawPduListener - listener instance implementing the RawPduListener interface
    • getMulticastGroup

      @Deprecated public String getMulticastGroup()
      Deprecated.
      Method renamed as getAddress() so use that method instead.
      Returns:
      current multicast address value
    • getAddress

      public String getAddress()
      Get current multicast (or unicast) network address for sendPDU and receive connections.
      Returns:
      current multicast address value
      See Also:
    • setAddress

      public void setAddress(String newAddress)
      Network address for sendPDU and receive connections.
      Parameters:
      newAddress - the new network address to set
      See Also:
    • sendPDU

      public void sendPDU(Pdu pdu)
      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

      @Deprecated public void kill()
      Deprecated.
      Method renamed as close() so use that method instead.
    • 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

      public void killThread(Thread threadToKill)
      Tell thread to stop.
      Parameters:
      threadToKill - of interest
    • reportThreadStatus

      public void reportThreadStatus()
      Report whether sendingThread and receiveThread are alive
    • findIpv4Interface

      public static NetworkInterface 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:
      • verboseReceipt
      • verboseSending
    • hasVerboseOutput

      public boolean hasVerboseOutput()
      Whether or not trace statements are provided when packets are sent or received.
      Returns:
      the verbose status
      See Also:
      • verboseReceipt
      • verboseSending
    • 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:
      • verbose
      • verboseSending
    • 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:
      • verbose
      • verboseReceipt
    • 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 public int getDisPort()
      Deprecated.
      Method renamed as getPort() 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

      public String getDescriptor()
      Get simple descriptor (such as parent class name) for this network interface, used in trace statements
      Returns:
      simple descriptor name
    • setDescriptor

      public void setDescriptor(String newDescriptor)
      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

      public static void main(String[] args)
      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: