Package jmri.jmrix.bidib.netbidib
Class NetBiDiBAdapter
- java.lang.Object
-
- jmri.jmrix.AbstractPortController
-
- jmri.jmrix.AbstractNetworkPortController
-
- jmri.jmrix.bidib.BiDiBNetworkPortController
-
- jmri.jmrix.bidib.netbidib.NetBiDiBAdapter
-
- All Implemented Interfaces:
BiDiBPortController
,NetworkPortAdapter
,PortAdapter
public class NetBiDiBAdapter extends BiDiBNetworkPortController
Implements BiDiBPortController for the netBiDiB system network connection.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
NetBiDiBAdapter.NetBiDiDDevice
-
Nested classes/interfaces inherited from class jmri.jmrix.AbstractPortController
AbstractPortController.Option
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static int
BIDIB_JMRI_PID
static java.lang.String
NET_BIDIB_DEFAULT_PAIRING_STORE_FILE
(package private) static java.lang.String
OPTION_DEVICE_LIST
(package private) static java.lang.String
OPTION_UNIQUE_ID
(package private) long
timeout
-
Fields inherited from class jmri.jmrix.bidib.BiDiBNetworkPortController
bidib, context
-
Fields inherited from class jmri.jmrix.AbstractNetworkPortController
connTimeout, m_HostName, m_port, socketConn
-
Fields inherited from class jmri.jmrix.AbstractPortController
allowConnectionRecovery, manufacturerName, opened, option1Name, option2Name, option3Name, option4Name, options, reconnectinterval, reconnectMaxAttempts, reconnectMaxInterval
-
-
Constructor Summary
Constructors Constructor Description NetBiDiBAdapter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addConnectionChangedListener(java.awt.event.ActionListener l)
void
autoConfigure()
Set the server's host name and port using mdns autoconfiguration.protected void
closeConnection()
Abstract class for controllers to close the connection.void
configure()
Configure all of the other jmrix widgets needed to work with this adapter.void
connect()
This methods is called from network connection config.void
connect(java.lang.String host, int port)
Connects to the end device using a hostname/ip address and portvoid
deviceListAddFromPairingStore()
java.lang.String
getAdvertisementName()
Get and set the ZeroConf/mDNS advertisement name.java.util.Map<java.lang.Long,java.lang.String>
getDeviceListEntries()
Get the device list of all found devices and return them as a map of strings suitable for display and indexed by the unique id.java.io.DataInputStream
getInputStream()
Get the InputStream from the port.boolean
getMdnsConfigure()
Get whether or not this adapter is configured to use autoconfiguration via MDNS.java.lang.Long
getNetBidibUniqueId()
Get a unique id for ourself.java.io.DataOutputStream
getOutputStream()
Get the outputStream to the port.java.lang.String
getServiceType()
Get the ZeroConf/mDNS service type.java.lang.Long
getUniqueId()
Get and set the BiDiB Unique ID.boolean
isConnectionReady()
Get the connection ready status from the traffic controllerboolean
isDetached()
Check if the connection is detached i.e. it is opened, paired but the logon has been rejected.boolean
isOpened()
Check of the connection is opened.void
registerAllListeners(org.bidib.jbidibc.messages.ConnectionListener connectionListener, java.util.Set<org.bidib.jbidibc.core.NodeListener> nodeListeners, java.util.Set<org.bidib.jbidibc.core.MessageListener> messageListeners, java.util.Set<org.bidib.jbidibc.core.node.listener.TransferListener> transferListeners)
Register all Listeners to the specific BiDiB Object.void
removeConnectionChangedListener(java.awt.event.ActionListener l)
void
selectDeviceListItem(int i)
Set hostname, port and unique id from the device list entry selected by a given index.void
setAdvertisementName(java.lang.String AdName)
void
setLogon(boolean logon)
Set or remove the detached state.void
setMdnsConfigure(boolean autoconfig)
Set whether or not this adapter should be configured automatically via MDNS.void
setPaired(boolean paired, java.awt.event.ActionListener l)
Set new pairing state.void
setUniqueId(java.lang.Long uniqueId)
-
Methods inherited from class jmri.jmrix.bidib.BiDiBNetworkPortController
getContext, getRealPortName, getSystemConnectionMemo
-
Methods inherited from class jmri.jmrix.AbstractNetworkPortController
getConnectionTimeout, getCurrentPortName, getHostAddress, getHostName, getPort, reconnectFromLoop, resetupConnection, setConnectionTimeout, setHostAddress, setHostName, setPort, setPort, setServiceType
-
Methods inherited from class jmri.jmrix.AbstractPortController
configureOption1, configureOption2, configureOption3, configureOption4, dispose, getDisabled, getManufacturer, getOption1Name, getOption2Name, getOption3Name, getOption4Name, getOptionChoices, getOptionDisplayName, getOptions, getOptionState, getReconnectMaxAttempts, getReconnectMaxInterval, getSystemPrefix, getUserName, isDirty, isOptionAdvanced, isOptionTypePassword, isOptionTypeText, isRestartRequired, purgeStream, reconnect, recover, safeSleep, setClosed, setDisabled, setManufacturer, setOpened, setOptionState, setReconnectMaxAttempts, setReconnectMaxInterval, setSystemConnectionMemo, setSystemPrefix, setUserName, status
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jmri.jmrix.NetworkPortAdapter
status
-
Methods inherited from interface jmri.jmrix.PortAdapter
configureOption1, configureOption2, configureOption3, configureOption4, dispose, getDisabled, getManufacturer, getOption1Name, getOption2Name, getOption3Name, getOption4Name, getOptionChoices, getOptionDisplayName, getOptions, getOptionState, getReconnectMaxAttempts, getReconnectMaxInterval, getSystemPrefix, getUserName, isDirty, isOptionAdvanced, isOptionTypePassword, isOptionTypeText, isRestartRequired, recover, setDisabled, setManufacturer, setOptionState, setReconnectMaxAttempts, setReconnectMaxInterval, setSystemConnectionMemo, setSystemPrefix, setUserName
-
-
-
-
Field Detail
-
NET_BIDIB_DEFAULT_PAIRING_STORE_FILE
public static final java.lang.String NET_BIDIB_DEFAULT_PAIRING_STORE_FILE
- See Also:
- Constant Field Values
-
OPTION_DEVICE_LIST
static final java.lang.String OPTION_DEVICE_LIST
- See Also:
- Constant Field Values
-
OPTION_UNIQUE_ID
static final java.lang.String OPTION_UNIQUE_ID
- See Also:
- Constant Field Values
-
BIDIB_JMRI_PID
static final int BIDIB_JMRI_PID
- See Also:
- Constant Field Values
-
timeout
long timeout
-
-
Constructor Detail
-
NetBiDiBAdapter
public NetBiDiBAdapter()
-
-
Method Detail
-
deviceListAddFromPairingStore
public void deviceListAddFromPairingStore()
-
connect
public void connect(java.lang.String host, int port) throws java.io.IOException
Description copied from interface:NetworkPortAdapter
Connects to the end device using a hostname/ip address and port- Specified by:
connect
in interfaceNetworkPortAdapter
- Specified by:
connect
in classBiDiBNetworkPortController
- Parameters:
host
- hostname / ip address.port
- network port.- Throws:
java.io.IOException
- on connection error.
-
connect
public void connect()
This methods is called from network connection config. It creates the BiDiB object from jbidibc and opens it. The connectPort method of the traffic controller is called for generic initialisation.- Specified by:
connect
in interfacePortAdapter
- Overrides:
connect
in classAbstractNetworkPortController
-
configure
public void configure()
Configure all of the other jmrix widgets needed to work with this adapter.
-
closeConnection
protected void closeConnection()
Abstract class for controllers to close the connection. Called prior to any re-connection attempts.- Overrides:
closeConnection
in classAbstractNetworkPortController
-
registerAllListeners
public void registerAllListeners(org.bidib.jbidibc.messages.ConnectionListener connectionListener, java.util.Set<org.bidib.jbidibc.core.NodeListener> nodeListeners, java.util.Set<org.bidib.jbidibc.core.MessageListener> messageListeners, java.util.Set<org.bidib.jbidibc.core.node.listener.TransferListener> transferListeners)
Register all Listeners to the specific BiDiB Object. We need this here since the BidibInterface does not provide this method.- Specified by:
registerAllListeners
in interfaceBiDiBPortController
- Specified by:
registerAllListeners
in classBiDiBNetworkPortController
- Parameters:
connectionListener
- add to thisnodeListeners
- listeners to addmessageListeners
- listeners to addtransferListeners
- listeners to add
-
getNetBidibUniqueId
public java.lang.Long getNetBidibUniqueId()
Get a unique id for ourself. The product id part is fixed and registered with bidib.org. The serial number is a hash from the MAC address. This is a variation of org.bidib.wizard.core.model.settings.NetBidibSettings.getNetBidibUniqueId(). Instead of just using the network interface from InetAddress.getLocalHost() - which can result to the loopback-interface, which does not have a hardware address - we loop through the list of interfaces until we find an interface which is up and not a loopback. It would be even better, if we check for virtual interfaces (those could be present if VMs run on the machine) and then exclude them. But there is no generic method to find those interfaces. So we just return an UID derived from the first found non-loopback interface or the default UID if there is no such interface.- Returns:
- Unique ID as long
-
getInputStream
public java.io.DataInputStream getInputStream()
Description copied from class:AbstractNetworkPortController
Get the InputStream from the port.- Specified by:
getInputStream
in interfacePortAdapter
- Overrides:
getInputStream
in classAbstractNetworkPortController
- Returns:
- the InputStream from the port
-
getOutputStream
public java.io.DataOutputStream getOutputStream()
Description copied from class:AbstractNetworkPortController
Get the outputStream to the port.- Specified by:
getOutputStream
in interfacePortAdapter
- Overrides:
getOutputStream
in classAbstractNetworkPortController
- Returns:
- the outputStream to the port
-
setMdnsConfigure
public void setMdnsConfigure(boolean autoconfig)
Set whether or not this adapter should be configured automatically via MDNS.- Specified by:
setMdnsConfigure
in interfaceNetworkPortAdapter
- Overrides:
setMdnsConfigure
in classAbstractNetworkPortController
- Parameters:
autoconfig
- boolean value.
-
getMdnsConfigure
public boolean getMdnsConfigure()
Get whether or not this adapter is configured to use autoconfiguration via MDNS.- Specified by:
getMdnsConfigure
in interfaceNetworkPortAdapter
- Overrides:
getMdnsConfigure
in classAbstractNetworkPortController
- Returns:
- true if configured using MDNS.
-
autoConfigure
public void autoConfigure()
Set the server's host name and port using mdns autoconfiguration.- Specified by:
autoConfigure
in interfaceNetworkPortAdapter
- Overrides:
autoConfigure
in classAbstractNetworkPortController
-
getAdvertisementName
public java.lang.String getAdvertisementName()
Get and set the ZeroConf/mDNS advertisement name.This value is the unique id in BiDiB.
- Specified by:
getAdvertisementName
in interfaceNetworkPortAdapter
- Overrides:
getAdvertisementName
in classAbstractNetworkPortController
- Returns:
- advertisement name.
-
setAdvertisementName
public void setAdvertisementName(java.lang.String AdName)
- Specified by:
setAdvertisementName
in interfaceNetworkPortAdapter
- Overrides:
setAdvertisementName
in classAbstractNetworkPortController
-
getServiceType
public java.lang.String getServiceType()
Get the ZeroConf/mDNS service type.This value is fixed in BiDiB, so return the default value.
- Specified by:
getServiceType
in interfaceNetworkPortAdapter
- Overrides:
getServiceType
in classAbstractNetworkPortController
- Returns:
- service type.
-
getDeviceListEntries
public java.util.Map<java.lang.Long,java.lang.String> getDeviceListEntries()
Get the device list of all found devices and return them as a map of strings suitable for display and indexed by the unique id. This is used by the connection config.- Returns:
- map of strings containing device info.
-
selectDeviceListItem
public void selectDeviceListItem(int i)
Set hostname, port and unique id from the device list entry selected by a given index.- Parameters:
i
- selected index into device list
-
getUniqueId
public java.lang.Long getUniqueId()
Get and set the BiDiB Unique ID.If we haven't set the unique ID of the connection before, try to find it from the root node of the connection. This will work only if the connection is open and not detached.
- Returns:
- unique Id as Long
-
setUniqueId
public void setUniqueId(java.lang.Long uniqueId)
-
isConnectionReady
public boolean isConnectionReady()
Get the connection ready status from the traffic controller- Returns:
- true if the connection is opened and ready to use (paired and logged in)
-
setPaired
public void setPaired(boolean paired, java.awt.event.ActionListener l)
Set new pairing state. If the pairing should be removed, close the connection, set pairing state in the device list and update the pairing store. If pairing should be initiated, a connection is temporary opened and a pariring dialog is displayed which informs the user to confirm the pairing on the remote device. If the process has completed, the temporary connection is closed. Pairing and unpairing is an asynchroneous process, so an action listener may be provided which is called when the process has completed.- Parameters:
paired
- - true if the pairing should be initiated, false if pairing should be removedl
- - and event listener, called when pairing or unpairing has finished.
-
isOpened
public boolean isOpened()
Check of the connection is opened. This does not mean that it is paired or logged on.- Returns:
- true if opened
-
isDetached
public boolean isDetached()
Check if the connection is detached i.e. it is opened, paired but the logon has been rejected.- Returns:
- true if detached
-
setLogon
public void setLogon(boolean logon)
Set or remove the detached state.- Parameters:
logon
- - true for logon (attach), false for logoff (detach)
-
addConnectionChangedListener
public void addConnectionChangedListener(java.awt.event.ActionListener l)
-
removeConnectionChangedListener
public void removeConnectionChangedListener(java.awt.event.ActionListener l)
-
-