    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.lang.management.ManagementFactory;
    006    import java.lang.management.RuntimeMXBean;
    007    import java.net.MalformedURLException;
    008    import java.net.URL;
    009    import java.util.HashMap;
    010    import java.util.Iterator;
    011    import java.util.LinkedHashMap;
    012    import java.util.List;
    013    import java.util.Map;
    014    import java.util.UUID;
    015    import java.util.Map.Entry;
    016
    017    public class PlayerUsageSnooper
    018    {
    019        /** String map for report data */
    020        private Map dataMap = new HashMap();
    021        private final String uniqueID = UUID.randomUUID().toString();
    022
    023        /** URL of the server to send the report to */
    024        private final URL serverUrl;
    025        private final IPlayerUsage playerStatsCollector;
    026
    027        /** set to fire the snooperThread every 15 mins */
    028        private final java.util.Timer threadTrigger = new java.util.Timer("Snooper Timer", true);
    029        private final Object syncLock = new Object();
    030        private boolean isRunning = false;
    031
    032        /** incremented on every getSelfCounterFor */
    033        private int selfCounter = 0;
    034
    035        public PlayerUsageSnooper(String par1Str, IPlayerUsage par2IPlayerUsage)
    036        {
    037            try
    038            {
    039                this.serverUrl = new URL("http://snoop.minecraft.net/" + par1Str + "?version=" + 1);
    040            }
    041            catch (MalformedURLException var4)
    042            {
    043                throw new IllegalArgumentException();
    044            }
    045
    046            this.playerStatsCollector = par2IPlayerUsage;
    047        }
    048
    049        /**
    050         * Note issuing start multiple times is not an error.
    051         */
    052        public void startSnooper()
    053        {
    054            if (!this.isRunning)
    055            {
    056                this.isRunning = true;
    057                this.addBaseDataToSnooper();
    058                this.threadTrigger.schedule(new PlayerUsageSnooperThread(this), 0L, 900000L);
    059            }
    060        }
    061
    062        private void addBaseDataToSnooper()
    063        {
    064            this.addJvmArgsToSnooper();
    065            this.addData("snooper_token", this.uniqueID);
    066            this.addData("os_name", System.getProperty("os.name"));
    067            this.addData("os_version", System.getProperty("os.version"));
    068            this.addData("os_architecture", System.getProperty("os.arch"));
    069            this.addData("java_version", System.getProperty("java.version"));
    070            this.addData("version", "1.4.1");
    071            this.playerStatsCollector.addServerTypeToSnooper(this);
    072        }
    073
    074        private void addJvmArgsToSnooper()
    075        {
    076            RuntimeMXBean var1 = ManagementFactory.getRuntimeMXBean();
    077            List var2 = var1.getInputArguments();
    078            int var3 = 0;
    079            Iterator var4 = var2.iterator();
    080
    081            while (var4.hasNext())
    082            {
    083                String var5 = (String)var4.next();
    084
    085                if (var5.startsWith("-X"))
    086                {
    087                    this.addData("jvm_arg[" + var3++ + "]", var5);
    088                }
    089            }
    090
    091            this.addData("jvm_args", Integer.valueOf(var3));
    092        }
    093
    094        public void addMemoryStatsToSnooper()
    095        {
    096            this.addData("memory_total", Long.valueOf(Runtime.getRuntime().totalMemory()));
    097            this.addData("memory_max", Long.valueOf(Runtime.getRuntime().maxMemory()));
    098            this.addData("memory_free", Long.valueOf(Runtime.getRuntime().freeMemory()));
    099            this.addData("cpu_cores", Integer.valueOf(Runtime.getRuntime().availableProcessors()));
    100            this.playerStatsCollector.addServerStatsToSnooper(this);
    101        }
    102
    103        /**
    104         * Adds information to the report
    105         */
    106        public void addData(String par1Str, Object par2Obj)
    107        {
    108            Object var3 = this.syncLock;
    109
    110            synchronized (this.syncLock)
    111            {
    112                this.dataMap.put(par1Str, par2Obj);
    113            }
    114        }
    115
    116        @SideOnly(Side.CLIENT)
    117        public Map getCurrentStats()
    118        {
    119            LinkedHashMap var1 = new LinkedHashMap();
    120            Object var2 = this.syncLock;
    121
    122            synchronized (this.syncLock)
    123            {
    124                this.addMemoryStatsToSnooper();
    125                Iterator var3 = this.dataMap.entrySet().iterator();
    126
    127                while (var3.hasNext())
    128                {
    129                    Entry var4 = (Entry)var3.next();
    130                    var1.put(var4.getKey(), var4.getValue().toString());
    131                }
    132
    133                return var1;
    134            }
    135        }
    136
    137        public boolean isSnooperRunning()
    138        {
    139            return this.isRunning;
    140        }
    141
    142        public void stopSnooper()
    143        {
    144            this.threadTrigger.cancel();
    145        }
    146
    147        @SideOnly(Side.CLIENT)
    148        public String getUniqueID()
    149        {
    150            return this.uniqueID;
    151        }
    152
    153        static IPlayerUsage getStatsCollectorFor(PlayerUsageSnooper par0PlayerUsageSnooper)
    154        {
    155            return par0PlayerUsageSnooper.playerStatsCollector;
    156        }
    157
    158        static Object getSyncLockFor(PlayerUsageSnooper par0PlayerUsageSnooper)
    159        {
    160            return par0PlayerUsageSnooper.syncLock;
    161        }
    162
    163        static Map getDataMapFor(PlayerUsageSnooper par0PlayerUsageSnooper)
    164        {
    165            return par0PlayerUsageSnooper.dataMap;
    166        }
    167
    168        /**
    169         * returns a value indicating how many times this function has been run on the snooper
    170         */
    171        static int getSelfCounterFor(PlayerUsageSnooper par0PlayerUsageSnooper)
    172        {
    173            return par0PlayerUsageSnooper.selfCounter++;
    174        }
    175
    176        static URL getServerUrlFor(PlayerUsageSnooper par0PlayerUsageSnooper)
    177        {
    178            return par0PlayerUsageSnooper.serverUrl;
    179        }
    180    }