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