Class UDP

All Implemented Interfaces:
Lifecycle, DiagnosticsHandler.ProbeHandler

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 239.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 8.
  • 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.
  • 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), decimal 4
      • IPTOS_THROUGHPUT (0x08), decimal 8
      • IPTOS_LOWDELAY (0x10), decimal 16
    • UCAST_NAME

      protected static final String UCAST_NAME
      See Also:
    • MCAST_NAME

      protected static final String MCAST_NAME
      See Also:
    • 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
    • suppress_time_out_of_buffer_space

      protected long suppress_time_out_of_buffer_space
    • unicast_receiver_threads

      protected int unicast_receiver_threads
    • multicast_receiver_threads

      protected int multicast_receiver_threads
    • mcast_addr

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

      protected MulticastSocket 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_receivers

      protected UDP.PacketReceiver[] mcast_receivers
      Runnable to receive multicast packets
    • ucast_receivers

      protected UDP.PacketReceiver[] ucast_receivers
      Runnable to receive unicast packets
    • suppress_log_out_of_buffer_space

      protected SuppressLog<InetAddress> suppress_log_out_of_buffer_space
    • is_android

      protected static final boolean is_android
    • is_mac

      protected static final boolean is_mac
  • Constructor Details

    • UDP

      public UDP()
  • Method Details

    • supportsMulticasting

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

      public <T extends UDP> T setMulticasting(boolean fl)
    • setMulticastAddress

      public <T extends UDP> T setMulticastAddress(InetAddress a)
    • getMulticastAddress

      public InetAddress getMulticastAddress()
    • getMulticastPort

      public int getMulticastPort()
    • setMulticastPort

      public <T extends UDP> T setMulticastPort(int mcast_port)
    • getTos

      public int getTos()
    • setTos

      public UDP setTos(int t)
    • getMcastGroupAddr

      public InetAddress getMcastGroupAddr()
    • setMcastGroupAddr

      public UDP setMcastGroupAddr(InetAddress m)
    • ipMcast

      public boolean ipMcast()
    • ipMcast

      public UDP ipMcast(boolean i)
    • getIpTTL

      public int getIpTTL()
    • setIpTTL

      public UDP setIpTTL(int i)
    • getMcastSendBufSize

      public int getMcastSendBufSize()
    • setMcastSendBufSize

      public UDP setMcastSendBufSize(int m)
    • getMcastRecvBufSize

      public int getMcastRecvBufSize()
    • setMcastRecvBufSize

      public UDP setMcastRecvBufSize(int m)
    • getUcastSendBufSize

      public int getUcastSendBufSize()
    • setUcastSendBufSize

      public UDP setUcastSendBufSize(int u)
    • getUcastRecvBufSize

      public int getUcastRecvBufSize()
    • setUcastRecvBufSize

      public UDP setUcastRecvBufSize(int u)
    • disableLoopback

      public boolean disableLoopback()
    • disableLoopback

      public UDP disableLoopback(boolean d)
    • getSuppressTimeOutOfBufferSpace

      public long getSuppressTimeOutOfBufferSpace()
    • setSuppressTimeOutOfBufferSpace

      public UDP setSuppressTimeOutOfBufferSpace(long s)
    • setMulticastTTL

      public <T extends UDP> T setMulticastTTL(int ttl)
      Set the ttl for multicast socket
      Parameters:
      ttl - the time to live for the socket.
    • getMulticastTTL

      public int getMulticastTTL()
    • getDroppedMessages

      public int getDroppedMessages()
    • clearDroppedMessagesCache

      public <T extends UDP> T clearDroppedMessagesCache()
    • setUcastReceiverThreads

      public <T extends UDP> T setUcastReceiverThreads(int num)
    • getUcastReceiverThreads

      public int getUcastReceiverThreads()
    • setMcastReceiverThreads

      public <T extends UDP> T setMcastReceiverThreads(int num)
    • getMcastReceiverThreads

      public int getMcastReceiverThreads()
    • getInfo

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

      public void sendToAll(byte[] data, int offset, int length) throws Exception
      Description copied from class: TP
      Fetches the physical addrs for all mbrs and sends the msg to each physical address. Asks discovery for missing members' physical addresses if needed
      Overrides:
      sendToAll in class TP
      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 a 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
      Throws:
      Exception
    • _send

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

      public Object down(Event evt)
      Description copied from class: Protocol
      An event is to be sent down the stack. A protocol may want to examine its type and perform some action on it, depending on the event's type. If the event is a message MSG, then the protocol may need to add a header to it (or do nothing at all) before sending it down the stack using down_prot.down().
      Overrides:
      down in class TP
    • init

      public void init() throws Exception
      Description copied from class: Protocol
      Called after a protocol has been created and before the protocol is started. Attributes are already set. Other protocols are not yet connected and events cannot yet be sent.
      Specified by:
      init in interface Lifecycle
      Overrides:
      init in class TP
      Throws:
      Exception - Thrown if protocol cannot be initialized successfully. This will cause the ProtocolStack to fail, so the the channel constructor will throw an exception
    • start

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

      public void stop()
      Description copied from class: Protocol
      Called on a JChannel.disconnect(); stops work (e.g. by closing multicast socket). Will be called from top to bottom.
      Specified by:
      stop in interface Lifecycle
      Overrides:
      stop in class TP
    • handleConnect

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

      protected void setCorrectSocketBufferSize(MulticastSocket s, int buf_size, int new_size, boolean send, boolean mcast) throws SocketException
      Throws:
      SocketException
    • findMethod

      protected static Method findMethod(Class<?> clazz, String method_name, Class<?>... parameters)
    • createSockets

      protected void createSockets() throws Exception
      Creates the UDP sender and receiver sockets
      Throws:
      Exception
    • destroySockets

      protected void destroySockets()
    • createReceivers

      protected UDP.PacketReceiver[] createReceivers(int num, DatagramSocket sock, String name)
    • createLocalAddress

      protected IpAddress createLocalAddress()
    • setTimeToLive

      protected <T extends UDP> T setTimeToLive(int ttl, MulticastSocket s)
    • setNetworkInterface

      protected <T extends UDP> T setNetworkInterface(InetAddress addr, MulticastSocket s)
    • getPhysicalAddress

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

      protected void joinGroupOnInterfaces(List<NetworkInterface> interfaces, MulticastSocket s, InetAddress mcast_addr)
      Joins a multicast address on all interfaces
      Parameters:
      interfaces - The interfaces to join mcast_addr:mcast_port
      s - The MulticastSocket to join on
      mcast_addr - The multicast address to join
    • createMulticastSocketWithBindPort

      protected MulticastSocket createMulticastSocketWithBindPort() 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
    • createMulticastSocket

      protected MulticastSocket createMulticastSocket(String service_name, int port) throws Exception
      Throws:
      Exception
    • dumpSocketInfo

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

      void setBufferSizes() throws SocketException
      Throws:
      SocketException
    • setBufferSize

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

      protected static int getBufferSize(DatagramSocket s, boolean send)
    • closeMulticastSocket

      void closeMulticastSocket()
    • closeUnicastSocket

      protected void closeUnicastSocket()
    • startThreads

      protected void startThreads() throws Exception
      Throws:
      Exception
    • startUcastReceiverThreads

      protected void startUcastReceiverThreads()
    • startMcastReceiverThreads

      protected void startMcastReceiverThreads()
    • stopThreads

      protected void stopThreads()
    • stopUcastReceiverThreads

      protected void stopUcastReceiverThreads()
    • stopMcastReceiverThreads

      protected void stopMcastReceiverThreads()
    • handleConfigEvent

      protected void handleConfigEvent(Map<String,Object> map) throws SocketException
      Throws:
      SocketException