2N/A/* -*- Mode: Java; tab-width: 4 -*-
2N/A *
2N/A * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
2N/A *
2N/A * Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
2N/A * ("Apple") in consideration of your agreement to the following terms, and your
2N/A * use, installation, modification or redistribution of this Apple software
2N/A * constitutes acceptance of these terms. If you do not agree with these terms,
2N/A * please do not use, install, modify or redistribute this Apple software.
2N/A *
2N/A * In consideration of your agreement to abide by the following terms, and subject
2N/A * to these terms, Apple grants you a personal, non-exclusive license, under Apple's
2N/A * copyrights in this original Apple software (the "Apple Software"), to use,
2N/A * reproduce, modify and redistribute the Apple Software, with or without
2N/A * modifications, in source and/or binary forms; provided that if you redistribute
2N/A * the Apple Software in its entirety and without modifications, you must retain
2N/A * this notice and the following text and disclaimers in all such redistributions of
2N/A * the Apple Software. Neither the name, trademarks, service marks or logos of
2N/A * Apple Computer, Inc. may be used to endorse or promote products derived from the
2N/A * Apple Software without specific prior written permission from Apple. Except as
2N/A * expressly stated in this notice, no other rights or licenses, express or implied,
2N/A * are granted by Apple herein, including but not limited to any patent rights that
2N/A * may be infringed by your derivative works or by other works in which the Apple
2N/A * Software may be incorporated.
2N/A *
2N/A * The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
2N/A * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
2N/A * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2N/A * PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
2N/A * COMBINATION WITH YOUR PRODUCTS.
2N/A *
2N/A * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
2N/A * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2N/A * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2N/A * ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
2N/A * OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
2N/A * (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
2N/A * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2N/A
2N/A BrowserApp demonstrates how to use DNS-SD to browse for and resolve services.
2N/A
2N/A To do:
2N/A - display resolved TXTRecord
2N/A
2N/Aident "%Z%%M% %I% %E% SMI"
2N/A
2N/A */
2N/A
2N/A
2N/Aimport java.awt.*;
2N/Aimport java.awt.event.*;
2N/Aimport java.util.*;
2N/Aimport java.text.*;
2N/Aimport javax.swing.*;
2N/Aimport javax.swing.event.*;
2N/A
2N/Aimport com.apple.dnssd.*;
2N/A
2N/A
2N/Aclass BrowserApp implements ListSelectionListener, ResolveListener
2N/A{
2N/A static BrowserApp app;
2N/A JFrame frame;
2N/A DomainListModel domainList;
2N/A BrowserListModel servicesList, serviceList;
2N/A JList domainPane, servicesPane, servicePane;
2N/A DNSSDService servicesBrowser, serviceBrowser, domainBrowser;
2N/A JLabel hostLabel, portLabel;
2N/A
2N/A public BrowserApp()
2N/A {
2N/A frame = new JFrame("DNS-SD Service Browser");
2N/A frame.addWindowListener(new WindowAdapter() {
2N/A public void windowClosing(WindowEvent e) {System.exit(0);}
2N/A });
2N/A
2N/A domainList = new DomainListModel();
2N/A servicesList = new ServicesBrowserListModel();
2N/A serviceList = new BrowserListModel();
2N/A
2N/A try {
2N/A domainBrowser = DNSSD.enumerateDomains( DNSSD.BROWSE_DOMAINS, 0, domainList);
2N/A
2N/A servicesBrowser = DNSSD.browse( 0, 0, "_services._dns-sd._udp.", "", servicesList);
2N/A serviceBrowser = null;
2N/A }
2N/A catch ( Exception ex) { terminateWithException( ex); }
2N/A
2N/A this.setupSubPanes( frame.getContentPane());
2N/A frame.pack();
2N/A frame.setVisible(true);
2N/A }
2N/A
2N/A protected void setupSubPanes( Container parent)
2N/A {
2N/A parent.setLayout( new BoxLayout( parent, BoxLayout.Y_AXIS));
2N/A
2N/A JPanel browserRow = new JPanel();
2N/A browserRow.setLayout( new BoxLayout( browserRow, BoxLayout.X_AXIS));
2N/A domainPane = new JList( domainList);
2N/A domainPane.addListSelectionListener( this);
2N/A JScrollPane domainScroller = new JScrollPane( domainPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
2N/A browserRow.add( domainScroller);
2N/A servicesPane = new JList( servicesList);
2N/A servicesPane.addListSelectionListener( this);
2N/A JScrollPane servicesScroller = new JScrollPane( servicesPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
2N/A browserRow.add( servicesScroller);
2N/A servicePane = new JList( serviceList);
2N/A servicePane.addListSelectionListener( this);
2N/A JScrollPane serviceScroller = new JScrollPane( servicePane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
2N/A browserRow.add( serviceScroller);
2N/A
2N/A/*
2N/A JPanel buttonRow = new JPanel();
2N/A buttonRow.setLayout( new BoxLayout( buttonRow, BoxLayout.X_AXIS));
2N/A buttonRow.add( Box.createHorizontalGlue());
2N/A JButton connectButton = new JButton( "Don't Connect");
2N/A buttonRow.add( connectButton);
2N/A buttonRow.add( Box.createRigidArea( new Dimension( 16, 0)));
2N/A*/
2N/A
2N/A JPanel labelRow = new JPanel();
2N/A labelRow.setLayout( new BoxLayout( labelRow, BoxLayout.X_AXIS));
2N/A labelRow.add( new JLabel( " Host: "));
2N/A hostLabel = new JLabel();
2N/A labelRow.add( hostLabel);
2N/A labelRow.add( Box.createRigidArea( new Dimension( 32, 0)));
2N/A labelRow.add( new JLabel( "Port: "));
2N/A portLabel = new JLabel();
2N/A labelRow.add( portLabel);
2N/A labelRow.add( Box.createHorizontalGlue());
2N/A
2N/A parent.add( browserRow);
2N/A parent.add( Box.createRigidArea( new Dimension( 0, 8)));
2N/A parent.add( labelRow);
2N/A// parent.add( buttonRow);
2N/A parent.add( Box.createRigidArea( new Dimension( 0, 16)));
2N/A }
2N/A
2N/A public void valueChanged( ListSelectionEvent e)
2N/A {
2N/A try {
2N/A if ( e.getSource() == domainPane && !e.getValueIsAdjusting())
2N/A {
2N/A int newSel = domainPane.getSelectedIndex();
2N/A if ( -1 != newSel)
2N/A {
2N/A if ( serviceBrowser != null)
2N/A serviceBrowser.stop();
2N/A serviceList.removeAllElements();
2N/A servicesBrowser = DNSSD.browse( 0, 0, "_services._dns-sd._udp.", "", servicesList);
2N/A }
2N/A }
2N/A else if ( e.getSource() == servicesPane && !e.getValueIsAdjusting())
2N/A {
2N/A int newSel = servicesPane.getSelectedIndex();
2N/A if ( serviceBrowser != null)
2N/A serviceBrowser.stop();
2N/A serviceList.removeAllElements();
2N/A if ( -1 != newSel)
2N/A serviceBrowser = DNSSD.browse( 0, 0, servicesList.getNthRegType( newSel), "", serviceList);
2N/A }
2N/A else if ( e.getSource() == servicePane && !e.getValueIsAdjusting())
2N/A {
2N/A int newSel = servicePane.getSelectedIndex();
2N/A
2N/A hostLabel.setText( "");
2N/A portLabel.setText( "");
2N/A
2N/A if ( -1 != newSel)
2N/A {
2N/A DNSSD.resolve( 0, serviceList.getNthInterface( newSel),
2N/A serviceList.getNthServiceName( newSel),
2N/A serviceList.getNthRegType( newSel),
2N/A serviceList.getNthDomain( newSel),
2N/A new SwingResolveListener( this));
2N/A }
2N/A }
2N/A }
2N/A catch ( Exception ex) { terminateWithException( ex); }
2N/A }
2N/A
2N/A public void serviceResolved( DNSSDService resolver, int flags, int ifIndex, String fullName,
2N/A String hostName, int port, TXTRecord txtRecord)
2N/A {
2N/A hostLabel.setText( hostName);
2N/A portLabel.setText( String.valueOf( port));
2N/A }
2N/A
2N/A public void operationFailed( DNSSDService service, int errorCode)
2N/A {
2N/A // handle failure here
2N/A }
2N/A
2N/A protected static void terminateWithException( Exception e)
2N/A {
2N/A e.printStackTrace();
2N/A System.exit( -1);
2N/A }
2N/A
2N/A public static void main(String s[])
2N/A {
2N/A app = new BrowserApp();
2N/A }
2N/A}
2N/A
2N/A
2N/Aclass BrowserListModel extends DefaultListModel implements BrowseListener, Runnable
2N/A{
2N/A public BrowserListModel()
2N/A {
2N/A addCache = new Vector();
2N/A removeCache = new Vector();
2N/A }
2N/A
2N/A /* The Browser invokes this callback when a service is discovered. */
2N/A public void serviceFound( DNSSDService browser, int flags, int ifIndex,
2N/A String serviceName, String regType, String domain)
2N/A {
2N/A addCache.add( new BrowserListElem( serviceName, domain, regType, ifIndex));
2N/A if ( ( flags & DNSSD.MORE_COMING) == 0)
2N/A this.scheduleOnEventThread();
2N/A }
2N/A
2N/A public void serviceLost( DNSSDService browser, int flags, int ifIndex,
2N/A String serviceName, String regType, String domain)
2N/A {
2N/A removeCache.add( serviceName);
2N/A if ( ( flags & DNSSD.MORE_COMING) == 0)
2N/A this.scheduleOnEventThread();
2N/A }
2N/A
2N/A public void run()
2N/A {
2N/A while ( removeCache.size() > 0)
2N/A {
2N/A String serviceName = (String) removeCache.remove( removeCache.size() - 1);
2N/A int matchInd = this.findMatching( serviceName); // probably doesn't handle near-duplicates well.
2N/A if ( matchInd != -1)
2N/A this.removeElementAt( matchInd);
2N/A }
2N/A while ( addCache.size() > 0)
2N/A {
2N/A BrowserListElem elem = (BrowserListElem) addCache.remove( addCache.size() - 1);
2N/A if ( -1 == this.findMatching( elem.fServiceName)) // probably doesn't handle near-duplicates well.
2N/A this.addInSortOrder( elem);
2N/A }
2N/A }
2N/A
2N/A public void operationFailed( DNSSDService service, int errorCode)
2N/A {
2N/A // handle failure here
2N/A }
2N/A
2N/A /* The list contains BrowserListElem's */
2N/A class BrowserListElem
2N/A {
2N/A public BrowserListElem( String serviceName, String domain, String type, int ifIndex)
2N/A { fServiceName = serviceName; fDomain = domain; fType = type; fInt = ifIndex; }
2N/A
2N/A public String toString() { return fServiceName; }
2N/A
2N/A public String fServiceName, fDomain, fType;
2N/A public int fInt;
2N/A }
2N/A
2N/A public String getNthServiceName( int n)
2N/A {
2N/A BrowserListElem sel = (BrowserListElem) this.get( n);
2N/A return sel.fServiceName;
2N/A }
2N/A
2N/A public String getNthRegType( int n)
2N/A {
2N/A BrowserListElem sel = (BrowserListElem) this.get( n);
2N/A return sel.fType;
2N/A }
2N/A
2N/A public String getNthDomain( int n)
2N/A {
2N/A BrowserListElem sel = (BrowserListElem) this.get( n);
2N/A return sel.fDomain;
2N/A }
2N/A
2N/A public int getNthInterface( int n)
2N/A {
2N/A BrowserListElem sel = (BrowserListElem) this.get( n);
2N/A return sel.fInt;
2N/A }
2N/A
2N/A protected void addInSortOrder( Object obj)
2N/A {
2N/A int i;
2N/A for ( i = 0; i < this.size(); i++)
2N/A if ( sCollator.compare( obj.toString(), this.getElementAt( i).toString()) < 0)
2N/A break;
2N/A this.add( i, obj);
2N/A }
2N/A
2N/A protected int findMatching( String match)
2N/A {
2N/A for ( int i = 0; i < this.size(); i++)
2N/A if ( match.equals( this.getElementAt( i).toString()))
2N/A return i;
2N/A return -1;
2N/A }
2N/A
2N/A protected void scheduleOnEventThread()
2N/A {
2N/A try {
2N/A SwingUtilities.invokeAndWait( this);
2N/A }
2N/A catch ( Exception e)
2N/A {
2N/A e.printStackTrace();
2N/A }
2N/A }
2N/A
2N/A protected Vector removeCache; // list of serviceNames to remove
2N/A protected Vector addCache; // list of BrowserListElem's to add
2N/A
2N/A protected static Collator sCollator;
2N/A
2N/A static // Initialize our static variables
2N/A {
2N/A sCollator = Collator.getInstance();
2N/A sCollator.setStrength( Collator.PRIMARY);
2N/A }
2N/A}
2N/A
2N/A
2N/Aclass ServicesBrowserListModel extends BrowserListModel
2N/A{
2N/A /* The Browser invokes this callback when a service is discovered. */
2N/A public void serviceFound( DNSSDService browser, int flags, int ifIndex,
2N/A String serviceName, String regType, String domain)
2N/A // Overridden to stuff serviceName into regType and make serviceName human-readable.
2N/A {
2N/A regType = serviceName + ( regType.startsWith( "_udp.") ? "._udp." : "._tcp.");
2N/A super.serviceFound( browser, flags, ifIndex, this.mapTypeToName( serviceName), regType, domain);
2N/A }
2N/A
2N/A public void serviceLost( DNSSDService browser, int flags, int ifIndex,
2N/A String serviceName, String regType, String domain)
2N/A // Overridden to make serviceName human-readable.
2N/A {
2N/A super.serviceLost( browser, flags, ifIndex, this.mapTypeToName( serviceName), regType, domain);
2N/A }
2N/A
2N/A protected String mapTypeToName( String type)
2N/A // Convert a registration type into a human-readable string. Returns original string on no-match.
2N/A {
2N/A final String[] namedServices = {
2N/A "_afpovertcp", "Apple File Sharing",
2N/A "_http", "World Wide Web servers",
2N/A "_daap", "Digital Audio Access",
2N/A "_apple-sasl", "Apple Password Servers",
2N/A "_distcc", "Distributed Compiler nodes",
2N/A "_finger", "Finger servers",
2N/A "_ichat", "iChat clients",
2N/A "_presence", "iChat AV clients",
2N/A "_ssh", "SSH servers",
2N/A "_telnet", "Telnet servers",
2N/A "_workstation", "Macintosh Manager clients",
2N/A "_bootps", "BootP servers",
2N/A "_xserveraid", "XServe RAID devices",
2N/A "_eppc", "Remote AppleEvents",
2N/A "_ftp", "FTP services",
2N/A "_tftp", "TFTP services"
2N/A };
2N/A
2N/A for ( int i = 0; i < namedServices.length; i+=2)
2N/A if ( namedServices[i].equals( type))
2N/A return namedServices[i + 1];
2N/A return type;
2N/A }
2N/A}
2N/A
2N/A
2N/Aclass DomainListModel extends DefaultListModel implements DomainListener
2N/A{
2N/A /* Called when a domain is discovered. */
2N/A public void domainFound( DNSSDService domainEnum, int flags, int ifIndex, String domain)
2N/A {
2N/A if ( !this.contains( domain))
2N/A this.addElement( domain);
2N/A }
2N/A
2N/A public void domainLost( DNSSDService domainEnum, int flags, int ifIndex, String domain)
2N/A {
2N/A if ( this.contains( domain))
2N/A this.removeElement( domain);
2N/A }
2N/A
2N/A public void operationFailed( DNSSDService service, int errorCode)
2N/A {
2N/A // handle failure here
2N/A }
2N/A}
2N/A