2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
|
|
|
* ====================================================================
|
|
|
|
* The Jive Software License (based on Apache Software License, Version 1.1)
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. The end-user documentation included with the redistribution,
|
|
|
|
* if any, must include the following acknowledgment:
|
|
|
|
* "This product includes software developed by
|
|
|
|
* Jive Software (http://www.jivesoftware.com)."
|
|
|
|
* Alternately, this acknowledgment may appear in the software itself,
|
|
|
|
* if and wherever such third-party acknowledgments normally appear.
|
|
|
|
*
|
|
|
|
* 4. The names "Smack" and "Jive Software" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please
|
2003-01-17 21:34:50 +01:00
|
|
|
* contact webmaster@jivesoftware.com.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "Smack",
|
|
|
|
* nor may "Smack" appear in their name, without prior written
|
|
|
|
* permission of Jive Software.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.*;
|
2003-04-07 07:40:28 +02:00
|
|
|
import org.jivesoftware.smack.filter.*;
|
2003-08-12 21:30:51 +02:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.net.*;
|
|
|
|
import java.io.*;
|
|
|
|
import java.awt.*;
|
2003-07-31 16:18:54 +02:00
|
|
|
import java.awt.datatransfer.Clipboard;
|
|
|
|
import java.awt.datatransfer.StringSelection;
|
|
|
|
import java.awt.event.*;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
/**
|
2003-04-18 06:38:27 +02:00
|
|
|
* Creates a connection to a XMPP server. A simple use of this API might
|
2003-01-13 17:58:47 +01:00
|
|
|
* look like the following:
|
|
|
|
* <pre>
|
|
|
|
* // Create a connection to the jivesoftware.com XMPP server.
|
|
|
|
* XMPPConnection con = new XMPPConnection("jivesoftware.com");
|
|
|
|
* // Most servers require you to login before performing other tasks.
|
|
|
|
* con.login("jsmith", "mypass");
|
|
|
|
* // Start a new conversation with John Doe and send him a message.
|
2003-03-09 00:09:48 +01:00
|
|
|
* Chat chat = con.createChat("jdoe@jabber.org");
|
2003-01-13 17:58:47 +01:00
|
|
|
* chat.sendMessage("Hey, how's it going?");
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
|
|
|
public class XMPPConnection {
|
|
|
|
|
2003-04-29 07:32:34 +02:00
|
|
|
private static final String NEWLINE = "\n";
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Value that indicates whether debugging is enabled. When enabled, a debug
|
|
|
|
* window will apear for each new connection that will contain the following
|
|
|
|
* information:<ul>
|
|
|
|
* <li> Client Traffic -- raw XML traffic generated by Smack and sent to the server.
|
|
|
|
* <li> Server Traffic -- raw XML traffic sent by the server to the client.
|
|
|
|
* <li> Interpreted Packets -- shows XML packets from the server as parsed by Smack.
|
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* Debugging can be enabled by setting this field to true, or by setting the Java system
|
|
|
|
* property <tt>smack.debugEnabled</tt> to true. The system property can be set on the
|
|
|
|
* command line such as "java SomeApp -Dsmack.debugEnabled=true".
|
|
|
|
*/
|
2003-04-29 07:32:34 +02:00
|
|
|
public static boolean DEBUG_ENABLED = false;
|
|
|
|
static {
|
2003-05-05 07:02:31 +02:00
|
|
|
// Use try block since we may not have permission to get a system
|
|
|
|
// property (for example, when an applet).
|
2003-04-29 07:32:34 +02:00
|
|
|
try {
|
|
|
|
DEBUG_ENABLED = Boolean.getBoolean("smack.debugEnabled");
|
|
|
|
}
|
2003-05-05 07:02:31 +02:00
|
|
|
catch (Exception e) { }
|
2003-04-29 07:32:34 +02:00
|
|
|
}
|
2003-03-07 20:50:24 +01:00
|
|
|
private JFrame debugFrame = null;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2003-09-18 23:37:52 +02:00
|
|
|
String host;
|
|
|
|
int port;
|
|
|
|
Socket socket;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
String connectionID;
|
2003-08-12 21:30:51 +02:00
|
|
|
private String user = null;
|
2003-01-13 17:58:47 +01:00
|
|
|
private boolean connected = false;
|
2003-03-09 00:09:48 +01:00
|
|
|
private boolean authenticated = false;
|
2003-08-12 21:30:51 +02:00
|
|
|
private boolean anonymous = false;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2003-09-19 00:35:42 +02:00
|
|
|
PacketWriter packetWriter;
|
|
|
|
PacketReader packetReader;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2003-04-07 07:40:28 +02:00
|
|
|
Roster roster = null;
|
2003-03-09 00:09:48 +01:00
|
|
|
private AccountManager accountManager = null;
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
Writer writer;
|
|
|
|
Reader reader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for use by classes extending this one.
|
|
|
|
*/
|
2003-09-18 23:37:52 +02:00
|
|
|
XMPPConnection() {
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-04-18 06:38:27 +02:00
|
|
|
* Creates a new connection to the specified XMPP server. The default port of 5222 will
|
2003-01-13 17:58:47 +01:00
|
|
|
* be used.
|
|
|
|
*
|
2003-04-18 06:38:27 +02:00
|
|
|
* @param host the name of the XMPP server to connect to; e.g. <tt>jivesoftware.com</tt>.
|
2003-08-19 18:32:13 +02:00
|
|
|
* @throws XMPPException if an error occurs while trying to establish the connection.
|
|
|
|
* Two possible errors can occur which will be wrapped by an XMPPException --
|
|
|
|
* UnknownHostException (XMPP error code 504), and IOException (XMPP error code
|
|
|
|
* 502). The error codes and wrapped exceptions can be used to present more
|
|
|
|
* appropiate error messages to end-users.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public XMPPConnection(String host) throws XMPPException {
|
|
|
|
this(host, 5222);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-04-18 06:38:27 +02:00
|
|
|
* Creates a new connection to the to the specified XMPP server on the given port.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2003-04-18 06:38:27 +02:00
|
|
|
* @param host the name of the XMPP server to connect to; e.g. <tt>jivesoftware.com</tt>.
|
2003-01-13 17:58:47 +01:00
|
|
|
* @param port the port on the server that should be used; e.g. <tt>5222</tt>.
|
2003-08-19 18:32:13 +02:00
|
|
|
* @throws XMPPException if an error occurs while trying to establish the connection.
|
|
|
|
* Two possible errors can occur which will be wrapped by an XMPPException --
|
|
|
|
* UnknownHostException (XMPP error code 504), and IOException (XMPP error code
|
|
|
|
* 502). The error codes and wrapped exceptions can be used to present more
|
|
|
|
* appropiate error messages to end-users.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public XMPPConnection(String host, int port) throws XMPPException {
|
|
|
|
this.host = host;
|
|
|
|
this.port = port;
|
|
|
|
try {
|
|
|
|
this.socket = new Socket(host, port);
|
|
|
|
}
|
|
|
|
catch (UnknownHostException uhe) {
|
2003-08-19 18:32:13 +02:00
|
|
|
throw new XMPPException("Could not connect to " + host + ":" + port + ".",
|
|
|
|
new XMPPError(502), uhe);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
catch (IOException ioe) {
|
2003-08-19 18:32:13 +02:00
|
|
|
throw new XMPPException("XMPPError connecting to " + host + ":" + port + ".",
|
|
|
|
new XMPPError(502), ioe);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the connection ID for this connection, which is the value set by the server
|
2003-04-18 06:38:27 +02:00
|
|
|
* when opening a XMPP stream. If the server does not set a connection ID, this value
|
2003-01-13 17:58:47 +01:00
|
|
|
* will be null.
|
|
|
|
*
|
2003-04-18 06:38:27 +02:00
|
|
|
* @return the ID of this connection returned from the XMPP server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public String getConnectionID() {
|
|
|
|
return connectionID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-04-18 06:38:27 +02:00
|
|
|
* Returns the host name of the XMPP server for this connection.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2003-04-18 06:38:27 +02:00
|
|
|
* @return the host name of the XMPP server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public String getHost() {
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the port number of the XMPP server for this connection. The default port
|
|
|
|
* for normal connections is 5222. The default port for SSL connections is 5223.
|
|
|
|
*
|
2003-04-18 06:38:27 +02:00
|
|
|
* @return the port number of the XMPP server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public int getPort() {
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2003-03-10 00:06:59 +01:00
|
|
|
/**
|
2003-08-12 21:30:51 +02:00
|
|
|
* Returns the full XMPP address of the user that is logged in to the connection or
|
|
|
|
* <tt>null</tt> if not logged in yet. An XMPP address is in the form
|
|
|
|
* username@server/resource.
|
2003-03-10 00:06:59 +01:00
|
|
|
*
|
2003-08-12 21:30:51 +02:00
|
|
|
* @return the full XMPP address of the user logged in.
|
2003-03-10 00:06:59 +01:00
|
|
|
*/
|
2003-08-12 21:30:51 +02:00
|
|
|
public String getUser() {
|
2003-03-10 00:06:59 +01:00
|
|
|
if (!isAuthenticated()) {
|
|
|
|
return null;
|
|
|
|
}
|
2003-08-12 21:30:51 +02:00
|
|
|
return user;
|
2003-03-10 00:06:59 +01:00
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
2003-01-17 08:11:33 +01:00
|
|
|
* Logs in to the server using the strongest authentication mode supported by
|
2003-01-15 15:54:11 +01:00
|
|
|
* the server, then set our presence to available. If more than five seconds
|
|
|
|
* elapses in each step of the authentication process without a response from
|
|
|
|
* the server, or if an error occurs, a XMPPException will be thrown.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @param username the username.
|
|
|
|
* @param password the password.
|
|
|
|
* @throws XMPPException if an error occurs.
|
|
|
|
*/
|
2003-01-16 02:01:27 +01:00
|
|
|
public void login(String username, String password) throws XMPPException {
|
|
|
|
login(username, password, "Smack");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-01-17 08:11:33 +01:00
|
|
|
* Logs in to the server using the strongest authentication mode supported by
|
2003-01-16 02:01:27 +01:00
|
|
|
* the server, then set our presence to available. If more than five seconds
|
|
|
|
* elapses in each step of the authentication process without a response from
|
|
|
|
* the server, or if an error occurs, a XMPPException will be thrown.
|
|
|
|
*
|
|
|
|
* @param username the username.
|
|
|
|
* @param password the password.
|
|
|
|
* @param resource the resource.
|
|
|
|
* @throws XMPPException if an error occurs.
|
2003-03-09 00:09:48 +01:00
|
|
|
* @throws IllegalStateException if not connected to the server, or already logged in
|
|
|
|
* to the serrver.
|
2003-01-16 02:01:27 +01:00
|
|
|
*/
|
|
|
|
public synchronized void login(String username, String password, String resource)
|
|
|
|
throws XMPPException
|
|
|
|
{
|
2003-01-13 17:58:47 +01:00
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
2003-03-09 00:09:48 +01:00
|
|
|
if (authenticated) {
|
|
|
|
throw new IllegalStateException("Already logged in to server.");
|
|
|
|
}
|
2003-01-15 15:54:11 +01:00
|
|
|
// If we send an authentication packet in "get" mode with just the username,
|
|
|
|
// the server will return the list of authentication protocols it supports.
|
|
|
|
Authentication discoveryAuth = new Authentication();
|
|
|
|
discoveryAuth.setType(IQ.Type.GET);
|
|
|
|
discoveryAuth.setUsername(username);
|
2003-01-16 02:01:27 +01:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
PacketCollector collector = packetReader.createPacketCollector(
|
2003-01-15 15:54:11 +01:00
|
|
|
new PacketIDFilter(discoveryAuth.getPacketID()));
|
2003-01-16 02:01:27 +01:00
|
|
|
// Send the packet
|
|
|
|
packetWriter.sendPacket(discoveryAuth);
|
|
|
|
// Wait up to five seconds for a response from the server.
|
2003-03-25 17:24:03 +01:00
|
|
|
IQ response = (IQ)collector.nextResult(5000);
|
|
|
|
if (response == null) {
|
2003-01-15 15:54:11 +01:00
|
|
|
throw new XMPPException("No response from the server.");
|
|
|
|
}
|
2003-03-25 17:24:03 +01:00
|
|
|
// If the server replied with an error, throw an exception.
|
|
|
|
else if (response.getType() == IQ.Type.ERROR) {
|
|
|
|
throw new XMPPException(response.getError());
|
2003-03-07 20:50:24 +01:00
|
|
|
}
|
2003-03-25 17:24:03 +01:00
|
|
|
// Otherwise, no error so continue processing.
|
|
|
|
Authentication authTypes = (Authentication)response;
|
|
|
|
collector.cancel();
|
2003-01-16 02:01:27 +01:00
|
|
|
|
2003-01-15 15:54:11 +01:00
|
|
|
// Now, create the authentication packet we'll send to the server.
|
|
|
|
Authentication auth = new Authentication();
|
|
|
|
auth.setUsername(username);
|
|
|
|
|
|
|
|
// Figure out if we should use digest or plain text authentication.
|
|
|
|
if (authTypes.getDigest() != null) {
|
|
|
|
auth.setDigest(connectionID, password);
|
|
|
|
}
|
|
|
|
else if (authTypes.getPassword() != null) {
|
|
|
|
auth.setPassword(password);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new XMPPException("Server does not support compatible authentication mechanism.");
|
|
|
|
}
|
|
|
|
|
2003-01-16 02:01:27 +01:00
|
|
|
auth.setResource(resource);
|
|
|
|
|
2003-08-20 22:31:47 +02:00
|
|
|
collector = packetReader.createPacketCollector(new PacketIDFilter(auth.getPacketID()));
|
2003-01-16 02:01:27 +01:00
|
|
|
// Send the packet.
|
|
|
|
packetWriter.sendPacket(auth);
|
|
|
|
// Wait up to five seconds for a response from the server.
|
2003-03-25 17:24:03 +01:00
|
|
|
response = (IQ)collector.nextResult(5000);
|
2003-01-15 15:54:11 +01:00
|
|
|
if (response == null) {
|
2003-01-13 17:58:47 +01:00
|
|
|
throw new XMPPException("Authentication failed.");
|
|
|
|
}
|
2003-01-15 15:54:11 +01:00
|
|
|
else if (response.getType() == IQ.Type.ERROR) {
|
2003-03-09 00:09:48 +01:00
|
|
|
throw new XMPPException(response.getError());
|
2003-01-15 15:54:11 +01:00
|
|
|
}
|
2003-08-12 21:30:51 +02:00
|
|
|
// Set the user.
|
|
|
|
if (response.getTo() != null) {
|
|
|
|
this.user = response.getTo();
|
|
|
|
}
|
|
|
|
else {
|
2003-08-20 22:31:47 +02:00
|
|
|
this.user = username + "@" + this.host;
|
|
|
|
if (resource != null) {
|
|
|
|
this.user += "/" + resource;
|
2003-08-12 21:30:51 +02:00
|
|
|
}
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
// We're done with the collector, so explicitly cancel it.
|
|
|
|
collector.cancel();
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2003-07-27 01:07:43 +02:00
|
|
|
// Create the roster.
|
2003-02-10 06:01:01 +01:00
|
|
|
this.roster = new Roster(this);
|
|
|
|
roster.reload();
|
2003-03-07 20:50:24 +01:00
|
|
|
|
2003-07-27 01:07:43 +02:00
|
|
|
// Set presence to online.
|
|
|
|
packetWriter.sendPacket(new Presence(Presence.Type.AVAILABLE));
|
|
|
|
|
2003-03-09 00:09:48 +01:00
|
|
|
// Indicate that we're now authenticated.
|
|
|
|
authenticated = true;
|
2003-08-12 21:30:51 +02:00
|
|
|
anonymous = false;
|
2003-03-09 00:09:48 +01:00
|
|
|
|
2003-03-07 20:50:24 +01:00
|
|
|
// If debugging is enabled, change the the debug window title to include the
|
|
|
|
// name we are now logged-in as.
|
|
|
|
if (DEBUG_ENABLED) {
|
|
|
|
String title = "Smack Debug Window -- " + username + "@" + getHost() + ":" + getPort();
|
|
|
|
if (resource != null) {
|
|
|
|
title += "/" + resource;
|
|
|
|
}
|
|
|
|
debugFrame.setTitle(title);
|
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
|
2003-08-12 21:30:51 +02:00
|
|
|
/**
|
|
|
|
* Logs in to the server anonymously. Very few servers are configured to support anonymous
|
|
|
|
* authentication, so it's fairly likely logging in anonymously will fail. If anonymous login
|
|
|
|
* does succeed, your XMPP address will likely be in the form "server/123ABC" (where "123ABC" is a
|
|
|
|
* random value generated by the server).
|
|
|
|
*
|
|
|
|
* @throws XMPPException if an error occurs or anonymous logins are not supported by the server.
|
|
|
|
* @throws IllegalStateException if not connected to the server, or already logged in
|
|
|
|
* to the serrver.
|
|
|
|
*/
|
|
|
|
public synchronized void loginAnonymously() throws XMPPException {
|
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
|
|
|
if (authenticated) {
|
|
|
|
throw new IllegalStateException("Already logged in to server.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the authentication packet we'll send to the server.
|
|
|
|
Authentication auth = new Authentication();
|
|
|
|
|
|
|
|
PacketCollector collector = packetReader.createPacketCollector(
|
|
|
|
new PacketIDFilter(auth.getPacketID()));
|
|
|
|
// Send the packet.
|
|
|
|
packetWriter.sendPacket(auth);
|
|
|
|
// Wait up to five seconds for a response from the server.
|
|
|
|
IQ response = (IQ)collector.nextResult(5000);
|
|
|
|
if (response == null) {
|
|
|
|
throw new XMPPException("Anonymous login failed.");
|
|
|
|
}
|
|
|
|
else if (response.getType() == IQ.Type.ERROR) {
|
|
|
|
throw new XMPPException(response.getError());
|
|
|
|
}
|
|
|
|
// Set the user value.
|
|
|
|
if (response.getTo() != null) {
|
|
|
|
this.user = response.getTo();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.user = this.host + "/" + ((Authentication)response).getResource();
|
|
|
|
}
|
|
|
|
// We're done with the collector, so explicitly cancel it.
|
|
|
|
collector.cancel();
|
|
|
|
|
|
|
|
// Anonymous users can't have a roster.
|
|
|
|
roster = null;
|
|
|
|
|
|
|
|
// Set presence to online.
|
|
|
|
packetWriter.sendPacket(new Presence(Presence.Type.AVAILABLE));
|
|
|
|
|
|
|
|
// Indicate that we're now authenticated.
|
|
|
|
authenticated = true;
|
|
|
|
anonymous = true;
|
|
|
|
|
|
|
|
// If debugging is enabled, change the the debug window title to include the
|
|
|
|
// name we are now logged-in as.
|
|
|
|
if (DEBUG_ENABLED) {
|
|
|
|
String title = "Smack Debug Window -- " + getHost() + ":" + getPort();
|
|
|
|
title += "/" + StringUtils.parseResource(user);
|
|
|
|
debugFrame.setTitle(title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-09 00:09:48 +01:00
|
|
|
/**
|
|
|
|
* Returns the roster for the user logged into the server. If the user has not yet
|
2003-08-12 21:30:51 +02:00
|
|
|
* logged into the server (or if the user is logged in anonymously), this method will return
|
|
|
|
* <tt>null</tt>.
|
2003-03-09 00:09:48 +01:00
|
|
|
*
|
|
|
|
* @return the user's roster, or <tt>null</tt> if the user has not logged in yet.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public Roster getRoster() {
|
2003-04-07 07:40:28 +02:00
|
|
|
if (roster == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// If this is the first time the user has asked for the roster after calling
|
|
|
|
// login, we want to wait up to 2 seconds for the server to send back the
|
|
|
|
// user's roster. This behavior shields API users from having to worry about the
|
|
|
|
// fact that roster operations are asynchronous, although they'll still have to
|
|
|
|
// listen for changes to the roster. Note: because of this waiting logic, internal
|
|
|
|
// Smack code should be wary about calling the getRoster method, and may need to
|
|
|
|
// access the roster object directly.
|
|
|
|
int elapsed = 0;
|
|
|
|
while (!roster.rosterInitialized && elapsed <= 2000) {
|
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
}
|
|
|
|
catch (Exception e) { }
|
|
|
|
elapsed += 500;
|
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
return roster;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2003-03-09 00:09:48 +01:00
|
|
|
/**
|
2003-03-10 00:06:59 +01:00
|
|
|
* Returns an account manager instance for this connection.
|
|
|
|
*
|
|
|
|
* @return an account manager for this connection.
|
2003-03-09 00:09:48 +01:00
|
|
|
*/
|
|
|
|
public synchronized AccountManager getAccountManager() {
|
|
|
|
if (accountManager == null) {
|
|
|
|
accountManager = new AccountManager(this);
|
|
|
|
}
|
|
|
|
return accountManager;
|
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Creates a new chat with the specified participant. The participant should
|
2003-04-18 06:38:27 +02:00
|
|
|
* be a valid XMPP user such as <tt>jdoe@jivesoftware.com</tt> or
|
2003-01-13 17:58:47 +01:00
|
|
|
* <tt>jdoe@jivesoftware.com/work</tt>.
|
|
|
|
*
|
|
|
|
* @param participant the person to start the conversation with.
|
|
|
|
* @return a new Chat object.
|
|
|
|
*/
|
|
|
|
public Chat createChat(String participant) {
|
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
|
|
|
return new Chat(this, participant);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new group chat connected to the specified room. The room name
|
2003-09-23 22:14:31 +02:00
|
|
|
* should be full address, such as <tt>room@chat.example.com</tt>.
|
|
|
|
* <p>
|
|
|
|
* Most XMPP servers use a sub-domain for the chat service (eg chat.example.com
|
|
|
|
* for the XMPP server example.com). You must ensure that the room address you're
|
|
|
|
* trying to connect to includes the proper chat sub-domain.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2003-09-23 22:14:31 +02:00
|
|
|
* @param room the fully qualifed name of the room.
|
2003-01-13 17:58:47 +01:00
|
|
|
* @return a new GroupChat object.
|
|
|
|
*/
|
|
|
|
public GroupChat createGroupChat(String room) {
|
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
|
|
|
return new GroupChat(this, room);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-03-09 00:09:48 +01:00
|
|
|
* Returns true if currently connected to the XMPP server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @return true if connected.
|
|
|
|
*/
|
|
|
|
public boolean isConnected() {
|
|
|
|
return connected;
|
|
|
|
}
|
|
|
|
|
2003-09-18 22:39:14 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the connection is a secured one, such as an SSL connection.
|
|
|
|
*
|
|
|
|
* @return true if a secure connection to the server.
|
|
|
|
*/
|
|
|
|
public boolean isSecureConnection() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-03-09 00:09:48 +01:00
|
|
|
/**
|
|
|
|
* Returns true if currently authenticated by successfully calling the login method.
|
|
|
|
*
|
|
|
|
* @return true if authenticated.
|
|
|
|
*/
|
|
|
|
public boolean isAuthenticated() {
|
|
|
|
return authenticated;
|
|
|
|
}
|
|
|
|
|
2003-08-12 21:30:51 +02:00
|
|
|
/**
|
|
|
|
* Returns true if currently authenticated anonymously.
|
|
|
|
*
|
|
|
|
* @return true if authenticated anonymously.
|
|
|
|
*/
|
|
|
|
public boolean isAnonymous() {
|
|
|
|
return anonymous;
|
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
2003-01-17 08:11:33 +01:00
|
|
|
* Closes the connection by setting presence to unavailable then closing the stream to
|
|
|
|
* the XMPP server. Once a connection has been closed, it cannot be re-opened.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public void close() {
|
|
|
|
// Set presence to offline.
|
2003-01-16 16:41:57 +01:00
|
|
|
packetWriter.sendPacket(new Presence(Presence.Type.UNAVAILABLE));
|
2003-01-13 17:58:47 +01:00
|
|
|
packetReader.shutdown();
|
2003-07-16 04:49:04 +02:00
|
|
|
packetWriter.shutdown();
|
|
|
|
// Wait 150 ms for processes to clean-up, then shutdown.
|
2003-01-27 16:56:21 +01:00
|
|
|
try {
|
2003-07-16 04:49:04 +02:00
|
|
|
Thread.sleep(150);
|
2003-01-27 16:56:21 +01:00
|
|
|
}
|
|
|
|
catch (Exception e) { }
|
2003-01-13 17:58:47 +01:00
|
|
|
try {
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
catch (Exception e) { }
|
2003-03-09 00:09:48 +01:00
|
|
|
authenticated = false;
|
2003-01-13 17:58:47 +01:00
|
|
|
connected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-01-17 08:11:33 +01:00
|
|
|
* Sends the specified packet to the server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2003-01-17 08:11:33 +01:00
|
|
|
* @param packet the packet to send.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2003-01-17 08:11:33 +01:00
|
|
|
public void sendPacket(Packet packet) {
|
2003-01-13 17:58:47 +01:00
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
2003-01-17 08:11:33 +01:00
|
|
|
packetWriter.sendPacket(packet);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-01-17 08:11:33 +01:00
|
|
|
* Registers a packet listener with this connection. A packet filter determines
|
|
|
|
* which packets will be delivered to the listener.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2003-01-17 08:11:33 +01:00
|
|
|
* @param packetListener the packet listener to notify of new packets.
|
|
|
|
* @param packetFilter the packet filter to use.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2003-01-17 08:11:33 +01:00
|
|
|
public void addPacketListener(PacketListener packetListener, PacketFilter packetFilter) {
|
2003-01-13 17:58:47 +01:00
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
2003-01-17 08:11:33 +01:00
|
|
|
packetReader.addPacketListener(packetListener, packetFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a packet listener from this connection.
|
|
|
|
*
|
|
|
|
* @param packetListener the packet listener to remove.
|
|
|
|
*/
|
|
|
|
public void removePacketListener(PacketListener packetListener) {
|
|
|
|
packetReader.removePacketListener(packetListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new packet collector for this connection. A packet filter determines
|
|
|
|
* which packets will be accumulated by the collector.
|
|
|
|
*
|
2003-01-17 19:26:52 +01:00
|
|
|
* @param packetFilter the packet filter to use.
|
|
|
|
* @return a new packet collector.
|
2003-01-17 08:11:33 +01:00
|
|
|
*/
|
|
|
|
public PacketCollector createPacketCollector(PacketFilter packetFilter) {
|
|
|
|
return packetReader.createPacketCollector(packetFilter);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2003-07-09 18:05:43 +02:00
|
|
|
/**
|
|
|
|
* Adds a connection listener to this connection that will be notified when
|
|
|
|
* the connection closes or fails.
|
|
|
|
*
|
|
|
|
* @param connectionListener a connection listener.
|
|
|
|
*/
|
|
|
|
public void addConnectionListener(ConnectionListener connectionListener) {
|
|
|
|
if (connectionListener == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
synchronized (packetReader.connectionListeners) {
|
|
|
|
if (!packetReader.connectionListeners.contains(connectionListener)) {
|
|
|
|
packetReader.connectionListeners.add(connectionListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a connection listener from this connection.
|
|
|
|
*
|
|
|
|
* @param connectionListener a connection listener.
|
|
|
|
*/
|
|
|
|
public void removeConnectionListener(ConnectionListener connectionListener) {
|
|
|
|
synchronized (packetReader.connectionListeners) {
|
|
|
|
packetReader.connectionListeners.remove(connectionListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Initializes the connection by creating a packet reader and writer and opening a
|
2003-04-18 06:38:27 +02:00
|
|
|
* XMPP stream to the server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @throws XMPPException if establishing a connection to the server fails.
|
|
|
|
*/
|
2003-01-17 08:11:33 +01:00
|
|
|
void init() throws XMPPException {
|
2003-01-13 17:58:47 +01:00
|
|
|
try {
|
2003-02-10 06:01:01 +01:00
|
|
|
reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
|
|
|
|
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
catch (IOException ioe) {
|
2003-08-19 18:32:13 +02:00
|
|
|
throw new XMPPException("XMPPError establishing connection with server.",
|
|
|
|
new XMPPError(502), ioe);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If debugging is enabled, we open a window and write out all network traffic.
|
2003-01-17 19:26:52 +01:00
|
|
|
// The method that creates the debug GUI returns PacketListener that we must add
|
|
|
|
// after the packet reader and writer are created.
|
|
|
|
PacketListener debugListener = null;
|
2003-01-13 17:58:47 +01:00
|
|
|
if (DEBUG_ENABLED) {
|
2003-01-17 19:26:52 +01:00
|
|
|
debugListener = createDebug();
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
packetWriter = new PacketWriter(this);
|
|
|
|
packetReader = new PacketReader(this);
|
|
|
|
|
|
|
|
// If debugging is enabled, we should start the thread that will listen for
|
|
|
|
// all packets and then log them.
|
|
|
|
if (DEBUG_ENABLED) {
|
2003-01-17 19:26:52 +01:00
|
|
|
packetReader.addPacketListener(debugListener, null);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2003-04-18 06:38:27 +02:00
|
|
|
// Start the packet writer. This will open a XMPP stream to the server
|
2003-01-15 15:54:11 +01:00
|
|
|
packetWriter.startup();
|
2003-01-13 17:58:47 +01:00
|
|
|
// Start the packet reader. The startup() method will block until we
|
|
|
|
// get an opening stream packet back from server.
|
|
|
|
packetReader.startup();
|
|
|
|
|
|
|
|
// Make note of the fact that we're now connected.
|
|
|
|
connected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the debug process, which is a GUI window that displays XML traffic.
|
2003-01-17 19:26:52 +01:00
|
|
|
* This method must be called before the packet reader and writer are created because
|
2003-01-13 17:58:47 +01:00
|
|
|
* it wraps the reader and writer objects with special logging implementations.
|
2003-01-17 19:26:52 +01:00
|
|
|
* The method returns a PacketListner that must be added after the packet reader is
|
|
|
|
* created.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2003-01-17 19:26:52 +01:00
|
|
|
* @return a PacketListener used by the debugging process that must be added after the packet
|
|
|
|
* reader is created.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2003-01-17 19:26:52 +01:00
|
|
|
private PacketListener createDebug() {
|
2003-01-13 17:58:47 +01:00
|
|
|
// Use the native look and feel.
|
|
|
|
try {
|
|
|
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2003-03-07 20:50:24 +01:00
|
|
|
debugFrame = new JFrame("Smack Debug Window -- " + getHost() + ":" + getPort());
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
// We'll arrange the UI into four tabs. The first tab contains all data, the second
|
|
|
|
// client generated XML, the third server generated XML, and the fourth is packet
|
|
|
|
// data from the server as seen by Smack.
|
|
|
|
JTabbedPane tabbedPane = new JTabbedPane();
|
|
|
|
|
|
|
|
JPanel allPane = new JPanel();
|
|
|
|
allPane.setLayout(new GridLayout(3, 1));
|
|
|
|
tabbedPane.add("All", allPane);
|
|
|
|
|
|
|
|
// Create UI elements for client generated XML traffic.
|
|
|
|
final JTextArea sentText1 = new JTextArea();
|
|
|
|
final JTextArea sentText2 = new JTextArea();
|
|
|
|
sentText1.setEditable(false);
|
|
|
|
sentText2.setEditable(false);
|
|
|
|
sentText1.setForeground(new Color(112, 3, 3));
|
|
|
|
sentText2.setForeground(new Color(112, 3, 3));
|
|
|
|
allPane.add(new JScrollPane(sentText1));
|
2003-08-04 23:05:29 +02:00
|
|
|
tabbedPane.add("Sent", new JScrollPane(sentText2));
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2003-07-31 16:18:54 +02:00
|
|
|
// Add pop-up menu.
|
|
|
|
JPopupMenu menu = new JPopupMenu();
|
|
|
|
JMenuItem menuItem1 = new JMenuItem("Copy");
|
|
|
|
menuItem1.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e)
|
|
|
|
{
|
|
|
|
// Get the clipboard
|
|
|
|
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
|
|
|
// Set the sent text as the new content of the clipboard
|
|
|
|
clipboard.setContents(new StringSelection(sentText1.getText()), null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
JMenuItem menuItem2 = new JMenuItem("Clear");
|
|
|
|
menuItem2.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e)
|
|
|
|
{
|
|
|
|
sentText1.setText("");
|
|
|
|
sentText2.setText("");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add listener to the text area so the popup menu can come up.
|
|
|
|
MouseListener popupListener = new PopupListener(menu);
|
|
|
|
sentText1.addMouseListener(popupListener);
|
|
|
|
sentText2.addMouseListener(popupListener);
|
|
|
|
menu.add(menuItem1);
|
|
|
|
menu.add(menuItem2);
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
// Create UI elements for server generated XML traffic.
|
|
|
|
final JTextArea receivedText1 = new JTextArea();
|
|
|
|
final JTextArea receivedText2 = new JTextArea();
|
|
|
|
receivedText1.setEditable(false);
|
|
|
|
receivedText2.setEditable(false);
|
|
|
|
receivedText1.setForeground(new Color(6, 76, 133));
|
|
|
|
receivedText2.setForeground(new Color(6, 76, 133));
|
|
|
|
allPane.add(new JScrollPane(receivedText1));
|
2003-08-04 23:05:29 +02:00
|
|
|
tabbedPane.add("Received", new JScrollPane(receivedText2));
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2003-07-31 16:18:54 +02:00
|
|
|
// Add pop-up menu.
|
|
|
|
menu = new JPopupMenu();
|
|
|
|
menuItem1 = new JMenuItem("Copy");
|
|
|
|
menuItem1.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e)
|
|
|
|
{
|
|
|
|
// Get the clipboard
|
|
|
|
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
|
|
|
// Set the sent text as the new content of the clipboard
|
|
|
|
clipboard.setContents(new StringSelection(receivedText1.getText()), null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
menuItem2 = new JMenuItem("Clear");
|
|
|
|
menuItem2.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e)
|
|
|
|
{
|
|
|
|
receivedText1.setText("");
|
|
|
|
receivedText2.setText("");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add listener to the text area so the popup menu can come up.
|
|
|
|
popupListener = new PopupListener(menu);
|
|
|
|
receivedText1.addMouseListener(popupListener);
|
|
|
|
receivedText2.addMouseListener(popupListener);
|
|
|
|
menu.add(menuItem1);
|
|
|
|
menu.add(menuItem2);
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
// Create UI elements for interpreted XML traffic.
|
|
|
|
final JTextArea interpretedText1 = new JTextArea();
|
|
|
|
final JTextArea interpretedText2 = new JTextArea();
|
|
|
|
interpretedText1.setEditable(false);
|
|
|
|
interpretedText2.setEditable(false);
|
|
|
|
interpretedText1.setForeground(new Color(1, 94, 35));
|
|
|
|
interpretedText2.setForeground(new Color(1, 94, 35));
|
|
|
|
allPane.add(new JScrollPane(interpretedText1));
|
2003-08-04 23:05:29 +02:00
|
|
|
tabbedPane.add("Interpreted", new JScrollPane(interpretedText2));
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2003-07-31 16:18:54 +02:00
|
|
|
// Add pop-up menu.
|
|
|
|
menu = new JPopupMenu();
|
|
|
|
menuItem1 = new JMenuItem("Copy");
|
|
|
|
menuItem1.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e)
|
|
|
|
{
|
|
|
|
// Get the clipboard
|
|
|
|
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
|
|
|
// Set the sent text as the new content of the clipboard
|
|
|
|
clipboard.setContents(new StringSelection(interpretedText1.getText()), null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
menuItem2 = new JMenuItem("Clear");
|
|
|
|
menuItem2.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e)
|
|
|
|
{
|
|
|
|
interpretedText1.setText("");
|
|
|
|
interpretedText2.setText("");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add listener to the text area so the popup menu can come up.
|
|
|
|
popupListener = new PopupListener(menu);
|
|
|
|
interpretedText1.addMouseListener(popupListener);
|
|
|
|
interpretedText2.addMouseListener(popupListener);
|
|
|
|
menu.add(menuItem1);
|
|
|
|
menu.add(menuItem2);
|
|
|
|
|
2003-06-17 20:14:20 +02:00
|
|
|
debugFrame.getContentPane().add(tabbedPane);
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2003-03-07 20:50:24 +01:00
|
|
|
debugFrame.setSize(550, 400);
|
2003-04-25 22:19:49 +02:00
|
|
|
debugFrame.setVisible(true);
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
// Create a special Reader that wraps the main Reader and logs data to the GUI.
|
|
|
|
Reader debugReader = new Reader() {
|
|
|
|
|
|
|
|
Reader myReader = reader;
|
|
|
|
|
|
|
|
public int read(char cbuf[], int off, int len) throws IOException {
|
|
|
|
int count = myReader.read(cbuf, off, len);
|
2003-01-15 15:54:11 +01:00
|
|
|
if (count > 0) {
|
|
|
|
String str = new String(cbuf, off, count);
|
2003-02-10 06:01:01 +01:00
|
|
|
int index = str.lastIndexOf(">");
|
|
|
|
if (index != -1) {
|
|
|
|
receivedText1.append(str.substring(0, index+1));
|
|
|
|
receivedText2.append(str.substring(0, index+1));
|
2003-01-15 15:54:11 +01:00
|
|
|
receivedText1.append(NEWLINE);
|
|
|
|
receivedText2.append(NEWLINE);
|
2003-02-10 06:01:01 +01:00
|
|
|
if (str.length() > index) {
|
|
|
|
receivedText1.append(str.substring(index+1));
|
|
|
|
receivedText2.append(str.substring(index+1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
receivedText1.append(str);
|
|
|
|
receivedText2.append(str);
|
2003-01-15 15:54:11 +01:00
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void close() throws IOException {
|
|
|
|
myReader.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int read() throws IOException {
|
|
|
|
return myReader.read();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int read(char cbuf[]) throws IOException {
|
|
|
|
return myReader.read(cbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long skip(long n) throws IOException {
|
|
|
|
return myReader.skip(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean ready() throws IOException {
|
|
|
|
return myReader.ready();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean markSupported() {
|
|
|
|
return myReader.markSupported();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void mark(int readAheadLimit) throws IOException {
|
|
|
|
myReader.mark(readAheadLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reset() throws IOException {
|
|
|
|
myReader.reset();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a special Writer that wraps the main Writer and logs data to the GUI.
|
|
|
|
Writer debugWriter = new Writer() {
|
|
|
|
|
|
|
|
Writer myWriter = writer;
|
|
|
|
|
|
|
|
public void write(char cbuf[], int off, int len) throws IOException {
|
|
|
|
myWriter.write(cbuf, off, len);
|
|
|
|
String str = new String(cbuf, off, len);
|
|
|
|
sentText1.append(str);
|
|
|
|
sentText2.append(str);
|
|
|
|
if (str.endsWith(">")) {
|
|
|
|
sentText1.append(NEWLINE);
|
|
|
|
sentText2.append(NEWLINE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void flush() throws IOException {
|
|
|
|
myWriter.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void close() throws IOException {
|
|
|
|
myWriter.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(int c) throws IOException {
|
|
|
|
myWriter.write(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(char cbuf[]) throws IOException {
|
|
|
|
myWriter.write(cbuf);
|
|
|
|
String str = new String(cbuf);
|
|
|
|
sentText1.append(str);
|
|
|
|
sentText2.append(str);
|
|
|
|
if (str.endsWith(">")) {
|
|
|
|
sentText1.append(NEWLINE);
|
|
|
|
sentText2.append(NEWLINE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(String str) throws IOException {
|
|
|
|
myWriter.write(str);
|
|
|
|
sentText1.append(str);
|
|
|
|
sentText2.append(str);
|
|
|
|
if (str.endsWith(">")) {
|
|
|
|
sentText1.append(NEWLINE);
|
|
|
|
sentText2.append(NEWLINE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(String str, int off, int len) throws IOException {
|
|
|
|
myWriter.write(str, off, len);
|
|
|
|
str = str.substring(off, off + len);
|
|
|
|
sentText1.append(str);
|
|
|
|
sentText2.append(str);
|
|
|
|
if (str.endsWith(">")) {
|
|
|
|
sentText1.append(NEWLINE);
|
|
|
|
sentText2.append(NEWLINE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Assign the reader/writer objects to use the debug versions. The packet reader
|
|
|
|
// and writer will use the debug versions when they are created.
|
|
|
|
reader = debugReader;
|
|
|
|
writer = debugWriter;
|
|
|
|
|
|
|
|
// Create a thread that will listen for all incoming packets and write them to
|
|
|
|
// the GUI. This is what we call "interpreted" packet data, since it's the packet
|
|
|
|
// data as Smack sees it and not as it's coming in as raw XML.
|
2003-01-17 19:26:52 +01:00
|
|
|
PacketListener debugListener = new PacketListener() {
|
|
|
|
public void processPacket(Packet packet) {
|
|
|
|
interpretedText1.append(packet.toXML());
|
|
|
|
interpretedText2.append(packet.toXML());
|
|
|
|
interpretedText1.append(NEWLINE);
|
|
|
|
interpretedText2.append(NEWLINE);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
};
|
2003-01-17 19:26:52 +01:00
|
|
|
return debugListener;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2003-07-31 16:18:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Listens for debug window popup dialog events.
|
|
|
|
*/
|
|
|
|
private class PopupListener extends MouseAdapter {
|
|
|
|
JPopupMenu popup;
|
|
|
|
|
|
|
|
PopupListener(JPopupMenu popupMenu) {
|
|
|
|
popup = popupMenu;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void mousePressed(MouseEvent e) {
|
|
|
|
maybeShowPopup(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void mouseReleased(MouseEvent e) {
|
|
|
|
maybeShowPopup(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void maybeShowPopup(MouseEvent e) {
|
|
|
|
if (e.isPopupTrigger()) {
|
|
|
|
popup.show(e.getComponent(), e.getX(), e.getY());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|