4b22b9337f359bfd063322244f5336cc7c6ffcfars/* -*- Mode: Java; tab-width: 4 -*-
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
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 * 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 * 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 * 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 SimpleChat is a simple peer-to-peer chat program that demonstrates
4b22b9337f359bfd063322244f5336cc7c6ffcfars DNS-SD registration, browsing, resolving and record-querying.
4b22b9337f359bfd063322244f5336cc7c6ffcfars - implement better coloring algorithm
4b22b9337f359bfd063322244f5336cc7c6ffcfarsclass SimpleChat implements ResolveListener, RegisterListener, QueryListener,
4b22b9337f359bfd063322244f5336cc7c6ffcfars String ourName; // name used to identify this user in chat
4b22b9337f359bfd063322244f5336cc7c6ffcfars DNSSDService browser; // object that actively browses for other chat clients
4b22b9337f359bfd063322244f5336cc7c6ffcfars DNSSDService resolver; // object that resolves other chat clients
4b22b9337f359bfd063322244f5336cc7c6ffcfars DNSSDRegistration registration; // object that maintains our connection advertisement
4b22b9337f359bfd063322244f5336cc7c6ffcfars JComboBox targetPicker; // Indicates who we're talking to
4b22b9337f359bfd063322244f5336cc7c6ffcfars JButton sendButton; // Will send text in inputField to target
4b22b9337f359bfd063322244f5336cc7c6ffcfars static final String kChatExampleRegType = "_p2pchat._udp";
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void windowClosing(WindowEvent e) {System.exit(0);}
4b22b9337f359bfd063322244f5336cc7c6ffcfars this.setupSubPanes( frame.getContentPane(), frame.getRootPane());
4b22b9337f359bfd063322244f5336cc7c6ffcfars browser = DNSSD.browse( 0, 0, kChatExampleRegType, "", new SwingBrowseListener( targetList));
4b22b9337f359bfd063322244f5336cc7c6ffcfars registration = DNSSD.register( 0, 0, ourName, kChatExampleRegType, "", "", inSocket.getLocalPort(), null, this);
4b22b9337f359bfd063322244f5336cc7c6ffcfars protected void setupSubPanes( Container parent, JRootPane rootPane)
4b22b9337f359bfd063322244f5336cc7c6ffcfars parent.setLayout( new BoxLayout( parent, BoxLayout.Y_AXIS));
4b22b9337f359bfd063322244f5336cc7c6ffcfars textRow.setLayout( new BoxLayout( textRow, BoxLayout.X_AXIS));
4b22b9337f359bfd063322244f5336cc7c6ffcfars textRow.add( Box.createRigidArea( new Dimension( 16, 0)));
4b22b9337f359bfd063322244f5336cc7c6ffcfars JEditorPane textPane = new JEditorPane( "text/html", "<BR>");
4b22b9337f359bfd063322244f5336cc7c6ffcfars textRow.add( Box.createRigidArea( new Dimension( 16, 0)));
4b22b9337f359bfd063322244f5336cc7c6ffcfars addressRow.setLayout( new BoxLayout( addressRow, BoxLayout.X_AXIS));
4b22b9337f359bfd063322244f5336cc7c6ffcfars addressRow.add( Box.createRigidArea( new Dimension( 16, 0)));
4b22b9337f359bfd063322244f5336cc7c6ffcfars buttonRow.setLayout( new BoxLayout( buttonRow, BoxLayout.X_AXIS));
4b22b9337f359bfd063322244f5336cc7c6ffcfars buttonRow.add( Box.createRigidArea( new Dimension( 16, 0)));
4b22b9337f359bfd063322244f5336cc7c6ffcfars // prevent inputField from hijacking <Enter> key
4b22b9337f359bfd063322244f5336cc7c6ffcfars inputField.getKeymap().removeKeyStrokeBinding( KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0));
4b22b9337f359bfd063322244f5336cc7c6ffcfars buttonRow.add( Box.createRigidArea( new Dimension( 8, 0)));
4b22b9337f359bfd063322244f5336cc7c6ffcfars buttonRow.add( Box.createRigidArea( new Dimension( 16, 0)));
4b22b9337f359bfd063322244f5336cc7c6ffcfars parent.add( Box.createRigidArea( new Dimension( 0, 16)));
4b22b9337f359bfd063322244f5336cc7c6ffcfars parent.add( Box.createRigidArea( new Dimension( 0, 16)));
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void serviceRegistered( DNSSDRegistration registration, int flags,
4b22b9337f359bfd063322244f5336cc7c6ffcfars ourName = serviceName; // might have been renamed on collision
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void operationFailed( DNSSDService service, int errorCode)
4b22b9337f359bfd063322244f5336cc7c6ffcfars System.out.println( "Service reported error " + String.valueOf( errorCode));
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void serviceResolved( DNSSDService resolver, int flags, int ifIndex, String fullName,
4b22b9337f359bfd063322244f5336cc7c6ffcfars // Start a record query to obtain IP address from hostname
4b22b9337f359bfd063322244f5336cc7c6ffcfars DNSSD.queryRecord( 0, ifIndex, hostName, 1 /* ns_t_a */, 1 /* ns_c_in */,
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void queryAnswered( DNSSDService query, int flags, int ifIndex, String fullName,
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void actionPerformed( ActionEvent e) // invoked when Send button is hit
4b22b9337f359bfd063322244f5336cc7c6ffcfars String sendString = ourName + ": " + inputField.getText();
4b22b9337f359bfd063322244f5336cc7c6ffcfars outSocket.send( new DatagramPacket( sendData, sendData.length, buddyAddr, buddyPort));
4b22b9337f359bfd063322244f5336cc7c6ffcfars textDoc.insertString( textDoc.getLength(), inputField.getText() + "\n", textAttribs);
4b22b9337f359bfd063322244f5336cc7c6ffcfars catch ( Exception exception) { terminateWithException( exception); }
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void itemStateChanged( ItemEvent e) // invoked when Target selection changes
4b22b9337f359bfd063322244f5336cc7c6ffcfars TargetListElem sel = (TargetListElem) targetList.getSelectedItem();
4b22b9337f359bfd063322244f5336cc7c6ffcfars resolver = DNSSD.resolve( 0, sel.fInt, sel.fServiceName, sel.fType, sel.fDomain, this);
4b22b9337f359bfd063322244f5336cc7c6ffcfars catch ( Exception exception) { terminateWithException( exception); }
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void run() // invoked on event thread when inbound packet arrives
4b22b9337f359bfd063322244f5336cc7c6ffcfars String inMessage = new String( dataPacket.getData(), 0, dataPacket.getLength(), kWireCharSet);
4b22b9337f359bfd063322244f5336cc7c6ffcfars StyleConstants.setForeground( textAttribs, this.getColorFor( dataPacket.getData(), dataPacket.getLength()));
4b22b9337f359bfd063322244f5336cc7c6ffcfars textDoc.insertString( textDoc.getLength(), inMessage + "\n", textAttribs);
4b22b9337f359bfd063322244f5336cc7c6ffcfars // Produce a mapping from a string to a color, suitable for text display
4b22b9337f359bfd063322244f5336cc7c6ffcfars return new Color( rgb & 0x007F7FFF); // mask off high bits so it is a dark color
4b22b9337f359bfd063322244f5336cc7c6ffcfars// for ( int i=0; i < length && chars[i] != ':'; i++)
4b22b9337f359bfd063322244f5336cc7c6ffcfars protected static void terminateWithException( Exception e)
4b22b9337f359bfd063322244f5336cc7c6ffcfars public TargetListElem( String serviceName, String domain, String type, int ifIndex)
4b22b9337f359bfd063322244f5336cc7c6ffcfars { fServiceName = serviceName; fDomain = domain; fType = type; fInt = ifIndex; }
4b22b9337f359bfd063322244f5336cc7c6ffcfarsclass TargetListModel extends DefaultComboBoxModel implements BrowseListener
4b22b9337f359bfd063322244f5336cc7c6ffcfars /* The Browser invokes this callback when a service is discovered. */
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void serviceFound( DNSSDService browser, int flags, int ifIndex,
4b22b9337f359bfd063322244f5336cc7c6ffcfars TargetListElem match = this.findMatching( serviceName); // probably doesn't handle near-duplicates well.
4b22b9337f359bfd063322244f5336cc7c6ffcfars this.addElement( new TargetListElem( serviceName, domain, regType, ifIndex));
4b22b9337f359bfd063322244f5336cc7c6ffcfars /* The Browser invokes this callback when a service disappears. */
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void serviceLost( DNSSDService browser, int flags, int ifIndex,
4b22b9337f359bfd063322244f5336cc7c6ffcfars TargetListElem match = this.findMatching( serviceName); // probably doesn't handle near-duplicates well.
4b22b9337f359bfd063322244f5336cc7c6ffcfars /* The Browser invokes this callback when a service disappears. */
4b22b9337f359bfd063322244f5336cc7c6ffcfars public void operationFailed( DNSSDService service, int errorCode)
4b22b9337f359bfd063322244f5336cc7c6ffcfars System.out.println( "Service reported error " + String.valueOf( errorCode));
4b22b9337f359bfd063322244f5336cc7c6ffcfars// A ListenerThread runs its owner when datagram packet p appears on socket s.
4b22b9337f359bfd063322244f5336cc7c6ffcfars public ListenerThread( Runnable owner, DatagramSocket s, DatagramPacket p)
4b22b9337f359bfd063322244f5336cc7c6ffcfars while ( true )
4b22b9337f359bfd063322244f5336cc7c6ffcfars SwingUtilities.invokeAndWait( fOwner); // process data on main thread
4b22b9337f359bfd063322244f5336cc7c6ffcfars break; // terminate thread