Java tutorial
/* * 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.server; 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.server.NetServerSocket; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; /** * Manages the messaging servers for Network Communication * * @author PlayerForceHD */ public final class NS_ServerSocketManager implements tk.playerforcehd.networklib.bungee.api.socket.server.ServerSocketManager { /** * A static serverSocketManager instance */ private static tk.playerforcehd.networklib.bungee.api.socket.server.ServerSocketManager serverSocketManager; /** * The main instance of NetworkLib */ private Main main; /** * Contains the number of created ServerSockets (needed to assign ID's) */ private int createdSockets = 0; /** * A collection of all running sockets */ private Collection<NS_NetServerSocket> NSNetServerSockets; public NS_ServerSocketManager(Main main) throws IllegalAccessException { if (serverSocketManager != null) { throw new IllegalAccessException( "Library users are not allowed to create a new instance of the NS_ServerSocketManager!"); } serverSocketManager = this; this.main = main; this.NSNetServerSockets = new ArrayList<>(); } /** * Creates a new asynchronous running NetworkLib Server Socket * NOTE: The Socket must be opened manually by calling {@link NetServerSocket#open()} * * @param ownerPlugin The plugin which owns the socket * @param port The port of the socket * @param log Should the opening/closing of the socket be logged to the console * @throws IOException Thrown when the creation of the socket fails */ @Override public NetServerSocket createServerSocket(Plugin ownerPlugin, int port, boolean log) throws IOException { Validate.notNull(ownerPlugin); if (port < 0 || port > 65535) { throw new IllegalArgumentException("Port must be between 0 and 65535!"); } this.createdSockets = this.createdSockets + 1; NS_NetServerSocket NSNetServerSocket = new NS_NetServerSocket(main, ownerPlugin, serverSocketManager, port, this.createdSockets, log); NSNetServerSockets.add(NSNetServerSocket); return NSNetServerSocket; } /** * Fully closes the given NS_NetServerSocket * * @param netServerSocket The socket to close */ @Override public void closeServerSocket(NetServerSocket netServerSocket) { Validate.notNull(netServerSocket); NS_NetServerSocket netServerSocket1 = (NS_NetServerSocket) netServerSocket; if (this.NSNetServerSockets.contains(netServerSocket1)) { this.NSNetServerSockets.remove(netServerSocket1); netServerSocket1.shutdownSocket(); } } /** * Shutdowns all opened ServerSockets */ public void closeAll() { for (NS_NetServerSocket NSNetServerSocket : this.NSNetServerSockets) { NSNetServerSocket.shutdownSocket(); } } @Getter public Collection<NetServerSocket> getNetServerSockets() { Collection<NetServerSocket> netServerSocketCollection = new ArrayList<>(); for (NS_NetServerSocket NSNetServerSocket : this.NSNetServerSockets) { netServerSocketCollection.add(NSNetServerSocket); } return netServerSocketCollection; } }