1.1 --- a/src/org/sonews/daemon/command/XDaemonCommand.java Sun Aug 29 17:28:58 2010 +0200
1.2 +++ b/src/org/sonews/daemon/command/XDaemonCommand.java Sun Aug 29 23:23:15 2010 +0200
1.3 @@ -43,228 +43,162 @@
1.4 public class XDaemonCommand implements Command
1.5 {
1.6
1.7 - @Override
1.8 - public String[] getSupportedCommandStrings()
1.9 - {
1.10 - return new String[]{"XDAEMON"};
1.11 - }
1.12 + @Override
1.13 + public String[] getSupportedCommandStrings()
1.14 + {
1.15 + return new String[] {"XDAEMON"};
1.16 + }
1.17
1.18 - @Override
1.19 - public boolean hasFinished()
1.20 - {
1.21 - return true;
1.22 - }
1.23 + @Override
1.24 + public boolean hasFinished()
1.25 + {
1.26 + return true;
1.27 + }
1.28
1.29 - @Override
1.30 - public String impliedCapability()
1.31 - {
1.32 - return null;
1.33 - }
1.34 + @Override
1.35 + public String impliedCapability()
1.36 + {
1.37 + return null;
1.38 + }
1.39
1.40 - @Override
1.41 - public boolean isStateful()
1.42 - {
1.43 - return false;
1.44 - }
1.45 + @Override
1.46 + public boolean isStateful()
1.47 + {
1.48 + return false;
1.49 + }
1.50
1.51 - private void channelAdd(String[] commands, NNTPConnection conn)
1.52 - throws IOException, StorageBackendException
1.53 - {
1.54 - String groupName = commands[2];
1.55 - if(StorageManager.current().isGroupExisting(groupName))
1.56 - {
1.57 - conn.println("400 group " + groupName + " already existing!");
1.58 - }
1.59 - else
1.60 - {
1.61 - StorageManager.current().addGroup(groupName, Integer.parseInt(commands[3]));
1.62 - conn.println("200 group " + groupName + " created");
1.63 - }
1.64 - }
1.65 + private void channelAdd(String[] commands, NNTPConnection conn)
1.66 + throws IOException, StorageBackendException
1.67 + {
1.68 + String groupName = commands[2];
1.69 + if (StorageManager.current().isGroupExisting(groupName)) {
1.70 + conn.println("400 group " + groupName + " already existing!");
1.71 + } else {
1.72 + StorageManager.current().addGroup(groupName, Integer.parseInt(commands[3]));
1.73 + conn.println("200 group " + groupName + " created");
1.74 + }
1.75 + }
1.76
1.77 - // TODO: Refactor this method to reduce complexity!
1.78 - @Override
1.79 - public void processLine(NNTPConnection conn, String line, byte[] raw)
1.80 - throws IOException, StorageBackendException
1.81 - {
1.82 - InetSocketAddress addr = (InetSocketAddress)conn.getSocketChannel().socket()
1.83 - .getRemoteSocketAddress();
1.84 - if(addr.getHostName().equals(
1.85 - Config.inst().get(Config.XDAEMON_HOST, "localhost")))
1.86 - {
1.87 - String[] commands = line.split(" ", 4);
1.88 - if(commands.length == 3 && commands[1].equalsIgnoreCase("LIST"))
1.89 - {
1.90 - if(commands[2].equalsIgnoreCase("CONFIGKEYS"))
1.91 - {
1.92 - conn.println("100 list of available config keys follows");
1.93 - for(String key : Config.AVAILABLE_KEYS)
1.94 - {
1.95 - conn.println(key);
1.96 - }
1.97 - conn.println(".");
1.98 - }
1.99 - else if(commands[2].equalsIgnoreCase("PEERINGRULES"))
1.100 - {
1.101 - List<Subscription> pull =
1.102 - StorageManager.current().getSubscriptions(FeedManager.TYPE_PULL);
1.103 - List<Subscription> push =
1.104 - StorageManager.current().getSubscriptions(FeedManager.TYPE_PUSH);
1.105 - conn.println("100 list of peering rules follows");
1.106 - for(Subscription sub : pull)
1.107 - {
1.108 - conn.println("PULL " + sub.getHost() + ":" + sub.getPort()
1.109 - + " " + sub.getGroup());
1.110 - }
1.111 - for(Subscription sub : push)
1.112 - {
1.113 - conn.println("PUSH " + sub.getHost() + ":" + sub.getPort()
1.114 - + " " + sub.getGroup());
1.115 - }
1.116 - conn.println(".");
1.117 - }
1.118 - else
1.119 - {
1.120 - conn.println("401 unknown sub command");
1.121 - }
1.122 - }
1.123 - else if(commands.length == 3 && commands[1].equalsIgnoreCase("DELETE"))
1.124 - {
1.125 - StorageManager.current().delete(commands[2]);
1.126 - conn.println("200 article " + commands[2] + " deleted");
1.127 - }
1.128 - else if(commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD"))
1.129 - {
1.130 - channelAdd(commands, conn);
1.131 - }
1.132 - else if(commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL"))
1.133 - {
1.134 - Group group = StorageManager.current().getGroup(commands[2]);
1.135 - if(group == null)
1.136 - {
1.137 - conn.println("400 group not found");
1.138 - }
1.139 - else
1.140 - {
1.141 - group.setFlag(Group.DELETED);
1.142 - group.update();
1.143 - conn.println("200 group " + commands[2] + " marked as deleted");
1.144 - }
1.145 - }
1.146 - else if(commands.length == 4 && commands[1].equalsIgnoreCase("SET"))
1.147 - {
1.148 - String key = commands[2];
1.149 - String val = commands[3];
1.150 - Config.inst().set(key, val);
1.151 - conn.println("200 new config value set");
1.152 - }
1.153 - else if(commands.length == 3 && commands[1].equalsIgnoreCase("GET"))
1.154 - {
1.155 - String key = commands[2];
1.156 - String val = Config.inst().get(key, null);
1.157 - if(val != null)
1.158 - {
1.159 - conn.println("100 config value for " + key + " follows");
1.160 - conn.println(val);
1.161 - conn.println(".");
1.162 - }
1.163 - else
1.164 - {
1.165 - conn.println("400 config value not set");
1.166 - }
1.167 - }
1.168 - else if(commands.length >= 3 && commands[1].equalsIgnoreCase("LOG"))
1.169 - {
1.170 - Group group = null;
1.171 - if(commands.length > 3)
1.172 - {
1.173 - group = (Group)Channel.getByName(commands[3]);
1.174 - }
1.175 + // TODO: Refactor this method to reduce complexity!
1.176 + @Override
1.177 + public void processLine(NNTPConnection conn, String line, byte[] raw)
1.178 + throws IOException, StorageBackendException
1.179 + {
1.180 + InetSocketAddress addr = (InetSocketAddress) conn.getSocketChannel().socket().getRemoteSocketAddress();
1.181 + if (addr.getHostName().equals(
1.182 + Config.inst().get(Config.XDAEMON_HOST, "localhost"))) {
1.183 + String[] commands = line.split(" ", 4);
1.184 + if (commands.length == 3 && commands[1].equalsIgnoreCase("LIST")) {
1.185 + if (commands[2].equalsIgnoreCase("CONFIGKEYS")) {
1.186 + conn.println("100 list of available config keys follows");
1.187 + for (String key : Config.AVAILABLE_KEYS) {
1.188 + conn.println(key);
1.189 + }
1.190 + conn.println(".");
1.191 + } else if (commands[2].equalsIgnoreCase("PEERINGRULES")) {
1.192 + List<Subscription> pull =
1.193 + StorageManager.current().getSubscriptions(FeedManager.TYPE_PULL);
1.194 + List<Subscription> push =
1.195 + StorageManager.current().getSubscriptions(FeedManager.TYPE_PUSH);
1.196 + conn.println("100 list of peering rules follows");
1.197 + for (Subscription sub : pull) {
1.198 + conn.println("PULL " + sub.getHost() + ":" + sub.getPort()
1.199 + + " " + sub.getGroup());
1.200 + }
1.201 + for (Subscription sub : push) {
1.202 + conn.println("PUSH " + sub.getHost() + ":" + sub.getPort()
1.203 + + " " + sub.getGroup());
1.204 + }
1.205 + conn.println(".");
1.206 + } else {
1.207 + conn.println("401 unknown sub command");
1.208 + }
1.209 + } else if (commands.length == 3 && commands[1].equalsIgnoreCase("DELETE")) {
1.210 + StorageManager.current().delete(commands[2]);
1.211 + conn.println("200 article " + commands[2] + " deleted");
1.212 + } else if (commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD")) {
1.213 + channelAdd(commands, conn);
1.214 + } else if (commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL")) {
1.215 + Group group = StorageManager.current().getGroup(commands[2]);
1.216 + if (group == null) {
1.217 + conn.println("400 group not found");
1.218 + } else {
1.219 + group.setFlag(Group.DELETED);
1.220 + group.update();
1.221 + conn.println("200 group " + commands[2] + " marked as deleted");
1.222 + }
1.223 + } else if (commands.length == 4 && commands[1].equalsIgnoreCase("SET")) {
1.224 + String key = commands[2];
1.225 + String val = commands[3];
1.226 + Config.inst().set(key, val);
1.227 + conn.println("200 new config value set");
1.228 + } else if (commands.length == 3 && commands[1].equalsIgnoreCase("GET")) {
1.229 + String key = commands[2];
1.230 + String val = Config.inst().get(key, null);
1.231 + if (val != null) {
1.232 + conn.println("100 config value for " + key + " follows");
1.233 + conn.println(val);
1.234 + conn.println(".");
1.235 + } else {
1.236 + conn.println("400 config value not set");
1.237 + }
1.238 + } else if (commands.length >= 3 && commands[1].equalsIgnoreCase("LOG")) {
1.239 + Group group = null;
1.240 + if (commands.length > 3) {
1.241 + group = (Group) Channel.getByName(commands[3]);
1.242 + }
1.243
1.244 - if(commands[2].equalsIgnoreCase("CONNECTED_CLIENTS"))
1.245 - {
1.246 - conn.println("100 number of connections follow");
1.247 - conn.println(Integer.toString(Stats.getInstance().connectedClients()));
1.248 - conn.println(".");
1.249 - }
1.250 - else if(commands[2].equalsIgnoreCase("POSTED_NEWS"))
1.251 - {
1.252 - conn.println("100 hourly numbers of posted news yesterday");
1.253 - for(int n = 0; n < 24; n++)
1.254 - {
1.255 - conn.println(n + " " + Stats.getInstance()
1.256 - .getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
1.257 - }
1.258 - conn.println(".");
1.259 - }
1.260 - else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS"))
1.261 - {
1.262 - conn.println("100 hourly numbers of gatewayed news yesterday");
1.263 - for(int n = 0; n < 24; n++)
1.264 - {
1.265 - conn.println(n + " " + Stats.getInstance()
1.266 - .getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
1.267 - }
1.268 - conn.println(".");
1.269 - }
1.270 - else if(commands[2].equalsIgnoreCase("TRANSMITTED_NEWS"))
1.271 - {
1.272 - conn.println("100 hourly numbers of news transmitted to peers yesterday");
1.273 - for(int n = 0; n < 24; n++)
1.274 - {
1.275 - conn.println(n + " " + Stats.getInstance()
1.276 - .getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
1.277 - }
1.278 - conn.println(".");
1.279 - }
1.280 - else if(commands[2].equalsIgnoreCase("HOSTED_NEWS"))
1.281 - {
1.282 - conn.println("100 number of overall hosted news");
1.283 - conn.println(Integer.toString(Stats.getInstance().getNumberOfNews()));
1.284 - conn.println(".");
1.285 - }
1.286 - else if(commands[2].equalsIgnoreCase("HOSTED_GROUPS"))
1.287 - {
1.288 - conn.println("100 number of hosted groups");
1.289 - conn.println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
1.290 - conn.println(".");
1.291 - }
1.292 - else if(commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR"))
1.293 - {
1.294 - conn.println("100 posted news per hour");
1.295 - conn.println(Double.toString(Stats.getInstance().postedPerHour(-1)));
1.296 - conn.println(".");
1.297 - }
1.298 - else if(commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR"))
1.299 - {
1.300 - conn.println("100 feeded news per hour");
1.301 - conn.println(Double.toString(Stats.getInstance().feededPerHour(-1)));
1.302 - conn.println(".");
1.303 - }
1.304 - else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR"))
1.305 - {
1.306 - conn.println("100 gatewayed news per hour");
1.307 - conn.println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
1.308 - conn.println(".");
1.309 - }
1.310 - else
1.311 - {
1.312 - conn.println("401 unknown sub command");
1.313 - }
1.314 - }
1.315 - else if(commands.length >= 3 && commands[1].equalsIgnoreCase("PLUGIN"))
1.316 - {
1.317 -
1.318 - }
1.319 - else
1.320 - {
1.321 - conn.println("400 invalid command usage");
1.322 - }
1.323 - }
1.324 - else
1.325 - {
1.326 - conn.println("501 not allowed");
1.327 - }
1.328 - }
1.329 -
1.330 + if (commands[2].equalsIgnoreCase("CONNECTED_CLIENTS")) {
1.331 + conn.println("100 number of connections follow");
1.332 + conn.println(Integer.toString(Stats.getInstance().connectedClients()));
1.333 + conn.println(".");
1.334 + } else if (commands[2].equalsIgnoreCase("POSTED_NEWS")) {
1.335 + conn.println("100 hourly numbers of posted news yesterday");
1.336 + for (int n = 0; n < 24; n++) {
1.337 + conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
1.338 + }
1.339 + conn.println(".");
1.340 + } else if (commands[2].equalsIgnoreCase("GATEWAYED_NEWS")) {
1.341 + conn.println("100 hourly numbers of gatewayed news yesterday");
1.342 + for (int n = 0; n < 24; n++) {
1.343 + conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
1.344 + }
1.345 + conn.println(".");
1.346 + } else if (commands[2].equalsIgnoreCase("TRANSMITTED_NEWS")) {
1.347 + conn.println("100 hourly numbers of news transmitted to peers yesterday");
1.348 + for (int n = 0; n < 24; n++) {
1.349 + conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
1.350 + }
1.351 + conn.println(".");
1.352 + } else if (commands[2].equalsIgnoreCase("HOSTED_NEWS")) {
1.353 + conn.println("100 number of overall hosted news");
1.354 + conn.println(Integer.toString(Stats.getInstance().getNumberOfNews()));
1.355 + conn.println(".");
1.356 + } else if (commands[2].equalsIgnoreCase("HOSTED_GROUPS")) {
1.357 + conn.println("100 number of hosted groups");
1.358 + conn.println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
1.359 + conn.println(".");
1.360 + } else if (commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR")) {
1.361 + conn.println("100 posted news per hour");
1.362 + conn.println(Double.toString(Stats.getInstance().postedPerHour(-1)));
1.363 + conn.println(".");
1.364 + } else if (commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR")) {
1.365 + conn.println("100 feeded news per hour");
1.366 + conn.println(Double.toString(Stats.getInstance().feededPerHour(-1)));
1.367 + conn.println(".");
1.368 + } else if (commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR")) {
1.369 + conn.println("100 gatewayed news per hour");
1.370 + conn.println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
1.371 + conn.println(".");
1.372 + } else {
1.373 + conn.println("401 unknown sub command");
1.374 + }
1.375 + } else if (commands.length >= 3 && commands[1].equalsIgnoreCase("PLUGIN")) {
1.376 + } else {
1.377 + conn.println("400 invalid command usage");
1.378 + }
1.379 + } else {
1.380 + conn.println("501 not allowed");
1.381 + }
1.382 + }
1.383 }