Class ZeroConfService
- java.lang.Object
-
- jmri.util.zeroconf.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
-
-
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.
-
-
-
Field Detail
-
IPv4
public static final java.lang.String IPv4
- See Also:
- Constant Field Values
-
IPv6
public static final java.lang.String IPv6
- See Also:
- Constant Field Values
-
LOOPBACK
public static final java.lang.String LOOPBACK
- See Also:
- Constant Field Values
-
LINKLOCAL
public static final java.lang.String LINKLOCAL
- See Also:
- Constant Field Values
-
-
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)
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(java.lang.String, java.lang.String, int, int, int, java.util.HashMap)
-
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 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(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 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
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 examplejmri._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 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
javax.jmdns.ServiceInfo addServiceInfo(java.net.InetAddress address)
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
void removeServiceInfo(java.net.InetAddress address)
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
boolean containsServiceInfo(java.net.InetAddress key)
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
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
public void addEventListener(ZeroConfServiceListener l)
-
removeEventListener
public void removeEventListener(ZeroConfServiceListener l)
-
getListeners
public java.util.List<ZeroConfServiceListener> getListeners()
Get a list of the listeners for this service.- Returns:
- the listeners or an empty list if none
-
-