Search in sources :

Example 6 with PacketListener

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);
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) Message(org.jivesoftware.smack.packet.Message) MessageEvent(org.jivesoftware.smackx.packet.MessageEvent) Iterator(java.util.Iterator) PacketListener(org.jivesoftware.smack.PacketListener)

Example 7 with PacketListener

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);
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) Message(org.jivesoftware.smack.packet.Message) PEPEvent(org.jivesoftware.smackx.packet.PEPEvent) PacketListener(org.jivesoftware.smack.PacketListener)

Example 8 with PacketListener

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");
                }
            });
        }
    };
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) ConnectionListener(org.jivesoftware.smack.ConnectionListener) PacketListener(org.jivesoftware.smack.PacketListener) SimpleDateFormat(java.text.SimpleDateFormat) BadLocationException(javax.swing.text.BadLocationException)

Example 9 with PacketListener

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;
        }
    });
}
Also used : PacketFilter(org.jivesoftware.smack.filter.PacketFilter) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) ConnectionListener(org.jivesoftware.smack.ConnectionListener) XMPPConnection(org.jivesoftware.smack.XMPPConnection) PacketListener(org.jivesoftware.smack.PacketListener) XMPPException(org.jivesoftware.smack.XMPPException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 10 with PacketListener

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);
        }
    };
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) AbstractConnectionListener(org.jivesoftware.smack.AbstractConnectionListener) PacketListener(org.jivesoftware.smack.PacketListener)

Aggregations

PacketListener (org.jivesoftware.smack.PacketListener)16 Packet (org.jivesoftware.smack.packet.Packet)12 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)5 Message (org.jivesoftware.smack.packet.Message)5 ConnectionListener (org.jivesoftware.smack.ConnectionListener)4 XMPPException (org.jivesoftware.smack.XMPPException)3 AndFilter (org.jivesoftware.smack.filter.AndFilter)3 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)3 IOException (java.io.IOException)2 XMPPConnection (org.jivesoftware.smack.XMPPConnection)2 Presence (org.jivesoftware.smack.packet.Presence)2 DatagramPacket (java.net.DatagramPacket)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 List (java.util.List)1 BadLocationException (javax.swing.text.BadLocationException)1 ContainerConnectException (org.eclipse.ecf.core.ContainerConnectException)1