Class UniqueIOProcessor

java.lang.Object
de.gmxhome.conrad.jpos.jpos_base.UniqueIOProcessor
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
SerialIOProcessor, TcpClientIOProcessor, UdpBaseIOProcessor

public class UniqueIOProcessor extends Object implements AutoCloseable
Unique implementation for IO processing. Derived classes should add support for serial (RS232), TCP and other IO operations
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    Device used for logging
    protected String
    Initial communication target, used to check whether source (read) or target (write) shall be logged in read or write methods.
    static int
    Extended error code for JposExceptions generated by IO processors.
    byte[]
    Buffer with data to be logged in case of normal processing.
    protected String
    Logging prefix.
    protected int
    Logging type, one of LoggingTypeHexString, LoggingTypeEscapeString or LoggingTypeNoLogging.
    static int
    Logging type constant for excape-string style logging.
    static int
    Logging type constant for hex-dump-style logging.
    static int
    Logging type constant for no data logging.
    protected String
    Communication port of the processor.
    protected String
    Communication source, used for reading data.
    protected String
    Communication target, used for writing data.
    protected int
    Read timeout in milliseconds.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Stores JposDevice and port of derived IO processors.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Retrieves no.
    void
    Finished any communication.
    void
    Empties input and output buffer
    Gets source of previously read data.
    Retrieves currently set target for writing data.
    protected String
    location​(boolean from)
    Generates string to be inserted into logging message whenever the source or target port does not match the initial port.
    int
    logerror​(String what, int error, Exception ex)
    Generate a logging error message and throw an JposException.
    int
    logerror​(String what, int error, String why)
    Generate a logging error message and throw the corresponding JposException.
    int
    logerror​(String what, jpos.JposException ex)
    Generate a logging error message and throw an JposException.
    void
    open​(boolean noErrorLog)
    Opens processor for communication to specific source / target.
    byte[]
    read​(int count)
    Reads a frame of given maximum byte length from communication source.
    int
    setLoggingType​(int type)
    Sets the logging type for data logging.
    setTarget​(String target)
    Sets communication target.
    int
    setTimeout​(int timeout)
    Sets read timeout
    toLogString​(byte[] buffer)
    Generate log string from byte buffer
    int
    write​(byte[] buffer)
    Write a frame to the communication target.

    Methods inherited from class java.lang.Object

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

    • Source

      protected String Source
      Communication source, used for reading data. For RS232, Source, Target and Port are the same. In case of network connections, they can be different.
    • IOProcessorError

      public static final int IOProcessorError
      Extended error code for JposExceptions generated by IO processors.
      See Also:
      Constant Field Values
    • Target

      protected String Target
      Communication target, used for writing data. For RS232, Source, Target and Port are the same. In case of network connections, they can be different.
    • InitialPort

      protected String InitialPort
      Initial communication target, used to check whether source (read) or target (write) shall be logged in read or write methods.
    • Port

      protected String Port
      Communication port of the processor. Can be the name of a COM port in case of RS232 or the (own) port number in case of TCP or UDP.
    • LoggingPrefix

      protected String LoggingPrefix
      Logging prefix. String that will be used as prefix for all logging messages. Default: Port + ": "
    • Timeout

      protected int Timeout
      Read timeout in milliseconds. Default: 0x10000000 (more than 8 years)
    • LoggingType

      protected int LoggingType
      Logging type, one of LoggingTypeHexString, LoggingTypeEscapeString or LoggingTypeNoLogging. Default LoggingTypeEscapeString.
    • LoggingTypeHexString

      public static final int LoggingTypeHexString
      Logging type constant for hex-dump-style logging. If used, data will be logged as pairs of two-digit hexadecimal digits, e.g. "Hello\n" as 48 65 62 62 6F 0A.
      See Also:
      Constant Field Values
    • LoggingTypeEscapeString

      public static final int LoggingTypeEscapeString
      Logging type constant for excape-string style logging. If used, 7-bit ASCII letters will be logged "as is", while all other values will be logged as 3-letter octal triples with leading backslash. The backslash will be duplicated, e.g. "Hello\n" as Hello\012
      See Also:
      Constant Field Values
    • LoggingTypeNoLogging

      public static final int LoggingTypeNoLogging
      Logging type constant for no data logging. Data will be replaced by ...
      See Also:
      Constant Field Values
    • Dev

      public JposDevice Dev
      Device used for logging
    • LoggingData

      public byte[] LoggingData
      Buffer with data to be logged in case of normal processing. In case of exceptional processing, specific messages must be generated by classes.
  • Constructor Details

    • UniqueIOProcessor

      public UniqueIOProcessor(JposDevice dev, String port) throws jpos.JposException
      Stores JposDevice and port of derived IO processors. The device will be used for logging while the port specifies the communication object.
      Parameters:
      dev - Device that uses the processor. Processor uses logging of device to produce logging entries
      port - Communication object, e.g. COM3 or 127.0.0.1:23456
      Throws:
      jpos.JposException - If port does not specify a valid communication object
  • Method Details

    • getSource

      public String getSource()
      Gets source of previously read data.
      Returns:
      Data source, null before data have been read
    • getTarget

      public String getTarget()
      Retrieves currently set target for writing data.
      Returns:
      Current target device, null if target has not been specified.
    • setTarget

      public String setTarget(String target) throws jpos.JposException
      Sets communication target. Depending on the IO processor type, setting communication target can be mandatory before calling method open.
      Parameters:
      target - communication target
      Returns:
      target
      Throws:
      jpos.JposException - if target is not a valid communication target.
    • setTimeout

      public int setTimeout(int timeout)
      Sets read timeout
      Parameters:
      timeout - Timeout in milliseconds
      Returns:
      timeout
    • setLoggingType

      public int setLoggingType(int type)
      Sets the logging type for data logging.
      Parameters:
      type - One of the LoggingType constants.
      Returns:
      type
    • toLogString

      public String toLogString(byte[] buffer)
      Generate log string from byte buffer
      Parameters:
      buffer - byte array containing input or output data
      Returns:
      String corresponding to previously specified logging type
    • write

      public int write(byte[] buffer) throws jpos.JposException
      Write a frame to the communication target. If called from a derived class, the byte array must be passed through unchanged for correct logging.
      Parameters:
      buffer - byte buffer containing the frame
      Returns:
      Number of bytes written to the target
      Throws:
      jpos.JposException - if something goes wrong
    • available

      public int available() throws jpos.JposException
      Retrieves no. of readable units from communication source. If called from a derived class, LoggingData must be filled with the count to be returned.
      Returns:
      Unit count
      Throws:
      jpos.JposException - if something goes wrong
    • read

      public byte[] read(int count) throws jpos.JposException
      Reads a frame of given maximum byte length from communication source. If called from a derived class, LoggingData must be filled with the byte array to be returned. If LoggingData is longer than the maximum length, the remaining bytes will be discarded.
      Parameters:
      count - Maximum no. of bytes to be read
      Returns:
      byte[] containing received bytes. In case of timeout, less than count bytes will be returned, in extreme case, the array has length zero.
      Throws:
      jpos.JposException - if something goes wrong
    • location

      protected String location(boolean from)
      Generates string to be inserted into logging message whenever the source or target port does not match the initial port.
      Parameters:
      from - True in case of reading date, false in case of writing data.
      Returns:
      enpty string if current port matches initial port, " from getSource()" or " to getTarget()" otherwise.
    • flush

      public void flush() throws jpos.JposException
      Empties input and output buffer
      Throws:
      jpos.JposException - Iif something goes wrong
    • open

      public void open(boolean noErrorLog) throws jpos.JposException
      Opens processor for communication to specific source / target. Communication parameters must be set previously by specific setup method(s).
      Parameters:
      noErrorLog - if set, no logging occurs in error case to avoid a flood of error messages.
      Throws:
      jpos.JposException - if an IO error occurs
    • close

      public void close() throws jpos.JposException
      Finished any communication.
      Specified by:
      close in interface AutoCloseable
      Throws:
      jpos.JposException - If an IO error occurs
    • logerror

      public int logerror(String what, int error, String why) throws jpos.JposException
      Generate a logging error message and throw the corresponding JposException.
      Parameters:
      what - Describes witch operation falied (e.g. "Read", "Available"...)
      error - The Jpos error constant
      why - The error message.
      Returns:
      Function never returns, it throws an exception always.
      Throws:
      jpos.JposException - The exception. Its message will be set to why.
    • logerror

      public int logerror(String what, int error, Exception ex) throws jpos.JposException
      Generate a logging error message and throw an JposException.
      Parameters:
      what - Describes witch operation falied (e.g. "Read", "Available"...)
      error - The Jpos error constant
      ex - JposException that lead to the error.
      Returns:
      Function never returns, it throws an exception always.
      Throws:
      jpos.JposException - The exception. Its message will be set to ex.getMessage().
    • logerror

      public int logerror(String what, jpos.JposException ex) throws jpos.JposException
      Generate a logging error message and throw an JposException.
      Parameters:
      what - Describes witch operation falied (e.g. "Read", "Available"...)
      ex - JposException that lead to the error.
      Returns:
      Function never returns, it throws an exception always.
      Throws:
      jpos.JposException - The exception. Its message will be set to ex.getMessage().