Class DeviceFinder


  • public class DeviceFinder
    extends LifecycleParticipant
    Watches for devices to report their presence by broadcasting announcement packets on port 50000, and keeps a list of the devices that have been seen, and the network address on which they were seen.
    Author:
    James Elliott
    • Field Detail

      • ANNOUNCEMENT_PORT

        public static final int ANNOUNCEMENT_PORT
        The port to which devices broadcast announcement messages to report their presence on the network.
        See Also:
        Constant Field Values
      • MAXIMUM_AGE

        public static final int MAXIMUM_AGE
        The number of milliseconds after which we will consider a device to have disappeared if we have not received an announcement from it.
        See Also:
        Constant Field Values
    • Method Detail

      • isRunning

        public boolean isRunning()
        Check whether we are presently listening for device announcements.
        Specified by:
        isRunning in class LifecycleParticipant
        Returns:
        true if our socket is open and monitoring for DJ Link device announcements on the network
      • getStartTime

        public long getStartTime()
        Get the timestamp of when we started listening for device announcements.
        Returns:
        the system millisecond timestamp when start() was called.
        Throws:
        IllegalStateException - if we are not listening for announcements.
      • getFirstDeviceTime

        public long getFirstDeviceTime()
        Get the timestamp of when we saw the first announcement packet, to help the VirtualCdj determine how long it needs to watch for devices in order to avoid conflicts when self-assigning a device number.
        Returns:
        the system millisecond timestamp when the first device announcement was received.
        Throws:
        IllegalStateException - if we are not listening for announcements, or if none have been seen.
      • addIgnoredAddress

        public void addIgnoredAddress​(InetAddress address)
        Start ignoring any device updates which are received from the specified address. Intended for use by the VirtualCdj, so that its updates do not cause it to appear as a device.
        Parameters:
        address - the address from which any device updates should be ignored.
      • removeIgnoredAddress

        public void removeIgnoredAddress​(InetAddress address)
        Stop ignoring device updates which are received from the specified address. Intended for use by the VirtualCdj, so that when it shuts down, its socket stops being treated specially.
        Parameters:
        address - the address from which any device updates should be ignored.
      • isAddressIgnored

        public boolean isAddressIgnored​(InetAddress address)
        Check whether an address is being ignored. (The BeatFinder will call this so it can filter out the VirtualCdj's beat messages when it is broadcasting them, for example.
        Parameters:
        address - the address to be checked as a candidate to be ignored
        Returns:
        true if packets from the address should be ignored
      • start

        public void start()
                   throws SocketException
        Start listening for device announcements and keeping track of the DJ Link devices visible on the network. If already listening, has no effect.
        Throws:
        SocketException - if the socket to listen on port 50000 cannot be created
      • stop

        public void stop()
        Stop listening for device announcements. Also discard any announcements which had been received, and notify any registered listeners that those devices have been lost.
      • getCurrentDevices

        public Set<DeviceAnnouncement> getCurrentDevices()
        Get the set of DJ Link devices which currently can be seen on the network. These can be passed to VirtualCdj.getLatestStatusFor(DeviceUpdate) to find the current detailed status for that device, as long as the Virtual CDJ is active.
        Returns:
        the devices which have been heard from recently enough to be considered present on the network
        Throws:
        IllegalStateException - if the DeviceFinder is not active
      • getLatestAnnouncementFrom

        public DeviceAnnouncement getLatestAnnouncementFrom​(int deviceNumber)
        Find and return the device announcement that was most recently received from a device identifying itself with the specified device number, if any.
        Parameters:
        deviceNumber - the device number of interest
        Returns:
        the matching announcement or null if no such device has been heard from
        Throws:
        IllegalStateException - if the DeviceFinder is not active
      • addDeviceAnnouncementListener

        public void addDeviceAnnouncementListener​(DeviceAnnouncementListener listener)
        Adds the specified device announcement listener to receive device announcements when DJ Link devices are found on or leave the network. If listener is null or already present in the list of registered listeners, no exception is thrown and no action is performed.

        Device announcements are delivered to listeners on the Event Dispatch thread, so it is fine to interact with user interface objects in listener methods. Any code in the listener method must finish quickly, or unhandled events will back up and the user interface will be come unresponsive.

        Parameters:
        listener - the device announcement listener to add
      • removeDeviceAnnouncementListener

        public void removeDeviceAnnouncementListener​(DeviceAnnouncementListener listener)
        Removes the specified device announcement listener so that it no longer receives device announcements when DJ Link devices are found on or leave the network. If listener is null or not present in the list of registered listeners, no exception is thrown and no action is performed.
        Parameters:
        listener - the device announcement listener to remove
      • getDeviceAnnouncementListeners

        public Set<DeviceAnnouncementListener> getDeviceAnnouncementListeners()
        Get the set of device announcement listeners that are currently registered.
        Returns:
        the currently registered device announcement listeners
      • getInstance

        public static DeviceFinder getInstance()
        Get the singleton instance of this class.
        Returns:
        the only instance of this class which exists.