001    package net.minecraft.src;
002    
003    import java.io.IOException;
004    import java.net.InetAddress;
005    import java.net.ServerSocket;
006    import java.net.Socket;
007    import java.net.SocketTimeoutException;
008    import java.util.HashMap;
009    import java.util.Iterator;
010    import java.util.Map;
011    import java.util.Map.Entry;
012    
013    public class RConThreadMain extends RConThreadBase
014    {
015        /** Port RCon is running on */
016        private int rconPort;
017    
018        /** Port the server is running on */
019        private int serverPort;
020    
021        /** Hostname RCon is running on */
022        private String hostname;
023    
024        /** The RCon ServerSocke */
025        private ServerSocket serverSocket = null;
026    
027        /** The RCon password */
028        private String rconPassword;
029        private Map field_72648_l;
030    
031        public RConThreadMain(IServer par1IServer)
032        {
033            super(par1IServer);
034            this.rconPort = par1IServer.getOrSetIntProperty("rcon.port", 0);
035            this.rconPassword = par1IServer.getOrSetProperty("rcon.password", "");
036            this.hostname = par1IServer.getHostName();
037            this.serverPort = par1IServer.getMyServerPort();
038    
039            if (0 == this.rconPort)
040            {
041                this.rconPort = this.serverPort + 10;
042                this.log("Setting default rcon port to " + this.rconPort);
043                par1IServer.setArbitraryProperty("rcon.port", Integer.valueOf(this.rconPort));
044    
045                if (0 == this.rconPassword.length())
046                {
047                    par1IServer.setArbitraryProperty("rcon.password", "");
048                }
049    
050                par1IServer.saveSettingsToFile();
051            }
052    
053            if (0 == this.hostname.length())
054            {
055                this.hostname = "0.0.0.0";
056            }
057    
058            this.initClientTh();
059            this.serverSocket = null;
060        }
061    
062        private void initClientTh()
063        {
064            this.field_72648_l = new HashMap();
065        }
066    
067        /**
068         * Cleans up the clientThreads map by removing client Threads that are not running
069         */
070        private void cleanClientThreadsMap()
071        {
072            Iterator var1 = this.field_72648_l.entrySet().iterator();
073    
074            while (var1.hasNext())
075            {
076                Entry var2 = (Entry)var1.next();
077    
078                if (!((RConThreadClient)var2.getValue()).isRunning())
079                {
080                    var1.remove();
081                }
082            }
083        }
084    
085        public void run()
086        {
087            this.log("RCON running on " + this.hostname + ":" + this.rconPort);
088    
089            try
090            {
091                while (this.running)
092                {
093                    try
094                    {
095                        Socket var1 = this.serverSocket.accept();
096                        var1.setSoTimeout(500);
097                        RConThreadClient var2 = new RConThreadClient(this.server, var1);
098                        var2.startThread();
099                        this.field_72648_l.put(var1.getRemoteSocketAddress(), var2);
100                        this.cleanClientThreadsMap();
101                    }
102                    catch (SocketTimeoutException var7)
103                    {
104                        this.cleanClientThreadsMap();
105                    }
106                    catch (IOException var8)
107                    {
108                        if (this.running)
109                        {
110                            this.log("IO: " + var8.getMessage());
111                        }
112                    }
113                }
114            }
115            finally
116            {
117                this.closeServerSocket(this.serverSocket);
118            }
119        }
120    
121        /**
122         * Creates a new Thread object from this class and starts running
123         */
124        public void startThread()
125        {
126            if (0 == this.rconPassword.length())
127            {
128                this.logWarning("No rcon password set in \'" + this.server.getSettingsFilePath() + "\', rcon disabled!");
129            }
130            else if (0 < this.rconPort && 65535 >= this.rconPort)
131            {
132                if (!this.running)
133                {
134                    try
135                    {
136                        this.serverSocket = new ServerSocket(this.rconPort, 0, InetAddress.getByName(this.hostname));
137                        this.serverSocket.setSoTimeout(500);
138                        super.startThread();
139                    }
140                    catch (IOException var2)
141                    {
142                        this.logWarning("Unable to initialise rcon on " + this.hostname + ":" + this.rconPort + " : " + var2.getMessage());
143                    }
144                }
145            }
146            else
147            {
148                this.logWarning("Invalid rcon port " + this.rconPort + " found in \'" + this.server.getSettingsFilePath() + "\', rcon disabled!");
149            }
150        }
151    }