Class UdpIOProcessor

All Implemented Interfaces:
AutoCloseable, Runnable

public class UdpIOProcessor extends UdpBaseIOProcessor
Class to process UDP communication. Client communication means communication with one UDP socket, initiated by this instance. Server socket communication adds the ability to communicate with several UDP sockets, initiated by this socket or any other socket. Includes functionality for automatic data logging. Implementation is based on Java class DatagramChannel.
Keep in mind: To allow correct message processing, the implementation has been made as follows:
  • After open, before the first message has been received, getSource() will return 0.0.0.0:0.
  • After the first message has been received, getSource() will always return the address of the sender of the last read message. After calling flush(), getSource() will return 0.0.0.0:0 again.
  • In case of a multi-threaded application, ensure to synchronize read() and the corresponding getSource() call to ensure that the message will be assigned to the correct source.
  • Before sending the first message, setTarget() must be used to set the target IP and port.
  • If a response to an incoming message shall be sent, you can use setTarget(getSource) as long as no other message has been read.
  • The target set by setTarget() is valid as long as another target will be set. In case of a multi-threaded application, ensure to synchronize setTarget() and the corresponding write() call to ensure that the message sent by write receives the correct target.
  • Constructor Details

    • UdpIOProcessor

      public UdpIOProcessor(JposDevice device, int ownport) throws jpos.JposException
      Stores JposDevice of derived IO processors and own port number. The device will be used for logging. The port number is the local port to be used to communicate with other sockets. If zero, a random port will be used.
      Parameters:
      device - Device that uses the processor. Processor uses logging of device to produce logging entries.
      ownport - Local port to be used by this socket.
      Throws:
      jpos.JposException - Will not be thrown.
  • Method Details

    • setTarget

      public String setTarget(String addr) throws jpos.JposException
      Sets target address for further write operations.
      Overrides:
      setTarget in class UniqueIOProcessor
      Parameters:
      addr - communication target, e.g. 127.0.0.1:23456 for IPv4 and [12:34:56:78:9a:bc:de:f0]:23456 for IPv6.
      Returns:
      Target in its internal String representation.
      Throws:
      jpos.JposException - If an error occurs.
    • getTarget

      public String getTarget()
      Description copied from class: UniqueIOProcessor
      Retrieves currently set target for writing data.
      Overrides:
      getTarget in class UniqueIOProcessor
      Returns:
      Current target device, null if target has not been specified.
    • getSource

      public String getSource()
      Description copied from class: UniqueIOProcessor
      Gets source of previously read data.
      Overrides:
      getSource in class UniqueIOProcessor
      Returns:
      Data source, null before data have been read