1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/org/sonews/daemon/command/XDaemonCommand.java Fri Jun 26 16:48:50 2009 +0200
1.3 @@ -0,0 +1,237 @@
1.4 +/*
1.5 + * SONEWS News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package org.sonews.daemon.command;
1.23 +
1.24 +import java.io.IOException;
1.25 +import java.net.InetSocketAddress;
1.26 +import java.sql.SQLException;
1.27 +import java.util.List;
1.28 +import org.sonews.daemon.BootstrapConfig;
1.29 +import org.sonews.daemon.Config;
1.30 +import org.sonews.daemon.NNTPConnection;
1.31 +import org.sonews.daemon.storage.Database;
1.32 +import org.sonews.daemon.storage.Group;
1.33 +import org.sonews.feed.FeedManager;
1.34 +import org.sonews.feed.Subscription;
1.35 +import org.sonews.util.Stats;
1.36 +
1.37 +/**
1.38 + * The XDAEMON command allows a client to get/set properties of the
1.39 + * running server daemon. Only locally connected clients are allowed to
1.40 + * use this command.
1.41 + * The restriction to localhost connection can be suppressed by overriding
1.42 + * the sonews.xdaemon.host bootstrap config property.
1.43 + * @author Christian Lins
1.44 + * @since sonews/0.5.0
1.45 + */
1.46 +public class XDaemonCommand extends AbstractCommand
1.47 +{
1.48 +
1.49 + public XDaemonCommand(NNTPConnection conn)
1.50 + {
1.51 + super(conn);
1.52 + }
1.53 +
1.54 + @Override
1.55 + public boolean hasFinished()
1.56 + {
1.57 + return true;
1.58 + }
1.59 +
1.60 + // TODO: Refactor this method to reduce complexity!
1.61 + @Override
1.62 + public void processLine(String line) throws IOException, SQLException
1.63 + {
1.64 + InetSocketAddress addr = (InetSocketAddress)connection.getChannel().socket()
1.65 + .getRemoteSocketAddress();
1.66 + if(addr.getHostName().equals(
1.67 + BootstrapConfig.getInstance().get(BootstrapConfig.XDAEMON_HOST, "localhost")))
1.68 + {
1.69 + String[] commands = line.split(" ", 4);
1.70 + if(commands.length == 3 && commands[1].equalsIgnoreCase("LIST"))
1.71 + {
1.72 + if(commands[2].equalsIgnoreCase("CONFIGKEYS"))
1.73 + {
1.74 + printStatus(200, "list of available config keys follows");
1.75 + for(String key : Config.AVAILABLE_KEYS)
1.76 + {
1.77 + println(key);
1.78 + }
1.79 + println(".");
1.80 + }
1.81 + else if(commands[2].equalsIgnoreCase("PEERINGRULES"))
1.82 + {
1.83 + List<Subscription> pull =
1.84 + Database.getInstance().getSubscriptions(FeedManager.TYPE_PULL);
1.85 + List<Subscription> push =
1.86 + Database.getInstance().getSubscriptions(FeedManager.TYPE_PUSH);
1.87 + printStatus(200,"list of peering rules follows");
1.88 + for(Subscription sub : pull)
1.89 + {
1.90 + println("PULL " + sub.getHost() + ":" + sub.getPort()
1.91 + + " " + sub.getGroup());
1.92 + }
1.93 + for(Subscription sub : push)
1.94 + {
1.95 + println("PUSH " + sub.getHost() + ":" + sub.getPort()
1.96 + + " " + sub.getGroup());
1.97 + }
1.98 + println(".");
1.99 + }
1.100 + else
1.101 + {
1.102 + printStatus(501, "unknown sub command");
1.103 + }
1.104 + }
1.105 + else if(commands.length == 3 && commands[1].equalsIgnoreCase("DELETE"))
1.106 + {
1.107 + Database.getInstance().delete(commands[2]);
1.108 + printStatus(200, "article " + commands[2] + " deleted");
1.109 + }
1.110 + else if(commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD"))
1.111 + {
1.112 + Database.getInstance().addGroup(commands[2], Integer.parseInt(commands[3]));
1.113 + printStatus(200, "group " + commands[2] + " created");
1.114 + }
1.115 + else if(commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL"))
1.116 + {
1.117 + Group group = Database.getInstance().getGroup(commands[2]);
1.118 + if(group == null)
1.119 + {
1.120 + printStatus(400, "group not found");
1.121 + }
1.122 + else
1.123 + {
1.124 + group.setFlag(Group.DELETED);
1.125 + printStatus(200, "group " + commands[2] + " marked as deleted");
1.126 + }
1.127 + }
1.128 + else if(commands.length == 4 && commands[1].equalsIgnoreCase("SET"))
1.129 + {
1.130 + String key = commands[2];
1.131 + String val = commands[3];
1.132 + Config.getInstance().set(key, val);
1.133 + printStatus(200, "new config value set");
1.134 + }
1.135 + else if(commands.length == 3 && commands[1].equalsIgnoreCase("GET"))
1.136 + {
1.137 + String key = commands[2];
1.138 + String val = Config.getInstance().get(key, null);
1.139 + if(val != null)
1.140 + {
1.141 + printStatus(200, "config value for " + key + " follows");
1.142 + println(val);
1.143 + println(".");
1.144 + }
1.145 + else
1.146 + {
1.147 + printStatus(400, "config value not set");
1.148 + }
1.149 + }
1.150 + else if(commands.length >= 3 && commands[1].equalsIgnoreCase("LOG"))
1.151 + {
1.152 + Group group = null;
1.153 + if(commands.length > 3)
1.154 + {
1.155 + group = Group.getByName(commands[3]);
1.156 + }
1.157 +
1.158 + if(commands[2].equalsIgnoreCase("CONNECTED_CLIENTS"))
1.159 + {
1.160 + printStatus(200, "number of connections follow");
1.161 + println(Integer.toString(Stats.getInstance().connectedClients()));
1.162 + println(".");
1.163 + }
1.164 + else if(commands[2].equalsIgnoreCase("POSTED_NEWS"))
1.165 + {
1.166 + printStatus(200, "hourly numbers of posted news yesterday");
1.167 + for(int n = 0; n < 24; n++)
1.168 + {
1.169 + println(n + " " + Stats.getInstance()
1.170 + .getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
1.171 + }
1.172 + println(".");
1.173 + }
1.174 + else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS"))
1.175 + {
1.176 + printStatus(200, "hourly numbers of gatewayed news yesterday");
1.177 + for(int n = 0; n < 24; n++)
1.178 + {
1.179 + println(n + " " + Stats.getInstance()
1.180 + .getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
1.181 + }
1.182 + println(".");
1.183 + }
1.184 + else if(commands[2].equalsIgnoreCase("TRANSMITTED_NEWS"))
1.185 + {
1.186 + printStatus(200, "hourly numbers of news transmitted to peers yesterday");
1.187 + for(int n = 0; n < 24; n++)
1.188 + {
1.189 + println(n + " " + Stats.getInstance()
1.190 + .getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
1.191 + }
1.192 + println(".");
1.193 + }
1.194 + else if(commands[2].equalsIgnoreCase("HOSTED_NEWS"))
1.195 + {
1.196 + printStatus(200, "number of overall hosted news");
1.197 + println(Integer.toString(Stats.getInstance().getNumberOfNews()));
1.198 + println(".");
1.199 + }
1.200 + else if(commands[2].equalsIgnoreCase("HOSTED_GROUPS"))
1.201 + {
1.202 + printStatus(200, "number of hosted groups");
1.203 + println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
1.204 + println(".");
1.205 + }
1.206 + else if(commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR"))
1.207 + {
1.208 + printStatus(200, "posted news per hour");
1.209 + println(Double.toString(Stats.getInstance().postedPerHour(-1)));
1.210 + println(".");
1.211 + }
1.212 + else if(commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR"))
1.213 + {
1.214 + printStatus(200, "feeded news per hour");
1.215 + println(Double.toString(Stats.getInstance().feededPerHour(-1)));
1.216 + println(".");
1.217 + }
1.218 + else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR"))
1.219 + {
1.220 + printStatus(200, "gatewayed news per hour");
1.221 + println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
1.222 + println(".");
1.223 + }
1.224 + else
1.225 + {
1.226 + printStatus(501, "unknown sub command");
1.227 + }
1.228 + }
1.229 + else
1.230 + {
1.231 + printStatus(500, "invalid command usage");
1.232 + }
1.233 + }
1.234 + else
1.235 + {
1.236 + printStatus(500, "not allowed");
1.237 + }
1.238 + }
1.239 +
1.240 +}