tk.playerforcehd.networklib.bungee.socket.client.NS_ClientSocketManager.java Source code

Java tutorial

Introduction

Here is the source code for tk.playerforcehd.networklib.bungee.socket.client.NS_ClientSocketManager.java

Source

/*
 *     NetworkLib - A Spigot/BungeeCord plugin messaging Library
 *     Copyright (C) 2015 Pascal Zarrad
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License along
 *     with this program; if not, write to the Free Software Foundation, Inc.,
 *     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package tk.playerforcehd.networklib.bungee.socket.client;

import jdk.nashorn.internal.objects.annotations.Getter;
import net.md_5.bungee.api.plugin.Plugin;
import org.apache.commons.lang3.Validate;
import tk.playerforcehd.networklib.bungee.Main;
import tk.playerforcehd.networklib.bungee.api.socket.client.NetClientSocket;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Manages client messaging sockets which can communicate with a master server
 *
 * @author PlayerForceHD
 */
public class NS_ClientSocketManager
        implements tk.playerforcehd.networklib.bungee.api.socket.client.ClientSocketManager {

    /**
     * The NS_ClientSocketManager instance
     */
    private static tk.playerforcehd.networklib.bungee.api.socket.client.ClientSocketManager clientSocketManager;

    /**
     * The main instance of NetworkLib
     */
    private Main main;

    /**
     * The count of created sockets (Needed for ID's)
     */
    private int createdSockets = 0;

    /**
     * A collection of the
     */
    private Collection<NS_NetClientSocket> NSNetClientSockets;

    public NS_ClientSocketManager(Main main) throws IllegalAccessException {
        if (clientSocketManager != null) {
            throw new IllegalAccessException(
                    "Library users are not allowed to create a new instance of the NS_ServerSocketManager!");
        }
        clientSocketManager = this;
        this.main = main;
        this.NSNetClientSockets = new ArrayList<>();
    }

    /**
     * Create a new Socket to connect to a messaging server
     *
     * @param ownerPlugin The plugin which owns the socket
     * @param host        The host address of the Messaging Server
     * @param port        The port of the Messaging Server
     * @param log         Should opening/closing be logged
     * @return The created NS_NetClientSocket which can be opened with {@link NS_NetClientSocket#open()}
     */
    @Override
    public NetClientSocket createNetClientSocket(Plugin ownerPlugin, String host, int port, boolean log) {
        Validate.notNull(ownerPlugin);
        Validate.notNull(host);
        if (port < 0 || port > 65535) {
            throw new IllegalArgumentException("Port must be between 0 and 65535!");
        }
        this.createdSockets = this.createdSockets + 1;
        NS_NetClientSocket NSNetClientSocket = new NS_NetClientSocket(this.main, ownerPlugin, this, host, port,
                this.createdSockets, log);
        this.NSNetClientSockets.add(NSNetClientSocket);
        return NSNetClientSocket;
    }

    /**
     * Closes the given socket
     *
     * @param NSNetClientSocket The socket to close
     */
    public void closeClientSocket(NS_NetClientSocket NSNetClientSocket) {
        Validate.notNull(NSNetClientSocket);
        if (this.NSNetClientSockets.contains(NSNetClientSocket)) {
            this.NSNetClientSockets.remove(NSNetClientSocket);
            NSNetClientSocket.shutdownSocket();
        }
    }

    /**
     * Closes the given socket
     *
     * @param netClientSocket The socket to close
     */
    @Override
    public void closeClientSocket(NetClientSocket netClientSocket) {
        Validate.notNull(netClientSocket);
        NS_NetClientSocket ns_netClientSocket = (NS_NetClientSocket) netClientSocket;
        closeClientSocket(ns_netClientSocket);
    }

    /**
     * Closes all open NetClientSockets
     */
    @Override
    public void closeAllNetClientSockets() {
        this.NSNetClientSockets.forEach(netClientSocket -> closeClientSocket(netClientSocket));
    }

    @Getter
    public Collection<NetClientSocket> getNetClientSockets() {
        Collection<NetClientSocket> tmp_copyCollection = new ArrayList<>();
        for (NS_NetClientSocket NSNetClientSocket : this.NSNetClientSockets) {
            tmp_copyCollection.add(NSNetClientSocket);
        }
        return tmp_copyCollection;
    }
}