Package com.thecoderscorner.menu.remote
Class StreamRemoteConnector
- java.lang.Object
-
- com.thecoderscorner.menu.remote.StreamRemoteConnector
-
- All Implemented Interfaces:
RemoteConnector,RemoteConnectorContext
- Direct Known Subclasses:
Rs232RemoteConnector,SocketBasedConnector
public abstract class StreamRemoteConnector extends java.lang.Object implements RemoteConnector, RemoteConnectorContext
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.ReadMode
-
Field Summary
Fields Modifier and Type Field Description protected java.time.Clockclockprotected java.util.concurrent.ScheduledExecutorServiceexecutorprotected java.lang.System.Loggerloggerprotected java.util.Map<AuthStatus,java.lang.Class<? extends RemoteConnectorState>>stateMachineMappings
-
Constructor Summary
Constructors Modifier Constructor Description protectedStreamRemoteConnector(LocalIdentifier ourLocalId, MenuCommandProtocol protocol, java.util.concurrent.ScheduledExecutorService executor, java.time.Clock clock)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidchangeState(AuthStatus desiredState)voidchangeState(RemoteConnectorState newState)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, StreamRemoteConnector.ReadMode mode)Reads at least the number of bytes requested waiting if need be for more data.AuthStatusgetAuthenticationStatus()java.time.ClockgetClock()RemoteInformationgetRemoteParty()java.util.concurrent.ScheduledExecutorServicegetScheduledExecutor()protected voidhandleCoreConnectionStates(ConnectMode connectMode)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 connectivityvoidnotifyListeners(MenuCommand mc)Helper method that notifies all listeners of a new command messageMenuCommandreadCommandFromStream()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.voidsendAcknowledgement(AckStatus ackStatus)voidsendHeartbeat(int frequency, MenuHeartbeatCommand.HeartbeatMode mode)protected abstract voidsendInternal(java.nio.ByteBuffer outputBuffer)performs a send of all bytes in the output buffer until the output buffer is emptyvoidsendJoin()voidsendMenuCommand(MenuCommand msg)Sends a command to the remote with the protocol and usual headers.voidsendPairing()voidsetRemoteParty(RemoteInformation remote)-
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, isDeviceConnected, start, stop
-
Methods inherited from interface com.thecoderscorner.menu.remote.states.RemoteConnectorContext
getConnectionName, isDeviceConnected, performConnection
-
-
-
-
Field Detail
-
logger
protected final java.lang.System.Logger logger
-
executor
protected final java.util.concurrent.ScheduledExecutorService executor
-
clock
protected final java.time.Clock clock
-
stateMachineMappings
protected final java.util.Map<AuthStatus,java.lang.Class<? extends RemoteConnectorState>> stateMachineMappings
-
-
Constructor Detail
-
StreamRemoteConnector
protected StreamRemoteConnector(LocalIdentifier ourLocalId, MenuCommandProtocol protocol, java.util.concurrent.ScheduledExecutorService executor, java.time.Clock clock)
-
-
Method Detail
-
readCommandFromStream
public MenuCommand readCommandFromStream() throws java.io.IOException
- Specified by:
readCommandFromStreamin interfaceRemoteConnectorContext- Throws:
java.io.IOException
-
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- Specified by:
closein interfaceRemoteConnectorContext
-
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
-
handleCoreConnectionStates
protected void handleCoreConnectionStates(ConnectMode connectMode)
-
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, StreamRemoteConnector.ReadMode mode) 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
-
notifyListeners
public void notifyListeners(MenuCommand mc)
Helper method that notifies all listeners of a new command message- Specified by:
notifyListenersin interfaceRemoteConnectorContext- Parameters:
mc- the message to notify
-
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)
-
changeState
public void changeState(AuthStatus desiredState)
- Specified by:
changeStatein interfaceRemoteConnectorContext
-
changeState
public void changeState(RemoteConnectorState newState)
- Specified by:
changeStatein interfaceRemoteConnectorContext
-
getRemoteParty
public RemoteInformation getRemoteParty()
- Specified by:
getRemotePartyin interfaceRemoteConnector- Returns:
- the remote party information of the current connection
-
setRemoteParty
public void setRemoteParty(RemoteInformation remote)
- Specified by:
setRemotePartyin interfaceRemoteConnectorContext
-
getAuthenticationStatus
public AuthStatus getAuthenticationStatus()
- Specified by:
getAuthenticationStatusin interfaceRemoteConnector- Returns:
- the status of the connection and authentication.
-
sendHeartbeat
public void sendHeartbeat(int frequency, MenuHeartbeatCommand.HeartbeatMode mode)- Specified by:
sendHeartbeatin interfaceRemoteConnectorContext
-
sendJoin
public void sendJoin() throws java.io.IOException- Specified by:
sendJoinin interfaceRemoteConnectorContext- Throws:
java.io.IOException
-
sendAcknowledgement
public void sendAcknowledgement(AckStatus ackStatus) throws java.io.IOException
- Specified by:
sendAcknowledgementin interfaceRemoteConnectorContext- Throws:
java.io.IOException
-
sendPairing
public void sendPairing() throws java.io.IOException- Specified by:
sendPairingin interfaceRemoteConnectorContext- Throws:
java.io.IOException
-
getScheduledExecutor
public java.util.concurrent.ScheduledExecutorService getScheduledExecutor()
- Specified by:
getScheduledExecutorin interfaceRemoteConnectorContext
-
getClock
public java.time.Clock getClock()
- Specified by:
getClockin interfaceRemoteConnectorContext
-
-