3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package org.sonews.daemon.command;
21 import java.io.IOException;
22 import java.net.InetSocketAddress;
23 import java.sql.SQLException;
24 import java.util.List;
25 import org.sonews.daemon.BootstrapConfig;
26 import org.sonews.daemon.Config;
27 import org.sonews.daemon.NNTPConnection;
28 import org.sonews.daemon.storage.Database;
29 import org.sonews.daemon.storage.Group;
30 import org.sonews.feed.FeedManager;
31 import org.sonews.feed.Subscription;
32 import org.sonews.util.Stats;
35 * The XDAEMON command allows a client to get/set properties of the
36 * running server daemon. Only locally connected clients are allowed to
38 * The restriction to localhost connection can be suppressed by overriding
39 * the sonews.xdaemon.host bootstrap config property.
40 * @author Christian Lins
43 public class XDaemonCommand extends AbstractCommand
46 public XDaemonCommand(NNTPConnection conn)
52 public boolean hasFinished()
57 // TODO: Refactor this method to reduce complexity!
59 public void processLine(String line) throws IOException, SQLException
61 InetSocketAddress addr = (InetSocketAddress)connection.getChannel().socket()
62 .getRemoteSocketAddress();
63 if(addr.getHostName().equals(
64 BootstrapConfig.getInstance().get(BootstrapConfig.XDAEMON_HOST, "localhost")))
66 String[] commands = line.split(" ", 4);
67 if(commands.length == 3 && commands[1].equalsIgnoreCase("LIST"))
69 if(commands[2].equalsIgnoreCase("CONFIGKEYS"))
71 printStatus(200, "list of available config keys follows");
72 for(String key : Config.AVAILABLE_KEYS)
78 else if(commands[2].equalsIgnoreCase("PEERINGRULES"))
80 List<Subscription> pull =
81 Database.getInstance().getSubscriptions(FeedManager.TYPE_PULL);
82 List<Subscription> push =
83 Database.getInstance().getSubscriptions(FeedManager.TYPE_PUSH);
84 printStatus(200,"list of peering rules follows");
85 for(Subscription sub : pull)
87 println("PULL " + sub.getHost() + ":" + sub.getPort()
88 + " " + sub.getGroup());
90 for(Subscription sub : push)
92 println("PUSH " + sub.getHost() + ":" + sub.getPort()
93 + " " + sub.getGroup());
99 printStatus(501, "unknown sub command");
102 else if(commands.length == 3 && commands[1].equalsIgnoreCase("DELETE"))
104 Database.getInstance().delete(commands[2]);
105 printStatus(200, "article " + commands[2] + " deleted");
107 else if(commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD"))
109 Database.getInstance().addGroup(commands[2], Integer.parseInt(commands[3]));
110 printStatus(200, "group " + commands[2] + " created");
112 else if(commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL"))
114 Group group = Database.getInstance().getGroup(commands[2]);
117 printStatus(400, "group not found");
121 group.setFlag(Group.DELETED);
122 printStatus(200, "group " + commands[2] + " marked as deleted");
125 else if(commands.length == 4 && commands[1].equalsIgnoreCase("SET"))
127 String key = commands[2];
128 String val = commands[3];
129 Config.getInstance().set(key, val);
130 printStatus(200, "new config value set");
132 else if(commands.length == 3 && commands[1].equalsIgnoreCase("GET"))
134 String key = commands[2];
135 String val = Config.getInstance().get(key, null);
138 printStatus(200, "config value for " + key + " follows");
144 printStatus(400, "config value not set");
147 else if(commands.length >= 3 && commands[1].equalsIgnoreCase("LOG"))
150 if(commands.length > 3)
152 group = Group.getByName(commands[3]);
155 if(commands[2].equalsIgnoreCase("CONNECTED_CLIENTS"))
157 printStatus(200, "number of connections follow");
158 println(Integer.toString(Stats.getInstance().connectedClients()));
161 else if(commands[2].equalsIgnoreCase("POSTED_NEWS"))
163 printStatus(200, "hourly numbers of posted news yesterday");
164 for(int n = 0; n < 24; n++)
166 println(n + " " + Stats.getInstance()
167 .getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
171 else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS"))
173 printStatus(200, "hourly numbers of gatewayed news yesterday");
174 for(int n = 0; n < 24; n++)
176 println(n + " " + Stats.getInstance()
177 .getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
181 else if(commands[2].equalsIgnoreCase("TRANSMITTED_NEWS"))
183 printStatus(200, "hourly numbers of news transmitted to peers yesterday");
184 for(int n = 0; n < 24; n++)
186 println(n + " " + Stats.getInstance()
187 .getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
191 else if(commands[2].equalsIgnoreCase("HOSTED_NEWS"))
193 printStatus(200, "number of overall hosted news");
194 println(Integer.toString(Stats.getInstance().getNumberOfNews()));
197 else if(commands[2].equalsIgnoreCase("HOSTED_GROUPS"))
199 printStatus(200, "number of hosted groups");
200 println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
203 else if(commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR"))
205 printStatus(200, "posted news per hour");
206 println(Double.toString(Stats.getInstance().postedPerHour(-1)));
209 else if(commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR"))
211 printStatus(200, "feeded news per hour");
212 println(Double.toString(Stats.getInstance().feededPerHour(-1)));
215 else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR"))
217 printStatus(200, "gatewayed news per hour");
218 println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
223 printStatus(501, "unknown sub command");
228 printStatus(500, "invalid command usage");
233 printStatus(500, "not allowed");