ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Zero Configuration Networking: Using the Java APIs, Part 1
Pages: 1, 2, 3, 4, 5

The Listener Interfaces

With most DNS-SD operations, you don't want to block and wait for a response. You will generally issue a request and pass in a handle to an object that implements the appropriate listener interface. This listener will then be called when interesting events occur, such as discovery of an instance of the service type you're looking for.



BaseListener

All of the interfaces in the com.apple.dnssd package extend BaseListener. As a result, every listener must implement the operationFailed method:

operationFailed(com.apple.dnssd.DNSSDService service, int errorCode)

If an asynchronous operation encounters a failure condition, then the listener's operationFailed( ) method is called. These kinds of failures are rare. For example, one contrived way you could deliberately cause the operationFailed( ) method to be called would be to start a DNS-SD operation and then kill the background daemon with a Unix kill 8-9 command. Currently, under normal circumstances, the only asynchronous failure that applications may reasonably need to expect are name conflicts for service registrations. If a program registers a service using the NO_AUTO_RENAME flag, and the computer subsequently joins a network where a service of the same type with that name already exists, then the program will get informed via an operationFailed( ) callback that it's service registration had to be cancelled, and if it wants to continue advertising, then it should pick a new name and try again. (If the program didn't specify NO_AUTO_RENAME, then its service registration will be automatically renamed on its behalf, and it will be notified of the new name via a serviceRegistered callback instead.)

Each asynchronous operation (e.g., register, browse, resolve) has its corresponding Listener interface (e.g., RegisterListener, BrowseListener, ResolveListener).

DNSSDException

The DNSSDException class is used to report DNS-SD error conditions. This exception generally indicates a programming error and is not expected to occur during normal program operation. (The only exceptional condition a program should be prepared to deal with during normal operation are name conflicts for advertised services, and those events are reported asynchronously via operationFailed or serviceRegistered callbacks.)

Just as with all Java exceptions, your exception handler can use getMessage( ) to get a string describing the nature of the error, and it can use printStackTrace( ) to show you where the error occurred. If you want to find the actual error code from the daemon, you can use the DNSSDException class's getErrorCode( ) method. Table 8-1 shows error codes returned by the mdnsd daemon.

Table 8-1. Error codes returned by the Java DNS-SD API

Error

Code

NO_ERROR

0

UNKNOWN

-65537

NO_SUCH_NAME

-65538

NO_MEMORY

-65539

BAD_PARAM

-65540

BAD_REFERENCE

-65541

BAD_STATE

-65542

BAD_FLAGS

-65543

UNSUPPORTED

-65544

NOT_INITIALIZED

-65545

ALREADY_REGISTERED

-65547

NAME_CONFLICT

-65548

INVALID

-65549

INCOMPATIBLE

-65551

BAD_INTERFACE_INDEX

-65552

Pages: 1, 2, 3, 4, 5

Next Pagearrow