Class ClientManager

    • 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 address
      java.util.HashMap<java.net.InetAddress,​AppClient> getRegisteredClients()
      Get a hash map of the registered clients, indexed by their InetAddress
      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.
      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
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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,​AppClientgetRegisteredClients()
        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 client
        locoAddress - - 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 InetAddress
        locoAddress - - the loco address
        speed - - the speed to set
        forward - - 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 InetAddress
        locoAddress - - the loco address
        functionNumber - - the function number to set
        functionState - - 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 InetAddress
        locoAddress - - 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 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​(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 InetAdress
        turnoutNumber - - 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 interface ThrottleListener
        Parameters:
        t - - the (new) throttle bound to the loco.
      • xor

        public static byte xor​(byte[] packet)
        Helper to construct the Z21 protocol XOR byte
        Parameters:
        packet - - Z21 packet
        Returns:
        the XOR byte