org/sonews/daemon/command/XDaemonCommand.java
changeset 14 efce4ec25564
parent 1 6fceb66e1ad7
child 20 6ae5e4f8329b
     1.1 --- a/org/sonews/daemon/command/XDaemonCommand.java	Fri Jun 26 16:48:50 2009 +0200
     1.2 +++ b/org/sonews/daemon/command/XDaemonCommand.java	Thu Aug 20 16:57:38 2009 +0200
     1.3 @@ -20,15 +20,14 @@
     1.4  
     1.5  import java.io.IOException;
     1.6  import java.net.InetSocketAddress;
     1.7 -import java.sql.SQLException;
     1.8  import java.util.List;
     1.9 -import org.sonews.daemon.BootstrapConfig;
    1.10 -import org.sonews.daemon.Config;
    1.11 +import org.sonews.config.Config;
    1.12  import org.sonews.daemon.NNTPConnection;
    1.13 -import org.sonews.daemon.storage.Database;
    1.14 -import org.sonews.daemon.storage.Group;
    1.15 +import org.sonews.storage.StorageBackendException;
    1.16 +import org.sonews.storage.StorageManager;
    1.17  import org.sonews.feed.FeedManager;
    1.18  import org.sonews.feed.Subscription;
    1.19 +import org.sonews.storage.Group;
    1.20  import org.sonews.util.Stats;
    1.21  
    1.22  /**
    1.23 @@ -40,12 +39,13 @@
    1.24   * @author Christian Lins
    1.25   * @since sonews/0.5.0
    1.26   */
    1.27 -public class XDaemonCommand extends AbstractCommand
    1.28 +public class XDaemonCommand implements Command
    1.29  {
    1.30 -  
    1.31 -  public XDaemonCommand(NNTPConnection conn)
    1.32 +
    1.33 +  @Override
    1.34 +  public String[] getSupportedCommandStrings()
    1.35    {
    1.36 -    super(conn);
    1.37 +    return new String[]{"XDAEMON"};
    1.38    }
    1.39  
    1.40    @Override
    1.41 @@ -54,94 +54,102 @@
    1.42      return true;
    1.43    }
    1.44  
    1.45 +  @Override
    1.46 +  public boolean isStateful()
    1.47 +  {
    1.48 +    return false;
    1.49 +  }
    1.50 +
    1.51    // TODO: Refactor this method to reduce complexity!
    1.52    @Override
    1.53 -  public void processLine(String line) throws IOException, SQLException
    1.54 +  public void processLine(NNTPConnection conn, String line, byte[] raw)
    1.55 +    throws IOException, StorageBackendException
    1.56    {
    1.57 -    InetSocketAddress addr = (InetSocketAddress)connection.getChannel().socket()
    1.58 +    InetSocketAddress addr = (InetSocketAddress)conn.getSocketChannel().socket()
    1.59        .getRemoteSocketAddress();
    1.60      if(addr.getHostName().equals(
    1.61 -      BootstrapConfig.getInstance().get(BootstrapConfig.XDAEMON_HOST, "localhost")))
    1.62 +      Config.inst().get(Config.XDAEMON_HOST, "localhost")))
    1.63      {
    1.64        String[] commands = line.split(" ", 4);
    1.65        if(commands.length == 3 && commands[1].equalsIgnoreCase("LIST"))
    1.66        {
    1.67          if(commands[2].equalsIgnoreCase("CONFIGKEYS"))
    1.68          {
    1.69 -          printStatus(200, "list of available config keys follows");
    1.70 +          conn.println("100 list of available config keys follows");
    1.71            for(String key : Config.AVAILABLE_KEYS)
    1.72            {
    1.73 -            println(key);
    1.74 +            conn.println(key);
    1.75            }
    1.76 -          println(".");
    1.77 +          conn.println(".");
    1.78          }
    1.79          else if(commands[2].equalsIgnoreCase("PEERINGRULES"))
    1.80          {
    1.81            List<Subscription> pull = 
    1.82 -            Database.getInstance().getSubscriptions(FeedManager.TYPE_PULL);
    1.83 +            StorageManager.current().getSubscriptions(FeedManager.TYPE_PULL);
    1.84            List<Subscription> push =
    1.85 -            Database.getInstance().getSubscriptions(FeedManager.TYPE_PUSH);
    1.86 -          printStatus(200,"list of peering rules follows");
    1.87 +            StorageManager.current().getSubscriptions(FeedManager.TYPE_PUSH);
    1.88 +          conn.println("100 list of peering rules follows");
    1.89            for(Subscription sub : pull)
    1.90            {
    1.91 -            println("PULL " + sub.getHost() + ":" + sub.getPort() 
    1.92 +            conn.println("PULL " + sub.getHost() + ":" + sub.getPort()
    1.93                + " " + sub.getGroup());
    1.94            }
    1.95            for(Subscription sub : push)
    1.96            {
    1.97 -            println("PUSH " + sub.getHost() + ":" + sub.getPort() 
    1.98 +            conn.println("PUSH " + sub.getHost() + ":" + sub.getPort()
    1.99                + " " + sub.getGroup());
   1.100            }
   1.101 -          println(".");
   1.102 +          conn.println(".");
   1.103          }
   1.104          else
   1.105          {
   1.106 -          printStatus(501, "unknown sub command");
   1.107 +          conn.println("401 unknown sub command");
   1.108          }
   1.109        }
   1.110        else if(commands.length == 3 && commands[1].equalsIgnoreCase("DELETE"))
   1.111        {
   1.112 -        Database.getInstance().delete(commands[2]);
   1.113 -        printStatus(200, "article " + commands[2] + " deleted");
   1.114 +        StorageManager.current().delete(commands[2]);
   1.115 +        conn.println("200 article " + commands[2] + " deleted");
   1.116        }
   1.117        else if(commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD"))
   1.118        {
   1.119 -        Database.getInstance().addGroup(commands[2], Integer.parseInt(commands[3]));
   1.120 -        printStatus(200, "group " + commands[2] + " created");
   1.121 +        StorageManager.current().addGroup(commands[2], Integer.parseInt(commands[3]));
   1.122 +        conn.println("200 group " + commands[2] + " created");
   1.123        }
   1.124        else if(commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL"))
   1.125        {
   1.126 -        Group group = Database.getInstance().getGroup(commands[2]);
   1.127 +        Group group = StorageManager.current().getGroup(commands[2]);
   1.128          if(group == null)
   1.129          {
   1.130 -          printStatus(400, "group not found");
   1.131 +          conn.println("400 group not found");
   1.132          }
   1.133          else
   1.134          {
   1.135            group.setFlag(Group.DELETED);
   1.136 -          printStatus(200, "group " + commands[2] + " marked as deleted");
   1.137 +          group.update();
   1.138 +          conn.println("200 group " + commands[2] + " marked as deleted");
   1.139          }
   1.140        }
   1.141        else if(commands.length == 4 && commands[1].equalsIgnoreCase("SET"))
   1.142        {
   1.143          String key = commands[2];
   1.144          String val = commands[3];
   1.145 -        Config.getInstance().set(key, val);
   1.146 -        printStatus(200, "new config value set");
   1.147 +        Config.inst().set(key, val);
   1.148 +        conn.println("200 new config value set");
   1.149        }
   1.150        else if(commands.length == 3 && commands[1].equalsIgnoreCase("GET"))
   1.151        {
   1.152          String key = commands[2];
   1.153 -        String val = Config.getInstance().get(key, null);
   1.154 +        String val = Config.inst().get(key, null);
   1.155          if(val != null)
   1.156          {
   1.157 -          printStatus(200, "config value for " + key + " follows");
   1.158 -          println(val);
   1.159 -          println(".");
   1.160 +          conn.println("100 config value for " + key + " follows");
   1.161 +          conn.println(val);
   1.162 +          conn.println(".");
   1.163          }
   1.164          else
   1.165          {
   1.166 -          printStatus(400, "config value not set");
   1.167 +          conn.println("400 config value not set");
   1.168          }
   1.169        }
   1.170        else if(commands.length >= 3 && commands[1].equalsIgnoreCase("LOG"))
   1.171 @@ -154,83 +162,83 @@
   1.172  
   1.173          if(commands[2].equalsIgnoreCase("CONNECTED_CLIENTS"))
   1.174          {
   1.175 -          printStatus(200, "number of connections follow");
   1.176 -          println(Integer.toString(Stats.getInstance().connectedClients()));
   1.177 -          println(".");
   1.178 +          conn.println("100 number of connections follow");
   1.179 +          conn.println(Integer.toString(Stats.getInstance().connectedClients()));
   1.180 +          conn.println(".");
   1.181          }
   1.182          else if(commands[2].equalsIgnoreCase("POSTED_NEWS"))
   1.183          {
   1.184 -          printStatus(200, "hourly numbers of posted news yesterday");
   1.185 +          conn.println("100 hourly numbers of posted news yesterday");
   1.186            for(int n = 0; n < 24; n++)
   1.187            {
   1.188 -            println(n + " " + Stats.getInstance()
   1.189 +            conn.println(n + " " + Stats.getInstance()
   1.190                .getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
   1.191            }
   1.192 -          println(".");
   1.193 +          conn.println(".");
   1.194          }
   1.195          else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS"))
   1.196          {
   1.197 -          printStatus(200, "hourly numbers of gatewayed news yesterday");
   1.198 +          conn.println("100 hourly numbers of gatewayed news yesterday");
   1.199            for(int n = 0; n < 24; n++)
   1.200            {
   1.201 -            println(n + " " + Stats.getInstance()
   1.202 +            conn.println(n + " " + Stats.getInstance()
   1.203                .getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
   1.204            }
   1.205 -          println(".");
   1.206 +          conn.println(".");
   1.207          }
   1.208          else if(commands[2].equalsIgnoreCase("TRANSMITTED_NEWS"))
   1.209          {
   1.210 -          printStatus(200, "hourly numbers of news transmitted to peers yesterday");
   1.211 +          conn.println("100 hourly numbers of news transmitted to peers yesterday");
   1.212            for(int n = 0; n < 24; n++)
   1.213            {
   1.214 -            println(n + " " + Stats.getInstance()
   1.215 +            conn.println(n + " " + Stats.getInstance()
   1.216                .getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
   1.217            }
   1.218 -          println(".");
   1.219 +          conn.println(".");
   1.220          }
   1.221          else if(commands[2].equalsIgnoreCase("HOSTED_NEWS"))
   1.222          {
   1.223 -          printStatus(200, "number of overall hosted news");
   1.224 -          println(Integer.toString(Stats.getInstance().getNumberOfNews()));
   1.225 -          println(".");
   1.226 +          conn.println("100 number of overall hosted news");
   1.227 +          conn.println(Integer.toString(Stats.getInstance().getNumberOfNews()));
   1.228 +          conn.println(".");
   1.229          }
   1.230          else if(commands[2].equalsIgnoreCase("HOSTED_GROUPS"))
   1.231          {
   1.232 -          printStatus(200, "number of hosted groups");
   1.233 -          println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
   1.234 -          println(".");
   1.235 +          conn.println("100 number of hosted groups");
   1.236 +          conn.println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
   1.237 +          conn.println(".");
   1.238          }
   1.239          else if(commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR"))
   1.240          {
   1.241 -          printStatus(200, "posted news per hour");
   1.242 -          println(Double.toString(Stats.getInstance().postedPerHour(-1)));
   1.243 -          println(".");
   1.244 +          conn.println("100 posted news per hour");
   1.245 +          conn.println(Double.toString(Stats.getInstance().postedPerHour(-1)));
   1.246 +          conn.println(".");
   1.247          }
   1.248          else if(commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR"))
   1.249          {
   1.250 -          printStatus(200, "feeded news per hour");
   1.251 -          println(Double.toString(Stats.getInstance().feededPerHour(-1)));
   1.252 -          println(".");
   1.253 +          conn.println("100 feeded news per hour");
   1.254 +          conn.println(Double.toString(Stats.getInstance().feededPerHour(-1)));
   1.255 +          conn.println(".");
   1.256          }
   1.257          else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR"))
   1.258          {
   1.259 -          printStatus(200, "gatewayed news per hour");
   1.260 -          println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
   1.261 -          println(".");
   1.262 +          conn.println("100 gatewayed news per hour");
   1.263 +          conn.println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
   1.264 +          conn.println(".");
   1.265          }
   1.266          else
   1.267          {
   1.268 -          printStatus(501, "unknown sub command");
   1.269 +          conn.println("401 unknown sub command");
   1.270          }
   1.271        }
   1.272        else
   1.273        {
   1.274 -        printStatus(500, "invalid command usage");
   1.275 +        conn.println("400 invalid command usage");
   1.276        }
   1.277      }
   1.278      else
   1.279      {
   1.280 -      printStatus(500, "not allowed");
   1.281 +      conn.println("501 not allowed");
   1.282      }
   1.283    }
   1.284