]> jfr.im git - irc/rizon/bncbot.git/commitdiff
Load balance users based on server user count.
authorN Lum <redacted>
Mon, 30 Aug 2010 18:10:59 +0000 (18:10 +0000)
committerN Lum <redacted>
Mon, 30 Aug 2010 18:10:59 +0000 (18:10 +0000)
--HG--
extra : convert_revision : svn%3Aafccd9b4-69c3-4f67-9d0d-5b27f80d3d7c/trunk%40161

src/net/rizon/bncbot/handler/RequestHandler.java
src/net/rizon/bncmanager/BncServer.java

index f08ec3819211c5c2e33384b76aded110a8d3e8bb..b722562b9bbdb2545f50d6c29d998b1abba22c02 100644 (file)
@@ -103,8 +103,9 @@ public class RequestHandler extends AbstractSimpleHandler {
                                                return;\r
                                        }\r
                                } else {\r
-                                       // TODO - Make this more intelligent.\r
-                                       s = bot.bncManager.getServer(bot.bncConfig.getProperty("defaultBNCAbbr", "EU"));\r
+                                       for(BncServer serv : bot.bncManager.getAllServers())\r
+                                               if(serv.getUserCount() < s.getUserCount())\r
+                                                       s = serv;\r
                                        bot.privmsg(nick, bot.getString("userDefaultServer", s.getServerAbbr()));\r
                                }\r
                                \r
index 680eb67177ab24ba623dbeca28d49ff4be757fc4..5e12f2ef24cf43664fee05a709e39f382e55e5e9 100644 (file)
@@ -28,6 +28,7 @@ public class BncServer {
        private String hostname, abbr, nick, pass, gecos, ident;\r
        private boolean ssl;\r
        private int id, port;\r
+       private int userCount;\r
 \r
        private Timer lastSeenTimer;\r
        private Thread connectionThread;\r
@@ -129,10 +130,13 @@ public class BncServer {
                privmsg("*admin", String.format("LoadModule %s nickserv", user));\r
                privmsg("*admin", String.format("LoadModule %s perform", user));\r
                privmsg("*admin", String.format("LoadModule %s simple_away", user));\r
+               \r
+               this.userCount++;\r
        }\r
 \r
        public void delUser(String user) {\r
                privmsg("*admin", String.format("deluser %s", user));\r
+               this.userCount--;\r
        }\r
 \r
        public void blockUser(String user, String reason) {\r
@@ -240,6 +244,10 @@ public class BncServer {
                                return (ArrayList<Pair<String, String>>) this.searchResults.clone();\r
                }\r
        }\r
+       \r
+       public int getUserCount() {\r
+               return this.userCount;\r
+       }\r
 \r
        public int[] getStats() {\r
                synchronized(this.statsResults) {\r
@@ -288,7 +296,7 @@ public class BncServer {
 \r
        @Override\r
        public String toString() {\r
-               return String.format("(%s) %s:%s", this.abbr, this.hostname, this.port);\r
+               return String.format("(%s) %s:%s w/ %s users", this.abbr, this.hostname, this.port, this.userCount);\r
        }\r
 \r
        private void privmsg(String target, String msg) {\r
@@ -367,6 +375,8 @@ public class BncServer {
                                }, 300000);\r
 \r
                                stdPrintln("BNC connection established.");\r
+                               \r
+                               BncServer.this.privmsg("*status", "userstats");\r
 \r
                                // TODO - Fix so that scan.nextLine() does not have to return for thread to exit.\r
                                netIn = new Scanner(socket.getInputStream());\r
@@ -435,6 +445,7 @@ public class BncServer {
                                                                        BncServer.this.statsResults.put("totalusers", jo.getInt("totalusers"));\r
                                                                        BncServer.this.statsResults.put("online", jo.getInt("online"));\r
                                                                        BncServer.this.statsResults.put("totalconnected", jo.getInt("totalconnected"));\r
+                                                                       BncServer.this.userCount = jo.getInt("totalusers");\r
                                                                        BncServer.this.statsResults.notifyAll();\r
                                                                }\r
                                                        }\r