Class ClientManager

java.lang.Object
jmri.jmrit.z21server.ClientManager
All Implemented Interfaces:
EventListener, ThrottleListener

public class ClientManager extends Object implements ThrottleListener
Register and unregister clients, set loco throttle
  • Field Details

  • Method Details

    • 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(PropertyChangeListener changeListener)
      Set the throttle change listener.
      Parameters:
      changeListener - - the property change listener instance
    • setClientListener

      public void setClientListener(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

      Get a hash map of the registered clients, indexed by their InetAddress
      Returns:
      the hash map of registered clients
    • registerLocoIfNeeded

      public void registerLocoIfNeeded(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 client
      locoAddress - - address of a loco
    • setLocoSpeedAndDirection

      public void setLocoSpeedAndDirection(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 InetAddress
      locoAddress - - the loco address
      speed - - the speed to set
      forward - - true of forward, false if reverse
    • setLocoFunction

      public void setLocoFunction(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 InetAddress
      locoAddress - - the loco address
      functionNumber - - the function number to set
      functionState - - the new state of the function
    • getLocoStatusMessage

      public byte[] getLocoStatusMessage(InetAddress address, Integer locoAddress)
      Return a Z21 LAN_X_LOCO_INFO packet for a given client and loco address
      Parameters:
      address - - client InetAddress
      locoAddress - - the loco address
      Returns:
      Z21 LAN_X_LOCO_INFO packet
    • setTurnout

      public void setTurnout(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 InetAddress
      turnoutNumber - - 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(InetAddress address, 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 InetAdress
      turnoutNumber - - the Z21 Turnout Number
      Returns:
      a Z21 LAN_X_TURNOUT_INFO packet
    • heartbeat

      public void heartbeat(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(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

      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 interface ThrottleListener
      Parameters:
      t - - the (new) throttle bound to the loco.
    • notifyFailedThrottleRequest

      public void notifyFailedThrottleRequest(LocoAddress address, String reason)
      Called from the throttle manager when no throttle can be created for a loco address.
      Specified by:
      notifyFailedThrottleRequest in interface ThrottleListener
      Parameters:
      address - - loco address
      reason - - a message from the throttle manager
    • notifyDecisionRequired

      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 interface ThrottleListener
      Parameters:
      address - - loco address
      question - - 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