src/org/sonews/daemon/command/XDaemonCommand.java
author cli
Sun Aug 29 18:17:37 2010 +0200 (2010-08-29)
changeset 37 74139325d305
parent 35 ed84c8bdd87b
child 39 73b21e9f3958
permissions -rw-r--r--
Switch intent style to Original K&R / Linux / Kernel.
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
cli@37
    46
	@Override
cli@37
    47
	public String[] getSupportedCommandStrings()
cli@37
    48
	{
cli@37
    49
		return new String[] {"XDAEMON"};
cli@37
    50
	}
chris@1
    51
cli@37
    52
	@Override
cli@37
    53
	public boolean hasFinished()
cli@37
    54
	{
cli@37
    55
		return true;
cli@37
    56
	}
chris@1
    57
cli@37
    58
	@Override
cli@37
    59
	public String impliedCapability()
cli@37
    60
	{
cli@37
    61
		return null;
cli@37
    62
	}
cli@20
    63
cli@37
    64
	@Override
cli@37
    65
	public boolean isStateful()
cli@37
    66
	{
cli@37
    67
		return false;
cli@37
    68
	}
chris@3
    69
cli@37
    70
	private void channelAdd(String[] commands, NNTPConnection conn)
cli@37
    71
		throws IOException, StorageBackendException
cli@37
    72
	{
cli@37
    73
		String groupName = commands[2];
cli@37
    74
		if (StorageManager.current().isGroupExisting(groupName)) {
cli@37
    75
			conn.println("400 group " + groupName + " already existing!");
cli@37
    76
		} else {
cli@37
    77
			StorageManager.current().addGroup(groupName, Integer.parseInt(commands[3]));
cli@37
    78
			conn.println("200 group " + groupName + " created");
cli@37
    79
		}
cli@37
    80
	}
cli@23
    81
cli@37
    82
	// TODO: Refactor this method to reduce complexity!
cli@37
    83
	@Override
cli@37
    84
	public void processLine(NNTPConnection conn, String line, byte[] raw)
cli@37
    85
		throws IOException, StorageBackendException
cli@37
    86
	{
cli@37
    87
		InetSocketAddress addr = (InetSocketAddress) conn.getSocketChannel().socket().getRemoteSocketAddress();
cli@37
    88
		if (addr.getHostName().equals(
cli@37
    89
			Config.inst().get(Config.XDAEMON_HOST, "localhost"))) {
cli@37
    90
			String[] commands = line.split(" ", 4);
cli@37
    91
			if (commands.length == 3 && commands[1].equalsIgnoreCase("LIST")) {
cli@37
    92
				if (commands[2].equalsIgnoreCase("CONFIGKEYS")) {
cli@37
    93
					conn.println("100 list of available config keys follows");
cli@37
    94
					for (String key : Config.AVAILABLE_KEYS) {
cli@37
    95
						conn.println(key);
cli@37
    96
					}
cli@37
    97
					conn.println(".");
cli@37
    98
				} else if (commands[2].equalsIgnoreCase("PEERINGRULES")) {
cli@37
    99
					List<Subscription> pull =
cli@37
   100
						StorageManager.current().getSubscriptions(FeedManager.TYPE_PULL);
cli@37
   101
					List<Subscription> push =
cli@37
   102
						StorageManager.current().getSubscriptions(FeedManager.TYPE_PUSH);
cli@37
   103
					conn.println("100 list of peering rules follows");
cli@37
   104
					for (Subscription sub : pull) {
cli@37
   105
						conn.println("PULL " + sub.getHost() + ":" + sub.getPort()
cli@37
   106
							+ " " + sub.getGroup());
cli@37
   107
					}
cli@37
   108
					for (Subscription sub : push) {
cli@37
   109
						conn.println("PUSH " + sub.getHost() + ":" + sub.getPort()
cli@37
   110
							+ " " + sub.getGroup());
cli@37
   111
					}
cli@37
   112
					conn.println(".");
cli@37
   113
				} else {
cli@37
   114
					conn.println("401 unknown sub command");
cli@37
   115
				}
cli@37
   116
			} else if (commands.length == 3 && commands[1].equalsIgnoreCase("DELETE")) {
cli@37
   117
				StorageManager.current().delete(commands[2]);
cli@37
   118
				conn.println("200 article " + commands[2] + " deleted");
cli@37
   119
			} else if (commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD")) {
cli@37
   120
				channelAdd(commands, conn);
cli@37
   121
			} else if (commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL")) {
cli@37
   122
				Group group = StorageManager.current().getGroup(commands[2]);
cli@37
   123
				if (group == null) {
cli@37
   124
					conn.println("400 group not found");
cli@37
   125
				} else {
cli@37
   126
					group.setFlag(Group.DELETED);
cli@37
   127
					group.update();
cli@37
   128
					conn.println("200 group " + commands[2] + " marked as deleted");
cli@37
   129
				}
cli@37
   130
			} else if (commands.length == 4 && commands[1].equalsIgnoreCase("SET")) {
cli@37
   131
				String key = commands[2];
cli@37
   132
				String val = commands[3];
cli@37
   133
				Config.inst().set(key, val);
cli@37
   134
				conn.println("200 new config value set");
cli@37
   135
			} else if (commands.length == 3 && commands[1].equalsIgnoreCase("GET")) {
cli@37
   136
				String key = commands[2];
cli@37
   137
				String val = Config.inst().get(key, null);
cli@37
   138
				if (val != null) {
cli@37
   139
					conn.println("100 config value for " + key + " follows");
cli@37
   140
					conn.println(val);
cli@37
   141
					conn.println(".");
cli@37
   142
				} else {
cli@37
   143
					conn.println("400 config value not set");
cli@37
   144
				}
cli@37
   145
			} else if (commands.length >= 3 && commands[1].equalsIgnoreCase("LOG")) {
cli@37
   146
				Group group = null;
cli@37
   147
				if (commands.length > 3) {
cli@37
   148
					group = (Group) Channel.getByName(commands[3]);
cli@37
   149
				}
chris@1
   150
cli@37
   151
				if (commands[2].equalsIgnoreCase("CONNECTED_CLIENTS")) {
cli@37
   152
					conn.println("100 number of connections follow");
cli@37
   153
					conn.println(Integer.toString(Stats.getInstance().connectedClients()));
cli@37
   154
					conn.println(".");
cli@37
   155
				} else if (commands[2].equalsIgnoreCase("POSTED_NEWS")) {
cli@37
   156
					conn.println("100 hourly numbers of posted news yesterday");
cli@37
   157
					for (int n = 0; n < 24; n++) {
cli@37
   158
						conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
cli@37
   159
					}
cli@37
   160
					conn.println(".");
cli@37
   161
				} else if (commands[2].equalsIgnoreCase("GATEWAYED_NEWS")) {
cli@37
   162
					conn.println("100 hourly numbers of gatewayed news yesterday");
cli@37
   163
					for (int n = 0; n < 24; n++) {
cli@37
   164
						conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
cli@37
   165
					}
cli@37
   166
					conn.println(".");
cli@37
   167
				} else if (commands[2].equalsIgnoreCase("TRANSMITTED_NEWS")) {
cli@37
   168
					conn.println("100 hourly numbers of news transmitted to peers yesterday");
cli@37
   169
					for (int n = 0; n < 24; n++) {
cli@37
   170
						conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
cli@37
   171
					}
cli@37
   172
					conn.println(".");
cli@37
   173
				} else if (commands[2].equalsIgnoreCase("HOSTED_NEWS")) {
cli@37
   174
					conn.println("100 number of overall hosted news");
cli@37
   175
					conn.println(Integer.toString(Stats.getInstance().getNumberOfNews()));
cli@37
   176
					conn.println(".");
cli@37
   177
				} else if (commands[2].equalsIgnoreCase("HOSTED_GROUPS")) {
cli@37
   178
					conn.println("100 number of hosted groups");
cli@37
   179
					conn.println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
cli@37
   180
					conn.println(".");
cli@37
   181
				} else if (commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR")) {
cli@37
   182
					conn.println("100 posted news per hour");
cli@37
   183
					conn.println(Double.toString(Stats.getInstance().postedPerHour(-1)));
cli@37
   184
					conn.println(".");
cli@37
   185
				} else if (commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR")) {
cli@37
   186
					conn.println("100 feeded news per hour");
cli@37
   187
					conn.println(Double.toString(Stats.getInstance().feededPerHour(-1)));
cli@37
   188
					conn.println(".");
cli@37
   189
				} else if (commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR")) {
cli@37
   190
					conn.println("100 gatewayed news per hour");
cli@37
   191
					conn.println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
cli@37
   192
					conn.println(".");
cli@37
   193
				} else {
cli@37
   194
					conn.println("401 unknown sub command");
cli@37
   195
				}
cli@37
   196
			} else if (commands.length >= 3 && commands[1].equalsIgnoreCase("PLUGIN")) {
cli@37
   197
			} else {
cli@37
   198
				conn.println("400 invalid command usage");
cli@37
   199
			}
cli@37
   200
		} else {
cli@37
   201
			conn.println("501 not allowed");
cli@37
   202
		}
cli@37
   203
	}
chris@1
   204
}