Package jmri.jmrit.z21server
Class ClientManager
- java.lang.Object
-
- jmri.jmrit.z21server.ClientManager
-
- All Implemented Interfaces:
java.util.EventListener
,ThrottleListener
public class ClientManager extends java.lang.Object implements ThrottleListener
Register and unregister clients, set loco throttle
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface jmri.ThrottleListener
ThrottleListener.DecisionType
-
-
Field Summary
Fields Modifier and Type Field Description static float
speedMultiplier
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ClientManager
getInstance()
Return the one running instance of the client manager.byte[]
getLocoStatusMessage(java.net.InetAddress address, java.lang.Integer locoAddress)
Return a Z21 LAN_X_LOCO_INFO packet for a given client and loco addressjava.util.HashMap<java.net.InetAddress,AppClient>
getRegisteredClients()
Get a hash map of the registered clients, indexed by their InetAddressbyte[]
getTurnoutStatusMessage(java.net.InetAddress address, java.lang.Integer turnoutNumber)
Get a Z21 LAN_X_TURNOUT_INFO packet to be sent to the client fpr a given turnout number.void
handleExpiredClients(boolean removeAll)
Check all clients if they have not sent anything for a time peroid (60 seconds).void
heartbeat(java.net.InetAddress clientAddress)
Send a heartbeat() to the AppClient instance.void
notifyDecisionRequired(LocoAddress address, ThrottleListener.DecisionType question)
Called from the throttle manager to ask if the throttle should be shared or the previous should be disconnected.void
notifyFailedThrottleRequest(LocoAddress address, java.lang.String reason)
Called from the throttle manager when no throttle can be created for a loco address.void
notifyThrottleFound(DccThrottle t)
Called from the throttle manager when a requested throttle for a given loco address was found.void
registerLocoIfNeeded(java.net.InetAddress clientAddress, int locoAddress)
Register a client if not already registered and add a throttle for the given loco address to the clients list of throttles.void
setChangeListener(java.beans.PropertyChangeListener changeListener)
Set the throttle change listener.void
setClientListener(java.beans.PropertyChangeListener clientListener)
Set the client change listener.void
setLocoFunction(java.net.InetAddress clientAddress, int locoAddress, int functionNumber, int functionState)
Set a JMRI throttle to new function state.void
setLocoSpeedAndDirection(java.net.InetAddress clientAddress, int locoAddress, int speed, boolean forward)
Set a JMRI throttle to new speed and direction.void
setTurnout(java.net.InetAddress clientAddress, int turnoutNumber, boolean state)
Set a JMRI component to new state.void
unregisterClient(java.net.InetAddress clientAddress)
Unregister a client.static byte
xor(byte[] packet)
Helper to construct the Z21 protocol XOR byte
-
-
-
Field Detail
-
speedMultiplier
public static float speedMultiplier
-
-
Method Detail
-
getInstance
public static ClientManager getInstance()
Return the one running instance of the client manager. If there is no instance, create it.- Returns:
- the client manager instance
-
setChangeListener
public void setChangeListener(java.beans.PropertyChangeListener changeListener)
Set the throttle change listener.- Parameters:
changeListener
- - the property change listener instance
-
setClientListener
public void setClientListener(java.beans.PropertyChangeListener clientListener)
Set the client change listener. The listener is called if a new is registered or a registered client is unregistered.- Parameters:
clientListener
- - the property change listener instance
-
getRegisteredClients
public java.util.HashMap<java.net.InetAddress,AppClient> getRegisteredClients()
Get a hash map of the registered clients, indexed by their InetAddress- Returns:
- the hash map of registered clients
-
registerLocoIfNeeded
public void registerLocoIfNeeded(java.net.InetAddress clientAddress, int locoAddress)
Register a client if not already registered and add a throttle for the given loco address to the clients list of throttles.- Parameters:
clientAddress
- - InetAddress of the clientlocoAddress
- - address of a loco
-
setLocoSpeedAndDirection
public void setLocoSpeedAndDirection(java.net.InetAddress clientAddress, int locoAddress, int speed, boolean forward)
Set a JMRI throttle to new speed and direction. Called when a Z21 client's user changes speed and/or direction.- Parameters:
clientAddress
- - the client's InetAddresslocoAddress
- - the loco addressspeed
- - the speed to setforward
- - true of forward, false if reverse
-
setLocoFunction
public void setLocoFunction(java.net.InetAddress clientAddress, int locoAddress, int functionNumber, int functionState)
Set a JMRI throttle to new function state. Called when a Z21 client's user changes function status.- Parameters:
clientAddress
- - the client's InetAddresslocoAddress
- - the loco addressfunctionNumber
- - the function number to setfunctionState
- - the new state of the function
-
getLocoStatusMessage
public byte[] getLocoStatusMessage(java.net.InetAddress address, java.lang.Integer locoAddress)
Return a Z21 LAN_X_LOCO_INFO packet for a given client and loco address- Parameters:
address
- - client InetAddresslocoAddress
- - the loco address- Returns:
- Z21 LAN_X_LOCO_INFO packet
-
setTurnout
public void setTurnout(java.net.InetAddress clientAddress, int turnoutNumber, boolean state)
Set a JMRI component to new state. The component may be a JMRI turnout, light, route, signal mast, signal head or sensor, depending on a property entry for the component containing the Z21 turnout number. Called when a Z21 client's user changes state of a turnout.- Parameters:
clientAddress
- - client's InetAddressturnoutNumber
- - the Z21 turnout number, starting from 1 as seen on the WlanMaus display (in the Z21 protocol turnouts start with 0).state
- - true if turnout should be THROWN, false if CLOSED.
-
getTurnoutStatusMessage
public byte[] getTurnoutStatusMessage(java.net.InetAddress address, java.lang.Integer turnoutNumber)
Get a Z21 LAN_X_TURNOUT_INFO packet to be sent to the client fpr a given turnout number.- Parameters:
address
- - client's InetAdressturnoutNumber
- - the Z21 Turnout Number- Returns:
- a Z21 LAN_X_TURNOUT_INFO packet
-
heartbeat
public void heartbeat(java.net.InetAddress clientAddress)
Send a heartbeat() to the AppClient instance.- Parameters:
clientAddress
- - the client's InetAdress
-
handleExpiredClients
public void handleExpiredClients(boolean removeAll)
Check all clients if they have not sent anything for a time peroid (60 seconds). If the client has expired, remove it from the list.- Parameters:
removeAll
- - if true, remove all clients regardless of their expiry time.
-
unregisterClient
public void unregisterClient(java.net.InetAddress clientAddress)
Unregister a client. Clean up the AppClient instance to remove listeners from throttles, Remove client from hash map, Call client listener to inform about removing the client- Parameters:
clientAddress
- - client's InetAddress
-
notifyThrottleFound
public void notifyThrottleFound(DccThrottle t)
Called from the throttle manager when a requested throttle for a given loco address was found. The thottle is then added to the list of throttles in the AppClient instance.- Specified by:
notifyThrottleFound
in interfaceThrottleListener
- Parameters:
t
- - the (new) throttle bound to the loco.
-
notifyFailedThrottleRequest
public void notifyFailedThrottleRequest(LocoAddress address, java.lang.String reason)
Called from the throttle manager when no throttle can be created for a loco address.- Specified by:
notifyFailedThrottleRequest
in interfaceThrottleListener
- Parameters:
address
- - loco addressreason
- - a message from the throttle manager
-
notifyDecisionRequired
public void notifyDecisionRequired(LocoAddress address, ThrottleListener.DecisionType question)
Called from the throttle manager to ask if the throttle should be shared or the previous should be disconnected. For now, we always use shared throttles.- Specified by:
notifyDecisionRequired
in interfaceThrottleListener
- Parameters:
address
- - loco addressquestion
- - STEAL, SHARE or both
-
xor
public static byte xor(byte[] packet)
Helper to construct the Z21 protocol XOR byte- Parameters:
packet
- - Z21 packet- Returns:
- the XOR byte
-
-