Jitsi: the OpenSource Java VoIP and Instant Messaging client.

net.java.sip.communicator.impl.protocol.zeroconf.jmdns
Class JmDNS

java.lang.Object
  extended by net.java.sip.communicator.impl.protocol.zeroconf.jmdns.JmDNS

public class JmDNS
extends Object

mDNS implementation in Java.

Version:
%I%, %G%
Author:
Arthur van Hoff, Rick Blair, Jeff Sonstein, Werner Randelshofer, Pierre Frisch, Scott Lewis

Nested Class Summary
 class JmDNS.Canceler
          The Canceler sends two announces with TTL=0 for the specified services.
(package private)  class JmDNS.SocketListener
          Listen for multicast packets.
 
Field Summary
protected  boolean closed
          Used to fix live lock problem on unregester.
(package private)  HostInfo localHost
          Handle on the local host
(package private)  Map<String,ServiceInfo> services
          This hashtable holds the services that have been registered.
(package private)  Map<String,String> serviceTypes
          This hashtable holds the service types that have been registered or that have been received in an incoming datagram.
(package private)  TimerTask task
          Timer task associated to the host name.
static String VERSION
          The version of JmDNS.
 
Constructor Summary
JmDNS()
          Create an instance of JmDNS.
JmDNS(InetAddress addr)
          Create an instance of JmDNS and bind it to a specific network interface given its IP-address.
 
Method Summary
(package private)  DNSOutgoing addAnswer(DNSIncoming in, InetAddress addr, int port, DNSOutgoing out, DNSRecord rec)
          Add an answer to a question.
 void addListener(DNSListener listener, DNSQuestion question)
          Add a listener for a question.
 void addServiceListener(String type, ServiceListener listener)
          Listen for services of a given type.
 void addServiceTypeListener(ServiceTypeListener listener)
          Listen for service types.
(package private)  void advanceState()
          Sets the state and notifies all objects that wait on JmDNS.
(package private)  void cancel()
          Sets the state and notifies all objects that wait on JmDNS.
 void close()
          Close down jmdns.
(package private)  DNSCache getCache()
          Return the DNSCache associated with the cache variable
 String getHostName()
          Return the HostName associated with this JmDNS instance.
 InetAddress getInterface()
          Return the address of the interface to which this instance of JmDNS is bound.
 HostInfo getLocalHost()
           
 ServiceInfo getLocalService(String FQN)
          SC-Bonjour Implementation: Method to retrieve the DNS Entry corresponding to a service that has been declared and return it as a ServiceInfo structure.
 ServiceInfo getServiceInfo(String type, String name)
          Get service information.
 ServiceInfo getServiceInfo(String type, String name, int timeout)
          Get service information.
(package private)  DNSState getState()
          Returns the current state of this info.
(package private)  void handleServiceResolved(ServiceInfo info)
           
(package private)  String incrementName(String name)
           
 ServiceInfo[] list(String type)
          Returns a list of service infos of the specified type.
(package private)  void print()
          List cache entries, for debugging only.
 void printServices()
          List Services and serviceTypes.
protected  void recover()
          Recover jmdns when there is an error.
 void registerService(ServiceInfo info)
          Register a service.
 void registerServiceType(String type)
          Register a service type.
(package private)  void removeListener(DNSListener listener)
          Remove a listener from all outstanding questions.
 void removeServiceListener(String type, ServiceListener listener)
          Remove listener for services of a given type.
 void removeServiceTypeListener(ServiceTypeListener listener)
          Remove listener for service types.
 void requestServiceInfo(String type, String name)
          Request service information.
 void requestServiceInfo(String type, String name, int timeout)
          Request service information.
(package private)  void revertState()
          Sets the state and notifies all objects that wait on JmDNS.
 String toString()
           
 void unregisterAllServices()
          Unregister all services.
 void unregisterService(ServiceInfo info)
          Unregister a service.
 void updateInfos(ServiceInfo info, byte[] old)
          SC-Bonjour Implementation : Method used to update the corresponding DNS entry in the cache of JmDNS with the new information in this ServiceInfo.
(package private)  void updateRecord(long now, DNSRecord rec)
          Notify all listeners that a record was updated.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

VERSION

public static String VERSION
The version of JmDNS.


closed

protected boolean closed
Used to fix live lock problem on unregester.


services

Map<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

Map<String,String> 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.


localHost

HostInfo localHost
Handle on the local host


task

TimerTask task
Timer task associated to the host name. This is used to prevent from having multiple tasks associated to the host name at the same time.

Constructor Detail

JmDNS

public JmDNS()
      throws IOException
Create an instance of JmDNS.

Throws:
IOException

JmDNS

public JmDNS(InetAddress addr)
      throws IOException
Create an instance of JmDNS and bind it to a specific network interface given its IP-address.

Parameters:
addr -
Throws:
IOException
Method Detail

advanceState

void advanceState()
Sets the state and notifies all objects that wait on JmDNS.


revertState

void revertState()
Sets the state and notifies all objects that wait on JmDNS.


cancel

void cancel()
Sets the state and notifies all objects that wait on JmDNS.


getState

DNSState getState()
Returns the current state of this info.


getCache

DNSCache getCache()
Return the DNSCache associated with the cache variable


getHostName

public 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.

Returns:
Return the HostName associated with this JmDNS instance.

getLocalHost

public HostInfo getLocalHost()

getInterface

public InetAddress getInterface()
                         throws IOException
Return the address of the interface to which this instance of JmDNS is bound.

Returns:
Return the address of the interface to which this instance of JmDNS is bound.
Throws:
IOException

getServiceInfo

public ServiceInfo getServiceInfo(String type,
                                  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.

Parameters:
type - fully qualified service type, such as _http._tcp.local. .
name - unqualified service name, such as foobar .
Returns:
null if the service information cannot be obtained

getServiceInfo

public ServiceInfo getServiceInfo(String type,
                                  String name,
                                  int 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 as foobar .
timeout - timeout in milliseconds
Returns:
null if the service information cannot be obtained

requestServiceInfo

public void requestServiceInfo(String type,
                               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.

Parameters:
type - full qualified service type, such as _http._tcp.local. .
name - unqualified service name, such as foobar .

requestServiceInfo

public void requestServiceInfo(String type,
                               String name,
                               int timeout)
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 as foobar .
timeout - timeout in milliseconds

handleServiceResolved

void handleServiceResolved(ServiceInfo info)

addServiceTypeListener

public void addServiceTypeListener(ServiceTypeListener listener)
                            throws IOException
Listen for service types.

Parameters:
listener - listener for service types
Throws:
IOException

removeServiceTypeListener

public void removeServiceTypeListener(ServiceTypeListener listener)
Remove listener for service types.

Parameters:
listener - listener for service types

addServiceListener

public void addServiceListener(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..

Parameters:
type - full qualified service type, such as _http._tcp.local..
listener - listener for service updates

removeServiceListener

public void removeServiceListener(String type,
                                  ServiceListener listener)
Remove listener for services of a given type.

Parameters:
type - of listener to be removed
listener - listener for service updates

registerService

public void registerService(ServiceInfo info)
                     throws 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.

Parameters:
info - of service
Throws:
IOException

unregisterService

public void unregisterService(ServiceInfo info)
Unregister a service. The service should have been registered.

Parameters:
info - of service

unregisterAllServices

public void unregisterAllServices()
Unregister all services.


registerServiceType

public void registerServiceType(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.

Parameters:
type - of service

incrementName

String incrementName(String name)

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 - to be added
question - - which the listener is responsible for.

removeListener

void removeListener(DNSListener listener)
Remove a listener from all outstanding questions. The listener will no longer receive any updates.


updateRecord

void updateRecord(long now,
                  DNSRecord rec)
Notify all listeners that a record was updated.


addAnswer

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

Throws:
IOException

recover

protected void recover()
Recover jmdns when there is an error.


close

public void close()
Close down jmdns. Release all resources and unregister all services.


print

void print()
List cache entries, for debugging only.


printServices

public void printServices()
List Services and serviceTypes. Debugging Only


toString

public String toString()
Overrides:
toString in class Object

list

public ServiceInfo[] list(String type)
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 names.

updateInfos

public void updateInfos(ServiceInfo info,
                        byte[] old)
SC-Bonjour Implementation : Method used to update the corresponding DNS entry in the cache of JmDNS with the new information in this ServiceInfo. A call to getLocalService must first be issued to get the ServiceInfo object to be modified. THIS METHOD MUST BE USED INSTEAD OF ANY DIRECT ACCESS TO JMDNS' CACHE!! This is used in the implementation of Zeroconf in SIP Communicator to be able to change fields declared by the local contact (status, etc).

Parameters:
info - Updated service data to be used to replace the old stuff contained in JmDNS' cache
old - info bytes

getLocalService

public ServiceInfo getLocalService(String FQN)
SC-Bonjour Implementation: Method to retrieve the DNS Entry corresponding to a service that has been declared and return it as a ServiceInfo structure. It is used in the implementation of Bonjour in SIP Communicator to retrieve the information concerning the service declared by the local contact. THIS METHOD MUST BE USED INSTEAD OF ANY LOCAL COPY SAVED BEFORE SERVICE REGISTRATION!!

Parameters:
FQN - String representing the Fully Qualified name of the service we want info about
Returns:
information corresponding to the specified service

Jitsi: the OpenSource Java VoIP and Instant Messaging client.

Jitsi, the OpenSource Java VoIP and Instant Messaging client.
Distributable under LGPL license.