org/sonews/daemon/command/XDaemonCommand.java
author cli
Wed, 26 Aug 2009 10:47:51 +0200
changeset 22 2541bdb54cb2
parent 21 4b2c8bedb094
child 23 e4345a26f81f
permissions -rw-r--r--
Not longer required to restart server when changing peering settings (#547).
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;
chris@3
    30
import org.sonews.storage.Group;
chris@1
    31
import org.sonews.util.Stats;
chris@1
    32
chris@1
    33
/**
chris@1
    34
 * The XDAEMON command allows a client to get/set properties of the
chris@1
    35
 * running server daemon. Only locally connected clients are allowed to
chris@1
    36
 * use this command.
chris@1
    37
 * The restriction to localhost connection can be suppressed by overriding
chris@1
    38
 * the sonews.xdaemon.host bootstrap config property.
chris@1
    39
 * @author Christian Lins
chris@1
    40
 * @since sonews/0.5.0
chris@1
    41
 */
chris@3
    42
public class XDaemonCommand implements Command
chris@1
    43
{
chris@3
    44
chris@3
    45
  @Override
chris@3
    46
  public String[] getSupportedCommandStrings()
chris@1
    47
  {
chris@3
    48
    return new String[]{"XDAEMON"};
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@3
    57
  @Override
cli@20
    58
  public String impliedCapability()
cli@20
    59
  {
cli@20
    60
    return null;
cli@20
    61
  }
cli@20
    62
cli@20
    63
  @Override
chris@3
    64
  public boolean isStateful()
chris@3
    65
  {
chris@3
    66
    return false;
chris@3
    67
  }
chris@3
    68
chris@1
    69
  // TODO: Refactor this method to reduce complexity!
chris@1
    70
  @Override
chris@3
    71
  public void processLine(NNTPConnection conn, String line, byte[] raw)
chris@3
    72
    throws IOException, StorageBackendException
chris@1
    73
  {
chris@3
    74
    InetSocketAddress addr = (InetSocketAddress)conn.getSocketChannel().socket()
chris@1
    75
      .getRemoteSocketAddress();
chris@1
    76
    if(addr.getHostName().equals(
chris@3
    77
      Config.inst().get(Config.XDAEMON_HOST, "localhost")))
chris@1
    78
    {
chris@1
    79
      String[] commands = line.split(" ", 4);
chris@1
    80
      if(commands.length == 3 && commands[1].equalsIgnoreCase("LIST"))
chris@1
    81
      {
chris@1
    82
        if(commands[2].equalsIgnoreCase("CONFIGKEYS"))
chris@1
    83
        {
chris@3
    84
          conn.println("100 list of available config keys follows");
chris@1
    85
          for(String key : Config.AVAILABLE_KEYS)
chris@1
    86
          {
chris@3
    87
            conn.println(key);
chris@1
    88
          }
chris@3
    89
          conn.println(".");
chris@1
    90
        }
chris@1
    91
        else if(commands[2].equalsIgnoreCase("PEERINGRULES"))
chris@1
    92
        {
chris@1
    93
          List<Subscription> pull = 
chris@3
    94
            StorageManager.current().getSubscriptions(FeedManager.TYPE_PULL);
chris@1
    95
          List<Subscription> push =
chris@3
    96
            StorageManager.current().getSubscriptions(FeedManager.TYPE_PUSH);
chris@3
    97
          conn.println("100 list of peering rules follows");
chris@1
    98
          for(Subscription sub : pull)
chris@1
    99
          {
chris@3
   100
            conn.println("PULL " + sub.getHost() + ":" + sub.getPort()
chris@1
   101
              + " " + sub.getGroup());
chris@1
   102
          }
chris@1
   103
          for(Subscription sub : push)
chris@1
   104
          {
chris@3
   105
            conn.println("PUSH " + sub.getHost() + ":" + sub.getPort()
chris@1
   106
              + " " + sub.getGroup());
chris@1
   107
          }
chris@3
   108
          conn.println(".");
chris@1
   109
        }
chris@1
   110
        else
chris@1
   111
        {
chris@3
   112
          conn.println("401 unknown sub command");
chris@1
   113
        }
chris@1
   114
      }
chris@1
   115
      else if(commands.length == 3 && commands[1].equalsIgnoreCase("DELETE"))
chris@1
   116
      {
chris@3
   117
        StorageManager.current().delete(commands[2]);
chris@3
   118
        conn.println("200 article " + commands[2] + " deleted");
chris@1
   119
      }
chris@1
   120
      else if(commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD"))
chris@1
   121
      {
chris@3
   122
        StorageManager.current().addGroup(commands[2], Integer.parseInt(commands[3]));
chris@3
   123
        conn.println("200 group " + commands[2] + " created");
chris@1
   124
      }
chris@1
   125
      else if(commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL"))
chris@1
   126
      {
chris@3
   127
        Group group = StorageManager.current().getGroup(commands[2]);
chris@1
   128
        if(group == null)
chris@1
   129
        {
chris@3
   130
          conn.println("400 group not found");
chris@1
   131
        }
chris@1
   132
        else
chris@1
   133
        {
chris@1
   134
          group.setFlag(Group.DELETED);
chris@3
   135
          group.update();
chris@3
   136
          conn.println("200 group " + commands[2] + " marked as deleted");
chris@1
   137
        }
chris@1
   138
      }
chris@1
   139
      else if(commands.length == 4 && commands[1].equalsIgnoreCase("SET"))
chris@1
   140
      {
chris@1
   141
        String key = commands[2];
chris@1
   142
        String val = commands[3];
chris@3
   143
        Config.inst().set(key, val);
chris@3
   144
        conn.println("200 new config value set");
chris@1
   145
      }
chris@1
   146
      else if(commands.length == 3 && commands[1].equalsIgnoreCase("GET"))
chris@1
   147
      {
chris@1
   148
        String key = commands[2];
chris@3
   149
        String val = Config.inst().get(key, null);
chris@1
   150
        if(val != null)
chris@1
   151
        {
chris@3
   152
          conn.println("100 config value for " + key + " follows");
chris@3
   153
          conn.println(val);
chris@3
   154
          conn.println(".");
chris@1
   155
        }
chris@1
   156
        else
chris@1
   157
        {
chris@3
   158
          conn.println("400 config value not set");
chris@1
   159
        }
chris@1
   160
      }
chris@1
   161
      else if(commands.length >= 3 && commands[1].equalsIgnoreCase("LOG"))
chris@1
   162
      {
chris@1
   163
        Group group = null;
chris@1
   164
        if(commands.length > 3)
chris@1
   165
        {
chris@1
   166
          group = Group.getByName(commands[3]);
chris@1
   167
        }
chris@1
   168
chris@1
   169
        if(commands[2].equalsIgnoreCase("CONNECTED_CLIENTS"))
chris@1
   170
        {
chris@3
   171
          conn.println("100 number of connections follow");
chris@3
   172
          conn.println(Integer.toString(Stats.getInstance().connectedClients()));
chris@3
   173
          conn.println(".");
chris@1
   174
        }
chris@1
   175
        else if(commands[2].equalsIgnoreCase("POSTED_NEWS"))
chris@1
   176
        {
chris@3
   177
          conn.println("100 hourly numbers of posted news yesterday");
chris@1
   178
          for(int n = 0; n < 24; n++)
chris@1
   179
          {
chris@3
   180
            conn.println(n + " " + Stats.getInstance()
chris@1
   181
              .getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
chris@1
   182
          }
chris@3
   183
          conn.println(".");
chris@1
   184
        }
chris@1
   185
        else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS"))
chris@1
   186
        {
chris@3
   187
          conn.println("100 hourly numbers of gatewayed news yesterday");
chris@1
   188
          for(int n = 0; n < 24; n++)
chris@1
   189
          {
chris@3
   190
            conn.println(n + " " + Stats.getInstance()
chris@1
   191
              .getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
chris@1
   192
          }
chris@3
   193
          conn.println(".");
chris@1
   194
        }
chris@1
   195
        else if(commands[2].equalsIgnoreCase("TRANSMITTED_NEWS"))
chris@1
   196
        {
chris@3
   197
          conn.println("100 hourly numbers of news transmitted to peers yesterday");
chris@1
   198
          for(int n = 0; n < 24; n++)
chris@1
   199
          {
chris@3
   200
            conn.println(n + " " + Stats.getInstance()
chris@1
   201
              .getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
chris@1
   202
          }
chris@3
   203
          conn.println(".");
chris@1
   204
        }
chris@1
   205
        else if(commands[2].equalsIgnoreCase("HOSTED_NEWS"))
chris@1
   206
        {
chris@3
   207
          conn.println("100 number of overall hosted news");
chris@3
   208
          conn.println(Integer.toString(Stats.getInstance().getNumberOfNews()));
chris@3
   209
          conn.println(".");
chris@1
   210
        }
chris@1
   211
        else if(commands[2].equalsIgnoreCase("HOSTED_GROUPS"))
chris@1
   212
        {
chris@3
   213
          conn.println("100 number of hosted groups");
chris@3
   214
          conn.println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
chris@3
   215
          conn.println(".");
chris@1
   216
        }
chris@1
   217
        else if(commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR"))
chris@1
   218
        {
chris@3
   219
          conn.println("100 posted news per hour");
chris@3
   220
          conn.println(Double.toString(Stats.getInstance().postedPerHour(-1)));
chris@3
   221
          conn.println(".");
chris@1
   222
        }
chris@1
   223
        else if(commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR"))
chris@1
   224
        {
chris@3
   225
          conn.println("100 feeded news per hour");
chris@3
   226
          conn.println(Double.toString(Stats.getInstance().feededPerHour(-1)));
chris@3
   227
          conn.println(".");
chris@1
   228
        }
chris@1
   229
        else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR"))
chris@1
   230
        {
chris@3
   231
          conn.println("100 gatewayed news per hour");
chris@3
   232
          conn.println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
chris@3
   233
          conn.println(".");
chris@1
   234
        }
chris@1
   235
        else
chris@1
   236
        {
chris@3
   237
          conn.println("401 unknown sub command");
chris@1
   238
        }
chris@1
   239
      }
cli@21
   240
      else if(commands.length >= 3 && commands[1].equalsIgnoreCase("PLUGIN"))
cli@21
   241
      {
cli@21
   242
        
cli@21
   243
      }
chris@1
   244
      else
chris@1
   245
      {
chris@3
   246
        conn.println("400 invalid command usage");
chris@1
   247
      }
chris@1
   248
    }
chris@1
   249
    else
chris@1
   250
    {
chris@3
   251
      conn.println("501 not allowed");
chris@1
   252
    }
chris@1
   253
  }
chris@1
   254
  
chris@1
   255
}