Package javax.jmdns.impl
Class JmDNSImpl
java.lang.Object
javax.jmdns.JmDNS
javax.jmdns.impl.JmDNSImpl
- All Implemented Interfaces:
Closeable,AutoCloseable,DNSStatefulObject,DNSTaskStarter
mDNS implementation in Java.
- Author:
- Arthur van Hoff, Rick Blair, Jeff Sonstein, Werner Randelshofer, Pierre Frisch, Scott Lewis, Kai Kreuzer, Victor Toni
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic classThis is used to store type entries.protected classShutdown operations.Nested classes/interfaces inherited from class javax.jmdns.JmDNS
JmDNS.DelegateNested classes/interfaces inherited from interface javax.jmdns.impl.DNSStatefulObject
DNSStatefulObject.DefaultImplementation, DNSStatefulObject.DNSStatefulObjectSemaphoreNested classes/interfaces inherited from interface javax.jmdns.impl.DNSTaskStarter
DNSTaskStarter.DNSTaskStarterImpl, DNSTaskStarter.Factory -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ThreadThis is the shutdown hook, we registered with the java runtime. -
Constructor Summary
ConstructorsConstructorDescriptionJmDNSImpl(InetAddress address, String name) Create an instance of JmDNS and bind it to a specific network interface given its IP-address. -
Method Summary
Modifier and TypeMethodDescriptionaddAnswer(DNSIncoming in, InetAddress addr, int port, DNSOutgoing out, DNSRecord rec) Add an answer to a question.voidaddListener(javax.jmdns.impl.DNSListener listener, DNSQuestion question) Add a listener for a question.voidaddServiceListener(String type, ServiceListener listener) Listen for services of a given type.voidaddServiceTypeListener(ServiceTypeListener listener) Listen for service types.booleanadvanceState(DNSTask task) Sets the state and notifies all objects that wait on the ServiceInfo.voidassociateWithTask(DNSTask task, DNSState state) Sets the task associated with this Object.booleanSets the state and notifies all objects that wait on the ServiceInfo.voidCancel the state task timervoidCancel the generals task timervoidChecks the cache of expired records and removes them.voidclose()booleanSets the state and notifies all objects that wait on the ServiceInfo.getCache()Return the DNSCache associated with the cache variableReturns the instance delegategetDns()Returns the DNS associated with this object.getGroup()Return the HostName associated with this JmDNS instance.Return the address of the interface to which this instance of JmDNS is bound.Deprecated.longReturns the local host infogetName()Return the name of the JmDNS instance.static RandomgetServiceInfo(String type, String name) Get service information.getServiceInfo(String type, String name, boolean persistent) Get service information.getServiceInfo(String type, String name, boolean persistent, long timeout) Get service information.getServiceInfo(String type, String name, long timeout) Get service information.intvoidioLock()voidioUnlock()booleanReturns true, if this is an announced state.booleanReturns true, if this is an announcing state.booleanisAssociatedWithTask(DNSTask task, DNSState state) Checks if this object is associated with the task and in the same state.booleanReturns true, if this is a canceled state.booleanReturns true, if this is a canceling state.booleanisClosed()Returns true, if this is a closed state.booleanReturns true, if this is a closing state.booleanReturns true, if this is a probing state.Returns a list of service infos of the specified type.Returns a list of service infos of the specified type.Map<String, ServiceInfo[]> listBySubtype(String type) Returns a list of service infos of the specified type sorted by subtype.Map<String, ServiceInfo[]> listBySubtype(String type, long timeout) Returns a list of service infos of the specified type sorted by subtype.static voidMain method to display API information if run from java -jarvoidDeprecated.voidPurge the state task timervoidPurge the general task timervoidrecover()Recover jmDNS when there is an error.booleanSets the state and notifies all objects that wait on the ServiceInfo.voidregisterService(ServiceInfo infoAbstract) Register a service.booleanregisterServiceType(String type) Register a service type.voidRemove the association of the task with this Object.voidremoveListener(javax.jmdns.impl.DNSListener listener) Remove a listener from all outstanding questions.voidremoveServiceListener(String type, ServiceListener listener) Remove listener for services of a given type.voidremoveServiceTypeListener(ServiceTypeListener listener) Remove listener for service types.voidrenewServiceCollector(String type) Renew a service when the record become stale.voidrequestServiceInfo(String type, String name) Request service information.voidrequestServiceInfo(String type, String name, boolean persistent) Request service information.voidrequestServiceInfo(String type, String name, boolean persistent, long timeout) Request service information.voidrequestServiceInfo(String type, String name, long timeout) Request service information.voidbooleanSets the state and notifies all objects that wait on the ServiceInfo.voidsend(DNSOutgoing out) Send an outgoing multicast DNS message.setDelegate(JmDNS.Delegate delegate) Sets the instance delegatevoidsetLastThrottleIncrement(long lastThrottleIncrement) voidsetPlannedAnswer(DNSIncoming plannedAnswer) voidsetThrottle(int throttle) voidStart a new announcer taskvoidStart a new canceler taskvoidStart a new prober taskvoidStart a new reaper task.voidStart a new renewer taskvoidstartResponder(DNSIncoming in, InetAddress addr, int port) Start a new responder taskvoidStart a new service info resolver taskvoidstartServiceResolver(String type) Start a new service resolver taskvoidStart a new service type resolver tasktoString()voidUnregister all services.voidunregisterService(ServiceInfo infoAbstract) Unregister a service.voidupdateRecord(long now, DNSRecord rec, JmDNSImpl.Operation operation) Notify all listeners that a record was updated.booleanwaitForAnnounced(long timeout) Waits for the object to be announced.booleanwaitForCanceled(long timeout) Waits for the object to be canceled.
-
Field Details
-
_shutdown
This is the shutdown hook, we registered with the java runtime.
-
-
Constructor Details
-
JmDNSImpl
Create an instance of JmDNS and bind it to a specific network interface given its IP-address.- Parameters:
address- IP address to bind to.name- name of the newly created JmDNS- Throws:
IOException
-
-
Method Details
-
main
Main method to display API information if run from java -jar- Parameters:
argv- the command line arguments
-
advanceState
Sets the state and notifies all objects that wait on the ServiceInfo.- Specified by:
advanceStatein interfaceDNSStatefulObject- Parameters:
task- associated task- Returns:
trueinvalid input: '<'/code if the state was changed by this thread,falseotherwise.- See Also:
-
revertState
public boolean revertState()Sets the state and notifies all objects that wait on the ServiceInfo.- Specified by:
revertStatein interfaceDNSStatefulObject- Returns:
trueinvalid input: '<'/code if the state was changed by this thread,falseotherwise.- See Also:
-
cancelState
public boolean cancelState()Sets the state and notifies all objects that wait on the ServiceInfo.- Specified by:
cancelStatein interfaceDNSStatefulObject- Returns:
trueinvalid input: '<'/code if the state was changed by this thread,falseotherwise.
-
closeState
public boolean closeState()Sets the state and notifies all objects that wait on the ServiceInfo.- Specified by:
closeStatein interfaceDNSStatefulObject- Returns:
trueinvalid input: '<'/code if the state was changed by this thread,falseotherwise.
-
recoverState
public boolean recoverState()Sets the state and notifies all objects that wait on the ServiceInfo.- Specified by:
recoverStatein interfaceDNSStatefulObject- Returns:
trueinvalid input: '<'/code if the state was changed by this thread,falseotherwise.
-
getDns
Returns the DNS associated with this object.- Specified by:
getDnsin interfaceDNSStatefulObject- Returns:
- DNS resolver
-
associateWithTask
Sets the task associated with this Object.- Specified by:
associateWithTaskin interfaceDNSStatefulObject- Parameters:
task- associated taskstate- state of the task
-
removeAssociationWithTask
Remove the association of the task with this Object.- Specified by:
removeAssociationWithTaskin interfaceDNSStatefulObject- Parameters:
task- associated task
-
isAssociatedWithTask
Checks if this object is associated with the task and in the same state.- Specified by:
isAssociatedWithTaskin interfaceDNSStatefulObject- Parameters:
task- associated taskstate- state of the task- Returns:
trueis the task is associated with this object,falseotherwise.
-
isProbing
public boolean isProbing()Returns true, if this is a probing state.- Specified by:
isProbingin interfaceDNSStatefulObject- Returns:
trueif probing state,falseotherwise
-
isAnnouncing
public boolean isAnnouncing()Returns true, if this is an announcing state.- Specified by:
isAnnouncingin interfaceDNSStatefulObject- Returns:
trueif announcing state,falseotherwise
-
isAnnounced
public boolean isAnnounced()Returns true, if this is an announced state.- Specified by:
isAnnouncedin interfaceDNSStatefulObject- Returns:
trueif announced state,falseotherwise
-
isCanceling
public boolean isCanceling()Returns true, if this is a canceling state.- Specified by:
isCancelingin interfaceDNSStatefulObject- Returns:
trueif canceling state,falseotherwise
-
isCanceled
public boolean isCanceled()Returns true, if this is a canceled state.- Specified by:
isCanceledin interfaceDNSStatefulObject- Returns:
trueif canceled state,falseotherwise
-
isClosing
public boolean isClosing()Returns true, if this is a closing state.- Specified by:
isClosingin interfaceDNSStatefulObject- Returns:
trueif closing state,falseotherwise
-
isClosed
public boolean isClosed()Returns true, if this is a closed state.- Specified by:
isClosedin interfaceDNSStatefulObject- Returns:
trueif closed state,falseotherwise
-
waitForAnnounced
public boolean waitForAnnounced(long timeout) Waits for the object to be announced.- Specified by:
waitForAnnouncedin interfaceDNSStatefulObject- Parameters:
timeout- the maximum time to wait in milliseconds.- Returns:
trueif the object is announced,falseotherwise
-
waitForCanceled
public boolean waitForCanceled(long timeout) Waits for the object to be canceled.- Specified by:
waitForCanceledin interfaceDNSStatefulObject- Parameters:
timeout- the maximum time to wait in milliseconds.- Returns:
trueif the object is canceled,falseotherwise
-
getCache
Return the DNSCache associated with the cache variable- Returns:
- DNS cache
-
getName
Return the name of the JmDNS instance. This is an arbitrary string that is useful for distinguishing instances. -
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.- Specified by:
getHostNamein classJmDNS- Returns:
- Host name
-
getLocalHost
Returns the local host info- Returns:
- local host info
-
getInetAddress
Return the address of the interface to which this instance of JmDNS is bound.- Specified by:
getInetAddressin classJmDNS- Returns:
- Internet Address
- Throws:
IOException- if there is an error in the underlying protocol, such as a TCP error.
-
getInterface
Deprecated.Return the address of the interface to which this instance of JmDNS is bound.- Specified by:
getInterfacein classJmDNS- 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.- Specified by:
getServiceInfoin classJmDNS- 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.- Specified by:
getServiceInfoin classJmDNS- 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.- Specified by:
getServiceInfoin classJmDNS- Parameters:
type- fully qualified service type, such as_http._tcp.local..name- unqualified service name, such asfoobar.persistent- iftrueServiceListener.resolveService will be called whenever new new information is received.- 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.- Specified by:
getServiceInfoin classJmDNS- Parameters:
type- full qualified service type, such as_http._tcp.local..name- unqualified service name, such asfoobar.persistent- iftrueServiceListener.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.- Specified by:
requestServiceInfoin classJmDNS- 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.- Specified by:
requestServiceInfoin classJmDNS- Parameters:
type- full qualified service type, such as_http._tcp.local..name- unqualified service name, such asfoobar.persistent- iftrueServiceListener.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.- Specified by:
requestServiceInfoin classJmDNS- 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.- Specified by:
requestServiceInfoin classJmDNS- Parameters:
type- full qualified service type, such as_http._tcp.local..name- unqualified service name, such asfoobar.persistent- iftrueServiceListener.resolveService will be called whenever new new information is received.timeout- timeout in milliseconds
-
addServiceTypeListener
Listen for service types.- Specified by:
addServiceTypeListenerin classJmDNS- 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.- Specified by:
removeServiceTypeListenerin classJmDNS- 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..- Specified by:
addServiceListenerin classJmDNS- 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.- Specified by:
removeServiceListenerin classJmDNS- 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 givenServiceInfois bound to thisJmDNSinstance, and should not be reused for any other JmDNS.registerService(ServiceInfo).- Specified by:
registerServicein classJmDNS- Parameters:
infoAbstract- 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.
- Specified by:
unregisterServicein classJmDNS- Parameters:
infoAbstract- service info to remove
-
unregisterAllServices
public void unregisterAllServices()Unregister all services.- Specified by:
unregisterAllServicesin classJmDNS
-
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.
- Specified by:
registerServiceTypein classJmDNS- Parameters:
type- full qualified service type, such as_http._tcp.local..- Returns:
trueif the type or subtype was added,falseif the type was already registered.
-
addListener
Add a listener for a question. The listener will receive updates of answers to the question as they arrive, or from the cache if they are already available.- Parameters:
listener- DSN listenerquestion- DNS query
-
removeListener
public void removeListener(javax.jmdns.impl.DNSListener listener) Remove a listener from all outstanding questions. The listener will no longer receive any updates.- Parameters:
listener- DSN listener
-
renewServiceCollector
Renew a service when the record become stale. If there is no service collector for the type this method does nothing.- Parameters:
type- Service Type
-
updateRecord
Notify all listeners that a record was updated.- Parameters:
now- update daterec- DNS recordoperation- DNS cache operation
-
respondToQuery
-
addAnswer
public DNSOutgoing addAnswer(DNSIncoming in, InetAddress addr, int port, DNSOutgoing out, DNSRecord rec) throws IOException Add an answer to a question. Deal with the case when the outgoing packet overflows- Parameters:
in-addr-port-out-rec-- Returns:
- outgoing answer
- Throws:
IOException
-
send
Send an outgoing multicast DNS message.- Parameters:
out-- Throws:
IOException
-
purgeTimer
public void purgeTimer()Description copied from interface:DNSTaskStarterPurge the general task timer- Specified by:
purgeTimerin interfaceDNSTaskStarter
-
purgeStateTimer
public void purgeStateTimer()Description copied from interface:DNSTaskStarterPurge the state task timer- Specified by:
purgeStateTimerin interfaceDNSTaskStarter
-
cancelTimer
public void cancelTimer()Description copied from interface:DNSTaskStarterCancel the generals task timer- Specified by:
cancelTimerin interfaceDNSTaskStarter
-
cancelStateTimer
public void cancelStateTimer()Description copied from interface:DNSTaskStarterCancel the state task timer- Specified by:
cancelStateTimerin interfaceDNSTaskStarter
-
startProber
public void startProber()Description copied from interface:DNSTaskStarterStart a new prober task- Specified by:
startProberin interfaceDNSTaskStarter
-
startAnnouncer
public void startAnnouncer()Description copied from interface:DNSTaskStarterStart a new announcer task- Specified by:
startAnnouncerin interfaceDNSTaskStarter
-
startRenewer
public void startRenewer()Description copied from interface:DNSTaskStarterStart a new renewer task- Specified by:
startRenewerin interfaceDNSTaskStarter
-
startCanceler
public void startCanceler()Description copied from interface:DNSTaskStarterStart a new canceler task- Specified by:
startCancelerin interfaceDNSTaskStarter
-
startReaper
public void startReaper()Description copied from interface:DNSTaskStarterStart a new reaper task. There is only supposed to be one reaper running at a time.- Specified by:
startReaperin interfaceDNSTaskStarter
-
startServiceInfoResolver
Description copied from interface:DNSTaskStarterStart a new service info resolver task- Specified by:
startServiceInfoResolverin interfaceDNSTaskStarter- Parameters:
info- service info to resolve
-
startTypeResolver
public void startTypeResolver()Description copied from interface:DNSTaskStarterStart a new service type resolver task- Specified by:
startTypeResolverin interfaceDNSTaskStarter
-
startServiceResolver
Description copied from interface:DNSTaskStarterStart a new service resolver task- Specified by:
startServiceResolverin interfaceDNSTaskStarter- Parameters:
type- service type to resolve
-
startResponder
Description copied from interface:DNSTaskStarterStart a new responder task- Specified by:
startResponderin interfaceDNSTaskStarter- Parameters:
in- incoming messageaddr- incoming addressport- incoming port
-
recover
public void recover()Recover jmDNS when there is an error. -
cleanCache
public void cleanCache()Checks the cache of expired records and removes them. If any records are about to expire it tries to get them refreshed.Implementation note:
This method is called by theRecordReapereveryDNSConstants.RECORD_REAPER_INTERVALmilliseconds.- See Also:
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
printServices
Deprecated.List Services and serviceTypes. Debugging Only- Specified by:
printServicesin classJmDNS
-
toString
-
list
Returns a list of service infos of the specified type. -
list
Returns a list of service infos of the specified type. -
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.- Specified by:
listBySubtypein classJmDNS- 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.- Specified by:
listBySubtypein classJmDNS- 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.
-
getServices
-
setLastThrottleIncrement
public void setLastThrottleIncrement(long lastThrottleIncrement) -
getLastThrottleIncrement
public long getLastThrottleIncrement() -
setThrottle
public void setThrottle(int throttle) -
getThrottle
public int getThrottle() -
getRandom
-
ioLock
public void ioLock() -
ioUnlock
public void ioUnlock() -
setPlannedAnswer
-
getPlannedAnswer
-
getServiceTypes
-
getSocket
-
getGroup
-
getDelegate
Description copied from class:JmDNSReturns the instance delegate- Specified by:
getDelegatein classJmDNS- Returns:
- instance delegate
-
setDelegate
Description copied from class:JmDNSSets the instance delegate- Specified by:
setDelegatein classJmDNS- Parameters:
delegate- new instance delegate- Returns:
- previous instance delegate
-