Class ZeroConfService

java.lang.Object
jmri.util.zeroconf.ZeroConfService

public class ZeroConfService extends 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 Details

  • Constructor Details

    • ZeroConfService

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

    • create

      public static ZeroConfService create(String type, int port)
      Create a ZeroConfService with the minimal required settings. This method calls create(java.lang.String, int, java.util.HashMap) with an empty props HashMap.
      Parameters:
      type - The service protocol
      port - The port the service runs over
      Returns:
      A new unpublished ZeroConfService, or an existing service
      See Also:
    • create

      public static ZeroConfService create(String type, int port, HashMap<String,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(String type, String name, int port, int weight, int priority, HashMap<String,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 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 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 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(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(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

      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

      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.
    • addEventListener

    • removeEventListener

    • getListeners

      Get a list of the listeners for this service.
      Returns:
      the listeners or an empty list if none