Package com.thecoderscorner.menu.remote
Class StreamRemoteConnector
- java.lang.Object
-
- com.thecoderscorner.menu.remote.StreamRemoteConnector
-
- All Implemented Interfaces:
RemoteConnector
- Direct Known Subclasses:
Rs232RemoteConnector,SocketBasedConnector
public abstract class StreamRemoteConnector extends java.lang.Object implements RemoteConnector
Stream remote connector is the base class for all stream implementations, such as Socket and RS232. Any remote with stream like semantics can use this as the base for building out an adapter.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classStreamRemoteConnector.StreamState
-
Field Summary
Fields Modifier and Type Field Description protected java.util.concurrent.ScheduledExecutorServiceexecutorprotected java.lang.System.Loggerloggerstatic byteSTART_OF_MSGprotected java.util.concurrent.atomic.AtomicReference<StreamRemoteConnector.StreamState>state
-
Constructor Summary
Constructors Modifier Constructor Description protectedStreamRemoteConnector(MenuCommandProtocol protocol, java.util.concurrent.ScheduledExecutorService executor)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclose()Force close a connection when its known to be bad, the connector will try and establish a new connection.static booleandoesBufferHaveEOM(java.nio.ByteBuffer inputBuffer)protected abstract voidgetAtLeastBytes(java.nio.ByteBuffer inputBuffer, int len)Reads at least the number of bytes requested waiting if need be for more data.booleanisConnected()Gets the underlying connection state for this connector.protected voidlogByteBuffer(java.lang.String msg, java.nio.ByteBuffer inBuffer)Helper method that logs the entire message buffer when at debug logging level.protected voidnotifyConnection()Helper method that notifies all connection listeners of a change in connectivityprotected voidprocessMessagesOnConnection()This is the loop that handles a connection by reading messages until there's an IOException, or the transport or thread change to an end state.protected voidreadCompleteMessage(java.nio.ByteBuffer inputBuffer)voidregisterConnectionChangeListener(ConnectionChangeListener listener)Register for connection change events, when there is a change in connectivity status on the underlying transport.voidregisterConnectorListener(RemoteConnectorListener listener)Register for connector messages, when new messages are received from this stream.protected abstract voidsendInternal(java.nio.ByteBuffer outputBuffer)performs a send of all bytes in the output buffer until the output buffer is emptyvoidsendMenuCommand(MenuCommand msg)Sends a command to the remote with the protocol and usual headers.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.thecoderscorner.menu.remote.RemoteConnector
getConnectionName, start, stop
-
-
-
-
Field Detail
-
START_OF_MSG
public static final byte START_OF_MSG
- See Also:
- Constant Field Values
-
logger
protected final java.lang.System.Logger logger
-
executor
protected final java.util.concurrent.ScheduledExecutorService executor
-
state
protected final java.util.concurrent.atomic.AtomicReference<StreamRemoteConnector.StreamState> state
-
-
Constructor Detail
-
StreamRemoteConnector
protected StreamRemoteConnector(MenuCommandProtocol protocol, java.util.concurrent.ScheduledExecutorService executor)
-
-
Method Detail
-
processMessagesOnConnection
protected void processMessagesOnConnection()
This is the loop that handles a connection by reading messages until there's an IOException, or the transport or thread change to an end state.
-
close
public void close()
Description copied from interface:RemoteConnectorForce close a connection when its known to be bad, the connector will try and establish a new connection.- Specified by:
closein interfaceRemoteConnector
-
isConnected
public boolean isConnected()
Description copied from interface:RemoteConnectorGets the underlying connection state for this connector.- Specified by:
isConnectedin interfaceRemoteConnector- Returns:
- the underlying connection state.
-
sendMenuCommand
public void sendMenuCommand(MenuCommand msg) throws java.io.IOException
Sends a command to the remote with the protocol and usual headers.- Specified by:
sendMenuCommandin interfaceRemoteConnector- Parameters:
msg- the message to send.- Throws:
java.io.IOException- if there are issues with the transport
-
sendInternal
protected abstract void sendInternal(java.nio.ByteBuffer outputBuffer) throws java.io.IOExceptionperforms a send of all bytes in the output buffer until the output buffer is empty- Parameters:
outputBuffer- the buffer data to be sent- Throws:
java.io.IOException- if there are problems writing
-
getAtLeastBytes
protected abstract void getAtLeastBytes(java.nio.ByteBuffer inputBuffer, int len) throws java.io.IOExceptionReads at least the number of bytes requested waiting if need be for more data.- Parameters:
inputBuffer- the buffer to read fromlen- the minimum number of bytes needed- Throws:
java.io.IOException- if there are problems reading.
-
readCompleteMessage
protected void readCompleteMessage(java.nio.ByteBuffer inputBuffer) throws java.io.IOException- Throws:
java.io.IOException
-
registerConnectorListener
public void registerConnectorListener(RemoteConnectorListener listener)
Register for connector messages, when new messages are received from this stream.- Specified by:
registerConnectorListenerin interfaceRemoteConnector- Parameters:
listener- the listener to be registered
-
registerConnectionChangeListener
public void registerConnectionChangeListener(ConnectionChangeListener listener)
Register for connection change events, when there is a change in connectivity status on the underlying transport.- Specified by:
registerConnectionChangeListenerin interfaceRemoteConnector- Parameters:
listener- the listener
-
notifyConnection
protected void notifyConnection()
Helper method that notifies all connection listeners of a change in connectivity
-
logByteBuffer
protected void logByteBuffer(java.lang.String msg, java.nio.ByteBuffer inBuffer)Helper method that logs the entire message buffer when at debug logging level.- Parameters:
msg- the message to print firstinBuffer- the buffer to be logged
-
doesBufferHaveEOM
public static boolean doesBufferHaveEOM(java.nio.ByteBuffer inputBuffer)
-
-