Package cbsw

Class DatagramTxRx


  • public class DatagramTxRx
    extends java.lang.Object

    A class that encapsulates a datagram (UDP) connection.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) java.net.DatagramSocket datagramSocket  
      (package private) java.net.InetAddress inetAddress  
      (package private) int port  
      (package private) java.nio.ByteBuffer rxBB  
      (package private) byte[] rxBuffer  
      (package private) java.io.ByteArrayInputStream rxStream  
      (package private) java.nio.ByteBuffer txBuffer  
    • Constructor Summary

      Constructors 
      Constructor Description
      DatagramTxRx​(byte[] ipAddress, int port, int bufferSize)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the UDP socket.
      java.nio.ByteBuffer getRxByteBuffer()
      Gets a ByteBuffer that wraps the receive buffer.
      java.io.ByteArrayInputStream getRxInputStream()
      Gets a ByteArrayInputStream that wraps the receive buffer.
      static void main​(java.lang.String[] args)
      An example program that replies ACK_OK to any message.
      void makeServerSocket()
      Creates a server UDP datagram socket.
      void makeSocket()
      Creates a UDP datagram socket.
      void rx​(int timeout)
      Reads a message from the rx channel.
      void tx​(byte[] message)
      Writes a message to the tx channel.
      void tx​(java.nio.ByteBuffer message)
      Writes a message to the tx channel.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • rxBuffer

        byte[] rxBuffer
      • rxBB

        java.nio.ByteBuffer rxBB
      • rxStream

        java.io.ByteArrayInputStream rxStream
      • txBuffer

        java.nio.ByteBuffer txBuffer
      • inetAddress

        java.net.InetAddress inetAddress
      • port

        int port
      • datagramSocket

        java.net.DatagramSocket datagramSocket
    • Constructor Detail

      • DatagramTxRx

        public DatagramTxRx​(byte[] ipAddress,
                            int port,
                            int bufferSize)
    • Method Detail

      • getRxInputStream

        public java.io.ByteArrayInputStream getRxInputStream()

        Gets a ByteArrayInputStream that wraps the receive buffer.

        Returns:
        The ByteArrayInputStream
      • getRxByteBuffer

        public java.nio.ByteBuffer getRxByteBuffer()

        Gets a ByteBuffer that wraps the receive buffer.

        Returns:
        The ByteBuffer
      • rx

        public void rx​(int timeout)
                throws java.lang.Exception,
                       java.nio.channels.NotYetConnectedException,
                       java.net.SocketTimeoutException

        Reads a message from the rx channel.

        Parameters:
        timeout - Socket timeout in milliseconds. 0 for no timeout.
        Throws:
        java.lang.Exception - Catch-all other exceptions.
        java.net.SocketTimeoutException - Socket timeout.
        java.nio.channels.NotYetConnectedException - Socket not yet connected.
      • tx

        public void tx​(byte[] message)
                throws java.net.PortUnreachableException

        Writes a message to the tx channel.

        Parameters:
        message - The message byte array
        Throws:
        java.net.PortUnreachableException - Port unreachable.
        java.lang.RuntimeException - Catch-all other exceptions.
      • tx

        public void tx​(java.nio.ByteBuffer message)
                throws java.net.PortUnreachableException

        Writes a message to the tx channel.

        Parameters:
        message - A ByteBuffer containing the message
        Throws:
        java.net.PortUnreachableException - Port unreachable.
      • makeServerSocket

        public void makeServerSocket()

        Creates a server UDP datagram socket.

        Throws:
        java.lang.RuntimeException - Exception during open or bind.
      • makeSocket

        public void makeSocket()

        Creates a UDP datagram socket.

      • close

        public void close()

        Closes the UDP socket.

      • main

        public static void main​(java.lang.String[] args)

        An example program that replies ACK_OK to any message.

        Creates and reads a server socket. Responds with ACK_OK to all messages.

        Parameters:
        args - Command-line parameters