src/org/sonews/daemon/command/XDaemonCommand.java
changeset 36 c404a87db5b7
parent 31 087ef6fe6a1a
child 37 74139325d305
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/sonews/daemon/command/XDaemonCommand.java	Sun Aug 29 17:43:58 2010 +0200
     1.3 @@ -0,0 +1,270 @@
     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.util.List;
    1.27 +import org.sonews.config.Config;
    1.28 +import org.sonews.daemon.NNTPConnection;
    1.29 +import org.sonews.storage.StorageBackendException;
    1.30 +import org.sonews.storage.StorageManager;
    1.31 +import org.sonews.feed.FeedManager;
    1.32 +import org.sonews.feed.Subscription;
    1.33 +import org.sonews.storage.Channel;
    1.34 +import org.sonews.storage.Group;
    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 implements Command
    1.47 +{
    1.48 +
    1.49 +  @Override
    1.50 +  public String[] getSupportedCommandStrings()
    1.51 +  {
    1.52 +    return new String[]{"XDAEMON"};
    1.53 +  }
    1.54 +
    1.55 +  @Override
    1.56 +  public boolean hasFinished()
    1.57 +  {
    1.58 +    return true;
    1.59 +  }
    1.60 +
    1.61 +  @Override
    1.62 +  public String impliedCapability()
    1.63 +  {
    1.64 +    return null;
    1.65 +  }
    1.66 +
    1.67 +  @Override
    1.68 +  public boolean isStateful()
    1.69 +  {
    1.70 +    return false;
    1.71 +  }
    1.72 +
    1.73 +  private void channelAdd(String[] commands, NNTPConnection conn)
    1.74 +    throws IOException, StorageBackendException
    1.75 +  {
    1.76 +    String groupName = commands[2];
    1.77 +    if(StorageManager.current().isGroupExisting(groupName))
    1.78 +    {
    1.79 +      conn.println("400 group " + groupName + " already existing!");
    1.80 +    }
    1.81 +    else
    1.82 +    {
    1.83 +      StorageManager.current().addGroup(groupName, Integer.parseInt(commands[3]));
    1.84 +      conn.println("200 group " + groupName + " created");
    1.85 +    }
    1.86 +  }
    1.87 +
    1.88 +  // TODO: Refactor this method to reduce complexity!
    1.89 +  @Override
    1.90 +  public void processLine(NNTPConnection conn, String line, byte[] raw)
    1.91 +    throws IOException, StorageBackendException
    1.92 +  {
    1.93 +    InetSocketAddress addr = (InetSocketAddress)conn.getSocketChannel().socket()
    1.94 +      .getRemoteSocketAddress();
    1.95 +    if(addr.getHostName().equals(
    1.96 +      Config.inst().get(Config.XDAEMON_HOST, "localhost")))
    1.97 +    {
    1.98 +      String[] commands = line.split(" ", 4);
    1.99 +      if(commands.length == 3 && commands[1].equalsIgnoreCase("LIST"))
   1.100 +      {
   1.101 +        if(commands[2].equalsIgnoreCase("CONFIGKEYS"))
   1.102 +        {
   1.103 +          conn.println("100 list of available config keys follows");
   1.104 +          for(String key : Config.AVAILABLE_KEYS)
   1.105 +          {
   1.106 +            conn.println(key);
   1.107 +          }
   1.108 +          conn.println(".");
   1.109 +        }
   1.110 +        else if(commands[2].equalsIgnoreCase("PEERINGRULES"))
   1.111 +        {
   1.112 +          List<Subscription> pull = 
   1.113 +            StorageManager.current().getSubscriptions(FeedManager.TYPE_PULL);
   1.114 +          List<Subscription> push =
   1.115 +            StorageManager.current().getSubscriptions(FeedManager.TYPE_PUSH);
   1.116 +          conn.println("100 list of peering rules follows");
   1.117 +          for(Subscription sub : pull)
   1.118 +          {
   1.119 +            conn.println("PULL " + sub.getHost() + ":" + sub.getPort()
   1.120 +              + " " + sub.getGroup());
   1.121 +          }
   1.122 +          for(Subscription sub : push)
   1.123 +          {
   1.124 +            conn.println("PUSH " + sub.getHost() + ":" + sub.getPort()
   1.125 +              + " " + sub.getGroup());
   1.126 +          }
   1.127 +          conn.println(".");
   1.128 +        }
   1.129 +        else
   1.130 +        {
   1.131 +          conn.println("401 unknown sub command");
   1.132 +        }
   1.133 +      }
   1.134 +      else if(commands.length == 3 && commands[1].equalsIgnoreCase("DELETE"))
   1.135 +      {
   1.136 +        StorageManager.current().delete(commands[2]);
   1.137 +        conn.println("200 article " + commands[2] + " deleted");
   1.138 +      }
   1.139 +      else if(commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD"))
   1.140 +      {
   1.141 +        channelAdd(commands, conn);
   1.142 +      }
   1.143 +      else if(commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL"))
   1.144 +      {
   1.145 +        Group group = StorageManager.current().getGroup(commands[2]);
   1.146 +        if(group == null)
   1.147 +        {
   1.148 +          conn.println("400 group not found");
   1.149 +        }
   1.150 +        else
   1.151 +        {
   1.152 +          group.setFlag(Group.DELETED);
   1.153 +          group.update();
   1.154 +          conn.println("200 group " + commands[2] + " marked as deleted");
   1.155 +        }
   1.156 +      }
   1.157 +      else if(commands.length == 4 && commands[1].equalsIgnoreCase("SET"))
   1.158 +      {
   1.159 +        String key = commands[2];
   1.160 +        String val = commands[3];
   1.161 +        Config.inst().set(key, val);
   1.162 +        conn.println("200 new config value set");
   1.163 +      }
   1.164 +      else if(commands.length == 3 && commands[1].equalsIgnoreCase("GET"))
   1.165 +      {
   1.166 +        String key = commands[2];
   1.167 +        String val = Config.inst().get(key, null);
   1.168 +        if(val != null)
   1.169 +        {
   1.170 +          conn.println("100 config value for " + key + " follows");
   1.171 +          conn.println(val);
   1.172 +          conn.println(".");
   1.173 +        }
   1.174 +        else
   1.175 +        {
   1.176 +          conn.println("400 config value not set");
   1.177 +        }
   1.178 +      }
   1.179 +      else if(commands.length >= 3 && commands[1].equalsIgnoreCase("LOG"))
   1.180 +      {
   1.181 +        Group group = null;
   1.182 +        if(commands.length > 3)
   1.183 +        {
   1.184 +          group = (Group)Channel.getByName(commands[3]);
   1.185 +        }
   1.186 +
   1.187 +        if(commands[2].equalsIgnoreCase("CONNECTED_CLIENTS"))
   1.188 +        {
   1.189 +          conn.println("100 number of connections follow");
   1.190 +          conn.println(Integer.toString(Stats.getInstance().connectedClients()));
   1.191 +          conn.println(".");
   1.192 +        }
   1.193 +        else if(commands[2].equalsIgnoreCase("POSTED_NEWS"))
   1.194 +        {
   1.195 +          conn.println("100 hourly numbers of posted news yesterday");
   1.196 +          for(int n = 0; n < 24; n++)
   1.197 +          {
   1.198 +            conn.println(n + " " + Stats.getInstance()
   1.199 +              .getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
   1.200 +          }
   1.201 +          conn.println(".");
   1.202 +        }
   1.203 +        else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS"))
   1.204 +        {
   1.205 +          conn.println("100 hourly numbers of gatewayed news yesterday");
   1.206 +          for(int n = 0; n < 24; n++)
   1.207 +          {
   1.208 +            conn.println(n + " " + Stats.getInstance()
   1.209 +              .getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
   1.210 +          }
   1.211 +          conn.println(".");
   1.212 +        }
   1.213 +        else if(commands[2].equalsIgnoreCase("TRANSMITTED_NEWS"))
   1.214 +        {
   1.215 +          conn.println("100 hourly numbers of news transmitted to peers yesterday");
   1.216 +          for(int n = 0; n < 24; n++)
   1.217 +          {
   1.218 +            conn.println(n + " " + Stats.getInstance()
   1.219 +              .getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
   1.220 +          }
   1.221 +          conn.println(".");
   1.222 +        }
   1.223 +        else if(commands[2].equalsIgnoreCase("HOSTED_NEWS"))
   1.224 +        {
   1.225 +          conn.println("100 number of overall hosted news");
   1.226 +          conn.println(Integer.toString(Stats.getInstance().getNumberOfNews()));
   1.227 +          conn.println(".");
   1.228 +        }
   1.229 +        else if(commands[2].equalsIgnoreCase("HOSTED_GROUPS"))
   1.230 +        {
   1.231 +          conn.println("100 number of hosted groups");
   1.232 +          conn.println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
   1.233 +          conn.println(".");
   1.234 +        }
   1.235 +        else if(commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR"))
   1.236 +        {
   1.237 +          conn.println("100 posted news per hour");
   1.238 +          conn.println(Double.toString(Stats.getInstance().postedPerHour(-1)));
   1.239 +          conn.println(".");
   1.240 +        }
   1.241 +        else if(commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR"))
   1.242 +        {
   1.243 +          conn.println("100 feeded news per hour");
   1.244 +          conn.println(Double.toString(Stats.getInstance().feededPerHour(-1)));
   1.245 +          conn.println(".");
   1.246 +        }
   1.247 +        else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR"))
   1.248 +        {
   1.249 +          conn.println("100 gatewayed news per hour");
   1.250 +          conn.println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
   1.251 +          conn.println(".");
   1.252 +        }
   1.253 +        else
   1.254 +        {
   1.255 +          conn.println("401 unknown sub command");
   1.256 +        }
   1.257 +      }
   1.258 +      else if(commands.length >= 3 && commands[1].equalsIgnoreCase("PLUGIN"))
   1.259 +      {
   1.260 +        
   1.261 +      }
   1.262 +      else
   1.263 +      {
   1.264 +        conn.println("400 invalid command usage");
   1.265 +      }
   1.266 +    }
   1.267 +    else
   1.268 +    {
   1.269 +      conn.println("501 not allowed");
   1.270 +    }
   1.271 +  }
   1.272 +  
   1.273 +}