mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-12-23 21:17:58 +01:00
Added Thread Safe behavior in Debugger.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2992 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
e76f335910
commit
a43b8a1cdb
1 changed files with 206 additions and 156 deletions
|
@ -20,29 +20,42 @@
|
|||
|
||||
package org.jivesoftware.smackx.debugger;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.datatransfer.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.Date;
|
||||
import org.jivesoftware.smack.ConnectionListener;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.debugger.SmackDebugger;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.jivesoftware.smack.util.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.*;
|
||||
import javax.swing.table.*;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import javax.xml.transform.*;
|
||||
import javax.xml.transform.stream.*;
|
||||
|
||||
import org.jivesoftware.smack.*;
|
||||
import org.jivesoftware.smack.debugger.*;
|
||||
import org.jivesoftware.smack.packet.*;
|
||||
import org.jivesoftware.smack.util.*;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import java.awt.*;
|
||||
import java.awt.datatransfer.Clipboard;
|
||||
import java.awt.datatransfer.StringSelection;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.net.URL;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* The EnhancedDebugger is a debugger that allows to debug sent, received and interpreted messages
|
||||
* but also provides the ability to send ad-hoc messages composed by the user.<p>
|
||||
*
|
||||
* <p/>
|
||||
* A new EnhancedDebugger will be created for each connection to debug. All the EnhancedDebuggers
|
||||
* will be shown in the same debug window provided by the class EnhancedDebuggerWindow.
|
||||
*
|
||||
|
@ -157,31 +170,53 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
// data as Smack sees it and not as it's coming in as raw XML.
|
||||
packetReaderListener = new PacketListener() {
|
||||
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa");
|
||||
public void processPacket(Packet packet) {
|
||||
|
||||
public void processPacket(final Packet packet) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
addReadPacketToTable(dateFormatter, packet);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
// Create a thread that will listen for all outgoing packets and write them to
|
||||
// the GUI.
|
||||
packetWriterListener = new PacketListener() {
|
||||
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa");
|
||||
public void processPacket(Packet packet) {
|
||||
|
||||
public void processPacket(final Packet packet) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
addSentPacketToTable(dateFormatter, packet);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
// Create a thread that will listen for any connection closed event
|
||||
connListener = new ConnectionListener() {
|
||||
public void connectionClosed() {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
statusField.setValue("Closed");
|
||||
EnhancedDebuggerWindow.connectionClosed(EnhancedDebugger.this);
|
||||
}
|
||||
});
|
||||
|
||||
public void connectionClosedOnError(Exception e) {
|
||||
}
|
||||
|
||||
public void connectionClosedOnError(final Exception e) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
statusField.setValue("Closed due to an exception");
|
||||
EnhancedDebuggerWindow.connectionClosedOnError(EnhancedDebugger.this, e);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -193,11 +228,12 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
|
||||
messagesTable =
|
||||
new DefaultTableModel(
|
||||
new Object[] { "Hide", "Timestamp", "", "", "Message", "Id", "Type", "To", "From" },
|
||||
new Object[]{"Hide", "Timestamp", "", "", "Message", "Id", "Type", "To", "From"},
|
||||
0) {
|
||||
public boolean isCellEditable(int rowIndex, int mColIndex) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Class getColumnClass(int columnIndex) {
|
||||
if (columnIndex == 2 || columnIndex == 3) {
|
||||
return Icon.class;
|
||||
|
@ -350,7 +386,9 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
// Create a special Writer that wraps the main Writer and logs data to the GUI.
|
||||
ObservableWriter debugWriter = new ObservableWriter(writer);
|
||||
writerListener = new WriterListener() {
|
||||
public void write(String str) {
|
||||
public void write(final String str) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER &&
|
||||
!EnhancedDebuggerWindow.getInstance().isVisible()) {
|
||||
// Do not add content if the parent is not visible
|
||||
|
@ -361,6 +399,10 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
if (str.endsWith(">")) {
|
||||
sentText.append(NEWLINE);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
debugWriter.addWriterListener(writerListener);
|
||||
|
@ -547,7 +589,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
packetsPanel.setBorder(BorderFactory.createTitledBorder("Transmitted Packets"));
|
||||
|
||||
statisticsTable =
|
||||
new DefaultTableModel(new Object[][] { { "IQ", new Integer(0), new Integer(0)}, {
|
||||
new DefaultTableModel(new Object[][]{{"IQ", new Integer(0), new Integer(0)}, {
|
||||
"Message", new Integer(0), new Integer(0)
|
||||
}, {
|
||||
"Presence", new Integer(0), new Integer(0)
|
||||
|
@ -556,7 +598,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
}, {
|
||||
"Total", new Integer(0), new Integer(0)
|
||||
}
|
||||
}, new Object[] { "Type", "Received", "Sent" }) {
|
||||
}, new Object[]{"Type", "Received", "Sent"}) {
|
||||
public boolean isCellEditable(int rowIndex, int mColIndex) {
|
||||
return false;
|
||||
}
|
||||
|
@ -574,7 +616,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
}
|
||||
|
||||
public Reader newConnectionReader(Reader newReader) {
|
||||
((ObservableReader)reader).removeReaderListener(readerListener);
|
||||
((ObservableReader) reader).removeReaderListener(readerListener);
|
||||
ObservableReader debugReader = new ObservableReader(newReader);
|
||||
debugReader.addReaderListener(readerListener);
|
||||
reader = debugReader;
|
||||
|
@ -582,20 +624,26 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
}
|
||||
|
||||
public Writer newConnectionWriter(Writer newWriter) {
|
||||
((ObservableWriter)writer).removeWriterListener(writerListener);
|
||||
((ObservableWriter) writer).removeWriterListener(writerListener);
|
||||
ObservableWriter debugWriter = new ObservableWriter(newWriter);
|
||||
debugWriter.addWriterListener(writerListener);
|
||||
writer = debugWriter;
|
||||
return writer;
|
||||
}
|
||||
|
||||
public void userHasLogged(String user) {
|
||||
public void userHasLogged(final String user) {
|
||||
final EnhancedDebugger debugger = this;
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
userField.setText(user);
|
||||
EnhancedDebuggerWindow.userHasLogged(this, user);
|
||||
EnhancedDebuggerWindow.userHasLogged(debugger, user);
|
||||
// Add the connection listener to the connection so that the debugger can be notified
|
||||
// whenever the connection is closed.
|
||||
connection.addConnectionListener(connListener);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public Reader getReader() {
|
||||
return reader;
|
||||
|
@ -680,7 +728,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
}
|
||||
|
||||
messagesTable.addRow(
|
||||
new Object[] {
|
||||
new Object[]{
|
||||
formatXML(packet.toXML()),
|
||||
dateFormatter.format(new Date()),
|
||||
packetReceivedIcon,
|
||||
|
@ -689,7 +737,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
packet.getPacketID(),
|
||||
type,
|
||||
"",
|
||||
from });
|
||||
from});
|
||||
// Update the statistics table
|
||||
updateStatistics();
|
||||
}
|
||||
|
@ -743,7 +791,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
}
|
||||
|
||||
messagesTable.addRow(
|
||||
new Object[] {
|
||||
new Object[]{
|
||||
formatXML(packet.toXML()),
|
||||
dateFormatter.format(new Date()),
|
||||
packetSentIcon,
|
||||
|
@ -752,7 +800,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
packet.getPacketID(),
|
||||
type,
|
||||
to,
|
||||
"" });
|
||||
""});
|
||||
|
||||
// Update the statistics table
|
||||
updateStatistics();
|
||||
|
@ -769,7 +817,8 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
try {
|
||||
tFactory.setAttribute("indent-number", new Integer(2));
|
||||
}
|
||||
catch (IllegalArgumentException e) {}
|
||||
catch (IllegalArgumentException e) {
|
||||
}
|
||||
Transformer transformer = tFactory.newTransformer();
|
||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
|
@ -821,24 +870,22 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
|
||||
/**
|
||||
* Stops debugging the connection. Removes any listener on the connection.
|
||||
*
|
||||
*/
|
||||
void cancel() {
|
||||
connection.removeConnectionListener(connListener);
|
||||
connection.removePacketListener(packetReaderListener);
|
||||
connection.removePacketWriterListener(packetWriterListener);
|
||||
((ObservableReader)reader).removeReaderListener(readerListener);
|
||||
((ObservableWriter)writer).removeWriterListener(writerListener);
|
||||
((ObservableReader) reader).removeReaderListener(readerListener);
|
||||
((ObservableWriter) writer).removeWriterListener(writerListener);
|
||||
messagesTable = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* An ad-hoc packet is like any regular packet but with the exception that it's intention is
|
||||
* to be used only <b>to send packets</b>.<p>
|
||||
*
|
||||
* <p/>
|
||||
* The whole text to send must be passed to the constructor. This implies that the client of
|
||||
* this class is responsible for sending a valid text to the constructor.
|
||||
*
|
||||
*/
|
||||
private class AdHocPacket extends Packet {
|
||||
|
||||
|
@ -864,6 +911,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
* Listens for debug window popup dialog events.
|
||||
*/
|
||||
private class PopupListener extends MouseAdapter {
|
||||
|
||||
JPopupMenu popup;
|
||||
|
||||
PopupListener(JPopupMenu popupMenu) {
|
||||
|
@ -886,6 +934,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
}
|
||||
|
||||
private class SelectionListener implements ListSelectionListener {
|
||||
|
||||
JTable table;
|
||||
|
||||
// It is necessary to keep the table since it is not possible
|
||||
|
@ -893,6 +942,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
|||
SelectionListener(JTable table) {
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
public void valueChanged(ListSelectionEvent e) {
|
||||
if (table.getSelectedRow() == -1) {
|
||||
// Clear the messageTextArea since there is none packet selected
|
||||
|
|
Loading…
Reference in a new issue