org/sonews/daemon/command/XDaemonCommand.java
author chris <chris@marvin>
Fri, 26 Jun 2009 16:48:50 +0200
changeset 1 6fceb66e1ad7
child 3 2fdc9cc89502
permissions -rw-r--r--
Hooray... sonews/0.5.0 final

HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Remove all lines to abort the collapse operation.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 
    19 package org.sonews.daemon.command;
    20 
    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;
    33 
    34 /**
    35  * The XDAEMON command allows a client to get/set properties of the
    36  * running server daemon. Only locally connected clients are allowed to
    37  * use this command.
    38  * The restriction to localhost connection can be suppressed by overriding
    39  * the sonews.xdaemon.host bootstrap config property.
    40  * @author Christian Lins
    41  * @since sonews/0.5.0
    42  */
    43 public class XDaemonCommand extends AbstractCommand
    44 {
    45   
    46   public XDaemonCommand(NNTPConnection conn)
    47   {
    48     super(conn);
    49   }
    50 
    51   @Override
    52   public boolean hasFinished()
    53   {
    54     return true;
    55   }
    56 
    57   // TODO: Refactor this method to reduce complexity!
    58   @Override
    59   public void processLine(String line) throws IOException, SQLException
    60   {
    61     InetSocketAddress addr = (InetSocketAddress)connection.getChannel().socket()
    62       .getRemoteSocketAddress();
    63     if(addr.getHostName().equals(
    64       BootstrapConfig.getInstance().get(BootstrapConfig.XDAEMON_HOST, "localhost")))
    65     {
    66       String[] commands = line.split(" ", 4);
    67       if(commands.length == 3 && commands[1].equalsIgnoreCase("LIST"))
    68       {
    69         if(commands[2].equalsIgnoreCase("CONFIGKEYS"))
    70         {
    71           printStatus(200, "list of available config keys follows");
    72           for(String key : Config.AVAILABLE_KEYS)
    73           {
    74             println(key);
    75           }
    76           println(".");
    77         }
    78         else if(commands[2].equalsIgnoreCase("PEERINGRULES"))
    79         {
    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)
    86           {
    87             println("PULL " + sub.getHost() + ":" + sub.getPort() 
    88               + " " + sub.getGroup());
    89           }
    90           for(Subscription sub : push)
    91           {
    92             println("PUSH " + sub.getHost() + ":" + sub.getPort() 
    93               + " " + sub.getGroup());
    94           }
    95           println(".");
    96         }
    97         else
    98         {
    99           printStatus(501, "unknown sub command");
   100         }
   101       }
   102       else if(commands.length == 3 && commands[1].equalsIgnoreCase("DELETE"))
   103       {
   104         Database.getInstance().delete(commands[2]);
   105         printStatus(200, "article " + commands[2] + " deleted");
   106       }
   107       else if(commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD"))
   108       {
   109         Database.getInstance().addGroup(commands[2], Integer.parseInt(commands[3]));
   110         printStatus(200, "group " + commands[2] + " created");
   111       }
   112       else if(commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL"))
   113       {
   114         Group group = Database.getInstance().getGroup(commands[2]);
   115         if(group == null)
   116         {
   117           printStatus(400, "group not found");
   118         }
   119         else
   120         {
   121           group.setFlag(Group.DELETED);
   122           printStatus(200, "group " + commands[2] + " marked as deleted");
   123         }
   124       }
   125       else if(commands.length == 4 && commands[1].equalsIgnoreCase("SET"))
   126       {
   127         String key = commands[2];
   128         String val = commands[3];
   129         Config.getInstance().set(key, val);
   130         printStatus(200, "new config value set");
   131       }
   132       else if(commands.length == 3 && commands[1].equalsIgnoreCase("GET"))
   133       {
   134         String key = commands[2];
   135         String val = Config.getInstance().get(key, null);
   136         if(val != null)
   137         {
   138           printStatus(200, "config value for " + key + " follows");
   139           println(val);
   140           println(".");
   141         }
   142         else
   143         {
   144           printStatus(400, "config value not set");
   145         }
   146       }
   147       else if(commands.length >= 3 && commands[1].equalsIgnoreCase("LOG"))
   148       {
   149         Group group = null;
   150         if(commands.length > 3)
   151         {
   152           group = Group.getByName(commands[3]);
   153         }
   154 
   155         if(commands[2].equalsIgnoreCase("CONNECTED_CLIENTS"))
   156         {
   157           printStatus(200, "number of connections follow");
   158           println(Integer.toString(Stats.getInstance().connectedClients()));
   159           println(".");
   160         }
   161         else if(commands[2].equalsIgnoreCase("POSTED_NEWS"))
   162         {
   163           printStatus(200, "hourly numbers of posted news yesterday");
   164           for(int n = 0; n < 24; n++)
   165           {
   166             println(n + " " + Stats.getInstance()
   167               .getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
   168           }
   169           println(".");
   170         }
   171         else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS"))
   172         {
   173           printStatus(200, "hourly numbers of gatewayed news yesterday");
   174           for(int n = 0; n < 24; n++)
   175           {
   176             println(n + " " + Stats.getInstance()
   177               .getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
   178           }
   179           println(".");
   180         }
   181         else if(commands[2].equalsIgnoreCase("TRANSMITTED_NEWS"))
   182         {
   183           printStatus(200, "hourly numbers of news transmitted to peers yesterday");
   184           for(int n = 0; n < 24; n++)
   185           {
   186             println(n + " " + Stats.getInstance()
   187               .getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
   188           }
   189           println(".");
   190         }
   191         else if(commands[2].equalsIgnoreCase("HOSTED_NEWS"))
   192         {
   193           printStatus(200, "number of overall hosted news");
   194           println(Integer.toString(Stats.getInstance().getNumberOfNews()));
   195           println(".");
   196         }
   197         else if(commands[2].equalsIgnoreCase("HOSTED_GROUPS"))
   198         {
   199           printStatus(200, "number of hosted groups");
   200           println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
   201           println(".");
   202         }
   203         else if(commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR"))
   204         {
   205           printStatus(200, "posted news per hour");
   206           println(Double.toString(Stats.getInstance().postedPerHour(-1)));
   207           println(".");
   208         }
   209         else if(commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR"))
   210         {
   211           printStatus(200, "feeded news per hour");
   212           println(Double.toString(Stats.getInstance().feededPerHour(-1)));
   213           println(".");
   214         }
   215         else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR"))
   216         {
   217           printStatus(200, "gatewayed news per hour");
   218           println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
   219           println(".");
   220         }
   221         else
   222         {
   223           printStatus(501, "unknown sub command");
   224         }
   225       }
   226       else
   227       {
   228         printStatus(500, "invalid command usage");
   229       }
   230     }
   231     else
   232     {
   233       printStatus(500, "not allowed");
   234     }
   235   }
   236   
   237 }