mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-30 07:52:06 +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;
|
package org.jivesoftware.smackx.debugger;
|
||||||
|
|
||||||
import java.awt.*;
|
import org.jivesoftware.smack.ConnectionListener;
|
||||||
import java.awt.datatransfer.*;
|
import org.jivesoftware.smack.PacketListener;
|
||||||
import java.awt.event.*;
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import java.io.*;
|
import org.jivesoftware.smack.debugger.SmackDebugger;
|
||||||
import java.net.*;
|
import org.jivesoftware.smack.packet.IQ;
|
||||||
import java.text.*;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import java.util.Date;
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
|
import org.jivesoftware.smack.packet.Presence;
|
||||||
|
import org.jivesoftware.smack.util.*;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.event.*;
|
import javax.swing.event.ListSelectionEvent;
|
||||||
import javax.swing.table.*;
|
import javax.swing.event.ListSelectionListener;
|
||||||
|
import javax.swing.table.DefaultTableModel;
|
||||||
import javax.xml.transform.*;
|
import javax.xml.transform.*;
|
||||||
import javax.xml.transform.stream.*;
|
import javax.xml.transform.stream.StreamResult;
|
||||||
|
import javax.xml.transform.stream.StreamSource;
|
||||||
import org.jivesoftware.smack.*;
|
import java.awt.*;
|
||||||
import org.jivesoftware.smack.debugger.*;
|
import java.awt.datatransfer.Clipboard;
|
||||||
import org.jivesoftware.smack.packet.*;
|
import java.awt.datatransfer.StringSelection;
|
||||||
import org.jivesoftware.smack.util.*;
|
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
|
* 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>
|
* 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
|
* 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.
|
* 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.
|
// data as Smack sees it and not as it's coming in as raw XML.
|
||||||
packetReaderListener = new PacketListener() {
|
packetReaderListener = new PacketListener() {
|
||||||
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa");
|
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);
|
addReadPacketToTable(dateFormatter, packet);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create a thread that will listen for all outgoing packets and write them to
|
// Create a thread that will listen for all outgoing packets and write them to
|
||||||
// the GUI.
|
// the GUI.
|
||||||
packetWriterListener = new PacketListener() {
|
packetWriterListener = new PacketListener() {
|
||||||
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa");
|
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);
|
addSentPacketToTable(dateFormatter, packet);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create a thread that will listen for any connection closed event
|
// Create a thread that will listen for any connection closed event
|
||||||
connListener = new ConnectionListener() {
|
connListener = new ConnectionListener() {
|
||||||
public void connectionClosed() {
|
public void connectionClosed() {
|
||||||
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
public void run() {
|
||||||
statusField.setValue("Closed");
|
statusField.setValue("Closed");
|
||||||
EnhancedDebuggerWindow.connectionClosed(EnhancedDebugger.this);
|
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");
|
statusField.setValue("Closed due to an exception");
|
||||||
EnhancedDebuggerWindow.connectionClosedOnError(EnhancedDebugger.this, e);
|
EnhancedDebuggerWindow.connectionClosedOnError(EnhancedDebugger.this, e);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,11 +228,12 @@ public class EnhancedDebugger implements SmackDebugger {
|
||||||
|
|
||||||
messagesTable =
|
messagesTable =
|
||||||
new DefaultTableModel(
|
new DefaultTableModel(
|
||||||
new Object[] { "Hide", "Timestamp", "", "", "Message", "Id", "Type", "To", "From" },
|
new Object[]{"Hide", "Timestamp", "", "", "Message", "Id", "Type", "To", "From"},
|
||||||
0) {
|
0) {
|
||||||
public boolean isCellEditable(int rowIndex, int mColIndex) {
|
public boolean isCellEditable(int rowIndex, int mColIndex) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Class getColumnClass(int columnIndex) {
|
public Class getColumnClass(int columnIndex) {
|
||||||
if (columnIndex == 2 || columnIndex == 3) {
|
if (columnIndex == 2 || columnIndex == 3) {
|
||||||
return Icon.class;
|
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.
|
// Create a special Writer that wraps the main Writer and logs data to the GUI.
|
||||||
ObservableWriter debugWriter = new ObservableWriter(writer);
|
ObservableWriter debugWriter = new ObservableWriter(writer);
|
||||||
writerListener = new WriterListener() {
|
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 &&
|
if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER &&
|
||||||
!EnhancedDebuggerWindow.getInstance().isVisible()) {
|
!EnhancedDebuggerWindow.getInstance().isVisible()) {
|
||||||
// Do not add content if the parent is not visible
|
// Do not add content if the parent is not visible
|
||||||
|
@ -361,6 +399,10 @@ public class EnhancedDebugger implements SmackDebugger {
|
||||||
if (str.endsWith(">")) {
|
if (str.endsWith(">")) {
|
||||||
sentText.append(NEWLINE);
|
sentText.append(NEWLINE);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
debugWriter.addWriterListener(writerListener);
|
debugWriter.addWriterListener(writerListener);
|
||||||
|
@ -547,7 +589,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
||||||
packetsPanel.setBorder(BorderFactory.createTitledBorder("Transmitted Packets"));
|
packetsPanel.setBorder(BorderFactory.createTitledBorder("Transmitted Packets"));
|
||||||
|
|
||||||
statisticsTable =
|
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)
|
"Message", new Integer(0), new Integer(0)
|
||||||
}, {
|
}, {
|
||||||
"Presence", 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)
|
"Total", new Integer(0), new Integer(0)
|
||||||
}
|
}
|
||||||
}, new Object[] { "Type", "Received", "Sent" }) {
|
}, new Object[]{"Type", "Received", "Sent"}) {
|
||||||
public boolean isCellEditable(int rowIndex, int mColIndex) {
|
public boolean isCellEditable(int rowIndex, int mColIndex) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -574,7 +616,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Reader newConnectionReader(Reader newReader) {
|
public Reader newConnectionReader(Reader newReader) {
|
||||||
((ObservableReader)reader).removeReaderListener(readerListener);
|
((ObservableReader) reader).removeReaderListener(readerListener);
|
||||||
ObservableReader debugReader = new ObservableReader(newReader);
|
ObservableReader debugReader = new ObservableReader(newReader);
|
||||||
debugReader.addReaderListener(readerListener);
|
debugReader.addReaderListener(readerListener);
|
||||||
reader = debugReader;
|
reader = debugReader;
|
||||||
|
@ -582,20 +624,26 @@ public class EnhancedDebugger implements SmackDebugger {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Writer newConnectionWriter(Writer newWriter) {
|
public Writer newConnectionWriter(Writer newWriter) {
|
||||||
((ObservableWriter)writer).removeWriterListener(writerListener);
|
((ObservableWriter) writer).removeWriterListener(writerListener);
|
||||||
ObservableWriter debugWriter = new ObservableWriter(newWriter);
|
ObservableWriter debugWriter = new ObservableWriter(newWriter);
|
||||||
debugWriter.addWriterListener(writerListener);
|
debugWriter.addWriterListener(writerListener);
|
||||||
writer = debugWriter;
|
writer = debugWriter;
|
||||||
return writer;
|
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);
|
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
|
// Add the connection listener to the connection so that the debugger can be notified
|
||||||
// whenever the connection is closed.
|
// whenever the connection is closed.
|
||||||
connection.addConnectionListener(connListener);
|
connection.addConnectionListener(connListener);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public Reader getReader() {
|
public Reader getReader() {
|
||||||
return reader;
|
return reader;
|
||||||
|
@ -680,7 +728,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
||||||
}
|
}
|
||||||
|
|
||||||
messagesTable.addRow(
|
messagesTable.addRow(
|
||||||
new Object[] {
|
new Object[]{
|
||||||
formatXML(packet.toXML()),
|
formatXML(packet.toXML()),
|
||||||
dateFormatter.format(new Date()),
|
dateFormatter.format(new Date()),
|
||||||
packetReceivedIcon,
|
packetReceivedIcon,
|
||||||
|
@ -689,7 +737,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
||||||
packet.getPacketID(),
|
packet.getPacketID(),
|
||||||
type,
|
type,
|
||||||
"",
|
"",
|
||||||
from });
|
from});
|
||||||
// Update the statistics table
|
// Update the statistics table
|
||||||
updateStatistics();
|
updateStatistics();
|
||||||
}
|
}
|
||||||
|
@ -743,7 +791,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
||||||
}
|
}
|
||||||
|
|
||||||
messagesTable.addRow(
|
messagesTable.addRow(
|
||||||
new Object[] {
|
new Object[]{
|
||||||
formatXML(packet.toXML()),
|
formatXML(packet.toXML()),
|
||||||
dateFormatter.format(new Date()),
|
dateFormatter.format(new Date()),
|
||||||
packetSentIcon,
|
packetSentIcon,
|
||||||
|
@ -752,7 +800,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
||||||
packet.getPacketID(),
|
packet.getPacketID(),
|
||||||
type,
|
type,
|
||||||
to,
|
to,
|
||||||
"" });
|
""});
|
||||||
|
|
||||||
// Update the statistics table
|
// Update the statistics table
|
||||||
updateStatistics();
|
updateStatistics();
|
||||||
|
@ -769,7 +817,8 @@ public class EnhancedDebugger implements SmackDebugger {
|
||||||
try {
|
try {
|
||||||
tFactory.setAttribute("indent-number", new Integer(2));
|
tFactory.setAttribute("indent-number", new Integer(2));
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException e) {}
|
catch (IllegalArgumentException e) {
|
||||||
|
}
|
||||||
Transformer transformer = tFactory.newTransformer();
|
Transformer transformer = tFactory.newTransformer();
|
||||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||||
transformer.setOutputProperty(OutputKeys.INDENT, "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.
|
* Stops debugging the connection. Removes any listener on the connection.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
void cancel() {
|
void cancel() {
|
||||||
connection.removeConnectionListener(connListener);
|
connection.removeConnectionListener(connListener);
|
||||||
connection.removePacketListener(packetReaderListener);
|
connection.removePacketListener(packetReaderListener);
|
||||||
connection.removePacketWriterListener(packetWriterListener);
|
connection.removePacketWriterListener(packetWriterListener);
|
||||||
((ObservableReader)reader).removeReaderListener(readerListener);
|
((ObservableReader) reader).removeReaderListener(readerListener);
|
||||||
((ObservableWriter)writer).removeWriterListener(writerListener);
|
((ObservableWriter) writer).removeWriterListener(writerListener);
|
||||||
messagesTable = null;
|
messagesTable = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An ad-hoc packet is like any regular packet but with the exception that it's intention is
|
* 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>
|
* 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
|
* 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.
|
* this class is responsible for sending a valid text to the constructor.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
private class AdHocPacket extends Packet {
|
private class AdHocPacket extends Packet {
|
||||||
|
|
||||||
|
@ -864,6 +911,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
||||||
* Listens for debug window popup dialog events.
|
* Listens for debug window popup dialog events.
|
||||||
*/
|
*/
|
||||||
private class PopupListener extends MouseAdapter {
|
private class PopupListener extends MouseAdapter {
|
||||||
|
|
||||||
JPopupMenu popup;
|
JPopupMenu popup;
|
||||||
|
|
||||||
PopupListener(JPopupMenu popupMenu) {
|
PopupListener(JPopupMenu popupMenu) {
|
||||||
|
@ -886,6 +934,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SelectionListener implements ListSelectionListener {
|
private class SelectionListener implements ListSelectionListener {
|
||||||
|
|
||||||
JTable table;
|
JTable table;
|
||||||
|
|
||||||
// It is necessary to keep the table since it is not possible
|
// It is necessary to keep the table since it is not possible
|
||||||
|
@ -893,6 +942,7 @@ public class EnhancedDebugger implements SmackDebugger {
|
||||||
SelectionListener(JTable table) {
|
SelectionListener(JTable table) {
|
||||||
this.table = table;
|
this.table = table;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void valueChanged(ListSelectionEvent e) {
|
public void valueChanged(ListSelectionEvent e) {
|
||||||
if (table.getSelectedRow() == -1) {
|
if (table.getSelectedRow() == -1) {
|
||||||
// Clear the messageTextArea since there is none packet selected
|
// Clear the messageTextArea since there is none packet selected
|
||||||
|
|
Loading…
Reference in a new issue