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

advertisement

AddThis Social Bookmark Button O'Reilly Book Excerpts: Zero Configuration Networking: The Definitive Guide

Zero Configuration Networking: Using the Java APIs, Part 2

by Daniel H. Steinberg, Stuart Cheshire

Browsing for Services

Browsing to discover our advertised service using the dns-sd tool is very easy, and doing so using Java code is barely any harder. To browse, you need to perform two steps similar to those you just used to register your service:

  • Call DNSSD.browse( ) using one of the two available signatures.

  • Provide a class that implements the BrowseListener interface.

As with the DNSSD.register( ) example, it is common for the object initiating the browse operation to be the one that wants to receive the results, so it implements the BrowseListener interface itself and specifies itself (this) as the listener object in the DNSSD.browse( ) call.

To function as a BrowseListener, a class must implement operationFailed( ), serviceFound( ), and serviceLost( ). Under normal circumstances, the operation-Failed( ) method will never be invoked. In our example program, the serviceFound( ) and serviceLost( ) methods just print out information to show the events they receive, very much like the output of dns-sd.


public void serviceFound(DNSSDService browser, int flags, int ifIndex,
          String name, String regType, String domain)
  {
  System.out.println("Add flags:" + flags + ", ifIndex:" + ifIndex +
    ", Name:" + name + ", Type:" + regType + ", Domain:" + domain);
  }

Whenever a new instance of a service is discovered, serviceFound( ) will be called and will write a line to standard out beginning with the word "Add," followed by the name, type, and domain.

The serviceLost( ) method takes the exact same parameter list as serviceFound( ). The only difference in our example program is that instead of printing "Add" it prints "Rmv" (which stands for remove).

Example 8-2 shows a complete listing, which you can compile with javac, to browse for services using DNS-SD. In this example, the program just runs for 30 seconds, displaying add and remove events as they arrive, and then calls b.stop( ) and exits. Of course, in a real program, you wouldn't use a fixed timeout like 30 seconds. You'd start the browse operation running when the user brings up a browsing window, and stop it when they close the browsing window.

Example 8-2. Java program to browse for services using DNS-SD

import com.apple.dnssd.*;

class TestBrowse implements BrowseListener
  {
  // Display error message on failure
  public void operationFailed(DNSSDService service, int errorCode)
    {
    System.out.println("Browse failed " + errorCode);
    System.exit(-1);
    }

  // Display services we discover
  public void serviceFound(DNSSDService browser, int flags, int ifIndex,
            String name, String regType, String domain)
    {
    System.out.println("Add flags:" + flags + ", ifIndex:" + ifIndex +
      ", Name:" + name + ", Type:" + regType + ", Domain:" + domain);
    }

  // Print a line when services go away
  public void serviceLost(DNSSDService browser, int flags, int ifIndex,
            String name, String regType, String domain)
    {
    System.out.println("Rmv flags:" + flags + ", ifIndex:" + ifIndex +
      ", Name:" + name + ", Type:" + regType + ", Domain:" + domain);
    }

  public TestBrowse(  ) throws DNSSDException, InterruptedException
    {
    System.out.println("TestBrowse Starting");
    DNSSDService b = DNSSD.browse("_example._tcp", this);
    System.out.println("TestBrowse Running");
    Thread.sleep(30000);
    System.out.println("TestBrowse Stopping");
    b.stop(  );
    }

  public static void main(String[] args)
    {
    try { new TestBrowse(  ); }
    catch(Exception e)
      {
      e.printStackTrace(  );
      System.exit(-1);
      }
    }
  }

After you've compiled the TestBrowse program, we'll demonstrate it using our TestRegister program. Open three terminal windows. In the first, run java TestRegister "My Chosen Name." In the second, run that same command a second time. In the third window, a second or two later, run java TestBrowse:


% java TestBrowse
TestBrowse Starting
Add flags:3 ifIndex:5 Name:My Chosen Name Type:_example._tcp. Domain:local.
Add flags:2 ifIndex:5 Name:My Chosen Name (2) Type:_example._tcp. Domain:local.
TestBrowse Running
Rmv flags:0 ifIndex:5 Name:My Chosen Name Type:_example._tcp. Domain:local.
Rmv flags:0 ifIndex:5 Name:My Chosen Name (2) Type:_example._tcp. Domain:local.
TestBrowse Stopping

You'll see that TestBrowse finds our two service instances, "My Chosen Name" and "My Chosen Name (2)."

Another thing to notice is that, even though TestBrowse prints out "TestBrowse Running" on the very next line of the program after the DNSSD.browse( ) call, the services are discovered so fast that they're printed even before that line gets to execute.

Each TestRegister process exits 30 seconds after it was started, and we see the "Rmv" line printed for each service as it goes away. Finally, after running for 30 seconds itself, TestBrowse calls b.stop( ) and exits.

You can now advertise a named service and discover a list of named services. The third step, to actually use a service, is to resolve its name to its current address and port number.

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow