Search in sources :

Example 1 with Stanza

use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.

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 StanzaListener() {

        SimpleDateFormat dateFormatter = new SimpleDateFormat("HH:mm:ss:SS");

        @Override
        public void processStanza(final Stanza packet) {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    addReadPacketToTable(dateFormatter, packet);
                }
            });
        }
    };
    // Create a thread that will listen for all outgoing packets and write them to
    // the GUI.
    packetWriterListener = new StanzaListener() {

        SimpleDateFormat dateFormatter = new SimpleDateFormat("HH:mm:ss:SS");

        @Override
        public void processStanza(final Stanza packet) {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    addSentPacketToTable(dateFormatter, packet);
                }
            });
        }
    };
    // Create a thread that will listen for any connection closed event
    connListener = new AbstractConnectionListener() {

        @Override
        public void connectionClosed() {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    statusField.setValue("Closed");
                    EnhancedDebuggerWindow.connectionClosed(EnhancedDebugger.this);
                }
            });
        }

        @Override
        public void connectionClosedOnError(final Exception e) {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    statusField.setValue("Closed due to an exception");
                    EnhancedDebuggerWindow.connectionClosedOnError(EnhancedDebugger.this, e);
                }
            });
        }

        @Override
        public void reconnectingIn(final int seconds) {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    statusField.setValue("Attempt to reconnect in " + seconds + " seconds");
                }
            });
        }

        @Override
        public void reconnectionSuccessful() {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    statusField.setValue("Reconnection stablished");
                    EnhancedDebuggerWindow.connectionEstablished(EnhancedDebugger.this);
                }
            });
        }

        @Override
        public void reconnectionFailed(Exception e) {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    statusField.setValue("Reconnection failed");
                }
            });
        }
    };
}
Also used : JTabbedPane(javax.swing.JTabbedPane) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) AbstractConnectionListener(org.jivesoftware.smack.AbstractConnectionListener) SimpleDateFormat(java.text.SimpleDateFormat) BadLocationException(javax.swing.text.BadLocationException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException)

Example 2 with Stanza

use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.

the class ForwardedProvider method parse.

@Override
public Forwarded parse(XmlPullParser parser, int initialDepth) throws Exception {
    DelayInformation di = null;
    Stanza packet = null;
    outerloop: while (true) {
        int eventType = parser.next();
        switch(eventType) {
            case XmlPullParser.START_TAG:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                switch(name) {
                    case DelayInformation.ELEMENT:
                        if (DelayInformation.NAMESPACE.equals(namespace)) {
                            di = DelayInformationProvider.INSTANCE.parse(parser, parser.getDepth());
                        } else {
                            LOGGER.warning("Namespace '" + namespace + "' does not match expected namespace '" + DelayInformation.NAMESPACE + "'");
                        }
                        break;
                    case Message.ELEMENT:
                        packet = PacketParserUtils.parseMessage(parser);
                        break;
                    default:
                        LOGGER.warning("Unsupported forwarded packet type: " + name);
                }
                break;
            case XmlPullParser.END_TAG:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
        }
    }
    if (packet == null)
        throw new SmackException("forwarded extension must contain a packet");
    return new Forwarded(di, packet);
}
Also used : DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) Stanza(org.jivesoftware.smack.packet.Stanza) SmackException(org.jivesoftware.smack.SmackException) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded)

Example 3 with Stanza

use of org.jivesoftware.smack.packet.Stanza in project yellowmessenger-sdk by yellowmessenger.

the class XMPPService method createConnection.

private void createConnection() throws XmppStringprepException, NoSuchAlgorithmException {
    if (mConnection == null) {
        AndroidSmackInitializer androidSmackInitializer = new AndroidSmackInitializer();
        androidSmackInitializer.initialize();
        ExtensionsInitializer extensionsInitializer = new ExtensionsInitializer();
        extensionsInitializer.initialize();
        XMPPUser xmppUser = PreferencesManager.getInstance(XMPPService.this.getApplicationContext()).getXMPPUser();
        XMPPTCPConnectionConfiguration connConfig = XMPPTCPConnectionConfiguration.builder().setXmppDomain(JidCreate.domainBareFrom(DOMAIN)).setHost(HOST).setPort(PORT).setSecurityMode(ConnectionConfiguration.SecurityMode.ifpossible).setCustomSSLContext(SSLContext.getInstance("TLS")).setSocketFactory(SSLSocketFactory.getDefault()).setUsernameAndPassword(xmppUser.getUsername(), xmppUser.getPassword()).build();
        SmackConfiguration.setDefaultPacketReplyTimeout(5000);
        XMPPTCPConnection.setUseStreamManagementDefault(true);
        XMPPTCPConnection.setUseStreamManagementResumptionDefault(true);
        mConnection = new XMPPTCPConnection(connConfig);
        mConnection.setPacketReplyTimeout(5000);
        mConnection.setPreferredResumptionTime(10);
        mConnection.setUseStreamManagement(true);
        mConnection.setUseStreamManagementResumption(true);
        mConnection.addAsyncStanzaListener(packetListener, packetFilter);
        mConnection.addAsyncStanzaListener(pingPacketListener, pingPacketFilter);
        mConnection.addStanzaAcknowledgedListener(new StanzaListener() {

            @Override
            public void processStanza(Stanza packet) throws SmackException.NotConnectedException {
                // TODO Acknowledgement
                ChatMessage chatMessage = ChatMessageDAO.getChatMessageByStanzaId(packet.getStanzaId());
                if (chatMessage != null) {
                    chatMessage.setAcknowledged(true);
                    chatMessage.save();
                    EventBus.getDefault().post(new MessageAcknowledgementEvent(chatMessage, chatMessage.getAcknowledged()));
                }
            }
        });
        SASLAuthentication.unregisterSASLMechanism("org.jivesoftware.smack.sasl.core.SCRAMSHA1Mechanism");
        SASLAuthentication.registerSASLMechanism(new CustomSCRAMSHA1Mechanism());
        mConnection.addConnectionListener(connectionListener);
        ServerPingWithAlarmManager.getInstanceFor(mConnection).setEnabled(true);
    // ReconnectionManager.getInstanceFor(mConnection).enableAutomaticReconnection();
    }
    try {
        if (!mConnection.isConnected() && !mConnection.isAuthenticated()) {
            mConnection.connect();
        } else if (mConnection.isConnected() && !mConnection.isAuthenticated()) {
            mConnection.login();
        }
    } catch (Exception e) {
    // e.printStackTrace();
    }
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) XMPPTCPConnectionConfiguration(org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration) CustomSCRAMSHA1Mechanism(com.yellowmessenger.sdk.xmpp.CustomSCRAMSHA1Mechanism) MessageAcknowledgementEvent(com.yellowmessenger.sdk.events.MessageAcknowledgementEvent) SmackException(org.jivesoftware.smack.SmackException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AndroidSmackInitializer(org.jivesoftware.smack.android.AndroidSmackInitializer) ExtensionsInitializer(org.jivesoftware.smack.extensions.ExtensionsInitializer) XMPPUser(com.yellowmessenger.sdk.models.XMPPUser)

Example 4 with Stanza

use of org.jivesoftware.smack.packet.Stanza in project fcmxmppserverv2 by carlosCharz.

the class CcsClient method sendPacket.

/**
 * Sends a downstream message to FCM with back off strategy
 */
public void sendPacket(String jsonRequest) {
    final Stanza request = new FcmPacketExtension(jsonRequest).toPacket();
    final BackOffStrategy backoff = new BackOffStrategy();
    while (backoff.shouldRetry()) {
        try {
            connection.sendStanza(request);
            backoff.doNotRetry();
        } catch (NotConnectedException | InterruptedException e) {
            logger.log(Level.INFO, "The packet could not be sent due to a connection problem. Packet: " + request.toXML());
            backoff.errorOccured();
        }
    }
}
Also used : BackOffStrategy(com.wedevol.xmpp.util.BackOffStrategy) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) Stanza(org.jivesoftware.smack.packet.Stanza) ForEveryStanza(org.jivesoftware.smack.sm.predicates.ForEveryStanza)

Example 5 with Stanza

use of org.jivesoftware.smack.packet.Stanza in project Spark by igniterealtime.

the class TicTacToePlugin method initialize.

@Override
public void initialize() {
    ClassLoader cl = getClass().getClassLoader();
    buttonimg = new ImageIcon(cl.getResource("ttt.button.png"));
    _currentInvitations = new HashSet<String>();
    ProviderManager.addIQProvider(GameOfferPacket.ELEMENT_NAME, GameOfferPacket.NAMESPACE, new GameOfferPacket.Provider());
    ProviderManager.addExtensionProvider(MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE, new MovePacket.Provider());
    ProviderManager.addExtensionProvider(InvalidMove.ELEMENT_NAME, InvalidMove.NAMESPACE, new InvalidMove.Provider());
    // Add IQ listener to listen for incoming game invitations.
    _gameOfferListener = new StanzaListener() {

        public void processPacket(Stanza stanza) {
            GameOfferPacket invitation = (GameOfferPacket) stanza;
            if (invitation.getType() == IQ.Type.get) {
                showInvitationAlert(invitation);
            }
        }
    };
    SparkManager.getConnection().addAsyncStanzaListener(_gameOfferListener, new StanzaTypeFilter(GameOfferPacket.class));
    addButtonToToolBar();
}
Also used : ImageIcon(javax.swing.ImageIcon) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) MovePacket(tic.tac.toe.packet.MovePacket) GameOfferPacket(tic.tac.toe.packet.GameOfferPacket) InvalidMove(tic.tac.toe.packet.InvalidMove)

Aggregations

Stanza (org.jivesoftware.smack.packet.Stanza)101 StanzaListener (org.jivesoftware.smack.StanzaListener)24 Test (org.junit.Test)22 IQ (org.jivesoftware.smack.packet.IQ)20 Test (org.junit.jupiter.api.Test)18 XMPPConnection (org.jivesoftware.smack.XMPPConnection)14 Message (org.jivesoftware.smack.packet.Message)14 ArrayList (java.util.ArrayList)11 SmackException (org.jivesoftware.smack.SmackException)11 Jid (org.jxmpp.jid.Jid)11 IOException (java.io.IOException)9 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)8 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)8 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)7 DelayInformation (org.jivesoftware.smackx.delay.packet.DelayInformation)7 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)6 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)6 Protocol (org.jivesoftware.util.Protocol)6 EntityFullJid (org.jxmpp.jid.EntityFullJid)6 LinkedList (java.util.LinkedList)5