Class CMFChannel
java.lang.Object
java.util.Observable
org.jpos.iso.BaseChannel
org.jpos.iso.channel.CMFChannel
- All Implemented Interfaces:
Cloneable, Configurable, ExceptionHandlerAware, BaseChannelMBean, ClientChannel, FactoryChannel, FilteredChannel, ISOChannel, ISOSource, ServerChannel, ISOMsgMetrics.Source, LogSource
ISOChannel implementation for the jPOS-CMF framing.
This channel uses a 3-byte big-endian length prefix (24-bit unsigned) and no
additional message header.
Keep-alive handling
A zero-length frame is treated as a keep-alive. When a keep-alive is received and eitherreplyKeepAlive is enabled or BaseChannel.isExpectKeepAlive() returns true, the channel
echoes the keep-alive back to the peer.
Configuration
reply-keepalive(boolean, defaulttrue): Whether to echo a received keep-alive (zero-length frame) back to the peer.max-keepalives-in-a-row(integer, default0): Maximum number of consecutive keep-alives allowed while waiting for a non-zero length frame. A value of0disables the limit (unlimited), preserving legacy behavior.
- Since:
- 3.0.1
- See Also:
-
Field Summary
Fields inherited from class BaseChannel
cnt, header, incomingFilters, logger, originalRealm, outgoingFilters, overrideHeader, packager, realm, serverIn, serverInLock, serverOut, serverOutLock, serverSocket, socketFactory, usableModifier and TypeFieldDescriptionprotected int[]Message counters array.protected byte[]Message header bytes.Incoming and outgoing filter chains.protected LoggerThe logger for this channel.protected StringThe original realm before any override.Incoming and outgoing filter chains.protected booleanWhen true, this channel's own header overrides the message's header.protected ISOPackagerThe packager used to pack/unpack ISO messages.protected StringThe realm string for this channel.protected DataInputStreamInput stream for reading from the connected socket.protected LockLock guardingBaseChannel.serverInand read operations.protected DataOutputStreamOutput stream for writing to the connected socket.protected LockLock guardingBaseChannel.serverOutand write operations.protected ServerSocketServer socket used when this channel acts as a server.protected ISOClientSocketFactoryOptional factory for creating client sockets.protected booleanWhether this channel is in a usable state.Fields inherited from interface ISOChannel
CONNECT, RX, SIZEOF_CNT, TXModifier and TypeFieldDescriptionstatic final intCounter index for connection events.static final intCounter index for received messages.static final intNumber of counters maintained by this channel.static final intCounter index for transmitted messages. -
Constructor Summary
ConstructorsConstructorDescriptionCreates an unconfiguredCMFChannel.CMFChannel(String host, int port, ISOPackager p) Constructs a clientCMFChannel.Constructs a serverCMFChannel.CMFChannel(ISOPackager p, ServerSocket serverSocket) Constructs a serverCMFChannelassociated with an existingServerSocket. -
Method Summary
Modifier and TypeMethodDescriptionprotected intReturns the channel header length.intReturns the maximum packet length supported by this channel.protected intReads the CMF message length prefix.protected voidsendMessageHeader(ISOMsg m, int len) Sends the message header (no-op).protected voidsendMessageLength(int len) Sends the CMF message length prefix.voidApplies configuration to this channel.Methods inherited from class BaseChannel
accept, addFilter, addFilter, addIncomingFilter, addOutgoingFilter, applyIncomingFilters, applyIncomingFilters, applyOutgoingFilters, applyTimeout, clone, closeSocket, connect, connect, createISOMsg, createMsg, disconnect, getBytes, getChannel, getConfiguration, getCounters, getDynamicHeader, getDynamicPackager, getDynamicPackager, getDynamicPackager, getExceptionHandlers, getHeader, getHeaderLength, getHeaderLength, getHost, getIncomingFilters, getISOMsgMetrics, getLogger, getMessage, getMessageTrailer, getMessageTrailler, getName, getOriginalRealm, getOutgoingFilters, getPackager, getPort, getRealm, getServerSocket, getSocket, getSocketFactory, getSoLingerSeconds, getTimeout, incrementMsgInCounter, incrementMsgOutCounter, isConnected, isExpectKeepAlive, isOverrideHeader, isRejected, isSoLingerOn, newSocket, newSocket, pack, postConnectHook, readHeader, receive, reconnect, removeFilter, removeFilter, removeIncomingFilter, removeOutgoingFilter, resetCounters, send, send, sendKeepAlive, sendMessage, sendMessageTrailer, sendMessageTrailler, sendMessageTrailler, setHeader, setHeader, setHost, setHost, setIncomingFilters, setISOMsgMetrics, setLocalAddress, setLogger, setMaxPacketLength, setName, setOutgoingFilters, setOverrideHeader, setPackager, setPort, setServerSocket, setSocketFactory, setSoLinger, setTimeout, setUsable, shouldIgnore, streamReceive, toEndpoint, unpackModifier and TypeMethodDescriptionvoidAccepts connectionvoidAdds a filter to this channel.voidAdds a filter to this channel.voidaddIncomingFilter(ISOFilter filter) Adds a filter to the incoming filter chain.voidaddOutgoingFilter(ISOFilter filter) Adds a filter to the outgoing filter chain.protected ISOMsgapplyIncomingFilters(ISOMsg m, byte[] header, byte[] image, LogEvent evt) Applies all registered incoming filters with the raw message header and image.protected ISOMsgapplyIncomingFilters(ISOMsg m, LogEvent evt) Applies all registered incoming filters to the given message.protected ISOMsgapplyOutgoingFilters(ISOMsg m, LogEvent evt) Applies all registered outgoing filters to the given message.protected voidsets timeout, and also keep aliveclone()Returns a clone of this channel.protected voidCloses the underlying socket.voidconnect()Connects client ISOChannel to serverprotected voidsetup I/O Streams from socketprotected ISOMsgCreates a new ISOMsg instance; may be overridden to return a subclass.protected ISOMsgsupport old factory method name for backward compatibilityvoiddisconnects the TCP/IP session.intgetBytes(byte[] b) Low level receivestatic ISOChannelgetChannel(String name) Retrieves a channel instance by name from the NameRegistrar.Returns the current configuration for this channel.int[]Returns the message counters array.protected ISOHeadergetDynamicHeader(byte[] image) Allow subclasses to override the Default header on incoming messages.protected ISOPackagergetDynamicPackager(byte[] image) allow subclasses to override default packager on incoming messagesprotected ISOPackagergetDynamicPackager(byte[] header, byte[] image) allow subclasses to override default packager on incoming messagesprotected ISOPackagerallow subclasses to override default packager on outgoing messagesMap<Class<? extends Exception>, List<ExceptionHandler>> Returns the map of targeted exception handlers, keyed by exception class.byte[]Returns the message header bytes.protected intgetHeaderLength(byte[] b) Returns the header length for the given raw message bytes.protected intReturns the header length for the given ISO message.getHost()Returns the remote host this client channel connects to.Returns all filters in the incoming filter chain.Returns the attached ISOMsgMetrics.Returns the Logger associated with this source.protected voidgetMessage(byte[] b, int offset, int len) Reads raw message bytes from the stream into the given buffer.protected voidReads the message trailer from the stream and optionally stores it in the given message.protected voidDeprecated.getName()Returns this channel's registered name.Returns the original realm before any override was applied.Returns all filters in the outgoing filter chain.Returns the packager used to pack/unpack ISO messages on this channel.intgetPort()Returns the remote port this client channel connects to.getRealm()Returns the log realm associated with this source.Returns the current server socket.Returns the current connected socket.Gets the ISOClientSocketFactory (may be null)intReturns the SO_LINGER timeout in seconds.intReturns the socket read timeout in milliseconds.protected voidIncrements the incoming message counter for the given message.protected voidIncrements the outgoing message counter for the given message.booleanIndicates whether this source is currently connected and able to send messages.booleanReturns whether this channel expects keep-alive messages.booleanReturns whether the channel header overrides the message header.protected booleanisRejected(byte[] b) Returns whether the given raw message should be rejected.booleanReturns whether SO_LINGER is enabled on the socket.protected SocketCreates a new socket connected to one of the given hosts/ports.protected Socketfactory method pattern (as suggested by Vincent.Greene@amo.com)protected byte[]Packs the given ISOMsg into raw bytes using this channel's packager.protected voidHook called after a successful connection.protected byte[]readHeader(int hLen) Reads in a message header.receive()Waits and receive an ISOMsg over the TCP/IP sessionvoidIssues a disconnect followed by a connectvoidremoveFilter(ISOFilter filter) Removes a filter from this channel.voidremoveFilter(ISOFilter filter, int direction) Removes a filter from this channel.voidremoveIncomingFilter(ISOFilter filter) Removes a filter from the incoming filter chain.voidremoveOutgoingFilter(ISOFilter filter) Removes a filter from the outgoing filter chain.voidreset stat infovoidsend(byte[] b) sends a byte[] over the TCP/IP sessionvoidSends the specifiedISOMsgover this ISOChannel.voidSends a high-level keep-alive message (zero length)protected voidsendMessage(byte[] b, int offset, int len) Writes the given bytes to the output stream.protected voidsendMessageTrailer(ISOMsg m, byte[] b) Sends the message trailer bytes.protected voidsendMessageTrailler(ISOMsg m, byte[] b) Deprecated.useBaseChannel.sendMessageTrailer(ISOMsg, byte[])instead.protected voidsendMessageTrailler(ISOMsg m, int len) Deprecated.use sendMessageTrailer(ISOMsg m, byte[] b) instead.voidsetHeader(byte[] header) Sets the message header bytes for this channel.voidSets the message header from a hex string.voidSets the remote host name or address.voidinitialize an ISOChannelvoidsetIncomingFilters(Collection filters) Replaces the entire incoming filter chain with the given collection.voidsetISOMsgMetrics(ISOMsgMetrics metrics) Sets the ISOMsgMetrics instance.voidsetLocalAddress(String iface, int port) initialize an ISOChannelvoidAttaches aLoggerand realm to this log source.voidsetMaxPacketLength(int maxPacketLength) Sets the maximum packet length accepted by this channel.voidassociates this ISOChannel with a name using NameRegistrarvoidsetOutgoingFilters(Collection filters) Replaces the entire outgoing filter chain with the given collection.voidsetOverrideHeader(boolean overrideHeader) Controls whether this channel's own header overrides the message header.voidset Packager for channelvoidsetPort(int port) Sets the remote port number.voidsetServerSocket(ServerSocket sock) Associates this ISOChannel with a server socketvoidsetSocketFactory(ISOClientSocketFactory socketFactory) Sets the specified Socket Factory to create socketsvoidsetSoLinger(boolean on, int linger) Socket SO_LINGER option to use when closing the socket.voidsetTimeout(int timeout) sets socket timeout (as suggested by Leonard Thomas <leonard@rhinosystemsinc.com>)voidsetUsable(boolean b) Marks this channel as usable or not; a non-usable channel will not accept messages.protected booleanshouldIgnore(byte[] b) Returns whether the given raw message should be silently ignored.protected byte[]Reads raw bytes from the stream for protocols without explicit length framing.protected StringtoEndpoint(Socket socket) Returns a human-readable endpoint string for the given socket.protected voidUnpacks raw bytes into the given ISOMsg using this channel's packager.Methods inherited from class Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChangedMethods inherited from class Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ExceptionHandlerAware
addHandler, addHandler, handle, removeHandler, removeHandler, removeHandlersModifier and TypeMethodDescriptiondefault voidaddHandler(ExceptionHandler handler) Add a handler to the default pipeline.default voidaddHandler(ExceptionHandler handler, Class<? extends Exception> clazz) Add a handler to an exception specific pipeline.default ExceptionExecute the pipeline by starting with the specific pipeline for the exception followed by the default pipeline.default voidremoveHandler(ExceptionHandler handler) Remove a handler from the default pipeline.default voidremoveHandler(ExceptionHandler handler, Class<? extends Exception> clazz) Remove a handler from an exception specific handler pipeline.default voidremoveHandlers(Class<? extends Exception> clazz) Remove all handler for a specific exception handling pipeline.
-
Constructor Details
-
CMFChannel
public CMFChannel()Creates an unconfiguredCMFChannel. The caller is expected to set the packager/connection parameters through the usualBaseChannelconfiguration and/or setters before use. -
CMFChannel
Constructs a clientCMFChannel.- Parameters:
host- server TCP address or hostname.port- server TCP port number.p- theISOPackagerused to pack/unpackISOMsgs.- See Also:
-
CMFChannel
Constructs a serverCMFChannel. This constructor creates aServerSocketinternally (as perBaseChannel) and waits for inbound connections whenBaseChannel.accept(java.net.ServerSocket)is invoked.- Parameters:
p- theISOPackagerused to pack/unpackISOMsgs.- Throws:
IOException- if the underlying server socket cannot be created.- See Also:
-
CMFChannel
Constructs a serverCMFChannelassociated with an existingServerSocket.- Parameters:
p- theISOPackagerused to pack/unpackISOMsgs.serverSocket- the server socket used to accept a connection.- Throws:
IOException- if an I/O error occurs while initializing the channel.- See Also:
-
-
Method Details
-
sendMessageLength
Sends the CMF message length prefix. The CMF framing uses a 3-byte big-endian length prefix (24-bit unsigned).- Overrides:
sendMessageLengthin classBaseChannel- Parameters:
len- the packed message length in bytes.- Throws:
IOException- if an I/O error occurs writing to the socket output stream or iflenis outside the valid range (0..0xFFFFFF).
-
getMessageLength
Reads the CMF message length prefix. Reads a 3-byte big-endian length value. A zero-length frame is treated as a keep-alive. When a keep-alive is received and eitherreplyKeepAliveis enabled orBaseChannel.isExpectKeepAlive()istrue, the keep-alive is echoed back to the peer. The method continues reading until a non-zero length is obtained.If
max-keepalives-in-a-rowis configured to a value greater than0, the method will fail after receiving more than that number of consecutive keep-alives without a subsequent non-zero length frame.- Overrides:
getMessageLengthin classBaseChannel- Returns:
- the packed message length in bytes (always
> 0). - Throws:
IOException- if an I/O error occurs reading from the socket input stream, or if too many consecutive keep-alives are received (when limited).ISOException- if the channel detects an ISO-level framing/processing error.
-
getHeaderLength
Returns the channel header length. CMF framing does not include a separate message header beyond the 3-byte length prefix.- Overrides:
getHeaderLengthin classBaseChannel- Returns:
0.
-
sendMessageHeader
Sends the message header (no-op). CMF framing does not define a message header; only the length prefix is used.- Overrides:
sendMessageHeaderin classBaseChannel- Parameters:
m- the message being sent.len- the packed message length in bytes.
-
setConfiguration
Applies configuration to this channel. In addition to the baseBaseChannelconfiguration, this method reads:reply-keepalive(boolean, defaulttrue).max-keepalives-in-a-row(integer, default0).
- Specified by:
setConfigurationin interfaceConfigurable- Overrides:
setConfigurationin classBaseChannel- Parameters:
cfg- the configuration source.- Throws:
ConfigurationException- if configuration cannot be applied.
-
getMaxPacketLength
Returns the maximum packet length supported by this channel. CMF framing uses a 3-byte unsigned length prefix (24-bit). Therefore, the maximum representable non-zero payload length is0xFFFFFF(16,777,215 bytes).- Overrides:
getMaxPacketLengthin classBaseChannel- Returns:
- the maximum packet length, in bytes.
-
BaseChannel.getMessageTrailer(ISOMsg)instead.