Class ZeroConfServiceManager
- All Implemented Interfaces:
Disposable,InstanceManagerAutoDefault
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 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:
-
JmDNSServiceInfo
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classstatic enum -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String(package private) static final HashMap<InetAddress,javax.jmdns.JmDNS> There can only be oneJmDNSobject perInetAddressper JVM, so this collection of JmDNS objects is static.protected final ZeroConfServiceManager.NetworkListenerprotected final ZeroConfPreferencesprotected final HashMap<String,ZeroConfService> protected final Runnable -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionA list of published ZeroConfServicesCreate a ZeroConfService with the minimal required settings.Create a ZeroConfService with an automatically detected server name.create(String type, String name, int port, int weight, int priority, HashMap<String, String> properties) Create a ZeroConfService.voiddispose()Called when disposing of a disposable.FQDN(InetAddress address) Return the fully qualified domain name or "computer" if the system name cannot be determined.Get all addresses that JmDNS instances can be created for excluding loopback addresses.getAddresses(ZeroConfServiceManager.Protocol protocol) Get all addresses that JmDNS instances can be created for excluding loopback addresses.getAddresses(ZeroConfServiceManager.Protocol protocol, boolean useLinkLocal, boolean useLoopback) Get all addresses of a specific IP protocol that JmDNS instances can be created for.(package private) HashMap<InetAddress,javax.jmdns.JmDNS> getDNSes()The list of JmDNS handlers.static StringReturn an RFC 1123 compliant host name in all lower-case punycode from a given string.hostName(InetAddress address) Return the system name or "computer" if the system name cannot be determined.booleanisPublished(ZeroConfService service) protected StringGenerate a ZeroConfService getKey for searching in the HashMap of running services.voidpublish(ZeroConfService service) Start advertising the service.voidstop(ZeroConfService service) Stop advertising the service.voidstopAll()Stop advertising all services.
-
Field Details
-
JMDNS_SERVICES
There can only be oneJmDNSobject perInetAddressper JVM, so this collection of JmDNS objects is static. All access must be throughgetDNSes()to ensure this is populated correctly. -
services
-
networkListener
-
shutDownTask
-
DNS_CLOSE_THREAD_NAME
- See Also:
-
preferences
-
-
Constructor Details
-
ZeroConfServiceManager
public ZeroConfServiceManager()
-
-
Method Details
-
create
Create a ZeroConfService with the minimal required settings. This method callscreate(java.lang.String, int, java.util.HashMap)with an empty props HashMap.- Parameters:
type- The service protocolport- The port the service runs over- Returns:
- A new unpublished ZeroConfService, or an existing service
- See Also:
-
create
Create a ZeroConfService with an automatically detected server name. This method callscreate(java.lang.String, java.lang.String, int, int, int, java.util.HashMap)with the default weight and priority, and with the result ofWebServerPreferences.getRailroadName()reformatted to replace dots and dashes with spaces.- Parameters:
type- The service protocolport- The port the service runs overproperties- Additional information to be listed in service advertisement- Returns:
- A new unpublished ZeroConfService, or an existing service
-
create
public 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 getKey as the new service is already published, the original service is returned unmodified.
- Parameters:
type- The service protocolname- The name of the JMRI server listed on client devicesport- The port the service runs overweight- Default value is 0priority- Default value is 0properties- Additional information to be listed in service advertisement- Returns:
- A new unpublished ZeroConfService, or an existing service
-
key
Generate a ZeroConfService getKey for searching in the HashMap of running services.- Parameters:
type- the service type (usually a protocol name or mapping)name- the service name (usually the JMRI railroad name or system host name)- Returns:
- The combination of the name and type of the service.
-
publish
Start advertising the service.- Parameters:
service- The service to publish
-
stop
Stop advertising the service.- Parameters:
service- The service to stop advertising
-
stopAll
Stop advertising all services. -
allServices
A list of published ZeroConfServices- Returns:
- Collection of ZeroConfServices
-
getDNSes
HashMap<InetAddress,javax.jmdns.JmDNS> getDNSes()The list of JmDNS handlers. This is package private.- Returns:
- a
HashMapofJmDNSobjects, accessible byInetAddresskeys.
-
getAddresses
Get all addresses that JmDNS instances can be created for excluding loopback addresses. -
getAddresses
Get all addresses that JmDNS instances can be created for excluding loopback addresses.- Parameters:
protocol- the Internet protocol- Returns:
- the addresses
- See Also:
-
getAddresses
@Nonnull public Set<InetAddress> getAddresses(ZeroConfServiceManager.Protocol protocol, boolean useLinkLocal, boolean useLoopback) Get all addresses of a specific IP protocol that JmDNS instances can be created for.- Parameters:
protocol- the IP protocol addresses to returnuseLinkLocal- true to include link-local addresses; false otherwiseuseLoopback- true to include loopback addresses; false otherwise- Returns:
- the addresses
- See Also:
-
hostName
Return an RFC 1123 compliant host name in all lower-case punycode from a given string.RFC 1123 mandates that host names contain only the ASCII characters a-z, digits, minus signs ("-") and that the host name be not longer than 63 characters.
Punycode converts non-ASCII characters into an ASCII encoding per RFC 3492, so this method repeatedly converts the name into punycode, shortening the name, until the punycode converted name is 63 characters or less in length.
If the input string cannot be converted to puny code, or is an empty string, the input is replaced with
NodeIdentity.networkIdentity().The algorithm for converting the input is:
- Convert to lower case using the
Locale.ROOTlocale. - Remove any leading whitespace, dots ("."), underscores ("_"), and minus signs ("-")
- Truncate to 63 characters if necessary
- Convert whitespace, dots ("."), and underscores ("_") to minus signs ("-")
- Repeatedly convert to punycode, removing the last character as needed until the punycode is 63 characters or less
- Repeat process with NodeIdentity#networkIdentity() as input if above never yields a usable host name
- Parameters:
string- String to convert to host name- Returns:
- An RFC 1123 compliant host name
- Convert to lower case using the
-
hostName
Return the system name or "computer" if the system name cannot be determined. This method returns the first part of the fully qualified domain name fromFQDN(java.net.InetAddress).- Parameters:
address- TheInetAddressfor the host name.- Returns:
- The hostName associated with the first interface encountered.
-
FQDN
Return the fully qualified domain name or "computer" if the system name cannot be determined. This method uses theJmDNS.getHostName()method to get the name.- Parameters:
address- TheInetAddressfor the FQDN.- Returns:
- The fully qualified domain name.
-
getPreferences
-
isPublished
-
dispose
Description copied from interface:DisposableCalled when disposing of a disposable.Note there are no assurances this method will not be called multiple times against a single instance of this Disposable. It is the responsibility of this Disposable to protect itself and the application from ensuring that calling this method multiple times has no unwanted side effects.
- Specified by:
disposein interfaceDisposable
-