SimpleChat.java revision 4b22b9337f359bfd063322244f5336cc7c6ffcfa
/* -*- Mode: Java; tab-width: 4 -*-
*
* Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
*
* Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
* ("Apple") in consideration of your agreement to the following terms, and your
* use, installation, modification or redistribution of this Apple software
* constitutes acceptance of these terms. If you do not agree with these terms,
* please do not use, install, modify or redistribute this Apple software.
*
* In consideration of your agreement to abide by the following terms, and subject
* to these terms, Apple grants you a personal, non-exclusive license, under Apple's
* copyrights in this original Apple software (the "Apple Software"), to use,
* reproduce, modify and redistribute the Apple Software, with or without
* the Apple Software in its entirety and without modifications, you must retain
* this notice and the following text and disclaimers in all such redistributions of
* the Apple Software. Neither the name, trademarks, service marks or logos of
* Apple Computer, Inc. may be used to endorse or promote products derived from the
* Apple Software without specific prior written permission from Apple. Except as
* expressly stated in this notice, no other rights or licenses, express or implied,
* are granted by Apple herein, including but not limited to any patent rights that
* may be infringed by your derivative works or by other works in which the Apple
* Software may be incorporated.
*
* The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
* WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
* COMBINATION WITH YOUR PRODUCTS.
*
* IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
SimpleChat is a simple peer-to-peer chat program that demonstrates
DNS-SD registration, browsing, resolving and record-querying.
To do:
- implement better coloring algorithm
ident "%Z%%M% %I% %E% SMI"
*/
{
int buddyPort; // and port
public SimpleChat() throws Exception
{
});
targetList = new TargetListModel();
textAttribs = new SimpleAttributeSet();
outSocket = new DatagramSocket();
frame.setVisible(true);
registration = DNSSD.register( 0, 0, ourName, kChatExampleRegType, "", "", inSocket.getLocalPort(), null, this);
}
{
textPane.setEditable( false);
targetPicker.addItemListener( this);
inputField = new JTextField();
// prevent inputField from hijacking <Enter> key
sendButton.addActionListener( this);
sendButton.setEnabled( false);
}
{
}
{
}
{
try {
// Start a record query to obtain IP address from hostname
new SwingQueryListener( this));
}
catch ( Exception e) { terminateWithException( e); }
}
{
try {
}
catch ( Exception e) { terminateWithException( e); }
sendButton.setEnabled( true);
}
{
try
{
}
}
{
sendButton.setEnabled( false);
{
try {
}
}
}
public void run() // invoked on event thread when inbound packet arrives
{
try
{
StyleConstants.setForeground( textAttribs, this.getColorFor( dataPacket.getData(), dataPacket.getLength()));
}
catch ( Exception e) { terminateWithException( e); }
}
// Produce a mapping from a string to a color, suitable for text display
{
int rgb = 0;
// for ( int i=0; i < length && chars[i] != ':'; i++)
}
protected static void terminateWithException( Exception e)
{
e.printStackTrace();
}
{
try {
new SimpleChat();
}
catch ( Exception e) { terminateWithException( e); }
}
}
class TargetListElem
{
public int fInt;
}
{
/* The Browser invokes this callback when a service is discovered. */
{
TargetListElem match = this.findMatching( serviceName); // probably doesn't handle near-duplicates well.
}
/* The Browser invokes this callback when a service disappears. */
{
TargetListElem match = this.findMatching( serviceName); // probably doesn't handle near-duplicates well.
this.removeElement( match);
}
/* The Browser invokes this callback when a service disappears. */
{
}
{
for ( int i = 0; i < this.getSize(); i++)
return (TargetListElem) this.getElementAt( i);
return null;
}
}
// A ListenerThread runs its owner when datagram packet p appears on socket s.
class ListenerThread extends Thread
{
public void run()
{
while ( true )
{
try
{
}
catch( Exception e)
{
break; // terminate thread
}
}
}
protected DatagramSocket fSocket;
protected DatagramPacket fPacket;
}