001/**
002 *
003 * Copyright 2018-2020 Florian Schmaus
004 *
005 * This file is part of smack-repl.
006 *
007 * smack-repl is free software; you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation; either version 3 of the License, or
010 * (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with this program; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
020 */
021package org.igniterealtime.smack.smackrepl;
022
023import java.io.BufferedWriter;
024import java.io.IOException;
025import java.io.OutputStreamWriter;
026import java.io.PrintWriter;
027import java.nio.charset.StandardCharsets;
028import java.util.Date;
029import java.util.logging.Logger;
030
031import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
032import org.jivesoftware.smack.SmackException;
033import org.jivesoftware.smack.XMPPException;
034import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection;
035import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration;
036import org.jivesoftware.smack.compression.CompressionModuleDescriptor;
037import org.jivesoftware.smack.compression.XMPPInputOutputStream;
038import org.jivesoftware.smack.compression.XMPPInputOutputStream.FlushMethod;
039import org.jivesoftware.smack.debugger.ConsoleDebugger;
040import org.jivesoftware.smack.debugger.SmackDebuggerFactory;
041import org.jivesoftware.smack.packet.Message;
042import org.jivesoftware.smack.sm.StreamManagementModuleDescriptor;
043import org.jivesoftware.smack.tcp.XmppTcpTransportModuleDescriptor;
044
045import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
046
047import org.jxmpp.util.XmppDateTime;
048
049public class Nio {
050
051    private static final Logger LOGGER = Logger.getLogger(Nio.class.getName());
052
053    public static void main(String[] args) throws SmackException, IOException, XMPPException, InterruptedException {
054        doNio(args[0], args[1], args[2]);
055    }
056
057    public static void doNio(String username, String password, String service)
058            throws SmackException, IOException, XMPPException, InterruptedException {
059        boolean useTls = true;
060        boolean useStreamMangement = false;
061        boolean useCompression = true;
062        boolean useFullFlush = true;
063        boolean javaNetDebug = false;
064        boolean smackDebug = true;
065
066        if (useFullFlush) {
067            XMPPInputOutputStream.setFlushMethod(FlushMethod.FULL_FLUSH);
068        }
069
070        if (javaNetDebug) {
071            System.setProperty("javax.net.debug", "all");
072        }
073
074        final SecurityMode securityMode;
075        if (useTls) {
076            securityMode = SecurityMode.required;
077        } else {
078            securityMode = SecurityMode.disabled;
079        }
080
081        final SmackDebuggerFactory smackDebuggerFactory;
082        if (smackDebug) {
083            smackDebuggerFactory = ConsoleDebugger.Factory.INSTANCE;
084        } else {
085            smackDebuggerFactory = null;
086        }
087
088        ModularXmppClientToServerConnectionConfiguration.Builder configurationBuilder = ModularXmppClientToServerConnectionConfiguration.builder()
089                .setUsernameAndPassword(username, password)
090                .setXmppDomain(service)
091                .setDebuggerFactory(smackDebuggerFactory)
092                .setSecurityMode(securityMode)
093                .removeAllModules()
094                .addModule(XmppTcpTransportModuleDescriptor.class);
095
096        if (useCompression) {
097            configurationBuilder.addModule(CompressionModuleDescriptor.class);
098            configurationBuilder.setCompressionEnabled(true);
099        }
100        if (useStreamMangement) {
101            configurationBuilder.addModule(StreamManagementModuleDescriptor.class);
102        }
103
104        ModularXmppClientToServerConnectionConfiguration configuration = configurationBuilder.build();
105
106        PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8)));
107        configuration.printStateGraphInDotFormat(printWriter, false);
108        printWriter.flush();
109
110        ModularXmppClientToServerConnection connection = new ModularXmppClientToServerConnection(configuration);
111
112        connection.setReplyTimeout(5 * 60 * 1000);
113
114        connection.addConnectionStateMachineListener((event, c) -> {
115            LOGGER.info("Connection event: " + event);
116        });
117
118        connection.connect();
119
120        connection.login();
121
122        Message message = connection.getStanzaFactory().buildMessageStanza()
123                .to("flo@geekplace.eu")
124                .setBody("It is alive! " + XmppDateTime.formatXEP0082Date(new Date()))
125                .build();
126        connection.sendStanza(message);
127
128        Thread.sleep(1000);
129
130        connection.disconnect();
131
132        ModularXmppClientToServerConnection.Stats connectionStats = connection.getStats();
133        ServiceDiscoveryManager.Stats serviceDiscoveryManagerStats = ServiceDiscoveryManager.getInstanceFor(connection).getStats();
134
135        // CHECKSTYLE:OFF
136        System.out.println("NIO successfully finished, yeah!\n" + connectionStats + '\n' + serviceDiscoveryManagerStats);
137        // CHECKSTYLE:ON
138    }
139
140}