1 /* -*- Mode: Java; tab-width: 4 -*- 2 * 3 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved. 4 * 5 * Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. 6 * ("Apple") in consideration of your agreement to the following terms, and your 7 * use, installation, modification or redistribution of this Apple software 8 * constitutes acceptance of these terms. If you do not agree with these terms, 9 * please do not use, install, modify or redistribute this Apple software. 10 * 11 * In consideration of your agreement to abide by the following terms, and subject 12 * to these terms, Apple grants you a personal, non-exclusive license, under Apple's 13 * copyrights in this original Apple software (the "Apple Software"), to use, 14 * reproduce, modify and redistribute the Apple Software, with or without 15 * modifications, in source and/or binary forms; provided that if you redistribute 16 * the Apple Software in its entirety and without modifications, you must retain 17 * this notice and the following text and disclaimers in all such redistributions of 18 * the Apple Software. Neither the name, trademarks, service marks or logos of 19 * Apple Computer, Inc. may be used to endorse or promote products derived from the 20 * Apple Software without specific prior written permission from Apple. Except as 21 * expressly stated in this notice, no other rights or licenses, express or implied, 22 * are granted by Apple herein, including but not limited to any patent rights that 23 * may be infringed by your derivative works or by other works in which the Apple 24 * Software may be incorporated. 25 * 26 * The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO 27 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED 28 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN 30 * COMBINATION WITH YOUR PRODUCTS. 31 * 32 * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 34 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION 36 * OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT 37 * (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN 38 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41 42 import javax.swing.*; 43 import com.apple.dnssd.*; 44 45 46 /** Use this to schedule BrowseListener callbacks via SwingUtilities.invokeAndWait(). */ 47 48 public class SwingBrowseListener implements Runnable, BrowseListener 49 { 50 /** Create a listener for DNSSD that will call your listener on the Swing/AWT event thread. */ 51 public SwingBrowseListener( BrowseListener listener) 52 { fListener = listener; fErrorCode = 0; } 53 54 /** (Clients should not call this method directly.) */ 55 public void operationFailed( DNSSDService service, int errorCode) 56 { 57 fBrowser = service; 58 fErrorCode = errorCode; 59 this.schedule(); 60 } 61 62 /** (Clients should not call this method directly.) */ 63 public void serviceFound( DNSSDService browser, int flags, int ifIndex, 64 String serviceName, String regType, String domain) 65 66 { 67 fBrowser = browser; 68 fIsAdd = true; 69 fFlags = flags; 70 fIndex = ifIndex; 71 fService = serviceName; 72 fRegType = regType; 73 fDomain = domain; 74 this.schedule(); 75 } 76 77 /** (Clients should not call this method directly.) */ 78 public void serviceLost( DNSSDService browser, int flags, int ifIndex, 79 String serviceName, String regType, String domain) 80 { 81 fBrowser = browser; 82 fIsAdd = false; 83 fFlags = flags; 84 fIndex = ifIndex; 85 fService = serviceName; 86 fRegType = regType; 87 fDomain = domain; 88 this.schedule(); 89 } 90 91 /** (Clients should not call this method directly.) */ 92 public void run() 93 { 94 if ( fErrorCode != 0) 95 fListener.operationFailed( fBrowser, fErrorCode); 96 else if ( fIsAdd) 97 fListener.serviceFound( fBrowser, fFlags, fIndex, fService, fRegType, fDomain); 98 else 99 fListener.serviceLost( fBrowser, fFlags, fIndex, fService, fRegType, fDomain); 100 } 101 102 protected void schedule() 103 { 104 try { 105 SwingUtilities.invokeAndWait( this); 106 } 107 catch ( Exception e) 108 { 109 e.printStackTrace(); 110 } 111 } 112 113 protected BrowseListener fListener; 114 115 protected boolean fIsAdd; 116 protected DNSSDService fBrowser; 117 protected int fFlags; 118 protected int fIndex; 119 protected int fErrorCode; 120 protected String fService; 121 protected String fRegType; 122 protected String fDomain; 123 } 124 125