Skip to content

Commit

Permalink
Channel control
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliot West committed Jan 18, 2011
1 parent 96a3b26 commit 3c5b2cb
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 54 deletions.
58 changes: 58 additions & 0 deletions src/main/java/uk/org/hackspace/ircensus/ChannelControlHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package uk.org.hackspace.ircensus;

public class ChannelControlHandler extends AbstractIrcHandler {

public ChannelControlHandler(IrcClient ircClient) {
super(ircClient);
}

@Override
public String executeCommand(String method, String[] tokens, String sender) {
if ("join".equals(method)) {
return commandJoinChannel(tokens);
} else if ("leave".equals(method)) {
return commandLeaveChannel(tokens);
} else if ("channels".equals(method)) {
return commandChannels();
} else {
return "Unrecognized command: " + method;
}
}

private String commandJoinChannel(String[] tokens) {
if (tokens.length == 1) {
ircClient.joinChannel(tokens[0]);
return "Joined channel: " + tokens[0];
} else if (tokens.length == 2) {
ircClient.joinChannel(tokens[0], tokens[1]);
return "Joined channel: " + tokens[0];
} else {
return "Usage: join <channel> [<password>]";
}
}

private String commandLeaveChannel(String[] tokens) {
if (tokens.length == 1) {
ircClient.partChannel(tokens[0]);
return "Left channel: " + tokens[0];
} else {
return "Usage: leave <channel>";
}
}

private String commandChannels() {
StringBuilder builder = new StringBuilder("Channels: ");
String[] channels = ircClient.getChannels();
boolean first = true;
for (String channel : channels) {
if (first) {
first = false;
} else {
builder.append(", ");
}
builder.append(channel);
}
return builder.toString();
}

}
109 changes: 55 additions & 54 deletions src/main/java/uk/org/hackspace/ircensus/IrcCensus.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,74 +13,75 @@
import uk.org.hackspace.ircensus.http.HttpEndpointHandler;
import uk.org.hackspace.ircensus.stats.ServerStatisticsHandler;


class IrcCensus {

private static final Logger LOG = LoggerFactory.getLogger(IrcCensus.class);
private static final Logger LOG = LoggerFactory.getLogger(IrcCensus.class);

private static final String IRCENSUS_CONFIG_FILE_PROP = "ircensus.config.file";
private static final String IRCENSUS_CONFIG_FILE_PROP = "ircensus.config.file";

private IrcClient ircClient;
private IrcClient ircClient;

private Server server;
private Server server;

void start() throws Exception {
String configFile = System.getProperty(IRCENSUS_CONFIG_FILE_PROP);
if (StringUtils.isEmpty(configFile)) {
throw new IllegalStateException("No configuration file defined in system property '" + IRCENSUS_CONFIG_FILE_PROP
+ "'");
}
void start() throws Exception {
String configFile = System.getProperty(IRCENSUS_CONFIG_FILE_PROP);
if (StringUtils.isEmpty(configFile)) {
throw new IllegalStateException("No configuration file defined in system property '"
+ IRCENSUS_CONFIG_FILE_PROP + "'");
}

XMLConfiguration xml = null;
try {
xml = new XMLConfiguration(configFile);
} catch (ConfigurationException cex) {
throw new IllegalStateException("Configuration error in: " + configFile, cex);
}
XMLConfiguration xml = null;
try {
xml = new XMLConfiguration(configFile);
} catch (ConfigurationException cex) {
throw new IllegalStateException("Configuration error in: " + configFile, cex);
}

Configuration config = new Configuration(xml);

ircClient = createIrcClient(config);
LifecycleHandler defaultHandler = createLifecycleHandler(config, ircClient);
ServerStatisticsHandler statisticsHandler = createStatisticsHandler(config, ircClient);
ChannelControlHandler channelHandler = new ChannelControlHandler(ircClient);

Configuration config = new Configuration(xml);
List<IrcHandler> handlers = new ArrayList<IrcHandler>();
handlers.add(defaultHandler);
handlers.add(statisticsHandler);
handlers.add(channelHandler);

ircClient = createIrcClient(config);
LifecycleHandler defaultHandler = createLifecycleHandler(config, ircClient);
ServerStatisticsHandler statisticsHandler = createStatisticsHandler(config, ircClient);
ircClient.setHandlers(handlers);

List<IrcHandler> handlers = new ArrayList<IrcHandler>();
handlers.add(defaultHandler);
handlers.add(statisticsHandler);
server = new Server(config.getHttpPort());
server.setHandler(new HttpEndpointHandler(statisticsHandler.getStatistics()));

ircClient.setHandlers(handlers);
server.start();
ircClient.start();
}

server = new Server(config.getHttpPort());
server.setHandler(new HttpEndpointHandler(statisticsHandler.getStatistics()));
void stop() {
try {
server.stop();
} catch (Exception e) {
LOG.error("Error shutting down HTTP server", e);
}
ircClient.dispose();
}

server.start();
ircClient.start();
}
private ServerStatisticsHandler createStatisticsHandler(Configuration config, IrcClient ircClient) {
ServerStatisticsHandler statisticsHandler = new ServerStatisticsHandler(ircClient, config.getSamplePeriodMs());
return statisticsHandler;
}

private LifecycleHandler createLifecycleHandler(Configuration config, IrcClient ircClient) {
LifecycleHandler defaultHandler = new LifecycleHandler(ircClient, config.getNickName(), config
.getNickNamePassword(), config.getDefaultChannel(), config.getChannels());
return defaultHandler;
}

void stop() {
try {
server.stop();
} catch (Exception e) {
LOG.error("Error shutting down HTTP server", e);
private IrcClient createIrcClient(Configuration config) throws Exception {
IrcClient ircClient = new IrcClient(config.getNickName(), config.getDefaultChannel(),
config.getServerAddress(), config.getServerPort(), config.getServerPassword());
return ircClient;
}
ircClient.dispose();
}

private ServerStatisticsHandler createStatisticsHandler(Configuration config, IrcClient ircClient) {
ServerStatisticsHandler statisticsHandler = new ServerStatisticsHandler(ircClient, config.getSamplePeriodMs());
return statisticsHandler;
}

private LifecycleHandler createLifecycleHandler(Configuration config, IrcClient ircClient) {
LifecycleHandler defaultHandler = new LifecycleHandler(ircClient, config.getNickName(), config
.getNickNamePassword(), config.getDefaultChannel(), config.getChannels());
return defaultHandler;
}

private IrcClient createIrcClient(Configuration config) throws Exception {
IrcClient ircClient = new IrcClient(config.getNickName(), config.getDefaultChannel(), config.getServerAddress(),
config.getServerPort(), config.getServerPassword());
return ircClient;
}

}

0 comments on commit 3c5b2cb

Please sign in to comment.