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