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
4b22b9337f359bfd063322244f5336cc7c6ffcfars
4b22b9337f359bfd063322244f5336cc7c6ffcfarsimport javax.swing.*;
4b22b9337f359bfd063322244f5336cc7c6ffcfarsimport com.apple.dnssd.*;
4b22b9337f359bfd063322244f5336cc7c6ffcfars
4b22b9337f359bfd063322244f5336cc7c6ffcfars
4b22b9337f359bfd063322244f5336cc7c6ffcfars/** Use this to schedule QueryListener callbacks via SwingUtilities.invokeAndWait(). */
4b22b9337f359bfd063322244f5336cc7c6ffcfars
4b22b9337f359bfd063322244f5336cc7c6ffcfarspublic class SwingQueryListener implements Runnable, QueryListener
4b22b9337f359bfd063322244f5336cc7c6ffcfars{
4b22b9337f359bfd063322244f5336cc7c6ffcfars /** Create a listener for DNSSD that will call your listener on the Swing/AWT event thread. */
4b22b9337f359bfd063322244f5336cc7c6ffcfars public SwingQueryListener( QueryListener listener)
4b22b9337f359bfd063322244f5336cc7c6ffcfars { fListener = listener; }
4b22b9337f359bfd063322244f5336cc7c6ffcfars
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void operationFailed( DNSSDService service, int errorCode)
4b22b9337f359bfd063322244f5336cc7c6ffcfars {
4b22b9337f359bfd063322244f5336cc7c6ffcfars fQuery = service;
4b22b9337f359bfd063322244f5336cc7c6ffcfars fErrorCode = errorCode;
4b22b9337f359bfd063322244f5336cc7c6ffcfars this.schedule();
4b22b9337f359bfd063322244f5336cc7c6ffcfars }
4b22b9337f359bfd063322244f5336cc7c6ffcfars
4b22b9337f359bfd063322244f5336cc7c6ffcfars /** (Clients should not call this method directly.) */
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void queryAnswered( DNSSDService query, int flags, int ifIndex, String fullName,
4b22b9337f359bfd063322244f5336cc7c6ffcfars int rrtype, int rrclass, byte[] rdata, int ttl)
4b22b9337f359bfd063322244f5336cc7c6ffcfars {
4b22b9337f359bfd063322244f5336cc7c6ffcfars fQuery = query;
4b22b9337f359bfd063322244f5336cc7c6ffcfars fFlags = flags;
4b22b9337f359bfd063322244f5336cc7c6ffcfars fIndex = ifIndex;
4b22b9337f359bfd063322244f5336cc7c6ffcfars fFullName = fullName;
4b22b9337f359bfd063322244f5336cc7c6ffcfars fType = rrtype;
4b22b9337f359bfd063322244f5336cc7c6ffcfars fClass = rrclass;
4b22b9337f359bfd063322244f5336cc7c6ffcfars fData = rdata;
4b22b9337f359bfd063322244f5336cc7c6ffcfars fTTL = ttl;
4b22b9337f359bfd063322244f5336cc7c6ffcfars this.schedule();
4b22b9337f359bfd063322244f5336cc7c6ffcfars }
4b22b9337f359bfd063322244f5336cc7c6ffcfars
4b22b9337f359bfd063322244f5336cc7c6ffcfars /** (Clients should not call this method directly.) */
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void run()
4b22b9337f359bfd063322244f5336cc7c6ffcfars {
4b22b9337f359bfd063322244f5336cc7c6ffcfars if ( fErrorCode != 0)
4b22b9337f359bfd063322244f5336cc7c6ffcfars fListener.operationFailed( fQuery, fErrorCode);
4b22b9337f359bfd063322244f5336cc7c6ffcfars else
4b22b9337f359bfd063322244f5336cc7c6ffcfars fListener.queryAnswered( fQuery, fFlags, fIndex, fFullName, fType, fClass, fData, fTTL);
4b22b9337f359bfd063322244f5336cc7c6ffcfars }
4b22b9337f359bfd063322244f5336cc7c6ffcfars
4b22b9337f359bfd063322244f5336cc7c6ffcfars protected void schedule()
4b22b9337f359bfd063322244f5336cc7c6ffcfars {
4b22b9337f359bfd063322244f5336cc7c6ffcfars try {
4b22b9337f359bfd063322244f5336cc7c6ffcfars SwingUtilities.invokeAndWait( this);
4b22b9337f359bfd063322244f5336cc7c6ffcfars }
4b22b9337f359bfd063322244f5336cc7c6ffcfars catch ( Exception e)
4b22b9337f359bfd063322244f5336cc7c6ffcfars {
4b22b9337f359bfd063322244f5336cc7c6ffcfars e.printStackTrace();
4b22b9337f359bfd063322244f5336cc7c6ffcfars }
4b22b9337f359bfd063322244f5336cc7c6ffcfars }
4b22b9337f359bfd063322244f5336cc7c6ffcfars
4b22b9337f359bfd063322244f5336cc7c6ffcfars protected QueryListener fListener;
4b22b9337f359bfd063322244f5336cc7c6ffcfars
4b22b9337f359bfd063322244f5336cc7c6ffcfars protected DNSSDService fQuery;
4b22b9337f359bfd063322244f5336cc7c6ffcfars protected int fFlags;
4b22b9337f359bfd063322244f5336cc7c6ffcfars protected int fIndex;
4b22b9337f359bfd063322244f5336cc7c6ffcfars protected int fErrorCode;
4b22b9337f359bfd063322244f5336cc7c6ffcfars protected String fFullName;
4b22b9337f359bfd063322244f5336cc7c6ffcfars protected int fType;
4b22b9337f359bfd063322244f5336cc7c6ffcfars protected int fClass;
4b22b9337f359bfd063322244f5336cc7c6ffcfars protected byte[] fData;
4b22b9337f359bfd063322244f5336cc7c6ffcfars protected int fTTL;
4b22b9337f359bfd063322244f5336cc7c6ffcfars}
4b22b9337f359bfd063322244f5336cc7c6ffcfars