xref: /titanic_44/usr/src/lib/libdns_sd/java/com/apple/dnssd/docs/examples/src/SwingQueryListener.java (revision 4b22b9337f359bfd063322244f5336cc7c6ffcfa)
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 ident	"%Z%%M%	%I%	%E% SMI"
41 
42  */
43 
44 
45 import javax.swing.*;
46 import com.apple.dnssd.*;
47 
48 
49 /**	Use this to schedule QueryListener callbacks via SwingUtilities.invokeAndWait(). */
50 
51 public class SwingQueryListener implements Runnable, QueryListener
52 {
53 	/** Create a listener for DNSSD that will call your listener on the Swing/AWT event thread. */
SwingQueryListener( QueryListener listener)54 	public	SwingQueryListener( QueryListener listener)
55 	{ fListener = listener; }
56 
operationFailed( DNSSDService service, int errorCode)57 	public void	operationFailed( DNSSDService service, int errorCode)
58 	{
59 		fQuery = service;
60 		fErrorCode = errorCode;
61 		this.schedule();
62 	}
63 
64 	/** (Clients should not call this method directly.) */
queryAnswered( DNSSDService query, int flags, int ifIndex, String fullName, int rrtype, int rrclass, byte[] rdata, int ttl)65 	public void	queryAnswered( DNSSDService query, int flags, int ifIndex, String fullName,
66 								int rrtype, int rrclass, byte[] rdata, int ttl)
67 	{
68 		fQuery = query;
69 		fFlags = flags;
70 		fIndex = ifIndex;
71 		fFullName = fullName;
72 		fType = rrtype;
73 		fClass = rrclass;
74 		fData = rdata;
75 		fTTL = ttl;
76 		this.schedule();
77 	}
78 
79 	/** (Clients should not call this method directly.) */
run()80 	public void		run()
81 	{
82 		if ( fErrorCode != 0)
83 			fListener.operationFailed( fQuery, fErrorCode);
84 		else
85 			fListener.queryAnswered( fQuery, fFlags, fIndex, fFullName, fType, fClass, fData, fTTL);
86 	}
87 
schedule()88 	protected void	schedule()
89 	{
90 		try {
91 			SwingUtilities.invokeAndWait( this);
92 		}
93 		catch ( Exception e)
94 		{
95 			e.printStackTrace();
96 		}
97 	}
98 
99 	protected QueryListener		fListener;
100 
101 	protected DNSSDService		fQuery;
102 	protected int				fFlags;
103 	protected int				fIndex;
104 	protected int				fErrorCode;
105 	protected String			fFullName;
106 	protected int				fType;
107 	protected int				fClass;
108 	protected byte[]			fData;
109 	protected int				fTTL;
110 }
111 
112