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