use of org.jivesoftware.smack.PacketListener in project ecf by eclipse.
the class MessageEventManager method init.
private void init() {
// Listens for all message event packets and fire the proper message event listeners.
packetListener = new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
MessageEvent messageEvent = (MessageEvent) message.getExtension("x", "jabber:x:event");
if (messageEvent.isMessageEventRequest()) {
// Fire event for requests of message events
for (Iterator<String> it = messageEvent.getEventTypes(); it.hasNext(); ) fireMessageEventRequestListeners(message.getFrom(), message.getPacketID(), it.next().concat("NotificationRequested"));
} else
// Fire event for notifications of message events
for (Iterator<String> it = messageEvent.getEventTypes(); it.hasNext(); ) fireMessageEventNotificationListeners(message.getFrom(), messageEvent.getPacketID(), it.next().concat("Notification"));
}
};
con.addPacketListener(packetListener, packetFilter);
}
use of org.jivesoftware.smack.PacketListener in project ecf by eclipse.
the class PEPManager method init.
private void init() {
// Listens for all roster exchange packets and fire the roster exchange listeners.
packetListener = new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
PEPEvent event = (PEPEvent) message.getExtension("event", "http://jabber.org/protocol/pubsub#event");
// Fire event for roster exchange listeners
firePEPListeners(message.getFrom(), event);
}
};
connection.addPacketListener(packetListener, packetFilter);
}
use of org.jivesoftware.smack.PacketListener in project ecf by eclipse.
the class EnhancedDebugger method createDebug.
/**
* Creates the debug process, which is a GUI window that displays XML traffic.
*/
private void createDebug() {
// We'll arrange the UI into six tabs. The first tab contains all data, the second
// client generated XML, the third server generated XML, the fourth allows to send
// ad-hoc messages and the fifth contains connection information.
tabbedPane = new JTabbedPane();
// Add the All Packets, Sent, Received and Interpreted panels
addBasicPanels();
// Add the panel to send ad-hoc messages
addAdhocPacketPanel();
// Add the connection information panel
addInformationPanel();
// 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.
packetReaderListener = new PacketListener() {
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss:SS aaa");
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:SS aaa");
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(final Exception e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
statusField.setValue("Closed due to an exception");
EnhancedDebuggerWindow.connectionClosedOnError(EnhancedDebugger.this, e);
}
});
}
public void reconnectingIn(final int seconds) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
statusField.setValue("Attempt to reconnect in " + seconds + " seconds");
}
});
}
public void reconnectionSuccessful() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
statusField.setValue("Reconnection stablished");
EnhancedDebuggerWindow.connectionEstablished(EnhancedDebugger.this);
}
});
}
public void reconnectionFailed(Exception e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
statusField.setValue("Reconnection failed");
}
});
}
};
}
use of org.jivesoftware.smack.PacketListener in project Zom-Android by zom.
the class XmppStreamHandler method startListening.
private void startListening() {
mConnection.addConnectionListener(new ConnectionListener() {
public void reconnectionSuccessful() {
}
public void reconnectionFailed(Exception e) {
}
public void reconnectingIn(int seconds) {
}
public void connectionClosedOnError(Exception e) {
if (e instanceof XMPPException && ((XMPPException) e).getMessage() != null) {
// Non-resumable stream error
close();
} else {
// Resumable
closeOnError();
}
}
@Override
public void connected(XMPPConnection connection) {
}
@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
}
public void connectionClosed() {
previousIncomingStanzaCount = -1;
}
});
mConnection.addPacketSendingListener(new PacketListener() {
public void processStanza(Stanza packet) {
if (isOutgoingSmEnabled && !outgoingQueue.contains(packet)) {
outgoingStanzaCount++;
outgoingQueue.add(packet);
trace("send " + outgoingStanzaCount + " : " + packet.toXML());
try {
// if acks are not coming.
if (outgoingQueue.size() >= maxOutgoingQueueSize / OUTGOING_FILL_RATIO) {
mConnection.sendStanza(new StreamHandlingPacket("r", URN_SM_2));
}
} catch (Exception e) {
// he's not watching
}
if (outgoingQueue.size() > maxOutgoingQueueSize) {
// Log.e(XmppConnection.TAG, "not receiving acks? outgoing queue full");
outgoingQueue.remove();
}
} else if (isOutgoingSmEnabled && outgoingQueue.contains(packet)) {
outgoingStanzaCount++;
trace("send DUPLICATE " + outgoingStanzaCount + " : " + packet.toXML());
} else {
trace("send " + packet.toXML());
}
}
}, new PacketFilter() {
public boolean accept(Stanza packet) {
return true;
}
});
mConnection.addAsyncStanzaListener(new StanzaListener() {
public void processStanza(Stanza stanza) {
if (isSmEnabled) {
incomingStanzaCount++;
trace("recv " + incomingStanzaCount + " : " + stanza.toXML());
} else {
trace("recv " + stanza.toXML());
}
if (stanza instanceof StreamHandlingPacket) {
StreamHandlingPacket shPacket = (StreamHandlingPacket) stanza;
String name = shPacket.getElementName();
try {
if ("sm".equals(name)) {
debug("sm avail");
isSmAvailable = true;
if (sessionId != null)
sendEnablePacket();
} else if ("r".equals(name)) {
StreamHandlingPacket ackPacket = new StreamHandlingPacket("a", URN_SM_2);
ackPacket.addAttribute("h", String.valueOf(incomingStanzaCount));
mConnection.sendPacket(ackPacket);
} else if ("a".equals(name)) {
long ackCount = Long.valueOf(shPacket.getAttribute("h"));
removeOutgoingAcked(ackCount);
trace(outgoingQueue.size() + " in outgoing queue after ack");
} else if ("enabled".equals(name)) {
incomingStanzaCount = 0;
isSmEnabled = true;
// mConnection.getRoster().setOfflineOnError(false);
String resume = shPacket.getAttribute("resume");
if ("true".equals(resume) || "1".equals(resume)) {
sessionId = shPacket.getAttribute("id");
}
debug("sm enabled " + sessionId);
} else if ("resumed".equals(name)) {
debug("sm resumed");
incomingStanzaCount = previousIncomingStanzaCount;
long resumeStanzaCount = Long.valueOf(shPacket.getAttribute("h"));
// Removed acked packets
removeOutgoingAcked(resumeStanzaCount);
trace(outgoingQueue.size() + " in outgoing queue after resume");
// Resend any unacked packets
for (Stanza resendPacket : outgoingQueue) {
// mConnection.sendPacket(resendPacket);
// mConnection.send(resendPacket);
}
// Enable only after resend, so that the interceptor does not
// queue these again or increment outgoingStanzaCount.
isSmEnabled = true;
// Re-notify the listener - we are really ready for packets now
// Before this point, isSuspendPending() was true, and the listener should have
// ignored reconnectionSuccessful() from XMPPConnection.
mConnectionListener.reconnectionSuccessful();
} else if ("failed".equals(name)) {
// Failed, shutdown and the parent will retry
debug("sm failed");
// mConnection.getRoster().setOfflineOnError(true);
// mConnection.getRoster().setOfflinePresences();
sessionId = null;
mConnection.disconnect();
// isSmEnabled / isOutgoingSmEnabled are already false
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}, new StanzaFilter() {
public boolean accept(Stanza packet) {
return true;
}
});
}
use of org.jivesoftware.smack.PacketListener in project asmack by Flowdalic.
the class AndroidDebugger method createDebug.
/**
* Creates the listeners that will print in the console when new activity is detected.
*/
private void createDebug() {
// Create a special Reader that wraps the main Reader and logs data to the GUI.
ObservableReader debugReader = new ObservableReader(reader);
readerListener = new ReaderListener() {
public void read(String str) {
Log.d("SMACK", "RCV (" + connection.getConnectionCounter() + "): " + str);
}
};
debugReader.addReaderListener(readerListener);
// 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) {
Log.d("SMACK", "SENT (" + connection.getConnectionCounter() + "): " + str);
}
};
debugWriter.addWriterListener(writerListener);
// 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.
listener = new PacketListener() {
public void processPacket(Packet packet) {
if (printInterpreted) {
Log.d("SMACK", "RCV PKT (" + connection.getConnectionCounter() + "): " + packet.toXML());
}
}
};
connListener = new AbstractConnectionListener() {
public void connectionClosed() {
Log.d("SMACK", "Connection closed (" + connection.getConnectionCounter() + ")");
}
public void connectionClosedOnError(Exception e) {
Log.d("SMACK", "Connection closed due to an exception (" + connection.getConnectionCounter() + ")");
}
public void reconnectionFailed(Exception e) {
Log.d("SMACK", "Reconnection failed due to an exception (" + connection.getConnectionCounter() + ")");
}
public void reconnectionSuccessful() {
Log.d("SMACK", "Connection reconnected (" + connection.getConnectionCounter() + ")");
}
public void reconnectingIn(int seconds) {
Log.d("SMACK", "Connection (" + connection.getConnectionCounter() + ") will reconnect in " + seconds);
}
};
}
Aggregations