src/org/sonews/daemon/command/XDaemonCommand.java
author cli
Sat Sep 10 20:20:19 2011 +0200 (2011-09-10)
changeset 46 28870db3b9fd
parent 39 73b21e9f3958
child 47 e118b4d60029
permissions -rwxr-xr-x
Work on XDaemon command
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@39
   130
			} else if(commands.length == 5 && commands[1].equalsIgnoreCase("GROUPFLAG")) {
cli@39
   131
				Group group = StorageManager.current().getGroup(commands[2]);
cli@39
   132
				String flagName = commands[4];
cli@39
   133
				if(commands[3].equalsIgnoreCase("SET")) {
cli@46
   134
					if(flagName.equals("MAILINGLIST")) {
cli@46
   135
cli@46
   136
					} else if(flagName.equals("DELETED")) {
cli@46
   137
cli@46
   138
					} else if(flagName.equals("READONLY")) {
cli@46
   139
cli@46
   140
					}
cli@39
   141
				} else if(commands[3].equalsIgnoreCase("UNSET")) {
cli@46
   142
					if(flagName.equals("MAILINGLIST")) {
cli@46
   143
cli@46
   144
					} else if(flagName.equals("DELETED")) {
cli@46
   145
cli@46
   146
					} else if(flagName.equals("READONLY")) {
cli@46
   147
cli@46
   148
					}
cli@39
   149
				} else {
cli@39
   150
					conn.println("500 invalid command usage");
cli@39
   151
				}
cli@37
   152
			} else if (commands.length == 4 && commands[1].equalsIgnoreCase("SET")) {
cli@37
   153
				String key = commands[2];
cli@37
   154
				String val = commands[3];
cli@37
   155
				Config.inst().set(key, val);
cli@37
   156
				conn.println("200 new config value set");
cli@37
   157
			} else if (commands.length == 3 && commands[1].equalsIgnoreCase("GET")) {
cli@37
   158
				String key = commands[2];
cli@37
   159
				String val = Config.inst().get(key, null);
cli@37
   160
				if (val != null) {
cli@37
   161
					conn.println("100 config value for " + key + " follows");
cli@37
   162
					conn.println(val);
cli@37
   163
					conn.println(".");
cli@37
   164
				} else {
cli@37
   165
					conn.println("400 config value not set");
cli@37
   166
				}
cli@37
   167
			} else if (commands.length >= 3 && commands[1].equalsIgnoreCase("LOG")) {
cli@37
   168
				Group group = null;
cli@37
   169
				if (commands.length > 3) {
cli@37
   170
					group = (Group) Channel.getByName(commands[3]);
cli@37
   171
				}
chris@1
   172
cli@37
   173
				if (commands[2].equalsIgnoreCase("CONNECTED_CLIENTS")) {
cli@37
   174
					conn.println("100 number of connections follow");
cli@37
   175
					conn.println(Integer.toString(Stats.getInstance().connectedClients()));
cli@37
   176
					conn.println(".");
cli@37
   177
				} else if (commands[2].equalsIgnoreCase("POSTED_NEWS")) {
cli@37
   178
					conn.println("100 hourly numbers of posted news yesterday");
cli@37
   179
					for (int n = 0; n < 24; n++) {
cli@37
   180
						conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
cli@37
   181
					}
cli@37
   182
					conn.println(".");
cli@37
   183
				} else if (commands[2].equalsIgnoreCase("GATEWAYED_NEWS")) {
cli@37
   184
					conn.println("100 hourly numbers of gatewayed news yesterday");
cli@37
   185
					for (int n = 0; n < 24; n++) {
cli@37
   186
						conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
cli@37
   187
					}
cli@37
   188
					conn.println(".");
cli@37
   189
				} else if (commands[2].equalsIgnoreCase("TRANSMITTED_NEWS")) {
cli@37
   190
					conn.println("100 hourly numbers of news transmitted to peers yesterday");
cli@37
   191
					for (int n = 0; n < 24; n++) {
cli@37
   192
						conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
cli@37
   193
					}
cli@37
   194
					conn.println(".");
cli@37
   195
				} else if (commands[2].equalsIgnoreCase("HOSTED_NEWS")) {
cli@37
   196
					conn.println("100 number of overall hosted news");
cli@37
   197
					conn.println(Integer.toString(Stats.getInstance().getNumberOfNews()));
cli@37
   198
					conn.println(".");
cli@37
   199
				} else if (commands[2].equalsIgnoreCase("HOSTED_GROUPS")) {
cli@37
   200
					conn.println("100 number of hosted groups");
cli@37
   201
					conn.println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
cli@37
   202
					conn.println(".");
cli@37
   203
				} else if (commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR")) {
cli@37
   204
					conn.println("100 posted news per hour");
cli@37
   205
					conn.println(Double.toString(Stats.getInstance().postedPerHour(-1)));
cli@37
   206
					conn.println(".");
cli@37
   207
				} else if (commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR")) {
cli@37
   208
					conn.println("100 feeded news per hour");
cli@37
   209
					conn.println(Double.toString(Stats.getInstance().feededPerHour(-1)));
cli@37
   210
					conn.println(".");
cli@37
   211
				} else if (commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR")) {
cli@37
   212
					conn.println("100 gatewayed news per hour");
cli@37
   213
					conn.println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
cli@37
   214
					conn.println(".");
cli@37
   215
				} else {
cli@37
   216
					conn.println("401 unknown sub command");
cli@37
   217
				}
cli@37
   218
			} else if (commands.length >= 3 && commands[1].equalsIgnoreCase("PLUGIN")) {
cli@39
   219
				conn.println("400 invalid command usage");
cli@37
   220
			} else {
cli@37
   221
				conn.println("400 invalid command usage");
cli@37
   222
			}
cli@37
   223
		} else {
cli@37
   224
			conn.println("501 not allowed");
cli@37
   225
		}
cli@37
   226
	}
chris@1
   227
}