    001    package net.minecraft.network.rcon;
    002
    003    import java.io.IOException;
    004    import java.net.DatagramSocket;
    005    import java.net.ServerSocket;
    006    import java.util.ArrayList;
    007    import java.util.Iterator;
    008    import java.util.List;
    009
    010    public abstract class RConThreadBase implements Runnable
    011    {
    012        /** True if the Thread is running, false otherwise */
    013        protected boolean running = false;
    014
    015        /** Reference to the IServer object. */
    016        protected IServer server;
    017
    018        /** Thread for this runnable class */
    019        protected Thread rconThread;
    020        protected int field_72615_d = 5;
    021
    022        /** A list of registered DatagramSockets */
    023        protected List socketList = new ArrayList();
    024
    025        /** A list of registered ServerSockets */
    026        protected List serverSocketList = new ArrayList();
    027
    028        RConThreadBase(IServer par1IServer)
    029        {
    030            this.server = par1IServer;
    031
    032            if (this.server.isDebuggingEnabled())
    033            {
    034                this.logWarning("Debugging is enabled, performance maybe reduced!");
    035            }
    036        }
    037
    038        /**
    039         * Creates a new Thread object from this class and starts running
    040         */
    041        public synchronized void startThread()
    042        {
    043            this.rconThread = new Thread(this);
    044            this.rconThread.start();
    045            this.running = true;
    046        }
    047
    048        /**
    049         * Returns true if the Thread is running, false otherwise
    050         */
    051        public boolean isRunning()
    052        {
    053            return this.running;
    054        }
    055
    056        /**
    057         * Log debug message
    058         */
    059        protected void logDebug(String par1Str)
    060        {
    061            this.server.logDebug(par1Str);
    062        }
    063
    064        /**
    065         * Log information message
    066         */
    067        protected void logInfo(String par1Str)
    068        {
    069            this.server.logInfo(par1Str);
    070        }
    071
    072        /**
    073         * Log warning message
    074         */
    075        protected void logWarning(String par1Str)
    076        {
    077            this.server.logWarning(par1Str);
    078        }
    079
    080        /**
    081         * Log severe error message
    082         */
    083        protected void logSevere(String par1Str)
    084        {
    085            this.server.logSevere(par1Str);
    086        }
    087
    088        /**
    089         * Returns the number of players on the server
    090         */
    091        protected int getNumberOfPlayers()
    092        {
    093            return this.server.getCurrentPlayerCount();
    094        }
    095
    096        /**
    097         * Registers a DatagramSocket with this thread
    098         */
    099        protected void registerSocket(DatagramSocket par1DatagramSocket)
    100        {
    101            this.logDebug("registerSocket: " + par1DatagramSocket);
    102            this.socketList.add(par1DatagramSocket);
    103        }
    104
    105        /**
    106         * Closes the specified DatagramSocket
    107         */
    108        protected boolean closeSocket(DatagramSocket par1DatagramSocket, boolean par2)
    109        {
    110            this.logDebug("closeSocket: " + par1DatagramSocket);
    111
    112            if (null == par1DatagramSocket)
    113            {
    114                return false;
    115            }
    116            else
    117            {
    118                boolean var3 = false;
    119
    120                if (!par1DatagramSocket.isClosed())
    121                {
    122                    par1DatagramSocket.close();
    123                    var3 = true;
    124                }
    125
    126                if (par2)
    127                {
    128                    this.socketList.remove(par1DatagramSocket);
    129                }
    130
    131                return var3;
    132            }
    133        }
    134
    135        /**
    136         * Closes the specified ServerSocket
    137         */
    138        protected boolean closeServerSocket(ServerSocket par1ServerSocket)
    139        {
    140            return this.closeServerSocket_do(par1ServerSocket, true);
    141        }
    142
    143        /**
    144         * Closes the specified ServerSocket
    145         */
    146        protected boolean closeServerSocket_do(ServerSocket par1ServerSocket, boolean par2)
    147        {
    148            this.logDebug("closeSocket: " + par1ServerSocket);
    149
    150            if (null == par1ServerSocket)
    151            {
    152                return false;
    153            }
    154            else
    155            {
    156                boolean var3 = false;
    157
    158                try
    159                {
    160                    if (!par1ServerSocket.isClosed())
    161                    {
    162                        par1ServerSocket.close();
    163                        var3 = true;
    164                    }
    165                }
    166                catch (IOException var5)
    167                {
    168                    this.logWarning("IO: " + var5.getMessage());
    169                }
    170
    171                if (par2)
    172                {
    173                    this.serverSocketList.remove(par1ServerSocket);
    174                }
    175
    176                return var3;
    177            }
    178        }
    179
    180        /**
    181         * Closes all of the opened sockets
    182         */
    183        protected void closeAllSockets()
    184        {
    185            this.closeAllSockets_do(false);
    186        }
    187
    188        /**
    189         * Closes all of the opened sockets
    190         */
    191        protected void closeAllSockets_do(boolean par1)
    192        {
    193            int var2 = 0;
    194            Iterator var3 = this.socketList.iterator();
    195
    196            while (var3.hasNext())
    197            {
    198                DatagramSocket var4 = (DatagramSocket)var3.next();
    199
    200                if (this.closeSocket(var4, false))
    201                {
    202                    ++var2;
    203                }
    204            }
    205
    206            this.socketList.clear();
    207            var3 = this.serverSocketList.iterator();
    208
    209            while (var3.hasNext())
    210            {
    211                ServerSocket var5 = (ServerSocket)var3.next();
    212
    213                if (this.closeServerSocket_do(var5, false))
    214                {
    215                    ++var2;
    216                }
    217            }
    218
    219            this.serverSocketList.clear();
    220
    221            if (par1 && 0 < var2)
    222            {
    223                this.logWarning("Force closed " + var2 + " sockets");
    224            }
    225        }
    226    }