001/**
002 *
003 * Copyright 2018 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.igniterealtime.smack.smackrepl;
018
019import java.io.IOException;
020import java.util.Date;
021
022import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
023import org.jivesoftware.smack.SmackException;
024import org.jivesoftware.smack.XMPPException;
025import org.jivesoftware.smack.packet.Message;
026import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
027import org.jivesoftware.smack.tcp.XmppNioTcpConnection;
028
029import org.jxmpp.util.XmppDateTime;
030
031public class Nio {
032
033    public static void main(String[] args) throws SmackException, IOException, XMPPException, InterruptedException {
034        doNio(args[0], args[1], args[2]);
035    }
036
037    public static void doNio(String username, String password, String service)
038            throws SmackException, IOException, XMPPException, InterruptedException {
039        boolean javaNetDebug = true;
040        if (javaNetDebug) {
041            System.setProperty("javax.net.debug", "all");
042        }
043
044        XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder()
045                .setUsernameAndPassword(username, password)
046                .setXmppDomain(service)
047//                .setSecurityMode(SecurityMode.disabled)
048                .setSecurityMode(SecurityMode.required)
049                .build();
050
051        XmppNioTcpConnection connection = new XmppNioTcpConnection(configuration);
052        connection.setReplyTimeout(5 * 60 * 1000);
053
054        connection.connect();
055
056        connection.login();
057
058        Message message = new Message("flo@geekplace.eu",
059                "It is alive! " + XmppDateTime.formatXEP0082Date(new Date()));
060        connection.sendStanza(message);
061
062        Thread.sleep(1000);
063
064        connection.disconnect();
065
066        XmppNioTcpConnection.Stats connectionStats = connection.getStats();
067
068        // CHECKSTYLE:OFF
069        System.out.println("NIO successfully finished, yeah!\n" + connectionStats);
070        // CHECKSTYLE:ON
071    }
072
073}