Package jmri.jmrix
Class AbstractPortController
- java.lang.Object
-
- jmri.jmrix.AbstractPortController
-
- All Implemented Interfaces:
PortAdapter
- Direct Known Subclasses:
AbstractNetworkPortController
,AbstractSerialPortController
,AbstractStreamPortController
,IpocsPortController
,RaspberryPiAdapter
,UsbPortAdapter
,XBeeIOStream
public abstract class AbstractPortController extends java.lang.Object implements PortAdapter
Provide an abstract base for *PortController classes.This is complicated by the lack of multiple inheritance. SerialPortAdapter is an Interface, and its implementing classes also inherit from various PortController types. But we want some common behaviors for those, so we put them here.
- See Also:
SerialPortAdapter
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AbstractPortController.Option
-
Field Summary
Fields Modifier and Type Field Description protected boolean
allowConnectionRecovery
protected java.lang.String
manufacturerName
protected boolean
opened
protected java.lang.String
option1Name
protected java.lang.String
option2Name
protected java.lang.String
option3Name
protected java.lang.String
option4Name
protected java.util.HashMap<java.lang.String,AbstractPortController.Option>
options
protected int
reconnectinterval
Initial interval between reconnection attempts.protected int
reconnectMaxAttempts
Maximum reconnection attempts that the port should make.protected int
reconnectMaxInterval
Maximum interval between reconnection attempts in seconds.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractPortController(SystemConnectionMemo connectionMemo)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
closeConnection()
Abstract class for controllers to close the connection.void
configureOption1(java.lang.String value)
Set the first port option.void
configureOption2(java.lang.String value)
Set the second port option.void
configureOption3(java.lang.String value)
Set the third port option.void
configureOption4(java.lang.String value)
Set the fourth port option.void
dispose()
Clean up before removal.abstract java.lang.String
getCurrentPortName()
boolean
getDisabled()
Return the disabled state of the adapter.abstract java.io.DataInputStream
getInputStream()
Get the InputStream from the port.java.lang.String
getManufacturer()
Get the system manufacturer's name.java.lang.String
getOption1Name()
java.lang.String
getOption2Name()
java.lang.String
getOption3Name()
java.lang.String
getOption4Name()
java.lang.String[]
getOptionChoices(java.lang.String option)
Get a list of the various choices allowed with a given option.java.lang.String
getOptionDisplayName(java.lang.String option)
java.lang.String[]
getOptions()
Get a list of all the options configured against this adapter.java.lang.String
getOptionState(java.lang.String option)
Get the string value of a specific option.abstract java.io.DataOutputStream
getOutputStream()
Get the outputStream to the port.int
getReconnectMaxAttempts()
Get the maximum number of reconnection attempts which should be made.int
getReconnectMaxInterval()
Get the maximum interval between reconnection attempts.SystemConnectionMemo
getSystemConnectionMemo()
Get theSystemConnectionMemo
associated with this object.java.lang.String
getSystemPrefix()
Get the system prefix for this adapter.java.lang.String
getUserName()
Get the user name for this adapter.boolean
isDirty()
Determine if configuration needs to be written to disk.boolean
isOptionAdvanced(java.lang.String option)
boolean
isOptionTypePassword(java.lang.String option)
Should this option be represented by a password fieldboolean
isOptionTypeText(java.lang.String option)
Should this option be represented by a text field (as opposed to a JCombobox)boolean
isRestartRequired()
Determine if application needs to be restarted for configuration changes to be applied.static void
purgeStream(java.io.InputStream serialStream)
Service method to purge a stream of initial contents while opening the connection.protected void
reconnect()
Attempts to reconnect to a failed port.protected void
reconnectFromLoop(int retryNum)
Abstract class for ports to attempt a single re-connection attempt.void
recover()
This is called when a connection is initially lost.protected void
resetupConnection()
Abstract class for controllers to re-setup a connection.protected static void
safeSleep(long milliseconds, java.lang.String s)
protected void
setClosed()
void
setDisabled(boolean disabled)
Set the connection disabled or enabled.void
setManufacturer(java.lang.String manufacturer)
Set the system manufacturer's name.protected void
setOpened()
void
setOptionState(java.lang.String option, java.lang.String value)
Set the value of an option.void
setReconnectMaxAttempts(int maxAttempts)
Set the maximum number of reconnection attemptsvoid
setReconnectMaxInterval(int maxInterval)
Set the maximum interval between reconnection attempts.void
setSystemConnectionMemo(SystemConnectionMemo connectionMemo)
Set theSystemConnectionMemo
associated with this object.void
setSystemPrefix(java.lang.String systemPrefix)
Set the system prefix for this adapter.void
setUserName(java.lang.String userName)
Set the user name for this adapter.boolean
status()
Query the status of this connection.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jmri.jmrix.PortAdapter
configure, connect
-
-
-
-
Field Detail
-
manufacturerName
protected java.lang.String manufacturerName
-
opened
protected boolean opened
-
option1Name
protected java.lang.String option1Name
-
option2Name
protected java.lang.String option2Name
-
option3Name
protected java.lang.String option3Name
-
option4Name
protected java.lang.String option4Name
-
options
protected java.util.HashMap<java.lang.String,AbstractPortController.Option> options
-
allowConnectionRecovery
protected boolean allowConnectionRecovery
-
reconnectinterval
protected int reconnectinterval
Initial interval between reconnection attempts. Default 1 second.
-
reconnectMaxAttempts
protected int reconnectMaxAttempts
Maximum reconnection attempts that the port should make. Default 100 attempts. A value of -1 indicates unlimited attempts.
-
reconnectMaxInterval
protected int reconnectMaxInterval
Maximum interval between reconnection attempts in seconds. Default 120 seconds.
-
-
Constructor Detail
-
AbstractPortController
protected AbstractPortController(SystemConnectionMemo connectionMemo)
-
-
Method Detail
-
getInputStream
public abstract java.io.DataInputStream getInputStream()
Get the InputStream from the port.- Specified by:
getInputStream
in interfacePortAdapter
- Returns:
- the InputStream from the port
-
getOutputStream
public abstract java.io.DataOutputStream getOutputStream()
Get the outputStream to the port.- Specified by:
getOutputStream
in interfacePortAdapter
- Returns:
- the outputStream to the port
-
dispose
@OverridingMethodsMustInvokeSuper public void dispose()
Clean up before removal. Overriding methods must callsuper.dispose()
or document why they are not calling the overridden implementation. In most cases, failure to call the overridden implementation will cause user-visible error.- Specified by:
dispose
in interfacePortAdapter
-
status
public boolean status()
Query the status of this connection. This is a question of configuration, not transient hardware status.- Specified by:
status
in interfacePortAdapter
- Returns:
- true if OK, at least as far as known
-
setOpened
protected void setOpened()
-
setClosed
protected void setClosed()
-
getCurrentPortName
public abstract java.lang.String getCurrentPortName()
- Specified by:
getCurrentPortName
in interfacePortAdapter
-
configureOption1
public void configureOption1(java.lang.String value)
Description copied from interface:PortAdapter
Set the first port option. Only to be used after construction, but before the openPort call.- Specified by:
configureOption1
in interfacePortAdapter
- Parameters:
value
- to set the option to
-
configureOption2
public void configureOption2(java.lang.String value)
Description copied from interface:PortAdapter
Set the second port option. Only to be used after construction, but before the openPort call.- Specified by:
configureOption2
in interfacePortAdapter
- Parameters:
value
- to set the option to
-
configureOption3
public void configureOption3(java.lang.String value)
Description copied from interface:PortAdapter
Set the third port option. Only to be used after construction, but before the openPort call.- Specified by:
configureOption3
in interfacePortAdapter
- Parameters:
value
- to set the option to
-
configureOption4
public void configureOption4(java.lang.String value)
Description copied from interface:PortAdapter
Set the fourth port option. Only to be used after construction, but before the openPort call.- Specified by:
configureOption4
in interfacePortAdapter
- Parameters:
value
- to set the option to
-
getOption1Name
public java.lang.String getOption1Name()
- Specified by:
getOption1Name
in interfacePortAdapter
-
getOption2Name
public java.lang.String getOption2Name()
- Specified by:
getOption2Name
in interfacePortAdapter
-
getOption3Name
public java.lang.String getOption3Name()
- Specified by:
getOption3Name
in interfacePortAdapter
-
getOption4Name
public java.lang.String getOption4Name()
- Specified by:
getOption4Name
in interfacePortAdapter
-
getOptions
public java.lang.String[] getOptions()
Get a list of all the options configured against this adapter.- Specified by:
getOptions
in interfacePortAdapter
- Returns:
- Array of option identifier strings
-
setOptionState
public void setOptionState(java.lang.String option, java.lang.String value)
Set the value of an option.- Specified by:
setOptionState
in interfacePortAdapter
- Parameters:
option
- the name string of the optionvalue
- the string value to set the option to
-
getOptionState
public java.lang.String getOptionState(java.lang.String option)
Get the string value of a specific option.- Specified by:
getOptionState
in interfacePortAdapter
- Parameters:
option
- the name of the option to query- Returns:
- the option value
-
getOptionChoices
public java.lang.String[] getOptionChoices(java.lang.String option)
Get a list of the various choices allowed with a given option.- Specified by:
getOptionChoices
in interfacePortAdapter
- Parameters:
option
- the name of the option to query- Returns:
- list of valid values for the option, null if none are available
-
isOptionTypeText
public boolean isOptionTypeText(java.lang.String option)
Description copied from interface:PortAdapter
Should this option be represented by a text field (as opposed to a JCombobox)- Specified by:
isOptionTypeText
in interfacePortAdapter
- Parameters:
option
- Name of the option to check- Returns:
- true for text representation preferred
-
isOptionTypePassword
public boolean isOptionTypePassword(java.lang.String option)
Description copied from interface:PortAdapter
Should this option be represented by a password field- Specified by:
isOptionTypePassword
in interfacePortAdapter
- Parameters:
option
- Name of the option to check- Returns:
- true for text representation preferred
-
getOptionDisplayName
public java.lang.String getOptionDisplayName(java.lang.String option)
- Specified by:
getOptionDisplayName
in interfacePortAdapter
-
isOptionAdvanced
public boolean isOptionAdvanced(java.lang.String option)
- Specified by:
isOptionAdvanced
in interfacePortAdapter
-
getManufacturer
public java.lang.String getManufacturer()
Description copied from interface:PortAdapter
Get the system manufacturer's name.- Specified by:
getManufacturer
in interfacePortAdapter
- Returns:
- manufacturer's name
-
setManufacturer
public void setManufacturer(java.lang.String manufacturer)
Description copied from interface:PortAdapter
Set the system manufacturer's name.- Specified by:
setManufacturer
in interfacePortAdapter
- Parameters:
manufacturer
- the manufacturer's name
-
getDisabled
public boolean getDisabled()
Description copied from interface:PortAdapter
Return the disabled state of the adapter.- Specified by:
getDisabled
in interfacePortAdapter
- Returns:
- true if disabled
-
setDisabled
public void setDisabled(boolean disabled)
Set the connection disabled or enabled. By default connections are enabled. If the implementing class does not use aSystemConnectionMemo
, this method must be overridden. Overriding methods must callsuper.setDisabled(boolean)
to ensure the configuration change state is correctly set.- Specified by:
setDisabled
in interfacePortAdapter
- Parameters:
disabled
- true if connection should be disabled
-
getSystemPrefix
public java.lang.String getSystemPrefix()
Description copied from interface:PortAdapter
Get the system prefix for this adapter.- Specified by:
getSystemPrefix
in interfacePortAdapter
- Returns:
- the system prefix or null
-
setSystemPrefix
public void setSystemPrefix(java.lang.String systemPrefix)
Description copied from interface:PortAdapter
Set the system prefix for this adapter.- Specified by:
setSystemPrefix
in interfacePortAdapter
- Parameters:
systemPrefix
- the new system prefix
-
getUserName
public java.lang.String getUserName()
Description copied from interface:PortAdapter
Get the user name for this adapter.- Specified by:
getUserName
in interfacePortAdapter
- Returns:
- the username or null
-
setUserName
public void setUserName(java.lang.String userName)
Description copied from interface:PortAdapter
Set the user name for this adapter.- Specified by:
setUserName
in interfacePortAdapter
- Parameters:
userName
- the new user name
-
recover
public void recover()
This is called when a connection is initially lost. After checking the allowConnectionRecovery flag, closes the connection, resets the open flag and attempts a reconnection.- Specified by:
recover
in interfacePortAdapter
-
closeConnection
protected void closeConnection()
Abstract class for controllers to close the connection. Called prior to any re-connection attempts.
-
reconnect
protected void reconnect()
Attempts to reconnect to a failed port. Starts a reconnect thread
-
resetupConnection
protected void resetupConnection()
Abstract class for controllers to re-setup a connection. Called on connection reconnect success.
-
reconnectFromLoop
protected void reconnectFromLoop(int retryNum)
Abstract class for ports to attempt a single re-connection attempt. Called from within main reconnect thread.- Parameters:
retryNum
- Reconnection attempt number.
-
setReconnectMaxInterval
public void setReconnectMaxInterval(int maxInterval)
Set the maximum interval between reconnection attempts.- Specified by:
setReconnectMaxInterval
in interfacePortAdapter
- Parameters:
maxInterval
- in seconds.
-
setReconnectMaxAttempts
public void setReconnectMaxAttempts(int maxAttempts)
Set the maximum number of reconnection attempts. -1 will set an infinite number of attempts.- Specified by:
setReconnectMaxAttempts
in interfacePortAdapter
- Parameters:
maxAttempts
- total maximum reconnection attempts.
-
getReconnectMaxInterval
public int getReconnectMaxInterval()
Get the maximum interval between reconnection attempts.- Specified by:
getReconnectMaxInterval
in interfacePortAdapter
- Returns:
- maximum interval in seconds.
-
getReconnectMaxAttempts
public int getReconnectMaxAttempts()
Get the maximum number of reconnection attempts which should be made. A value of -1 means no maximum value, i.e. infinite attempts.- Specified by:
getReconnectMaxAttempts
in interfacePortAdapter
- Returns:
- total number of attempts which should be made.
-
safeSleep
protected static void safeSleep(long milliseconds, java.lang.String s)
-
isDirty
public boolean isDirty()
Description copied from interface:PortAdapter
Determine if configuration needs to be written to disk.- Specified by:
isDirty
in interfacePortAdapter
- Returns:
- true if configuration needs to be saved, false otherwise
-
isRestartRequired
public boolean isRestartRequired()
Description copied from interface:PortAdapter
Determine if application needs to be restarted for configuration changes to be applied.- Specified by:
isRestartRequired
in interfacePortAdapter
- Returns:
- true if application needs to restart, false otherwise
-
purgeStream
public static void purgeStream(@Nonnull java.io.InputStream serialStream) throws java.io.IOException
Service method to purge a stream of initial contents while opening the connection.- Parameters:
serialStream
- input data- Throws:
java.io.IOException
- if the stream is e.g. closed due to failure to open the port completely
-
getSystemConnectionMemo
public SystemConnectionMemo getSystemConnectionMemo()
Get theSystemConnectionMemo
associated with this object.This method should only be overridden to ensure that a specific subclass of SystemConnectionMemo is returned. The recommended pattern is:
public MySystemConnectionMemo getSystemConnectionMemo() { return (MySystemConnectionMemo) super.getSystemConnectionMemo(); }
- Specified by:
getSystemConnectionMemo
in interfacePortAdapter
- Returns:
- the currently associated SystemConnectionMemo
-
setSystemConnectionMemo
@OverridingMethodsMustInvokeSuper public void setSystemConnectionMemo(@Nonnull SystemConnectionMemo connectionMemo)
Set theSystemConnectionMemo
associated with this object.Overriding implementations must call
super.setSystemConnectionMemo(memo)
at some point to ensure the SystemConnectionMemo gets set.- Specified by:
setSystemConnectionMemo
in interfacePortAdapter
- Parameters:
connectionMemo
- the SystemConnectionMemo to associate with this PortController
-
-