Class BaseChannel

java.lang.Object
java.util.Observable
org.jpos.iso.BaseChannel
All Implemented Interfaces:
Cloneable, Configurable, ExceptionHandlerAware, BaseChannelMBean, ClientChannel, FactoryChannel, FilteredChannel, ISOChannel, ISOSource, ServerChannel, ISOMsgMetrics.Source, LogSource
Direct Known Subclasses:
AmexChannel, ASCIIChannel, BASE24Channel, BASE24TCPChannel, BCDChannel, CMFChannel, CSChannel, GICCChannel, GZIPChannel, HEXChannel, LogChannel, NACChannel, NCCChannel, PADChannel, PostChannel, RawChannel, RBPChannel, TelnetXMLChannel, VAPChannel, X25Channel, XMLChannel

ISOChannel is an abstract class that provides functionality that allows the transmission and reception of ISO 8583 Messages over a TCP/IP session.

This class is not thread-safe.

ISOChannel is Observable in order to support GUI components such as ISOChannelPanel.
It now support the new Logger architecture so we will probably setup ISOChannelPanel to be a LogListener instead of being an Observer in future releases.

See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • setHost

      public void setHost(String host, int port)
      initialize an ISOChannel
      Specified by:
      setHost in interface ClientChannel
      Parameters:
      host - server TCP Address
      port - server port number
    • setLocalAddress

      public void setLocalAddress(String iface, int port)
      initialize an ISOChannel
      Parameters:
      iface - server TCP Address
      port - server port number
    • setHost

      public void setHost(String host)
      Description copied from interface: BaseChannelMBean
      Sets the remote host name or address.
      Specified by:
      setHost in interface BaseChannelMBean
      Parameters:
      host - to connect (client ISOChannel)
    • setPort

      public void setPort(int port)
      Description copied from interface: BaseChannelMBean
      Sets the remote port number.
      Specified by:
      setPort in interface BaseChannelMBean
      Parameters:
      port - to connect (client ISOChannel)
    • getHost

      public String getHost()
      Description copied from interface: ClientChannel
      Returns the remote host this client channel connects to.
      Specified by:
      getHost in interface BaseChannelMBean
      Specified by:
      getHost in interface ClientChannel
      Returns:
      hostname (may be null)
    • getPort

      public int getPort()
      Description copied from interface: ClientChannel
      Returns the remote port this client channel connects to.
      Specified by:
      getPort in interface BaseChannelMBean
      Specified by:
      getPort in interface ClientChannel
      Returns:
      port number
    • setPackager

      public void setPackager(ISOPackager p)
      set Packager for channel
      Specified by:
      setPackager in interface ISOChannel
      Parameters:
      p - an ISOPackager
      See Also:
    • getPackager

      Description copied from interface: ISOChannel
      Returns the packager used to pack/unpack ISO messages on this channel.
      Specified by:
      getPackager in interface ISOChannel
      Returns:
      current packager
    • setServerSocket

      public void setServerSocket(ServerSocket sock)
      Associates this ISOChannel with a server socket
      Parameters:
      sock - where to accept a connection
    • getExceptionHandlers

      Description copied from interface: ExceptionHandlerAware
      Returns the map of targeted exception handlers, keyed by exception class.
      Specified by:
      getExceptionHandlers in interface ExceptionHandlerAware
      Returns:
      A map of exception classes to exception handlers. These handlers only execute if the exception matches.
    • resetCounters

      public void resetCounters()
      reset stat info
    • getCounters

      public int[] getCounters()
      Returns the message counters array.
      Returns:
      counters
    • isConnected

      public boolean isConnected()
      Description copied from interface: ISOSource
      Indicates whether this source is currently connected and able to send messages.
      Specified by:
      isConnected in interface BaseChannelMBean
      Specified by:
      isConnected in interface ISOChannel
      Specified by:
      isConnected in interface ISOSource
      Returns:
      the connection state
    • setISOMsgMetrics

      public void setISOMsgMetrics(ISOMsgMetrics metrics)
      Description copied from interface: ISOMsgMetrics.Source
      Sets the ISOMsgMetrics instance.
      Specified by:
      setISOMsgMetrics in interface ISOMsgMetrics.Source
      Parameters:
      metrics - the ISOMsgMetrics to attach
    • getISOMsgMetrics

      Description copied from interface: ISOMsgMetrics.Source
      Returns the attached ISOMsgMetrics.
      Specified by:
      getISOMsgMetrics in interface ISOMsgMetrics.Source
      Returns:
      the ISOMsgMetrics, or null
    • connect

      protected void connect(Socket socket) throws IOException
      setup I/O Streams from socket
      Parameters:
      socket - a Socket (client or server)
      Throws:
      IOException - on error on I/O error on I/O error
    • postConnectHook

      protected void postConnectHook() throws IOException
      Hook called after a successful connection. May be overridden by subclasses.
      Throws:
      IOException - on I/O error
    • newSocket

      protected Socket newSocket(String host, int port) throws IOException
      factory method pattern (as suggested by Vincent.Greene@amo.com)
      Parameters:
      host - remote host
      port - remote port
      Returns:
      newly created socket
      Throws:
      IOException - on error Use Socket factory if exists. If it is missing create a normal socket
      See Also:
    • newSocket

      protected Socket newSocket(String[] hosts, int[] ports, LogEvent evt) throws IOException
      Creates a new socket connected to one of the given hosts/ports.
      Parameters:
      hosts - array of host names to try
      ports - corresponding array of port numbers
      evt - log event for connection diagnostics
      Returns:
      the connected socket
      Throws:
      IOException - on connection failure
    • getSocket

      public Socket getSocket()
      Returns the current connected socket.
      Returns:
      current socket
    • getServerSocket

      Returns the current server socket.
      Returns:
      current serverSocket
    • setTimeout

      public void setTimeout(int timeout) throws SocketException
      sets socket timeout (as suggested by Leonard Thomas <leonard@rhinosystemsinc.com>)
      Parameters:
      timeout - in milliseconds
      Throws:
      SocketException - on error
    • getTimeout

      public int getTimeout()
      Returns the socket read timeout in milliseconds.
      Returns:
      socket timeout in milliseconds
    • applyTimeout

      protected void applyTimeout() throws SocketException
      sets timeout, and also keep alive
      Throws:
      SocketException - on error setting the timeout
    • setSoLinger

      public void setSoLinger(boolean on, int linger)
      Socket SO_LINGER option to use when closing the socket.
      Parameters:
      on - if true, enable SO_LINGER
      linger - SO_LINGER timeout in seconds
      See Also:
    • isSoLingerOn

      public boolean isSoLingerOn()
      Returns whether SO_LINGER is enabled on the socket.
      Returns:
      true if SO_LINGER is enabled
    • getSoLingerSeconds

      public int getSoLingerSeconds()
      Returns the SO_LINGER timeout in seconds.
      Returns:
      SO_LINGER timeout in seconds
    • connect

      public void connect() throws IOException
      Connects client ISOChannel to server
      Specified by:
      connect in interface BaseChannelMBean
      Specified by:
      connect in interface ISOChannel
      Throws:
      IOException - on I/O error on I/O error on I/O error
    • accept

      public void accept(ServerSocket s) throws IOException
      Accepts connection
      Specified by:
      accept in interface ServerChannel
      Parameters:
      s - the server socket to accept on
      Throws:
      IOException - on I/O error on I/O error on I/O error
    • setUsable

      public void setUsable(boolean b)
      Description copied from interface: ISOChannel
      Marks this channel as usable or not; a non-usable channel will not accept messages.
      Specified by:
      setUsable in interface ISOChannel
      Parameters:
      b - - new Usable state (used by ISOMUX internals to flag as unusable in order to force a reconnection)
    • getDynamicPackager

      allow subclasses to override default packager on outgoing messages
      Parameters:
      m - outgoing ISOMsg
      Returns:
      ISOPackager
    • getDynamicPackager

      protected ISOPackager getDynamicPackager(byte[] image)
      allow subclasses to override default packager on incoming messages
      Parameters:
      image - incoming message image
      Returns:
      ISOPackager
    • getDynamicPackager

      protected ISOPackager getDynamicPackager(byte[] header, byte[] image)
      allow subclasses to override default packager on incoming messages
      Parameters:
      header - message header
      image - incoming message image
      Returns:
      ISOPackager
    • getDynamicHeader

      protected ISOHeader getDynamicHeader(byte[] image)
      Allow subclasses to override the Default header on incoming messages.
      Parameters:
      image - message image
      Returns:
      ISOHeader instance
    • sendMessageLength

      protected void sendMessageLength(int len) throws IOException
      Sends the message length prefix; default implementation is a no-op.
      Parameters:
      len - the message length to send
      Throws:
      IOException - on I/O error
    • sendMessageHeader

      protected void sendMessageHeader(ISOMsg m, int len) throws IOException
      Sends the message header bytes.
      Parameters:
      m - the ISO message being sent
      len - the message length
      Throws:
      IOException - on I/O error
    • sendMessageTrailler

      @Deprecated protected void sendMessageTrailler(ISOMsg m, int len) throws IOException
      Deprecated.
      use sendMessageTrailer(ISOMsg m, byte[] b) instead.
      Parameters:
      m - a reference to the ISOMsg
      len - the packed image length
      Throws:
      IOException - on error
    • sendMessageTrailler

      @Deprecated protected void sendMessageTrailler(ISOMsg m, byte[] b) throws IOException
      Deprecated.
      Parameters:
      m - the ISO message being sent
      b - the trailer bytes to send
      Throws:
      IOException - on I/O error
    • sendMessageTrailer

      protected void sendMessageTrailer(ISOMsg m, byte[] b) throws IOException
      Sends the message trailer bytes.
      Parameters:
      m - the unpacked ISO message
      b - the packed message image
      Throws:
      IOException - on I/O error
    • getMessageTrailler

      @Deprecated protected void getMessageTrailler() throws IOException
      Deprecated.
      Throws:
      IOException - on I/O error
    • getMessageTrailer

      protected void getMessageTrailer(ISOMsg m) throws IOException
      Reads the message trailer from the stream and optionally stores it in the given message.
      Parameters:
      m - the ISOMessage to store the trailer data in
      Throws:
      IOException - on I/O error
      See Also:
    • getMessage

      protected void getMessage(byte[] b, int offset, int len) throws IOException, ISOException
      Reads raw message bytes from the stream into the given buffer.
      Parameters:
      b - the destination buffer
      offset - starting offset in the buffer
      len - number of bytes to read
      Throws:
      IOException - on I/O error
      ISOException - on protocol error
    • getMessageLength

      protected int getMessageLength() throws IOException, ISOException
      Reads and returns the message length prefix from the stream.
      Returns:
      the message length
      Throws:
      IOException - on I/O error
      ISOException - on protocol error
    • getHeaderLength

      protected int getHeaderLength()
      Returns the fixed header length for this channel.
      Returns:
      header length in bytes
    • getHeaderLength

      protected int getHeaderLength(byte[] b)
      Returns the header length for the given raw message bytes.
      Parameters:
      b - the raw message bytes
      Returns:
      header length in bytes
    • getHeaderLength

      protected int getHeaderLength(ISOMsg m)
      Returns the header length for the given ISO message.
      Parameters:
      m - the ISO message
      Returns:
      header length in bytes
    • streamReceive

      protected byte[] streamReceive() throws IOException
      Reads raw bytes from the stream for protocols without explicit length framing.
      Returns:
      the received bytes
      Throws:
      IOException - on I/O error
    • sendMessage

      protected void sendMessage(byte[] b, int offset, int len) throws IOException
      Writes the given bytes to the output stream.
      Parameters:
      b - the byte array to send
      offset - starting offset
      len - number of bytes to send
      Throws:
      IOException - on I/O error
    • send

      public void send(ISOMsg m) throws IOException, ISOException
      Sends the specified ISOMsg over this ISOChannel.

      This method performs the following steps:

      • Verifies the channel is connected.
      • Sets the message direction to ISOMsg.OUTGOING.
      • Retrieves and sets a dynamic packager for the message.
      • Applies all registered outgoing filters, allowing them to modify or veto the message.
      • Packs the message and writes its length, header, body, and trailer to the underlying stream, protected by a locking mechanism to ensure thread safety.
      • Flushes the output stream and increments message counters.
      • Notifies observers of the sent message.
      • Logs both the message and the send operation through ChannelEvent and LogEvent.
      If a ISOFilter.VetoException is thrown by a filter, the message is not sent, and the exception is logged.
      Specified by:
      send in interface ISOChannel
      Specified by:
      send in interface ISOSource
      Parameters:
      m - the ISO message to be sent. The message will be modified in-place: its direction and packager will be updated, and filters may alter its content.
      Throws:
      IOException - if the channel is not connected, if the output stream fails, if locking times out, or if an unexpected I/O error occurs.
      ISOException - if packing the message fails or other ISO-specific issues occur.
      ISOFilter.VetoException - if an outgoing filter vetoes the message.
      See Also:
    • send

      public void send(byte[] b) throws IOException, ISOException
      sends a byte[] over the TCP/IP session
      Specified by:
      send in interface ISOChannel
      Parameters:
      b - the byte array to be sent
      Throws:
      IOException - on I/O error on I/O error on I/O error
      ISOException - on ISO processing error
      ISOFilter.VetoException - if a filter vetoes the message
    • sendKeepAlive

      public void sendKeepAlive() throws IOException
      Sends a high-level keep-alive message (zero length)
      Throws:
      IOException - on exception
    • isExpectKeepAlive

      public boolean isExpectKeepAlive()
      Returns whether this channel expects keep-alive messages.
      Returns:
      true if keep-alive is expected
    • isRejected

      protected boolean isRejected(byte[] b)
      Returns whether the given raw message should be rejected.
      Parameters:
      b - the raw message bytes
      Returns:
      true if the message should be rejected
    • shouldIgnore

      protected boolean shouldIgnore(byte[] b)
      Returns whether the given raw message should be silently ignored.
      Parameters:
      b - the raw message bytes
      Returns:
      true if the message should be ignored
    • createMsg

      protected ISOMsg createMsg()
      support old factory method name for backward compatibility
      Returns:
      newly created ISOMsg
    • createISOMsg

      protected ISOMsg createISOMsg()
      Creates a new ISOMsg instance; may be overridden to return a subclass.
      Returns:
      a new ISOMsg
    • readHeader

      protected byte[] readHeader(int hLen) throws IOException
      Reads in a message header.
      Parameters:
      hLen - The Length og the reader to read
      Returns:
      The header bytes that were read in
      Throws:
      IOException - on error
    • receive

      Waits and receive an ISOMsg over the TCP/IP session
      Specified by:
      receive in interface ISOChannel
      Returns:
      the Message received
      Throws:
      IOException - on I/O error
      ISOException - on ISO processing error
    • getBytes

      public int getBytes(byte[] b) throws IOException
      Low level receive
      Parameters:
      b - byte array
      Returns:
      the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
      Throws:
      IOException - on error
    • disconnect

      public void disconnect() throws IOException
      disconnects the TCP/IP session. The instance is ready for a reconnection. There is no need to create a new ISOChannel
      Specified by:
      disconnect in interface BaseChannelMBean
      Specified by:
      disconnect in interface ISOChannel
      Throws:
      IOException - on I/O error on I/O error on I/O error
    • reconnect

      public void reconnect() throws IOException
      Issues a disconnect followed by a connect
      Specified by:
      reconnect in interface BaseChannelMBean
      Specified by:
      reconnect in interface ISOChannel
      Throws:
      IOException - on I/O error on I/O error on I/O error
    • setLogger

      public void setLogger(Logger logger, String realm)
      Description copied from interface: LogSource
      Attaches a Logger and realm to this log source.
      Specified by:
      setLogger in interface LogSource
      Parameters:
      logger - the logger to use
      realm - the log realm (diagnostic label)
    • getRealm

      public String getRealm()
      Description copied from interface: LogSource
      Returns the log realm associated with this source.
      Specified by:
      getRealm in interface LogSource
      Returns:
      log realm
    • getLogger

      public Logger getLogger()
      Description copied from interface: LogSource
      Returns the Logger associated with this source.
      Specified by:
      getLogger in interface LogSource
      Returns:
      the Logger
    • toEndpoint

      protected String toEndpoint(Socket socket)
      Returns a human-readable endpoint string for the given socket.
      Parameters:
      socket - the connected socket
      Returns:
      endpoint string in the form host:port
    • getOriginalRealm

      Returns the original realm before any override was applied.
      Returns:
      the original realm string
    • setName

      public void setName(String name)
      associates this ISOChannel with a name using NameRegistrar
      Specified by:
      setName in interface ISOChannel
      Parameters:
      name - name to register
      See Also:
    • getName

      public String getName()
      Description copied from interface: ISOChannel
      Returns this channel's registered name.
      Specified by:
      getName in interface ISOChannel
      Returns:
      this ISOChannel's name ("" if no name was set)
    • addFilter

      public void addFilter(ISOFilter filter, int direction)
      Adds a filter to this channel.
      Parameters:
      filter - filter to add
      direction - ISOMsg.INCOMING, ISOMsg.OUTGOING, 0 for both
    • addIncomingFilter

      public void addIncomingFilter(ISOFilter filter)
      Description copied from interface: FilteredChannel
      Adds a filter to the incoming filter chain.
      Specified by:
      addIncomingFilter in interface FilteredChannel
      Parameters:
      filter - incoming filter to add
    • addOutgoingFilter

      public void addOutgoingFilter(ISOFilter filter)
      Description copied from interface: FilteredChannel
      Adds a filter to the outgoing filter chain.
      Specified by:
      addOutgoingFilter in interface FilteredChannel
      Parameters:
      filter - outgoing filter to add
    • addFilter

      public void addFilter(ISOFilter filter)
      Adds a filter to this channel.
      Specified by:
      addFilter in interface FilteredChannel
      Parameters:
      filter - filter to add (both directions, incoming/outgoing)
    • removeFilter

      public void removeFilter(ISOFilter filter, int direction)
      Removes a filter from this channel.
      Parameters:
      filter - filter to remove
      direction - ISOMsg.INCOMING, ISOMsg.OUTGOING, 0 for both
    • removeFilter

      public void removeFilter(ISOFilter filter)
      Removes a filter from this channel.
      Specified by:
      removeFilter in interface FilteredChannel
      Parameters:
      filter - filter to remove (both directions)
    • removeIncomingFilter

      public void removeIncomingFilter(ISOFilter filter)
      Description copied from interface: FilteredChannel
      Removes a filter from the incoming filter chain.
      Specified by:
      removeIncomingFilter in interface FilteredChannel
      Parameters:
      filter - incoming filter to remove
    • removeOutgoingFilter

      public void removeOutgoingFilter(ISOFilter filter)
      Description copied from interface: FilteredChannel
      Removes a filter from the outgoing filter chain.
      Specified by:
      removeOutgoingFilter in interface FilteredChannel
      Parameters:
      filter - outgoing filter to remove
    • applyOutgoingFilters

      Applies all registered outgoing filters to the given message.
      Parameters:
      m - the message to filter
      evt - the log event for diagnostics
      Returns:
      the filtered message
      Throws:
      ISOFilter.VetoException - if a filter vetoes the message
    • applyIncomingFilters

      Applies all registered incoming filters to the given message.
      Parameters:
      m - the message to filter
      evt - the log event for diagnostics
      Returns:
      the filtered message
      Throws:
      ISOFilter.VetoException - if a filter vetoes the message
    • applyIncomingFilters

      protected ISOMsg applyIncomingFilters(ISOMsg m, byte[] header, byte[] image, LogEvent evt) throws ISOFilter.VetoException
      Applies all registered incoming filters with the raw message header and image.
      Parameters:
      m - the decoded message
      header - the message header bytes
      image - the raw message bytes
      evt - the log event for diagnostics
      Returns:
      the filtered message
      Throws:
      ISOFilter.VetoException - if a filter vetoes the message
    • unpack

      protected void unpack(ISOMsg m, byte[] b) throws ISOException
      Unpacks raw bytes into the given ISOMsg using this channel's packager.
      Parameters:
      m - the message to populate
      b - the raw bytes to unpack
      Throws:
      ISOException - on unpack error
    • pack

      protected byte[] pack(ISOMsg m) throws ISOException
      Packs the given ISOMsg into raw bytes using this channel's packager.
      Parameters:
      m - the message to pack
      Returns:
      the packed byte array
      Throws:
      ISOException - on pack error
    • setConfiguration

      Implements Configurable
      Properties:
      • host - destination host (if ClientChannel)
      • port - port number (if ClientChannel)
      • local-iface - local interfase to use (if ClientChannel)
      • local-port - local port to bind (if ClientChannel)
      (host not present indicates a ServerChannel)
      Specified by:
      setConfiguration in interface Configurable
      Parameters:
      cfg - Configuration
      Throws:
      ConfigurationException - if configuration is invalid
    • getConfiguration

      Returns the current configuration for this channel.
      Returns:
      the current Configuration
    • getIncomingFilters

      Description copied from interface: FilteredChannel
      Returns all filters in the incoming filter chain.
      Specified by:
      getIncomingFilters in interface FilteredChannel
      Returns:
      Collection containing all incoming filters
    • getOutgoingFilters

      Description copied from interface: FilteredChannel
      Returns all filters in the outgoing filter chain.
      Specified by:
      getOutgoingFilters in interface FilteredChannel
      Returns:
      Collection containing all outgoing filters
    • setIncomingFilters

      public void setIncomingFilters(Collection filters)
      Description copied from interface: FilteredChannel
      Replaces the entire incoming filter chain with the given collection.
      Specified by:
      setIncomingFilters in interface FilteredChannel
      Parameters:
      filters - incoming filter set
    • setOutgoingFilters

      public void setOutgoingFilters(Collection filters)
      Description copied from interface: FilteredChannel
      Replaces the entire outgoing filter chain with the given collection.
      Specified by:
      setOutgoingFilters in interface FilteredChannel
      Parameters:
      filters - outgoing filter set
    • setHeader

      public void setHeader(byte[] header)
      Sets the message header bytes for this channel.
      Parameters:
      header - the header bytes
    • setHeader

      public void setHeader(String header)
      Sets the message header from a hex string.
      Parameters:
      header - hex-encoded header string
    • getHeader

      public byte[] getHeader()
      Returns the message header bytes.
      Returns:
      the header bytes, or null if not set
    • setOverrideHeader

      public void setOverrideHeader(boolean overrideHeader)
      Controls whether this channel's own header overrides the message header.
      Parameters:
      overrideHeader - if true, the channel header takes precedence
    • isOverrideHeader

      public boolean isOverrideHeader()
      Returns whether the channel header overrides the message header.
      Returns:
      true if override is active
    • getChannel

      Retrieves a channel instance by name from the NameRegistrar.
      Parameters:
      name - the Channel's name (without the "channel." prefix)
      Returns:
      ISOChannel instance with given name.
      Throws:
      NameRegistrar.NotFoundException - if no channel with that name is registered
      See Also:
    • getSocketFactory

      Gets the ISOClientSocketFactory (may be null)
      Returns:
      ISOClientSocketFactory
      Since:
      1.3.3 \
      See Also:
    • setSocketFactory

      public void setSocketFactory(ISOClientSocketFactory socketFactory)
      Sets the specified Socket Factory to create sockets
      Specified by:
      setSocketFactory in interface FactoryChannel
      Parameters:
      socketFactory - the ISOClientSocketFactory
      Since:
      1.3.3
      See Also:
    • getMaxPacketLength

      public int getMaxPacketLength()
      Returns the maximum packet length accepted by this channel.
      Returns:
      maximum packet length in bytes
    • setMaxPacketLength

      public void setMaxPacketLength(int maxPacketLength)
      Sets the maximum packet length accepted by this channel.
      Parameters:
      maxPacketLength - maximum packet length in bytes
    • closeSocket

      protected void closeSocket() throws IOException
      Closes the underlying socket.
      Throws:
      IOException - on I/O error
    • clone

      public Object clone()
      Description copied from interface: ISOChannel
      Returns a clone of this channel.
      Specified by:
      clone in interface ISOChannel
      Overrides:
      clone in class Object
      Returns:
      a deep copy of this ISOChannel
    • incrementMsgInCounter

      protected void incrementMsgInCounter(ISOMsg m) throws ISOException
      Increments the incoming message counter for the given message.
      Parameters:
      m - the received message
      Throws:
      ISOException - on error
    • incrementMsgOutCounter

      protected void incrementMsgOutCounter(ISOMsg m) throws ISOException
      Increments the outgoing message counter for the given message.
      Parameters:
      m - the sent message
      Throws:
      ISOException - on error