Class UDP


public class UDP extends TP
IP multicast transport based on UDP. Messages to the group (msg.dest == null) will be multicast (to all group members), whereas point-to-point messages (msg.dest != null) will be unicast to a single member. Uses a multicast and a unicast socket.

The following properties are read by the UDP protocol:

  • param mcast_addr - the multicast address to use; default is 228.8.8.8.
  • param mcast_port - (int) the port that the multicast is sent on; default is 7600
  • param ip_mcast - (boolean) flag whether to use IP multicast; default is true.
  • param ip_ttl - the default time-to-live for multicast packets sent out on this socket; default is 32.
  • param use_packet_handler - boolean, defaults to false. If set, the mcast and ucast receiver threads just put the datagram's payload (a byte buffer) into a queue, from where a separate thread will dequeue and handle them (unmarshal and pass up). This frees the receiver threads from having to do message unmarshalling; this time can now be spent receiving packets. If you have lots of retransmissions because of network input buffer overflow, consider setting this property to true.
Author:
Bela Ban
  • Field Details

    • tos

      protected int tos
      Traffic class for sending unicast and multicast datagrams. Valid values are (check DatagramSocket.setTrafficClass(int) ); for details):
      • IPTOS_LOWCOST (0x02), decimal 2
      • IPTOS_RELIABILITY (0x04)invalid input: '<', decimal 4/LI>
      • IPTOS_THROUGHPUT (0x08), decimal 8
      • IPTOS_LOWDELAY (0x10), decimal 16
    • mcast_group_addr

      protected InetAddress mcast_group_addr
    • mcast_port

      protected int mcast_port
    • ip_mcast

      protected boolean ip_mcast
    • ip_ttl

      protected int ip_ttl
    • mcast_send_buf_size

      protected int mcast_send_buf_size
    • mcast_recv_buf_size

      protected int mcast_recv_buf_size
    • ucast_send_buf_size

      protected int ucast_send_buf_size
    • ucast_recv_buf_size

      protected int ucast_recv_buf_size
    • disable_loopback

      protected boolean disable_loopback
    • mcast_addr

      protected IpAddress mcast_addr
      The multicast address (mcast address and port) this member uses
    • sock

      protected DatagramSocket sock
      Socket used for
      1. sending unicast and multicast packets and
      2. receiving unicast packets
      The address of this socket will be our local address (local_addr)
    • mcast_sock

      protected MulticastSocket mcast_sock
      IP multicast socket for receiving multicast packets
    • mcast_receiver

      protected UDP.PacketReceiver mcast_receiver
      Runnable to receive multicast packets
    • ucast_receiver

      protected UDP.PacketReceiver ucast_receiver
      Runnable to receive unicast packets
  • Constructor Details

    • UDP

      public UDP()
      Creates the UDP protocol, and initializes the state variables, does however not start any sockets or threads.
  • Method Details

    • supportsMulticasting

      public boolean supportsMulticasting()
      Description copied from class: TP
      Whether or not hardware multicasting is supported
      Specified by:
      supportsMulticasting in class TP
    • setMulticastAddress

      public void setMulticastAddress(InetAddress addr)
    • getMulticastAddress

      public InetAddress getMulticastAddress()
    • getMulticastPort

      public int getMulticastPort()
    • setMulticastPort

      public void setMulticastPort(int mcast_port)
    • setMcastPort

      public void setMcastPort(int mcast_port)
    • setMulticastTTL

      public void setMulticastTTL(int ttl) throws IOException
      Set the ttl for multicast socket
      Parameters:
      ttl - the time to live for the socket.
      Throws:
      IOException
    • getMulticastTTL

      public int getMulticastTTL()
      Getter for current multicast TTL
      Returns:
    • getInfo

      public String getInfo()
      Specified by:
      getInfo in class TP
    • sendMulticast

      public void sendMulticast(byte[] data, int offset, int length) throws Exception
      Description copied from class: TP
      Send to all members in the group. UDP would use an IP multicast message, whereas TCP would send N messages, one for each member
      Specified by:
      sendMulticast in class TP
      Parameters:
      data - The data to be sent. This is not a copy, so don't modify it
      offset -
      length -
      Throws:
      Exception
    • sendUnicast

      public void sendUnicast(PhysicalAddress dest, byte[] data, int offset, int length) throws Exception
      Description copied from class: TP
      Send a unicast to 1 member. Note that the destination address is a *physical*, not a logical address
      Specified by:
      sendUnicast in class TP
      Parameters:
      dest - Must be a non-null unicast address
      data - The data to be sent. This is not a copy, so don't modify it
      offset -
      length -
      Throws:
      Exception
    • _send

      protected void _send(InetAddress dest, int port, boolean mcast, byte[] data, int offset, int length) throws Exception
      Throws:
      Exception
    • start

      public void start() throws Exception
      Creates the unicast and multicast sockets and starts the unicast and multicast receiver threads
      Overrides:
      start in class TP
      Throws:
      Exception - Thrown if protocol cannot be started successfully. This will cause the ProtocolStack to fail, so Channel.connect(String) will throw an exception
    • stop

      public void stop()
      Description copied from class: Protocol
      This method is called on a Channel.disconnect(). Stops work (e.g. by closing multicast socket). Will be called from top to bottom. This means that at the time of the method invocation the neighbor protocol below is still working. This method will replace the STOP, STOP_OK, CLEANUP and CLEANUP_OK events. The ProtocolStack guarantees that when this method is called all messages in the down queue will have been flushed
      Overrides:
      stop in class TP
    • destroy

      public void destroy()
      Description copied from class: Protocol
      This method is called on a Channel.close(). Does some cleanup; after the call the VM will terminate
      Overrides:
      destroy in class TP
    • handleConnect

      protected void handleConnect() throws Exception
      Overrides:
      handleConnect in class TP
      Throws:
      Exception
    • handleDisconnect

      protected void handleDisconnect()
      Overrides:
      handleDisconnect in class TP
    • createSockets

      protected void createSockets() throws Exception
      Create UDP sender and receiver sockets. Currently there are 2 sockets (sending and receiving). This is due to Linux's non-BSD compatibility in the JDK port (see DESIGN).
      Throws:
      Exception
    • destroySockets

      protected void destroySockets()
    • createLocalAddress

      protected IpAddress createLocalAddress()
    • getPhysicalAddress

      protected PhysicalAddress getPhysicalAddress()
      Specified by:
      getPhysicalAddress in class TP
    • bindToInterfaces

      protected void bindToInterfaces(List<NetworkInterface> interfaces, MulticastSocket s, InetAddress mcastAddr)
      Parameters:
      interfaces - List. Guaranteed to have no duplicates
      s -
      mcastAddr -
      Throws:
      IOException
    • createEphemeralDatagramSocket

      protected DatagramSocket createEphemeralDatagramSocket() throws SocketException
      Creates a DatagramSocket with a random port. Because in certain operating systems, ports are reused, we keep a list of the n last used ports, and avoid port reuse
      Throws:
      SocketException
    • createDatagramSocketWithBindPort

      protected DatagramSocket createDatagramSocketWithBindPort() throws Exception
      Creates a DatagramSocket when bind_port > 0. Attempts to allocate the socket with port == bind_port, and increments until it finds a valid port, or until port_range has been exceeded
      Returns:
      DatagramSocket The newly created socket
      Throws:
      Exception
    • dumpSocketInfo

      protected String dumpSocketInfo() throws Exception
      Throws:
      Exception
    • setBufferSize

      protected void setBufferSize(DatagramSocket sock, int send_buf_size, int recv_buf_size)
    • closeUnicastSocket

      protected void closeUnicastSocket()
    • handleConfigEvent

      protected void handleConfigEvent(Map<String,Object> map)