org/sonews/daemon/command/XDaemonCommand.java
author chris <chris@marvin>
Wed, 01 Jul 2009 10:48:22 +0200
changeset 2 1090e2141798
parent 1 6fceb66e1ad7
child 3 2fdc9cc89502
permissions -rw-r--r--
sonews/0.5.1 fixes merged
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
chris@1
    19
package org.sonews.daemon.command;
chris@1
    20
chris@1
    21
import java.io.IOException;
chris@1
    22
import java.net.InetSocketAddress;
chris@1
    23
import java.sql.SQLException;
chris@1
    24
import java.util.List;
chris@1
    25
import org.sonews.daemon.BootstrapConfig;
chris@1
    26
import org.sonews.daemon.Config;
chris@1
    27
import org.sonews.daemon.NNTPConnection;
chris@1
    28
import org.sonews.daemon.storage.Database;
chris@1
    29
import org.sonews.daemon.storage.Group;
chris@1
    30
import org.sonews.feed.FeedManager;
chris@1
    31
import org.sonews.feed.Subscription;
chris@1
    32
import org.sonews.util.Stats;
chris@1
    33
chris@1
    34
/**
chris@1
    35
 * The XDAEMON command allows a client to get/set properties of the
chris@1
    36
 * running server daemon. Only locally connected clients are allowed to
chris@1
    37
 * use this command.
chris@1
    38
 * The restriction to localhost connection can be suppressed by overriding
chris@1
    39
 * the sonews.xdaemon.host bootstrap config property.
chris@1
    40
 * @author Christian Lins
chris@1
    41
 * @since sonews/0.5.0
chris@1
    42
 */
chris@1
    43
public class XDaemonCommand extends AbstractCommand
chris@1
    44
{
chris@1
    45
  
chris@1
    46
  public XDaemonCommand(NNTPConnection conn)
chris@1
    47
  {
chris@1
    48
    super(conn);
chris@1
    49
  }
chris@1
    50
chris@1
    51
  @Override
chris@1
    52
  public boolean hasFinished()
chris@1
    53
  {
chris@1
    54
    return true;
chris@1
    55
  }
chris@1
    56
chris@1
    57
  // TODO: Refactor this method to reduce complexity!
chris@1
    58
  @Override
chris@1
    59
  public void processLine(String line) throws IOException, SQLException
chris@1
    60
  {
chris@1
    61
    InetSocketAddress addr = (InetSocketAddress)connection.getChannel().socket()
chris@1
    62
      .getRemoteSocketAddress();
chris@1
    63
    if(addr.getHostName().equals(
chris@1
    64
      BootstrapConfig.getInstance().get(BootstrapConfig.XDAEMON_HOST, "localhost")))
chris@1
    65
    {
chris@1
    66
      String[] commands = line.split(" ", 4);
chris@1
    67
      if(commands.length == 3 && commands[1].equalsIgnoreCase("LIST"))
chris@1
    68
      {
chris@1
    69
        if(commands[2].equalsIgnoreCase("CONFIGKEYS"))
chris@1
    70
        {
chris@1
    71
          printStatus(200, "list of available config keys follows");
chris@1
    72
          for(String key : Config.AVAILABLE_KEYS)
chris@1
    73
          {
chris@1
    74
            println(key);
chris@1
    75
          }
chris@1
    76
          println(".");
chris@1
    77
        }
chris@1
    78
        else if(commands[2].equalsIgnoreCase("PEERINGRULES"))
chris@1
    79
        {
chris@1
    80
          List<Subscription> pull = 
chris@1
    81
            Database.getInstance().getSubscriptions(FeedManager.TYPE_PULL);
chris@1
    82
          List<Subscription> push =
chris@1
    83
            Database.getInstance().getSubscriptions(FeedManager.TYPE_PUSH);
chris@1
    84
          printStatus(200,"list of peering rules follows");
chris@1
    85
          for(Subscription sub : pull)
chris@1
    86
          {
chris@1
    87
            println("PULL " + sub.getHost() + ":" + sub.getPort() 
chris@1
    88
              + " " + sub.getGroup());
chris@1
    89
          }
chris@1
    90
          for(Subscription sub : push)
chris@1
    91
          {
chris@1
    92
            println("PUSH " + sub.getHost() + ":" + sub.getPort() 
chris@1
    93
              + " " + sub.getGroup());
chris@1
    94
          }
chris@1
    95
          println(".");
chris@1
    96
        }
chris@1
    97
        else
chris@1
    98
        {
chris@1
    99
          printStatus(501, "unknown sub command");
chris@1
   100
        }
chris@1
   101
      }
chris@1
   102
      else if(commands.length == 3 && commands[1].equalsIgnoreCase("DELETE"))
chris@1
   103
      {
chris@1
   104
        Database.getInstance().delete(commands[2]);
chris@1
   105
        printStatus(200, "article " + commands[2] + " deleted");
chris@1
   106
      }
chris@1
   107
      else if(commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD"))
chris@1
   108
      {
chris@1
   109
        Database.getInstance().addGroup(commands[2], Integer.parseInt(commands[3]));
chris@1
   110
        printStatus(200, "group " + commands[2] + " created");
chris@1
   111
      }
chris@1
   112
      else if(commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL"))
chris@1
   113
      {
chris@1
   114
        Group group = Database.getInstance().getGroup(commands[2]);
chris@1
   115
        if(group == null)
chris@1
   116
        {
chris@1
   117
          printStatus(400, "group not found");
chris@1
   118
        }
chris@1
   119
        else
chris@1
   120
        {
chris@1
   121
          group.setFlag(Group.DELETED);
chris@1
   122
          printStatus(200, "group " + commands[2] + " marked as deleted");
chris@1
   123
        }
chris@1
   124
      }
chris@1
   125
      else if(commands.length == 4 && commands[1].equalsIgnoreCase("SET"))
chris@1
   126
      {
chris@1
   127
        String key = commands[2];
chris@1
   128
        String val = commands[3];
chris@1
   129
        Config.getInstance().set(key, val);
chris@1
   130
        printStatus(200, "new config value set");
chris@1
   131
      }
chris@1
   132
      else if(commands.length == 3 && commands[1].equalsIgnoreCase("GET"))
chris@1
   133
      {
chris@1
   134
        String key = commands[2];
chris@1
   135
        String val = Config.getInstance().get(key, null);
chris@1
   136
        if(val != null)
chris@1
   137
        {
chris@1
   138
          printStatus(200, "config value for " + key + " follows");
chris@1
   139
          println(val);
chris@1
   140
          println(".");
chris@1
   141
        }
chris@1
   142
        else
chris@1
   143
        {
chris@1
   144
          printStatus(400, "config value not set");
chris@1
   145
        }
chris@1
   146
      }
chris@1
   147
      else if(commands.length >= 3 && commands[1].equalsIgnoreCase("LOG"))
chris@1
   148
      {
chris@1
   149
        Group group = null;
chris@1
   150
        if(commands.length > 3)
chris@1
   151
        {
chris@1
   152
          group = Group.getByName(commands[3]);
chris@1
   153
        }
chris@1
   154
chris@1
   155
        if(commands[2].equalsIgnoreCase("CONNECTED_CLIENTS"))
chris@1
   156
        {
chris@1
   157
          printStatus(200, "number of connections follow");
chris@1
   158
          println(Integer.toString(Stats.getInstance().connectedClients()));
chris@1
   159
          println(".");
chris@1
   160
        }
chris@1
   161
        else if(commands[2].equalsIgnoreCase("POSTED_NEWS"))
chris@1
   162
        {
chris@1
   163
          printStatus(200, "hourly numbers of posted news yesterday");
chris@1
   164
          for(int n = 0; n < 24; n++)
chris@1
   165
          {
chris@1
   166
            println(n + " " + Stats.getInstance()
chris@1
   167
              .getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
chris@1
   168
          }
chris@1
   169
          println(".");
chris@1
   170
        }
chris@1
   171
        else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS"))
chris@1
   172
        {
chris@1
   173
          printStatus(200, "hourly numbers of gatewayed news yesterday");
chris@1
   174
          for(int n = 0; n < 24; n++)
chris@1
   175
          {
chris@1
   176
            println(n + " " + Stats.getInstance()
chris@1
   177
              .getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
chris@1
   178
          }
chris@1
   179
          println(".");
chris@1
   180
        }
chris@1
   181
        else if(commands[2].equalsIgnoreCase("TRANSMITTED_NEWS"))
chris@1
   182
        {
chris@1
   183
          printStatus(200, "hourly numbers of news transmitted to peers yesterday");
chris@1
   184
          for(int n = 0; n < 24; n++)
chris@1
   185
          {
chris@1
   186
            println(n + " " + Stats.getInstance()
chris@1
   187
              .getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
chris@1
   188
          }
chris@1
   189
          println(".");
chris@1
   190
        }
chris@1
   191
        else if(commands[2].equalsIgnoreCase("HOSTED_NEWS"))
chris@1
   192
        {
chris@1
   193
          printStatus(200, "number of overall hosted news");
chris@1
   194
          println(Integer.toString(Stats.getInstance().getNumberOfNews()));
chris@1
   195
          println(".");
chris@1
   196
        }
chris@1
   197
        else if(commands[2].equalsIgnoreCase("HOSTED_GROUPS"))
chris@1
   198
        {
chris@1
   199
          printStatus(200, "number of hosted groups");
chris@1
   200
          println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
chris@1
   201
          println(".");
chris@1
   202
        }
chris@1
   203
        else if(commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR"))
chris@1
   204
        {
chris@1
   205
          printStatus(200, "posted news per hour");
chris@1
   206
          println(Double.toString(Stats.getInstance().postedPerHour(-1)));
chris@1
   207
          println(".");
chris@1
   208
        }
chris@1
   209
        else if(commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR"))
chris@1
   210
        {
chris@1
   211
          printStatus(200, "feeded news per hour");
chris@1
   212
          println(Double.toString(Stats.getInstance().feededPerHour(-1)));
chris@1
   213
          println(".");
chris@1
   214
        }
chris@1
   215
        else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR"))
chris@1
   216
        {
chris@1
   217
          printStatus(200, "gatewayed news per hour");
chris@1
   218
          println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
chris@1
   219
          println(".");
chris@1
   220
        }
chris@1
   221
        else
chris@1
   222
        {
chris@1
   223
          printStatus(501, "unknown sub command");
chris@1
   224
        }
chris@1
   225
      }
chris@1
   226
      else
chris@1
   227
      {
chris@1
   228
        printStatus(500, "invalid command usage");
chris@1
   229
      }
chris@1
   230
    }
chris@1
   231
    else
chris@1
   232
    {
chris@1
   233
      printStatus(500, "not allowed");
chris@1
   234
    }
chris@1
   235
  }
chris@1
   236
  
chris@1
   237
}