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 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 String
manufacturerName
protected boolean
opened
protected String
option1Name
protected String
option2Name
protected String
option3Name
protected String
option4Name
protected HashMap<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
Modifier and Type Method Description protected void
closeConnection()
Abstract class for controllers to close the connection.void
configureOption1(String value)
Set the first port option.void
configureOption2(String value)
Set the second port option.void
configureOption3(String value)
Set the third port option.void
configureOption4(String value)
Set the fourth port option.void
dispose()
Clean up before removal.abstract String
getCurrentPortName()
boolean
getDisabled()
Return the disabled state of the adapter.abstract DataInputStream
getInputStream()
Get the InputStream from the port.String
getManufacturer()
Get the system manufacturer's name.String
getOption1Name()
String
getOption2Name()
String
getOption3Name()
String
getOption4Name()
String[]
getOptionChoices(String option)
Get a list of the various choices allowed with a given option.String
getOptionDisplayName(String option)
String[]
getOptions()
Get a list of all the options configured against this adapter.String
getOptionState(String option)
Get the string value of a specific option.abstract 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.String
getSystemPrefix()
Get the system prefix for this adapter.String
getUserName()
Get the user name for this adapter.boolean
isDirty()
Determine if configuration needs to be written to disk.boolean
isOptionAdvanced(String option)
boolean
isOptionTypeText(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.protected void
purgeStream(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, String s)
protected void
setClosed()
void
setDisabled(boolean disabled)
Set the connection disabled or enabled.void
setManufacturer(String manufacturer)
Set the system manufacturer's name.protected void
setOpened()
void
setOptionState(String option, 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(String systemPrefix)
Set the system prefix for this adapter.void
setUserName(String userName)
Set the user name for this adapter.boolean
status()
Query the status of this connection.
-
Field Details
-
manufacturerName
-
opened
-
option1Name
-
option2Name
-
option3Name
-
option4Name
-
options
-
allowConnectionRecovery
-
reconnectinterval
Initial interval between reconnection attempts. Default 1 second. -
reconnectMaxAttempts
Maximum reconnection attempts that the port should make. Default 100 attempts. A value of -1 indicates unlimited attempts. -
reconnectMaxInterval
Maximum interval between reconnection attempts in seconds. Default 120 seconds.
-
-
Constructor Details
-
Method Details
-
getInputStream
Get the InputStream from the port.- Specified by:
getInputStream
in interfacePortAdapter
- Returns:
- the InputStream from the port
-
getOutputStream
Get the outputStream to the port.- Specified by:
getOutputStream
in interfacePortAdapter
- Returns:
- the outputStream to the port
-
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
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
-
setClosed
-
getCurrentPortName
- Specified by:
getCurrentPortName
in interfacePortAdapter
-
configureOption1
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
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
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
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
- Specified by:
getOption1Name
in interfacePortAdapter
-
getOption2Name
- Specified by:
getOption2Name
in interfacePortAdapter
-
getOption3Name
- Specified by:
getOption3Name
in interfacePortAdapter
-
getOption4Name
- Specified by:
getOption4Name
in interfacePortAdapter
-
getOptions
Get a list of all the options configured against this adapter.- Specified by:
getOptions
in interfacePortAdapter
- Returns:
- Array of option identifier strings
-
setOptionState
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
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
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
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
-
getOptionDisplayName
- Specified by:
getOptionDisplayName
in interfacePortAdapter
-
isOptionAdvanced
- Specified by:
isOptionAdvanced
in interfacePortAdapter
-
getManufacturer
Description copied from interface:PortAdapter
Get the system manufacturer's name.- Specified by:
getManufacturer
in interfacePortAdapter
- Returns:
- manufacturer's name
-
setManufacturer
Description copied from interface:PortAdapter
Set the system manufacturer's name.- Specified by:
setManufacturer
in interfacePortAdapter
- Parameters:
manufacturer
- the manufacturer's name
-
getDisabled
Description copied from interface:PortAdapter
Return the disabled state of the adapter.- Specified by:
getDisabled
in interfacePortAdapter
- Returns:
- true if disabled
-
setDisabled
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
Description copied from interface:PortAdapter
Get the system prefix for this adapter.- Specified by:
getSystemPrefix
in interfacePortAdapter
- Returns:
- the system prefix or null
-
setSystemPrefix
Description copied from interface:PortAdapter
Set the system prefix for this adapter.- Specified by:
setSystemPrefix
in interfacePortAdapter
- Parameters:
systemPrefix
- the new system prefix
-
getUserName
Description copied from interface:PortAdapter
Get the user name for this adapter.- Specified by:
getUserName
in interfacePortAdapter
- Returns:
- the username or null
-
setUserName
Description copied from interface:PortAdapter
Set the user name for this adapter.- Specified by:
setUserName
in interfacePortAdapter
- Parameters:
userName
- the new user name
-
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
Abstract class for controllers to close the connection. Called prior to any re-connection attempts. -
reconnect
Attempts to reconnect to a failed port. Starts a reconnect thread -
resetupConnection
Abstract class for controllers to re-setup a connection. Called on connection reconnect success. -
reconnectFromLoop
Abstract class for ports to attempt a single re-connection attempt. Called from within main reconnect thread.- Parameters:
retryNum
- Reconnection attempt number.
-
setReconnectMaxInterval
Set the maximum interval between reconnection attempts.- Specified by:
setReconnectMaxInterval
in interfacePortAdapter
- Parameters:
maxInterval
- in seconds.
-
setReconnectMaxAttempts
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
Get the maximum interval between reconnection attempts.- Specified by:
getReconnectMaxInterval
in interfacePortAdapter
- Returns:
- maximum interval in seconds.
-
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
-
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
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
Service method to purge a stream of initial contents while opening the connection.- Parameters:
serialStream
- input data- Throws:
IOException
- from underlying operations
-
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
-