chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: chris@1: package org.sonews.daemon.command; chris@1: chris@1: import java.io.IOException; chris@1: import java.net.InetSocketAddress; chris@1: import java.sql.SQLException; chris@1: import java.util.List; chris@1: import org.sonews.daemon.BootstrapConfig; chris@1: import org.sonews.daemon.Config; chris@1: import org.sonews.daemon.NNTPConnection; chris@1: import org.sonews.daemon.storage.Database; chris@1: import org.sonews.daemon.storage.Group; chris@1: import org.sonews.feed.FeedManager; chris@1: import org.sonews.feed.Subscription; chris@1: import org.sonews.util.Stats; chris@1: chris@1: /** chris@1: * The XDAEMON command allows a client to get/set properties of the chris@1: * running server daemon. Only locally connected clients are allowed to chris@1: * use this command. chris@1: * The restriction to localhost connection can be suppressed by overriding chris@1: * the sonews.xdaemon.host bootstrap config property. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ chris@1: public class XDaemonCommand extends AbstractCommand chris@1: { chris@1: chris@1: public XDaemonCommand(NNTPConnection conn) chris@1: { chris@1: super(conn); chris@1: } chris@1: chris@1: @Override chris@1: public boolean hasFinished() chris@1: { chris@1: return true; chris@1: } chris@1: chris@1: // TODO: Refactor this method to reduce complexity! chris@1: @Override chris@1: public void processLine(String line) throws IOException, SQLException chris@1: { chris@1: InetSocketAddress addr = (InetSocketAddress)connection.getChannel().socket() chris@1: .getRemoteSocketAddress(); chris@1: if(addr.getHostName().equals( chris@1: BootstrapConfig.getInstance().get(BootstrapConfig.XDAEMON_HOST, "localhost"))) chris@1: { chris@1: String[] commands = line.split(" ", 4); chris@1: if(commands.length == 3 && commands[1].equalsIgnoreCase("LIST")) chris@1: { chris@1: if(commands[2].equalsIgnoreCase("CONFIGKEYS")) chris@1: { chris@1: printStatus(200, "list of available config keys follows"); chris@1: for(String key : Config.AVAILABLE_KEYS) chris@1: { chris@1: println(key); chris@1: } chris@1: println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("PEERINGRULES")) chris@1: { chris@1: List pull = chris@1: Database.getInstance().getSubscriptions(FeedManager.TYPE_PULL); chris@1: List push = chris@1: Database.getInstance().getSubscriptions(FeedManager.TYPE_PUSH); chris@1: printStatus(200,"list of peering rules follows"); chris@1: for(Subscription sub : pull) chris@1: { chris@1: println("PULL " + sub.getHost() + ":" + sub.getPort() chris@1: + " " + sub.getGroup()); chris@1: } chris@1: for(Subscription sub : push) chris@1: { chris@1: println("PUSH " + sub.getHost() + ":" + sub.getPort() chris@1: + " " + sub.getGroup()); chris@1: } chris@1: println("."); chris@1: } chris@1: else chris@1: { chris@1: printStatus(501, "unknown sub command"); chris@1: } chris@1: } chris@1: else if(commands.length == 3 && commands[1].equalsIgnoreCase("DELETE")) chris@1: { chris@1: Database.getInstance().delete(commands[2]); chris@1: printStatus(200, "article " + commands[2] + " deleted"); chris@1: } chris@1: else if(commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD")) chris@1: { chris@1: Database.getInstance().addGroup(commands[2], Integer.parseInt(commands[3])); chris@1: printStatus(200, "group " + commands[2] + " created"); chris@1: } chris@1: else if(commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL")) chris@1: { chris@1: Group group = Database.getInstance().getGroup(commands[2]); chris@1: if(group == null) chris@1: { chris@1: printStatus(400, "group not found"); chris@1: } chris@1: else chris@1: { chris@1: group.setFlag(Group.DELETED); chris@1: printStatus(200, "group " + commands[2] + " marked as deleted"); chris@1: } chris@1: } chris@1: else if(commands.length == 4 && commands[1].equalsIgnoreCase("SET")) chris@1: { chris@1: String key = commands[2]; chris@1: String val = commands[3]; chris@1: Config.getInstance().set(key, val); chris@1: printStatus(200, "new config value set"); chris@1: } chris@1: else if(commands.length == 3 && commands[1].equalsIgnoreCase("GET")) chris@1: { chris@1: String key = commands[2]; chris@1: String val = Config.getInstance().get(key, null); chris@1: if(val != null) chris@1: { chris@1: printStatus(200, "config value for " + key + " follows"); chris@1: println(val); chris@1: println("."); chris@1: } chris@1: else chris@1: { chris@1: printStatus(400, "config value not set"); chris@1: } chris@1: } chris@1: else if(commands.length >= 3 && commands[1].equalsIgnoreCase("LOG")) chris@1: { chris@1: Group group = null; chris@1: if(commands.length > 3) chris@1: { chris@1: group = Group.getByName(commands[3]); chris@1: } chris@1: chris@1: if(commands[2].equalsIgnoreCase("CONNECTED_CLIENTS")) chris@1: { chris@1: printStatus(200, "number of connections follow"); chris@1: println(Integer.toString(Stats.getInstance().connectedClients())); chris@1: println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("POSTED_NEWS")) chris@1: { chris@1: printStatus(200, "hourly numbers of posted news yesterday"); chris@1: for(int n = 0; n < 24; n++) chris@1: { chris@1: println(n + " " + Stats.getInstance() chris@1: .getYesterdaysEvents(Stats.POSTED_NEWS, n, group)); chris@1: } chris@1: println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS")) chris@1: { chris@1: printStatus(200, "hourly numbers of gatewayed news yesterday"); chris@1: for(int n = 0; n < 24; n++) chris@1: { chris@1: println(n + " " + Stats.getInstance() chris@1: .getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group)); chris@1: } chris@1: println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("TRANSMITTED_NEWS")) chris@1: { chris@1: printStatus(200, "hourly numbers of news transmitted to peers yesterday"); chris@1: for(int n = 0; n < 24; n++) chris@1: { chris@1: println(n + " " + Stats.getInstance() chris@1: .getYesterdaysEvents(Stats.FEEDED_NEWS, n, group)); chris@1: } chris@1: println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("HOSTED_NEWS")) chris@1: { chris@1: printStatus(200, "number of overall hosted news"); chris@1: println(Integer.toString(Stats.getInstance().getNumberOfNews())); chris@1: println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("HOSTED_GROUPS")) chris@1: { chris@1: printStatus(200, "number of hosted groups"); chris@1: println(Integer.toString(Stats.getInstance().getNumberOfGroups())); chris@1: println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR")) chris@1: { chris@1: printStatus(200, "posted news per hour"); chris@1: println(Double.toString(Stats.getInstance().postedPerHour(-1))); chris@1: println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR")) chris@1: { chris@1: printStatus(200, "feeded news per hour"); chris@1: println(Double.toString(Stats.getInstance().feededPerHour(-1))); chris@1: println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR")) chris@1: { chris@1: printStatus(200, "gatewayed news per hour"); chris@1: println(Double.toString(Stats.getInstance().gatewayedPerHour(-1))); chris@1: println("."); chris@1: } chris@1: else chris@1: { chris@1: printStatus(501, "unknown sub command"); chris@1: } chris@1: } chris@1: else chris@1: { chris@1: printStatus(500, "invalid command usage"); chris@1: } chris@1: } chris@1: else chris@1: { chris@1: printStatus(500, "not allowed"); chris@1: } chris@1: } chris@1: chris@1: }