Class ZeroConfService
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:
-
JmDNSServiceInfo
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedZeroConfService(javax.jmdns.ServiceInfo service) Create a ZeroConfService object. -
Method Summary
Modifier and TypeMethodDescriptionvoid(package private) javax.jmdns.ServiceInfoaddServiceInfo(InetAddress address) Add the ServiceInfo for the given address.(package private) booleanCheck if a ServiceInfo exists for the given address.static ZeroConfServiceCreate a ZeroConfService with the minimal required settings.static ZeroConfServiceCreate a ZeroConfService with an automatically detected server name.static ZeroConfServicecreate(String type, String name, int port, int weight, int priority, HashMap<String, String> properties) Create a ZeroConfService.getKey()Get the key of the ZeroConfService object.Get a list of the listeners for this service.getName()Get the name of the ZeroConfService object.javax.jmdns.ServiceInfoGet the reference ServiceInfo for the object.(package private) javax.jmdns.ServiceInfogetServiceInfo(InetAddress address) Get the ServiceInfo for the given address.getType()Get the type of the ZeroConfService object.booleanGet the state of the service.voidpublish()Start advertising the service.void(package private) voidremoveServiceInfo(InetAddress address) Remove the ServiceInfo for the given address.voidstop()Stop advertising the service.
-
Field Details
-
IPv4
- See Also:
-
IPv6
- See Also:
-
LOOPBACK
- See Also:
-
LINKLOCAL
- See Also:
-
-
Constructor Details
-
ZeroConfService
Create a ZeroConfService object.- Parameters:
service- the JmDNS service information
-
-
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 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 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
-
getKey
Get the key of the ZeroConfService object. The key is fully qualified name of the service in all lowercase, for examplejmri._http.local.- Returns:
- The fully qualified name of the service
-
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
ServiceInfoobject
-
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
ServiceInfoobject
-
getServiceInfo
Get the ServiceInfo for the given address. Package private so can be managed byZeroConfServiceManager, 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
Add the ServiceInfo for the given address. Package private so can be managed byZeroConfServiceManager, 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 byZeroConfServiceManager, 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 byZeroConfServiceManager, 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
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
Get the state of the service.- Returns:
- True if the service is being advertised, and false otherwise.
-
publish
Start advertising the service. -
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
-