use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class StanzaCollector method processStanza.
/**
* Processes a stanza to see if it meets the criteria for this stanza collector.
* If so, the stanza is added to the result queue.
*
* @param packet the stanza to process.
*/
void processStanza(Stanza packet) {
if (packetFilter == null || packetFilter.accept(packet)) {
synchronized (this) {
if (resultQueue.size() == maxQueueSize) {
Stanza rolledOverStanza = resultQueue.poll();
assert rolledOverStanza != null;
}
resultQueue.add(packet);
notifyAll();
}
if (collectorToReset != null) {
collectorToReset.waitStart = System.currentTimeMillis();
}
}
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class EnhancedDebugger method addReadPacketToTable.
/**
* Adds the received stanza detail to the messages table.
*
* @param dateFormatter the SimpleDateFormat to use to format Dates
* @param packet the read stanza to add to the table
*/
private void addReadPacketToTable(final SimpleDateFormat dateFormatter, final TopLevelStreamElement packet) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
String messageType;
Jid from;
String stanzaId;
if (packet instanceof Stanza) {
Stanza stanza = (Stanza) packet;
from = stanza.getFrom();
stanzaId = stanza.getStanzaId();
} else {
from = null;
stanzaId = "(Nonza)";
}
String type = "";
Icon packetTypeIcon;
receivedPackets++;
if (packet instanceof IQ) {
packetTypeIcon = iqPacketIcon;
messageType = "IQ Received (class=" + packet.getClass().getName() + ")";
type = ((IQ) packet).getType().toString();
receivedIQPackets++;
} else if (packet instanceof Message) {
packetTypeIcon = messagePacketIcon;
messageType = "Message Received";
type = ((Message) packet).getType().toString();
receivedMessagePackets++;
} else if (packet instanceof Presence) {
packetTypeIcon = presencePacketIcon;
messageType = "Presence Received";
type = ((Presence) packet).getType().toString();
receivedPresencePackets++;
} else {
packetTypeIcon = unknownPacketTypeIcon;
messageType = packet.getClass().getName() + " Received";
receivedOtherPackets++;
}
// Check if we need to remove old rows from the table to keep memory consumption low
if (EnhancedDebuggerWindow.MAX_TABLE_ROWS > 0 && messagesTable.getRowCount() >= EnhancedDebuggerWindow.MAX_TABLE_ROWS) {
messagesTable.removeRow(0);
}
messagesTable.addRow(new Object[] { XmlUtil.prettyFormatXml(packet.toXML().toString()), dateFormatter.format(new Date()), packetReceivedIcon, packetTypeIcon, messageType, stanzaId, type, "", from });
// Update the statistics table
updateStatistics();
}
});
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class EnhancedDebugger method addSentPacketToTable.
/**
* Adds the sent stanza detail to the messages table.
*
* @param dateFormatter the SimpleDateFormat to use to format Dates
* @param packet the sent stanza to add to the table
*/
private void addSentPacketToTable(final SimpleDateFormat dateFormatter, final TopLevelStreamElement packet) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
String messageType;
Jid to;
String stanzaId;
if (packet instanceof Stanza) {
Stanza stanza = (Stanza) packet;
to = stanza.getTo();
stanzaId = stanza.getStanzaId();
} else {
to = null;
stanzaId = "(Nonza)";
}
String type = "";
Icon packetTypeIcon;
sentPackets++;
if (packet instanceof IQ) {
packetTypeIcon = iqPacketIcon;
messageType = "IQ Sent (class=" + packet.getClass().getName() + ")";
type = ((IQ) packet).getType().toString();
sentIQPackets++;
} else if (packet instanceof Message) {
packetTypeIcon = messagePacketIcon;
messageType = "Message Sent";
type = ((Message) packet).getType().toString();
sentMessagePackets++;
} else if (packet instanceof Presence) {
packetTypeIcon = presencePacketIcon;
messageType = "Presence Sent";
type = ((Presence) packet).getType().toString();
sentPresencePackets++;
} else {
packetTypeIcon = unknownPacketTypeIcon;
messageType = packet.getClass().getName() + " Sent";
sentOtherPackets++;
}
// Check if we need to remove old rows from the table to keep memory consumption low
if (EnhancedDebuggerWindow.MAX_TABLE_ROWS > 0 && messagesTable.getRowCount() >= EnhancedDebuggerWindow.MAX_TABLE_ROWS) {
messagesTable.removeRow(0);
}
messagesTable.addRow(new Object[] { XmlUtil.prettyFormatXml(packet.toXML().toString()), dateFormatter.format(new Date()), packetSentIcon, packetTypeIcon, messageType, stanzaId, type, to, "" });
// Update the statistics table
updateStatistics();
}
});
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class PacketParserUtilsTest method multipleMessageBodiesParsingTest.
@Test
public void multipleMessageBodiesParsingTest() throws Exception {
String control = XMLBuilder.create("message").namespace(StreamOpen.CLIENT_NAMESPACE).a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").a("xml:lang", "en").e("body").t("This is a test of the emergency broadcast system, 1.").up().e("body").a("xml:lang", "ru").t("This is a test of the emergency broadcast system, 2.").up().e("body").a("xml:lang", "sp").t("This is a test of the emergency broadcast system, 3.").asString(outputProperties);
Stanza message = PacketParserUtils.parseStanza(control);
assertXmlSimilar(control, message.toXML());
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class StanzaCollectorTest method verifyThreadSafety.
/**
* Although this doesn't guarantee anything due to the nature of threading, it can potentially
* catch problems.
*
* @throws InterruptedException if interrupted.
*/
@SuppressWarnings("ThreadPriorityCheck")
@Test
public void verifyThreadSafety() throws InterruptedException {
final int insertCount = 500;
final StanzaCollector collector = createTestStanzaCollector(null, new OKEverything(), insertCount);
final AtomicInteger consumer1Dequeued = new AtomicInteger();
final AtomicInteger consumer2Dequeued = new AtomicInteger();
final AtomicInteger consumer3Dequeued = new AtomicInteger();
Thread consumer1 = new Thread(new Runnable() {
@Override
public void run() {
int dequeueCount = 0;
try {
while (true) {
Thread.yield();
Stanza packet = collector.nextResultBlockForever();
if (packet != null) {
dequeueCount++;
}
}
} catch (InterruptedException e) {
// Ignore as it is expected.
} finally {
consumer1Dequeued.set(dequeueCount);
}
}
});
consumer1.setName("consumer 1");
Thread consumer2 = new Thread(new Runnable() {
@Override
public void run() {
Stanza p;
int dequeueCount = 0;
do {
Thread.yield();
try {
p = collector.nextResult(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (p != null) {
dequeueCount++;
}
} while (p != null);
consumer2Dequeued.set(dequeueCount);
}
});
consumer2.setName("consumer 2");
Thread consumer3 = new Thread(new Runnable() {
@Override
public void run() {
Stanza p;
int dequeueCount = 0;
do {
Thread.yield();
p = collector.pollResult();
if (p != null) {
dequeueCount++;
}
} while (p != null);
consumer3Dequeued.set(dequeueCount);
}
});
consumer3.setName("consumer 3");
for (int i = 0; i < insertCount; i++) {
collector.processStanza(new TestPacket(i));
}
consumer1.start();
consumer2.start();
consumer3.start();
consumer3.join();
consumer2.join();
consumer1.interrupt();
consumer1.join();
// We cannot guarantee that this is going to pass due to the possible issue of timing between consumer 1
// and main, but the probability is extremely remote.
assertNull(collector.pollResult());
int consumer1DequeuedLocal = consumer1Dequeued.get();
int consumer2DequeuedLocal = consumer2Dequeued.get();
int consumer3DequeuedLocal = consumer3Dequeued.get();
final int totalDequeued = consumer1DequeuedLocal + consumer2DequeuedLocal + consumer3DequeuedLocal;
assertEquals("Inserted " + insertCount + " but only " + totalDequeued + " c1: " + consumer1DequeuedLocal + " c2: " + consumer2DequeuedLocal + " c3: " + consumer3DequeuedLocal, insertCount, totalDequeued);
}
Aggregations