Class ZeroConfService


  • public class ZeroConfService
    extends java.lang.Object
    ZeroConfService objects manage a zeroConf network service advertisement.

    ZeroConfService objects encapsulate zeroConf network services created using JmDNS, providing methods to start and stop service advertisements and to query service state. Typical usage would be:

     ZeroConfService myService = ZeroConfService.create("_withrottle._tcp.local.", port);
     myService.publish();
     
    or, if you do not wish to retain the ZeroConfService object:
     ZeroConfService.create("_http._tcp.local.", port).publish();
     
    ZeroConfService objects can also be created with a HashMap of properties that are included in the TXT record for the service advertisement. This HashMap should remain small, but it could include information such as the default path (for a web server), a specific protocol version, or other information. Note that all service advertisements include the JMRI version, using the key "version", and the JMRI version numbers in a string "major.minor.test" with the key "jmri"

    All ZeroConfServices are published with the computer's hostname as the mDNS hostname (unless it cannot be determined by JMRI), as well as the JMRI node name in the TXT record with the key "node".

    All ZeroConfServices are automatically stopped when the JMRI application shuts down. Use ZeroConfServiceManager.allServices() to get a collection of all published ZeroConfService objects.


    This file is part of JMRI.

    JMRI is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. See the "COPYING" file for a copy of this license.

    JMRI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

    See Also:
    JmDNS, ServiceInfo
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String IPv4  
      static java.lang.String IPv6  
      static java.lang.String LINKLOCAL  
      static java.lang.String LOOPBACK  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected ZeroConfService​(javax.jmdns.ServiceInfo service)
      Create a ZeroConfService object.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addEventListener​(ZeroConfServiceListener l)  
      (package private) javax.jmdns.ServiceInfo addServiceInfo​(java.net.InetAddress address)
      Add the ServiceInfo for the given address.
      (package private) boolean containsServiceInfo​(java.net.InetAddress key)
      Check if a ServiceInfo exists for the given address.
      static ZeroConfService create​(java.lang.String type, int port)
      Create a ZeroConfService with the minimal required settings.
      static ZeroConfService create​(java.lang.String type, int port, java.util.HashMap<java.lang.String,​java.lang.String> properties)
      Create a ZeroConfService with an automatically detected server name.
      static ZeroConfService create​(java.lang.String type, java.lang.String name, int port, int weight, int priority, java.util.HashMap<java.lang.String,​java.lang.String> properties)
      Create a ZeroConfService.
      java.lang.String getKey()
      Get the key of the ZeroConfService object.
      java.util.List<ZeroConfServiceListener> getListeners()
      Get a list of the listeners for this service.
      java.lang.String getName()
      Get the name of the ZeroConfService object.
      javax.jmdns.ServiceInfo getServiceInfo()
      Get the reference ServiceInfo for the object.
      (package private) javax.jmdns.ServiceInfo getServiceInfo​(java.net.InetAddress address)
      Get the ServiceInfo for the given address.
      java.lang.String getType()
      Get the type of the ZeroConfService object.
      boolean isPublished()
      Get the state of the service.
      void publish()
      Start advertising the service.
      void removeEventListener​(ZeroConfServiceListener l)  
      (package private) void removeServiceInfo​(java.net.InetAddress address)
      Remove the ServiceInfo for the given address.
      void stop()
      Stop advertising the service.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ZeroConfService

        protected ZeroConfService​(javax.jmdns.ServiceInfo service)
        Create a ZeroConfService object.
        Parameters:
        service - the JmDNS service information
    • Method Detail

      • create

        public static ZeroConfService create​(java.lang.String type,
                                             int port,
                                             java.util.HashMap<java.lang.String,​java.lang.String> properties)
        Create a ZeroConfService with an automatically detected server name. This method calls create(java.lang.String, java.lang.String, int, int, int, java.util.HashMap) with the default weight and priority, and with the result of WebServerPreferences.getRailroadName() reformatted to replace dots and dashes with spaces.
        Parameters:
        type - The service protocol
        port - The port the service runs over
        properties - Additional information to be listed in service advertisement
        Returns:
        A new unpublished ZeroConfService, or an existing service
      • create

        public static ZeroConfService create​(java.lang.String type,
                                             java.lang.String name,
                                             int port,
                                             int weight,
                                             int priority,
                                             java.util.HashMap<java.lang.String,​java.lang.String> properties)
        Create a ZeroConfService. The property version is added or replaced with the current JMRI version as its value. The property jmri is added or replaced with the JMRI major.minor.test version string as its value.

        If a service with the same key as the new service is already published, the original service is returned unmodified.

        Parameters:
        type - The service protocol
        name - The name of the JMRI server listed on client devices
        port - The port the service runs over
        weight - Default value is 0
        priority - Default value is 0
        properties - Additional information to be listed in service advertisement
        Returns:
        A new unpublished ZeroConfService, or an existing service
      • getKey

        public java.lang.String getKey()
        Get the key of the ZeroConfService object. The key is fully qualified name of the service in all lowercase, for example jmri._http.local .
        Returns:
        The fully qualified name of the service
      • getName

        public java.lang.String getName()
        Get the name of the ZeroConfService object. The name can only be set when creating the object.
        Returns:
        The service name as reported by the ServiceInfo object
      • getType

        public java.lang.String getType()
        Get the type of the ZeroConfService object. The type can only be set when creating the object.
        Returns:
        The service type as reported by the ServiceInfo object
      • getServiceInfo

        javax.jmdns.ServiceInfo getServiceInfo​(java.net.InetAddress address)
        Get the ServiceInfo for the given address. Package private so can be managed by ZeroConfServiceManager, but not in public API.
        Parameters:
        address - the address associated with the ServiceInfo to get
        Returns:
        the ServiceInfo for the address or null if none exists
      • addServiceInfo

        javax.jmdns.ServiceInfo addServiceInfo​(java.net.InetAddress address)
        Add the ServiceInfo for the given address. Package private so can be managed by ZeroConfServiceManager, but not in public API.
        Parameters:
        address - the address associated with the ServiceInfo to add
        Returns:
        the added ServiceInfo for the address
      • removeServiceInfo

        void removeServiceInfo​(java.net.InetAddress address)
        Remove the ServiceInfo for the given address. Package private so can be managed by ZeroConfServiceManager, but not in public API.
        Parameters:
        address - the address associated with the ServiceInfo to remove
      • containsServiceInfo

        boolean containsServiceInfo​(java.net.InetAddress key)
        Check if a ServiceInfo exists for the given address. Package private so can be managed by ZeroConfServiceManager, but not in public API.
        Parameters:
        key - the address associated with the ServiceInfo to check for
        Returns:
        true if the ServiceInfo exists; false otherwise
      • getServiceInfo

        public javax.jmdns.ServiceInfo getServiceInfo()
        Get the reference ServiceInfo for the object. This is the JmDNS implementation of a zeroConf service. The reference ServiceInfo is never actually registered with a JmDNS service, since registrations with a JmDNS service are unique per InetAddress.
        Returns:
        The getServiceInfo object.
      • isPublished

        public boolean isPublished()
        Get the state of the service.
        Returns:
        True if the service is being advertised, and false otherwise.
      • publish

        public void publish()
        Start advertising the service.
      • stop

        public void stop()
        Stop advertising the service.