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