Class JmDNSImpl
- java.lang.Object
-
- javax.jmdns.JmDNS
-
- javax.jmdns.impl.JmDNSImpl
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,DNSStatefulObject,DNSTaskStarter
public class JmDNSImpl extends JmDNS implements DNSStatefulObject, DNSTaskStarter
mDNS implementation in Java.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJmDNSImpl.Operationprivate static classJmDNSImpl.ServiceCollectorInstances of ServiceCollector are used internally to speed up the performance of methodlist(type).static classJmDNSImpl.ServiceTypeEntryThis is used to store type entries.protected classJmDNSImpl.ShutdownShutdown operations.-
Nested classes/interfaces inherited from class javax.jmdns.JmDNS
JmDNS.Delegate
-
Nested classes/interfaces inherited from interface javax.jmdns.impl.DNSStatefulObject
DNSStatefulObject.DefaultImplementation, DNSStatefulObject.DNSStatefulObjectSemaphore
-
Nested classes/interfaces inherited from interface javax.jmdns.impl.DNSTaskStarter
DNSTaskStarter.DNSTaskStarterImpl, DNSTaskStarter.Factory
-
-
Field Summary
Fields Modifier and Type Field Description private DNSCache_cacheCache for DNSEntry's.private JmDNS.Delegate_delegateprivate java.util.concurrent.ExecutorService_executorprivate java.net.InetAddress_groupThis is the multicast group, we are listening to for multicast DNS messages.private java.lang.Thread_incomingListenerprivate java.util.concurrent.locks.ReentrantLock_ioLockThis lock is used to coordinate processing of incoming and outgoing messages.private long_lastThrottleIncrementLast throttle increment.private java.util.List<DNSListener>_listenersHolds instances of JmDNS.DNSListener.private HostInfo_localHostHandle on the local hostprivate java.lang.String_nameprivate DNSIncoming_plannedAnswerIf an incoming package which needs an answer is truncated, we store it here.private static java.util.Random_randomThe source for random values.private java.lang.Object_recoverLockprivate java.util.concurrent.ConcurrentMap<java.lang.String,JmDNSImpl.ServiceCollector>_serviceCollectorsThis hashtable is used to maintain a list of service types being collected by this JmDNS instance.(package private) java.util.concurrent.ConcurrentMap<java.lang.String,java.util.List<ListenerStatus.ServiceListenerStatus>>_serviceListenersHolds instances of ServiceListener's.private java.util.concurrent.ConcurrentMap<java.lang.String,ServiceInfo>_servicesThis hashtable holds the services that have been registered.private java.util.concurrent.ConcurrentMap<java.lang.String,JmDNSImpl.ServiceTypeEntry>_serviceTypesThis hashtable holds the service types that have been registered or that have been received in an incoming datagram.
Keys are instances of String which hold an all lower-case version of the fully qualified service type.
Values hold the fully qualified service type.protected java.lang.Thread_shutdownThis is the shutdown hook, we registered with the java runtime.private java.net.MulticastSocket_socketThis is our multicast socket.protected long_threadSleepDurationMsprivate int_throttleThrottle count.private java.util.Set<ListenerStatus.ServiceTypeListenerStatus>_typeListenersHolds instances of ServiceTypeListener's.private static org.slf4j.Loggerlogger
-
Constructor Summary
Constructors Constructor Description JmDNSImpl(java.net.InetAddress address, java.lang.String name)Create an instance of JmDNS and bind it to a specific network interface given its IP-address.JmDNSImpl(java.net.InetAddress address, java.lang.String name, long threadSleepDurationMs)Create an instance of JmDNS and bind it to a specific network interface given its IP-address.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description (package private) void__recover()DNSOutgoingaddAnswer(DNSIncoming in, java.net.InetAddress addr, int port, DNSOutgoing out, DNSRecord rec)Add an answer to a question.voidaddListener(DNSListener listener, DNSQuestion question)Add a listener for a question.voidaddServiceListener(java.lang.String type, ServiceListener listener)Listen for services of a given type.private voidaddServiceListener(java.lang.String type, ServiceListener listener, boolean synch)voidaddServiceTypeListener(ServiceTypeListener listener)Listen for service types.booleanadvanceState(DNSTask task)Sets the state and notifies all objects that wait on the ServiceInfo.private java.util.List<DNSRecord>aRecordsLast(java.util.List<DNSRecord> allAnswers)In case the a record is received before the srv record the ip address would not be set.voidassociateWithTask(DNSTask task, DNSState state)Sets the task associated with this Object.booleancancelState()Sets the state and notifies all objects that wait on the ServiceInfo.voidcancelStateTimer()Cancel the state task timervoidcancelTimer()Cancel the generals task timervoidcleanCache()Checks the cache of expired records and removes them.voidclose()private voidcloseMulticastSocket()booleancloseState()Sets the state and notifies all objects that wait on the ServiceInfo.private voiddisposeServiceCollectors()This method disposes all ServiceCollector instances which have been created by calls to methodlist(type).DNSCachegetCache()Return the DNSCache associated with the cache variableJmDNS.DelegategetDelegate()Returns the instance delegateJmDNSImplgetDns()Returns the DNS associated with this object.java.net.InetAddressgetGroup()java.lang.StringgetHostName()Return the HostName associated with this JmDNS instance.java.net.InetAddressgetInetAddress()Return the address of the interface to which this instance of JmDNS is bound.java.net.InetAddressgetInterface()Deprecated.longgetLastThrottleIncrement()HostInfogetLocalHost()Returns the local host infojava.lang.StringgetName()Return the name of the JmDNS instance.DNSIncominggetPlannedAnswer()static java.util.RandomgetRandom()ServiceInfogetServiceInfo(java.lang.String type, java.lang.String name)Get service information.ServiceInfogetServiceInfo(java.lang.String type, java.lang.String name, boolean persistent)Get service information.ServiceInfogetServiceInfo(java.lang.String type, java.lang.String name, boolean persistent, long timeout)Get service information.ServiceInfogetServiceInfo(java.lang.String type, java.lang.String name, long timeout)Get service information.(package private) ServiceInfoImplgetServiceInfoFromCache(java.lang.String type, java.lang.String name, java.lang.String subtype, boolean persistent)java.util.Map<java.lang.String,ServiceInfo>getServices()java.util.Map<java.lang.String,JmDNSImpl.ServiceTypeEntry>getServiceTypes()java.net.MulticastSocketgetSocket()intgetThrottle()(package private) voidhandleQuery(DNSIncoming in, java.net.InetAddress addr, int port)Handle an incoming query.(package private) voidhandleRecord(DNSRecord record, long now)(package private) voidhandleResponse(DNSIncoming msg)Handle an incoming response.(package private) voidhandleServiceResolved(ServiceEvent event)voidioLock()voidioUnlock()booleanisAnnounced()Returns true, if this is an announced state.booleanisAnnouncing()Returns 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.booleanisCanceled()Returns true, if this is a canceled state.booleanisCanceling()Returns true, if this is a canceling state.booleanisClosed()Returns true, if this is a closed state.booleanisClosing()Returns true, if this is a closing state.private booleanisOlderThanOneSecond(DNSRecord dnsRecord, long timeToCompare)booleanisProbing()Returns true, if this is a probing state.ServiceInfo[]list(java.lang.String type)Returns a list of service infos of the specified type.ServiceInfo[]list(java.lang.String type, long timeout)Returns a list of service infos of the specified type.java.util.Map<java.lang.String,ServiceInfo[]>listBySubtype(java.lang.String type)Returns a list of service infos of the specified type sorted by subtype.java.util.Map<java.lang.String,ServiceInfo[]>listBySubtype(java.lang.String type, long timeout)Returns a list of service infos of the specified type sorted by subtype.static voidmain(java.lang.String[] argv)Main method to display API information if run from java -jarprivate booleanmakeServiceNameUnique(ServiceInfoImpl info)Generate a possibly unique name for a service using the information we have in the cache.private voidopenMulticastSocket(HostInfo hostInfo)voidprintServices()Deprecated.voidpurgeStateTimer()Purge the state task timervoidpurgeTimer()Purge the general task timervoidrecover()Recover jmDNS when there is an error.booleanrecoverState()Sets the state and notifies all objects that wait on the ServiceInfo.voidregisterService(ServiceInfo infoAbstract)Register a service.booleanregisterServiceType(java.lang.String type)Register a service type.voidremoveAssociationWithTask(DNSTask task)Remove the association of the task with this Object.voidremoveListener(DNSListener listener)Remove a listener from all outstanding questions.private voidremoveObsoleteDnsListener(ServiceEvent event)voidremoveServiceListener(java.lang.String type, ServiceListener listener)Remove listener for services of a given type.voidremoveServiceTypeListener(ServiceTypeListener listener)Remove listener for service types.voidrenewServiceCollector(java.lang.String type)Renew a service when the record become stale.voidrequestServiceInfo(java.lang.String type, java.lang.String name)Request service information.voidrequestServiceInfo(java.lang.String type, java.lang.String name, boolean persistent)Request service information.voidrequestServiceInfo(java.lang.String type, java.lang.String name, boolean persistent, long timeout)Request service information.voidrequestServiceInfo(java.lang.String type, java.lang.String name, long timeout)Request service information.(package private) ServiceInfoImplresolveServiceInfo(java.lang.String type, java.lang.String name, java.lang.String subtype, boolean persistent)voidrespondToQuery(DNSIncoming in)booleanrevertState()Sets the state and notifies all objects that wait on the ServiceInfo.voidsend(DNSOutgoing out)Send an outgoing multicast DNS message.JmDNS.DelegatesetDelegate(JmDNS.Delegate delegate)Sets the instance delegatevoidsetLastThrottleIncrement(long lastThrottleIncrement)(package private) voidsetLocalHost(HostInfo localHost)voidsetPlannedAnswer(DNSIncoming plannedAnswer)voidsetThrottle(int throttle)private voidstart(java.util.Collection<? extends ServiceInfo> serviceInfos)voidstartAnnouncer()Start a new announcer taskvoidstartCanceler()Start a new canceler taskvoidstartProber()Start a new prober taskvoidstartReaper()Start a new reaper task.voidstartRenewer()Start a new renewer taskvoidstartResponder(DNSIncoming in, java.net.InetAddress addr, int port)Start a new responder taskvoidstartServiceInfoResolver(ServiceInfoImpl info)Start a new service info resolver taskvoidstartServiceResolver(java.lang.String type)Start a new service resolver taskvoidstartTypeResolver()Start a new service type resolver taskjava.lang.StringtoString()(package private) static java.lang.StringtoUnqualifiedName(java.lang.String type, java.lang.String qualifiedName)voidunregisterAllServices()Unregister 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.private voidwaitForInfoData(ServiceInfo info, long timeout)
-
-
-
Field Detail
-
logger
private static org.slf4j.Logger logger
-
_group
private volatile java.net.InetAddress _group
This is the multicast group, we are listening to for multicast DNS messages.
-
_socket
private volatile java.net.MulticastSocket _socket
This is our multicast socket.
-
_listeners
private final java.util.List<DNSListener> _listeners
Holds instances of JmDNS.DNSListener. Must by a synchronized collection, because it is updated from concurrent threads.
-
_serviceListeners
final java.util.concurrent.ConcurrentMap<java.lang.String,java.util.List<ListenerStatus.ServiceListenerStatus>> _serviceListeners
Holds instances of ServiceListener's. Keys are Strings holding a fully qualified service type. Values are LinkedList's of ServiceListener's.
-
_typeListeners
private final java.util.Set<ListenerStatus.ServiceTypeListenerStatus> _typeListeners
Holds instances of ServiceTypeListener's.
-
_cache
private final DNSCache _cache
Cache for DNSEntry's.
-
_services
private final java.util.concurrent.ConcurrentMap<java.lang.String,ServiceInfo> _services
This hashtable holds the services that have been registered. Keys are instances of String which hold an all lower-case version of the fully qualified service name. Values are instances of ServiceInfo.
-
_serviceTypes
private final java.util.concurrent.ConcurrentMap<java.lang.String,JmDNSImpl.ServiceTypeEntry> _serviceTypes
This hashtable holds the service types that have been registered or that have been received in an incoming datagram.
Keys are instances of String which hold an all lower-case version of the fully qualified service type.
Values hold the fully qualified service type.
-
_delegate
private volatile JmDNS.Delegate _delegate
-
_threadSleepDurationMs
protected final long _threadSleepDurationMs
-
_shutdown
protected java.lang.Thread _shutdown
This is the shutdown hook, we registered with the java runtime.
-
_localHost
private HostInfo _localHost
Handle on the local host
-
_incomingListener
private java.lang.Thread _incomingListener
-
_throttle
private int _throttle
Throttle count. This is used to count the overall number of probes sent by JmDNS. When the last throttle increment happened .
-
_lastThrottleIncrement
private long _lastThrottleIncrement
Last throttle increment.
-
_executor
private final java.util.concurrent.ExecutorService _executor
-
_random
private static final java.util.Random _random
The source for random values. This is used to introduce random delays in responses. This reduces the potential for collisions on the network.
-
_ioLock
private final java.util.concurrent.locks.ReentrantLock _ioLock
This lock is used to coordinate processing of incoming and outgoing messages. This is needed, because the Rendezvous Conformance Test does not forgive race conditions.
-
_plannedAnswer
private DNSIncoming _plannedAnswer
If an incoming package which needs an answer is truncated, we store it here. We add more incoming DNSRecords to it, until the JmDNS.Responder timer picks it up.
FIXME [PJYF June 8 2010]: This does not work well with multiple planned answers for packages that came in from different clients.
-
_serviceCollectors
private final java.util.concurrent.ConcurrentMap<java.lang.String,JmDNSImpl.ServiceCollector> _serviceCollectors
This hashtable is used to maintain a list of service types being collected by this JmDNS instance. The key of the hashtable is a service type name, the value is an instance of JmDNS.ServiceCollector.- See Also:
list(java.lang.String)
-
_name
private final java.lang.String _name
-
_recoverLock
private final java.lang.Object _recoverLock
-
-
Constructor Detail
-
JmDNSImpl
public JmDNSImpl(java.net.InetAddress address, java.lang.String name) throws java.io.IOExceptionCreate 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:
java.io.IOException
-
JmDNSImpl
public JmDNSImpl(java.net.InetAddress address, java.lang.String name, long threadSleepDurationMs) throws java.io.IOExceptionCreate 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 JmDNSthreadSleepDurationMs- time in milliseconds that the JmDNS listener thread should sleep between multicast receives- Throws:
java.io.IOException
-
-
Method Detail
-
main
public static void main(java.lang.String[] argv)
Main method to display API information if run from java -jar- Parameters:
argv- the command line arguments
-
start
private void start(java.util.Collection<? extends ServiceInfo> serviceInfos)
-
openMulticastSocket
private void openMulticastSocket(HostInfo hostInfo) throws java.io.IOException
- Throws:
java.io.IOException
-
closeMulticastSocket
private void closeMulticastSocket()
-
advanceState
public boolean advanceState(DNSTask task)
Sets the state and notifies all objects that wait on the ServiceInfo.- Specified by:
advanceStatein interfaceDNSStatefulObject- Parameters:
task- associated task- Returns:
trueif the state was changed by this thread,falseotherwise.- See Also:
DNSState.advance()
-
revertState
public boolean revertState()
Sets the state and notifies all objects that wait on the ServiceInfo.- Specified by:
revertStatein interfaceDNSStatefulObject- Returns:
trueif the state was changed by this thread,falseotherwise.- See Also:
DNSState.revert()
-
cancelState
public boolean cancelState()
Sets the state and notifies all objects that wait on the ServiceInfo.- Specified by:
cancelStatein interfaceDNSStatefulObject- Returns:
trueif 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:
trueif 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:
trueif the state was changed by this thread,falseotherwise.
-
getDns
public JmDNSImpl getDns()
Returns the DNS associated with this object.- Specified by:
getDnsin interfaceDNSStatefulObject- Returns:
- DNS resolver
-
associateWithTask
public void associateWithTask(DNSTask task, DNSState state)
Sets the task associated with this Object.- Specified by:
associateWithTaskin interfaceDNSStatefulObject- Parameters:
task- associated taskstate- state of the task
-
removeAssociationWithTask
public void removeAssociationWithTask(DNSTask task)
Remove the association of the task with this Object.- Specified by:
removeAssociationWithTaskin interfaceDNSStatefulObject- Parameters:
task- associated task
-
isAssociatedWithTask
public boolean isAssociatedWithTask(DNSTask task, DNSState state)
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
public DNSCache getCache()
Return the DNSCache associated with the cache variable- Returns:
- DNS cache
-
getName
public java.lang.String getName()
Return the name of the JmDNS instance. This is an arbitrary string that is useful for distinguishing instances.
-
getHostName
public java.lang.String 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
public HostInfo getLocalHost()
Returns the local host info- Returns:
- local host info
-
getInetAddress
public java.net.InetAddress getInetAddress() throws java.io.IOExceptionReturn the address of the interface to which this instance of JmDNS is bound.- Specified by:
getInetAddressin classJmDNS- Returns:
- Internet Address
- Throws:
java.io.IOException- if there is an error in the underlying protocol, such as a TCP error.
-
getInterface
@Deprecated public java.net.InetAddress getInterface() throws java.io.IOExceptionDeprecated.Return the address of the interface to which this instance of JmDNS is bound.- Specified by:
getInterfacein classJmDNS- Returns:
- Internet Address
- Throws:
java.io.IOException- if there is an error in the underlying protocol, such as a TCP error.
-
getServiceInfo
public ServiceInfo getServiceInfo(java.lang.String type, java.lang.String name)
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
public ServiceInfo getServiceInfo(java.lang.String type, java.lang.String name, 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.- 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
public ServiceInfo getServiceInfo(java.lang.String type, java.lang.String name, boolean persistent)
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
public ServiceInfo getServiceInfo(java.lang.String type, java.lang.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.- 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
-
resolveServiceInfo
ServiceInfoImpl resolveServiceInfo(java.lang.String type, java.lang.String name, java.lang.String subtype, boolean persistent)
-
getServiceInfoFromCache
ServiceInfoImpl getServiceInfoFromCache(java.lang.String type, java.lang.String name, java.lang.String subtype, boolean persistent)
-
waitForInfoData
private void waitForInfoData(ServiceInfo info, long timeout)
-
requestServiceInfo
public void requestServiceInfo(java.lang.String type, java.lang.String name)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
public void requestServiceInfo(java.lang.String type, java.lang.String name, boolean persistent)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
public void requestServiceInfo(java.lang.String type, java.lang.String name, long timeout)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
public void requestServiceInfo(java.lang.String type, java.lang.String name, boolean persistent, long timeout)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
-
handleServiceResolved
void handleServiceResolved(ServiceEvent event)
-
addServiceTypeListener
public void addServiceTypeListener(ServiceTypeListener listener) throws java.io.IOException
Listen for service types.- Specified by:
addServiceTypeListenerin classJmDNS- Parameters:
listener- listener for service types- Throws:
java.io.IOException- if there is an error in the underlying protocol, such as a TCP error.
-
removeServiceTypeListener
public void removeServiceTypeListener(ServiceTypeListener listener)
Remove listener for service types.- Specified by:
removeServiceTypeListenerin classJmDNS- Parameters:
listener- listener for service types
-
addServiceListener
public void addServiceListener(java.lang.String type, ServiceListener listener)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
-
addServiceListener
private void addServiceListener(java.lang.String type, ServiceListener listener, boolean synch)
-
removeServiceListener
public void removeServiceListener(java.lang.String type, ServiceListener listener)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
public void registerService(ServiceInfo infoAbstract) throws java.io.IOException
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:
java.io.IOException- if there is an error in the underlying protocol, such as a TCP error.
-
unregisterService
public void unregisterService(ServiceInfo infoAbstract)
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
public boolean registerServiceType(java.lang.String type)
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.
-
makeServiceNameUnique
private boolean makeServiceNameUnique(ServiceInfoImpl info)
Generate a possibly unique name for a service using the information we have in the cache.- Returns:
- returns true, if the name of the service info had to be changed.
-
addListener
public void addListener(DNSListener listener, DNSQuestion question)
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(DNSListener listener)
Remove a listener from all outstanding questions. The listener will no longer receive any updates.- Parameters:
listener- DSN listener
-
renewServiceCollector
public void renewServiceCollector(java.lang.String type)
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
public void updateRecord(long now, DNSRecord rec, JmDNSImpl.Operation operation)Notify all listeners that a record was updated.- Parameters:
now- update daterec- DNS recordoperation- DNS cache operation
-
removeObsoleteDnsListener
private void removeObsoleteDnsListener(ServiceEvent event)
-
handleRecord
void handleRecord(DNSRecord record, long now)
-
isOlderThanOneSecond
private boolean isOlderThanOneSecond(DNSRecord dnsRecord, long timeToCompare)
- Parameters:
dnsRecord-timeToCompare- a given times for comparison- Returns:
- true if dnsRecord create time is older than 1 second, relative to the given time; false otherwise
-
handleResponse
void handleResponse(DNSIncoming msg) throws java.io.IOException
Handle an incoming response. Cache answers, and pass them on to the appropriate questions.- Throws:
java.io.IOException
-
aRecordsLast
private java.util.List<DNSRecord> aRecordsLast(java.util.List<DNSRecord> allAnswers)
In case the a record is received before the srv record the ip address would not be set.Wireshark record: see also file a_record_before_srv.pcapng and
ServiceInfoImplTest#test_ip_address_is_set()Multicast Domain Name System (response) Transaction ID: 0x0000 Flags: 0x8400 Standard query response, No error Questions: 0 Answer RRs: 2 Authority RRs: 0 Additional RRs: 8 Answers _ibisip_http._tcp.local: type PTR, class IN, DeviceManagementService._ibisip_http._tcp.local _ibisip_http._tcp.local: type PTR, class IN, PassengerCountingService._ibisip_http._tcp.local Additional records DeviceManagementService._ibisip_http._tcp.local: type TXT, class IN, cache flush PassengerCountingService._ibisip_http._tcp.local: type TXT, class IN, cache flush DIST500_7-F07_OC030_05_03941.local: type A, class IN, cache flush, addr 192.168.88.236 DeviceManagementService._ibisip_http._tcp.local: type SRV, class IN, cache flush, priority 0, weight 0, port 5000, target DIST500_7-F07_OC030_05_03941.local PassengerCountingService._ibisip_http._tcp.local: type SRV, class IN, cache flush, priority 0, weight 0, port 5001, target DIST500_7-F07_OC030_05_03941.local DeviceManagementService._ibisip_http._tcp.local: type NSEC, class IN, cache flush, next domain name DeviceManagementService._ibisip_http._tcp.local PassengerCountingService._ibisip_http._tcp.local: type NSEC, class IN, cache flush, next domain name PassengerCountingService._ibisip_http._tcp.local DIST500_7-F07_OC030_05_03941.local: type NSEC, class IN, cache flush, next domain name DIST500_7-F07_OC030_05_03941.local
-
handleQuery
void handleQuery(DNSIncoming in, java.net.InetAddress addr, int port) throws java.io.IOException
Handle an incoming query. See if we can answer any part of it given our service infos.- Parameters:
in-addr-port-- Throws:
java.io.IOException
-
respondToQuery
public void respondToQuery(DNSIncoming in)
-
addAnswer
public DNSOutgoing addAnswer(DNSIncoming in, java.net.InetAddress addr, int port, DNSOutgoing out, DNSRecord rec) throws java.io.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:
java.io.IOException
-
send
public void send(DNSOutgoing out) throws java.io.IOException
Send an outgoing multicast DNS message.- Parameters:
out-- Throws:
java.io.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
public void startServiceInfoResolver(ServiceInfoImpl info)
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
public void startServiceResolver(java.lang.String type)
Description copied from interface:DNSTaskStarterStart a new service resolver task- Specified by:
startServiceResolverin interfaceDNSTaskStarter- Parameters:
type- service type to resolve
-
startResponder
public void startResponder(DNSIncoming in, java.net.InetAddress addr, int port)
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.
-
__recover
void __recover()
-
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:
DNSRecord,RecordReaper
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
printServices
@Deprecated public void printServices()
Deprecated.List Services and serviceTypes. Debugging Only- Specified by:
printServicesin classJmDNS
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
list
public ServiceInfo[] list(java.lang.String type)
Returns a list of service infos of the specified type.
-
list
public ServiceInfo[] list(java.lang.String type, long timeout)
Returns a list of service infos of the specified type.
-
listBySubtype
public java.util.Map<java.lang.String,ServiceInfo[]> listBySubtype(java.lang.String type)
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
public java.util.Map<java.lang.String,ServiceInfo[]> listBySubtype(java.lang.String type, long timeout)
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.
-
disposeServiceCollectors
private void disposeServiceCollectors()
This method disposes all ServiceCollector instances which have been created by calls to methodlist(type).- See Also:
list(java.lang.String)
-
toUnqualifiedName
static java.lang.String toUnqualifiedName(java.lang.String type, java.lang.String qualifiedName)
-
getServices
public java.util.Map<java.lang.String,ServiceInfo> getServices()
-
setLastThrottleIncrement
public void setLastThrottleIncrement(long lastThrottleIncrement)
-
getLastThrottleIncrement
public long getLastThrottleIncrement()
-
setThrottle
public void setThrottle(int throttle)
-
getThrottle
public int getThrottle()
-
getRandom
public static java.util.Random getRandom()
-
ioLock
public void ioLock()
-
ioUnlock
public void ioUnlock()
-
setPlannedAnswer
public void setPlannedAnswer(DNSIncoming plannedAnswer)
-
getPlannedAnswer
public DNSIncoming getPlannedAnswer()
-
setLocalHost
void setLocalHost(HostInfo localHost)
-
getServiceTypes
public java.util.Map<java.lang.String,JmDNSImpl.ServiceTypeEntry> getServiceTypes()
-
getSocket
public java.net.MulticastSocket getSocket()
-
getGroup
public java.net.InetAddress getGroup()
-
getDelegate
public JmDNS.Delegate getDelegate()
Description copied from class:JmDNSReturns the instance delegate- Specified by:
getDelegatein classJmDNS- Returns:
- instance delegate
-
setDelegate
public JmDNS.Delegate setDelegate(JmDNS.Delegate delegate)
Description copied from class:JmDNSSets the instance delegate- Specified by:
setDelegatein classJmDNS- Parameters:
delegate- new instance delegate- Returns:
- previous instance delegate
-
-