org/sonews/daemon/command/XDaemonCommand.java
author cli
Wed, 12 May 2010 11:18:02 +0200
changeset 31 087ef6fe6a1a
parent 23 e4345a26f81f
permissions -rw-r--r--
Group.getByName() removed. Channel.getByName() does no longer catch StorageBackendExceptions but throw them further.
     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.util.List;
    24 import org.sonews.config.Config;
    25 import org.sonews.daemon.NNTPConnection;
    26 import org.sonews.storage.StorageBackendException;
    27 import org.sonews.storage.StorageManager;
    28 import org.sonews.feed.FeedManager;
    29 import org.sonews.feed.Subscription;
    30 import org.sonews.storage.Channel;
    31 import org.sonews.storage.Group;
    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 implements Command
    44 {
    45 
    46   @Override
    47   public String[] getSupportedCommandStrings()
    48   {
    49     return new String[]{"XDAEMON"};
    50   }
    51 
    52   @Override
    53   public boolean hasFinished()
    54   {
    55     return true;
    56   }
    57 
    58   @Override
    59   public String impliedCapability()
    60   {
    61     return null;
    62   }
    63 
    64   @Override
    65   public boolean isStateful()
    66   {
    67     return false;
    68   }
    69 
    70   private void channelAdd(String[] commands, NNTPConnection conn)
    71     throws IOException, StorageBackendException
    72   {
    73     String groupName = commands[2];
    74     if(StorageManager.current().isGroupExisting(groupName))
    75     {
    76       conn.println("400 group " + groupName + " already existing!");
    77     }
    78     else
    79     {
    80       StorageManager.current().addGroup(groupName, Integer.parseInt(commands[3]));
    81       conn.println("200 group " + groupName + " created");
    82     }
    83   }
    84 
    85   // TODO: Refactor this method to reduce complexity!
    86   @Override
    87   public void processLine(NNTPConnection conn, String line, byte[] raw)
    88     throws IOException, StorageBackendException
    89   {
    90     InetSocketAddress addr = (InetSocketAddress)conn.getSocketChannel().socket()
    91       .getRemoteSocketAddress();
    92     if(addr.getHostName().equals(
    93       Config.inst().get(Config.XDAEMON_HOST, "localhost")))
    94     {
    95       String[] commands = line.split(" ", 4);
    96       if(commands.length == 3 && commands[1].equalsIgnoreCase("LIST"))
    97       {
    98         if(commands[2].equalsIgnoreCase("CONFIGKEYS"))
    99         {
   100           conn.println("100 list of available config keys follows");
   101           for(String key : Config.AVAILABLE_KEYS)
   102           {
   103             conn.println(key);
   104           }
   105           conn.println(".");
   106         }
   107         else if(commands[2].equalsIgnoreCase("PEERINGRULES"))
   108         {
   109           List<Subscription> pull = 
   110             StorageManager.current().getSubscriptions(FeedManager.TYPE_PULL);
   111           List<Subscription> push =
   112             StorageManager.current().getSubscriptions(FeedManager.TYPE_PUSH);
   113           conn.println("100 list of peering rules follows");
   114           for(Subscription sub : pull)
   115           {
   116             conn.println("PULL " + sub.getHost() + ":" + sub.getPort()
   117               + " " + sub.getGroup());
   118           }
   119           for(Subscription sub : push)
   120           {
   121             conn.println("PUSH " + sub.getHost() + ":" + sub.getPort()
   122               + " " + sub.getGroup());
   123           }
   124           conn.println(".");
   125         }
   126         else
   127         {
   128           conn.println("401 unknown sub command");
   129         }
   130       }
   131       else if(commands.length == 3 && commands[1].equalsIgnoreCase("DELETE"))
   132       {
   133         StorageManager.current().delete(commands[2]);
   134         conn.println("200 article " + commands[2] + " deleted");
   135       }
   136       else if(commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD"))
   137       {
   138         channelAdd(commands, conn);
   139       }
   140       else if(commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL"))
   141       {
   142         Group group = StorageManager.current().getGroup(commands[2]);
   143         if(group == null)
   144         {
   145           conn.println("400 group not found");
   146         }
   147         else
   148         {
   149           group.setFlag(Group.DELETED);
   150           group.update();
   151           conn.println("200 group " + commands[2] + " marked as deleted");
   152         }
   153       }
   154       else if(commands.length == 4 && commands[1].equalsIgnoreCase("SET"))
   155       {
   156         String key = commands[2];
   157         String val = commands[3];
   158         Config.inst().set(key, val);
   159         conn.println("200 new config value set");
   160       }
   161       else if(commands.length == 3 && commands[1].equalsIgnoreCase("GET"))
   162       {
   163         String key = commands[2];
   164         String val = Config.inst().get(key, null);
   165         if(val != null)
   166         {
   167           conn.println("100 config value for " + key + " follows");
   168           conn.println(val);
   169           conn.println(".");
   170         }
   171         else
   172         {
   173           conn.println("400 config value not set");
   174         }
   175       }
   176       else if(commands.length >= 3 && commands[1].equalsIgnoreCase("LOG"))
   177       {
   178         Group group = null;
   179         if(commands.length > 3)
   180         {
   181           group = (Group)Channel.getByName(commands[3]);
   182         }
   183 
   184         if(commands[2].equalsIgnoreCase("CONNECTED_CLIENTS"))
   185         {
   186           conn.println("100 number of connections follow");
   187           conn.println(Integer.toString(Stats.getInstance().connectedClients()));
   188           conn.println(".");
   189         }
   190         else if(commands[2].equalsIgnoreCase("POSTED_NEWS"))
   191         {
   192           conn.println("100 hourly numbers of posted news yesterday");
   193           for(int n = 0; n < 24; n++)
   194           {
   195             conn.println(n + " " + Stats.getInstance()
   196               .getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
   197           }
   198           conn.println(".");
   199         }
   200         else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS"))
   201         {
   202           conn.println("100 hourly numbers of gatewayed news yesterday");
   203           for(int n = 0; n < 24; n++)
   204           {
   205             conn.println(n + " " + Stats.getInstance()
   206               .getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
   207           }
   208           conn.println(".");
   209         }
   210         else if(commands[2].equalsIgnoreCase("TRANSMITTED_NEWS"))
   211         {
   212           conn.println("100 hourly numbers of news transmitted to peers yesterday");
   213           for(int n = 0; n < 24; n++)
   214           {
   215             conn.println(n + " " + Stats.getInstance()
   216               .getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
   217           }
   218           conn.println(".");
   219         }
   220         else if(commands[2].equalsIgnoreCase("HOSTED_NEWS"))
   221         {
   222           conn.println("100 number of overall hosted news");
   223           conn.println(Integer.toString(Stats.getInstance().getNumberOfNews()));
   224           conn.println(".");
   225         }
   226         else if(commands[2].equalsIgnoreCase("HOSTED_GROUPS"))
   227         {
   228           conn.println("100 number of hosted groups");
   229           conn.println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
   230           conn.println(".");
   231         }
   232         else if(commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR"))
   233         {
   234           conn.println("100 posted news per hour");
   235           conn.println(Double.toString(Stats.getInstance().postedPerHour(-1)));
   236           conn.println(".");
   237         }
   238         else if(commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR"))
   239         {
   240           conn.println("100 feeded news per hour");
   241           conn.println(Double.toString(Stats.getInstance().feededPerHour(-1)));
   242           conn.println(".");
   243         }
   244         else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR"))
   245         {
   246           conn.println("100 gatewayed news per hour");
   247           conn.println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
   248           conn.println(".");
   249         }
   250         else
   251         {
   252           conn.println("401 unknown sub command");
   253         }
   254       }
   255       else if(commands.length >= 3 && commands[1].equalsIgnoreCase("PLUGIN"))
   256       {
   257         
   258       }
   259       else
   260       {
   261         conn.println("400 invalid command usage");
   262       }
   263     }
   264     else
   265     {
   266       conn.println("501 not allowed");
   267     }
   268   }
   269   
   270 }