Class JmDNS
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
JmDNSImpl
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract void
addServiceListener
(String type, ServiceListener listener) Listen for services of a given type.abstract void
addServiceTypeListener
(ServiceTypeListener listener) Listen for service types.static JmDNS
create()
Create an instance of JmDNS.static JmDNS
Create an instance of JmDNS.static JmDNS
create
(InetAddress addr) Create an instance of JmDNS and bind it to a specific network interface given its IP-address.static JmDNS
create
(InetAddress addr, String name) static JmDNS
create
(InetAddress addr, String name, long threadSleepDurationMs) Create an instance of JmDNS and bind it to a specific network interface given its IP-address.abstract JmDNS.Delegate
Returns the instance delegateabstract String
Return the HostName associated with this JmDNS instance.abstract InetAddress
Return the address of the interface to which this instance of JmDNS is bound.abstract InetAddress
Deprecated.abstract String
getName()
Return the name of the JmDNS instance.abstract ServiceInfo
getServiceInfo
(String type, String name) Get service information.abstract ServiceInfo
getServiceInfo
(String type, String name, boolean persistent) Get service information.abstract ServiceInfo
getServiceInfo
(String type, String name, boolean persistent, long timeout) Get service information.abstract ServiceInfo
getServiceInfo
(String type, String name, long timeout) Get service information.abstract ServiceInfo[]
Returns a list of service infos of the specified type.abstract ServiceInfo[]
Returns a list of service infos of the specified type.abstract Map
<String, ServiceInfo[]> listBySubtype
(String type) Returns a list of service infos of the specified type sorted by subtype.abstract Map
<String, ServiceInfo[]> listBySubtype
(String type, long timeout) Returns a list of service infos of the specified type sorted by subtype.abstract void
Deprecated.since 3.2.2abstract void
registerService
(ServiceInfo info) Register a service.abstract boolean
registerServiceType
(String type) Register a service type.abstract void
removeServiceListener
(String type, ServiceListener listener) Remove listener for services of a given type.abstract void
removeServiceTypeListener
(ServiceTypeListener listener) Remove listener for service types.abstract void
requestServiceInfo
(String type, String name) Request service information.abstract void
requestServiceInfo
(String type, String name, boolean persistent) Request service information.abstract void
requestServiceInfo
(String type, String name, boolean persistent, long timeout) Request service information.abstract void
requestServiceInfo
(String type, String name, long timeout) Request service information.abstract JmDNS.Delegate
setDelegate
(JmDNS.Delegate value) Sets the instance delegateabstract void
Unregister all services.abstract void
unregisterService
(ServiceInfo info) Unregister a service.
-
Field Details
-
VERSION
The version of JmDNS.
-
-
Constructor Details
-
JmDNS
public JmDNS()
-
-
Method Details
-
create
Create an instance of JmDNS.
Note: This is a convenience method. The preferred constructor is
create(InetAddress, String)
.
Check that your platform correctly handle the default localhost IP address and the local hostname. In doubt use the explicit constructor.
This call is equivalent tocreate(null, null)
.- Returns:
- jmDNS instance
- Throws:
IOException
- if an exception occurs during the socket creation- See Also:
-
create
Create an instance of JmDNS and bind it to a specific network interface given its IP-address.
Note: This is a convenience method. The preferred constructor is
create(InetAddress, String)
.
Check that your platform correctly handle the default localhost IP address and the local hostname. In doubt use the explicit constructor.
This call is equivalent tocreate(addr, null)
.- Parameters:
addr
- IP address to bind to.- Returns:
- jmDNS instance
- Throws:
IOException
- if an exception occurs during the socket creation- See Also:
-
create
Create an instance of JmDNS.
Note: This is a convenience method. The preferred constructor is
create(InetAddress, String)
.
Check that your platform correctly handle the default localhost IP address and the local hostname. In doubt use the explicit constructor.
This call is equivalent tocreate(null, name)
.- Parameters:
name
- name of the newly created JmDNS- Returns:
- jmDNS instance
- Throws:
IOException
- if an exception occurs during the socket creation- See Also:
-
create
public static JmDNS create(InetAddress addr, String name, long threadSleepDurationMs) throws IOException Create an instance of JmDNS and bind it to a specific network interface given its IP-address.
Ifaddr
parameter is null this method will try to resolve to a local IP address of the machine using a network discovery:- Check the system property
net.mdns.interface
- Check the JVM local host
- Use the
NetworkTopologyDiscovery
to find a valid network interface and IP. - In the last resort bind to the loopback address. This is non functional in most cases.
name
parameter is null will use the hostname. The hostname is determined by the following algorithm:- Get the hostname from the InetAdress obtained before.
- If the hostname is a reverse lookup default to
JmDNS name
orcomputer
if null. - If the name contains
'.'
replace them by'-'
- Add
.local.
at the end of the name.
Note: If you need to use a custom
NetworkTopologyDiscovery
it must be setup before any call to this method. This is done by setting up aNetworkTopologyDiscovery.Factory.ClassDelegate
and installing it usingNetworkTopologyDiscovery.Factory.setClassDelegate(NetworkTopologyDiscovery.Factory.ClassDelegate)
. This must be done before creating aJmDNS
orJmmDNS
instance.- Parameters:
addr
- IP address to bind to.name
- name of the newly created JmDNSthreadSleepDurationMs
- time in milliseconds that the JmDNS listener thread should sleep between multicast receives- Returns:
- jmDNS instance
- Throws:
IOException
- if an exception occurs during the socket creation
- Check the system property
-
create
create(InetAddress, String, long)
. Default value for threadSleepDurationMs parameter is 0.- Throws:
IOException
-
getName
Return the name of the JmDNS instance. This is an arbitrary string that is useful for distinguishing instances.- Returns:
- name of the JmDNS
-
getHostName
Return the HostName associated with this JmDNS instance. Note: May not be the same as what started. The host name is subject to negotiation.- Returns:
- Host name
-
getInetAddress
Return the address of the interface to which this instance of JmDNS is bound.- Returns:
- Internet Address
- Throws:
IOException
- if there is an error in the underlying protocol, such as a TCP error.
-
getInterface
Deprecated.do not use this implementation yields unpredictable results usegetInetAddress()
Return the address of the interface to which this instance of JmDNS is bound.- Returns:
- Internet Address
- Throws:
IOException
- if there is an error in the underlying protocol, such as a TCP error.
-
getServiceInfo
Get service information. If the information is not cached, the method will block until updated information is received. Usage note: Do not call this method from the AWT event dispatcher thread. You will make the user interface unresponsive.- Parameters:
type
- fully qualified service type, such as_http._tcp.local.
.name
- unqualified service name, such asfoobar
.- Returns:
- null if the service information cannot be obtained
-
getServiceInfo
Get service information. If the information is not cached, the method will block for the given timeout until updated information is received. Usage note: If you call this method from the AWT event dispatcher thread, use a small timeout, or you will make the user interface unresponsive.- Parameters:
type
- full qualified service type, such as_http._tcp.local.
.name
- unqualified service name, such asfoobar
.timeout
- timeout in milliseconds. Typical timeout should be 5s.- Returns:
- null if the service information cannot be obtained
-
getServiceInfo
Get service information. If the information is not cached, the method will block until updated information is received. Usage note: Do not call this method from the AWT event dispatcher thread. You will make the user interface unresponsive.- Parameters:
type
- fully qualified service type, such as_http._tcp.local.
.name
- unqualified service name, such asfoobar
.persistent
- iftrue
ServiceListener.resolveService will be called whenever new new information is received.- Returns:
- null if the service information cannot be obtained
-
getServiceInfo
public abstract ServiceInfo getServiceInfo(String type, String name, boolean persistent, long timeout) Get service information. If the information is not cached, the method will block for the given timeout until updated information is received. Usage note: If you call this method from the AWT event dispatcher thread, use a small timeout, or you will make the user interface unresponsive.- Parameters:
type
- full qualified service type, such as_http._tcp.local.
.name
- unqualified service name, such asfoobar
.persistent
- iftrue
ServiceListener.resolveService will be called whenever new new information is received.timeout
- timeout in milliseconds. Typical timeout should be 5s.- Returns:
- null if the service information cannot be obtained
-
requestServiceInfo
Request service information. The information about the service is requested and the ServiceListener.resolveService method is called as soon as it is available. Usage note: Do not call this method from the AWT event dispatcher thread. You will make the user interface unresponsive.- Parameters:
type
- full qualified service type, such as_http._tcp.local.
.name
- unqualified service name, such asfoobar
.
-
requestServiceInfo
Request service information. The information about the service is requested and the ServiceListener.resolveService method is called as soon as it is available. Usage note: Do not call this method from the AWT event dispatcher thread. You will make the user interface unresponsive.- Parameters:
type
- full qualified service type, such as_http._tcp.local.
.name
- unqualified service name, such asfoobar
.persistent
- iftrue
ServiceListener.resolveService will be called whenever new new information is received.
-
requestServiceInfo
Request service information. The information about the service is requested and the ServiceListener.resolveService method is called as soon as it is available.- Parameters:
type
- full qualified service type, such as_http._tcp.local.
.name
- unqualified service name, such asfoobar
.timeout
- timeout in milliseconds
-
requestServiceInfo
Request service information. The information about the service is requested and the ServiceListener.resolveService method is called as soon as it is available.- Parameters:
type
- full qualified service type, such as_http._tcp.local.
.name
- unqualified service name, such asfoobar
.persistent
- iftrue
ServiceListener.resolveService will be called whenever new new information is received.timeout
- timeout in milliseconds
-
addServiceTypeListener
Listen for service types.- Parameters:
listener
- listener for service types- Throws:
IOException
- if there is an error in the underlying protocol, such as a TCP error.
-
removeServiceTypeListener
Remove listener for service types.- Parameters:
listener
- listener for service types
-
addServiceListener
Listen for services of a given type. The type has to be a fully qualified type name such as_http._tcp.local.
.- Parameters:
type
- full qualified service type, such as_http._tcp.local.
.listener
- listener for service updates
-
removeServiceListener
Remove listener for services of a given type.- Parameters:
type
- full qualified service type, such as_http._tcp.local.
.listener
- listener for service updates
-
registerService
Register a service. The service is registered for access by other jmdns clients. The name of the service may be changed to make it unique.
Note that the givenServiceInfo
is bound to thisJmDNS
instance, and should not be reused for any other registerService(ServiceInfo).- Parameters:
info
- service info to register- Throws:
IOException
- if there is an error in the underlying protocol, such as a TCP error.
-
unregisterService
Unregister a service. The service should have been registered.Note: Unregistered services will not disappear form the list of services immediately. According to the specification, when unregistering services we send goodbye packets and then wait 1s before purging the cache.
This is support for shared records that can be rescued by some other cooperation DNS.Clients receiving a Multicast DNS Response with a TTL of zero SHOULD NOT immediately delete the record from the cache, but instead record a TTL of 1 and then delete the record one second later.
- Parameters:
info
- service info to remove
-
unregisterAllServices
public abstract void unregisterAllServices()Unregister all services. -
registerServiceType
Register a service type. If this service type was not already known, all service listeners will be notified of the new service type.Service types are automatically registered as they are discovered.
- Parameters:
type
- full qualified service type, such as_http._tcp.local.
.- Returns:
true
if the type or subtype was added,false
if the type was already registered.
-
printServices
Deprecated.since 3.2.2List Services and serviceTypes. Debugging Only -
list
Returns a list of service infos of the specified type.- Parameters:
type
- Service type name, such as_http._tcp.local.
.- Returns:
- An array of service instance.
-
list
Returns a list of service infos of the specified type.- Parameters:
type
- Service type name, such as_http._tcp.local.
.timeout
- timeout in milliseconds. Typical timeout should be 6s.- Returns:
- An array of service instance.
-
listBySubtype
Returns a list of service infos of the specified type sorted by subtype. Any service that do not register a subtype is listed in the empty subtype section.- Parameters:
type
- Service type name, such as_http._tcp.local.
.- Returns:
- A dictionary of service info by subtypes.
-
listBySubtype
Returns a list of service infos of the specified type sorted by subtype. Any service that do not register a subtype is listed in the empty subtype section.- Parameters:
type
- Service type name, such as_http._tcp.local.
.timeout
- timeout in milliseconds. Typical timeout should be 6s.- Returns:
- A dictionary of service info by subtypes.
-
getDelegate
Returns the instance delegate- Returns:
- instance delegate
-
setDelegate
Sets the instance delegate- Parameters:
value
- new instance delegate- Returns:
- previous instance delegate
-
getInetAddress()