Class UDP

All Implemented Interfaces:
Runnable

public class UDP extends TP implements Runnable
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.
Version:
$Id: UDP.java,v 1.156.2.16 2009/01/05 08:33:20 belaban Exp $
Author:
Bela Ban
  • Constructor Details

    • UDP

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

    • setProperties

      public boolean setProperties(Properties props)
      Setup the Protocol instance acording to the configuration string. 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 - Set the default time-to-live for multicast packets sent out on this socket. default is 32
      Overrides:
      setProperties in class TP
      Returns:
      true if no other properties are left. false if the properties still have data in them, ie , properties are left over and not handled by the protocol stack
    • setMcastPort

      public void setMcastPort(int p)
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • getInfo

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

      public void sendToAllMembers(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:
      sendToAllMembers in class TP
      Parameters:
      data - The data to be sent. This is not a copy, so don't modify it
      offset -
      length -
      Throws:
      Exception
    • sendToSingleMember

      public void sendToSingleMember(Address dest, 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:
      sendToSingleMember 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
    • postUnmarshalling

      public void postUnmarshalling(Message msg, Address dest, Address src, boolean multicast)
      Specified by:
      postUnmarshalling in class TP
    • postUnmarshallingList

      public void postUnmarshallingList(Message msg, Address dest, boolean multicast)
      Specified by:
      postUnmarshallingList in class TP
    • getName

      public String getName()
      Specified by:
      getName in class Protocol
    • 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
    • handleConnect

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

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

      protected Address createLocalAddress()
    • 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
    • setThreadNames

      protected void setThreadNames()
      Overrides:
      setThreadNames in class TP
    • unsetThreadNames

      protected void unsetThreadNames()
      Overrides:
      unsetThreadNames in class TP
    • handleConfigEvent

      protected void handleConfigEvent(Map<String,Object> map)
      Overrides:
      handleConfigEvent in class TP