Commit 002c2932 authored by Adam Gausmann's avatar Adam Gausmann

Give connections names so they can be easily referenced.

parent 5edfbf50
Pipeline #5022957 passed with stages
in 15 seconds
......@@ -5,6 +5,8 @@ import ninja.nonemu.irc.Message;
public interface Connection {
String getName();
/**
* Sends a custom message to the server.
* @param message The message to send.
......
package ninja.nonemu.samurai.connection;
import java.util.List;
import ninja.nonemu.irc.ConnectionBuilder;
/**
......@@ -11,11 +10,18 @@ import ninja.nonemu.irc.ConnectionBuilder;
* @see Connection
*/
public interface ConnectionManager {
List<Connection> forHost(String hostname);
Connection[] getConnections();
Connection getConnection(String name);
void addConnection(ConnectionBuilder builder);
void removeConnection(Connection connection);
void addConnection(String name, ConnectionBuilder builder);
default void removeConnection(Connection connection) {
removeConnection(connection.getName());
}
void removeConnection(String name);
}
......@@ -49,19 +49,19 @@ public class CommandExecutorImpl {
commandSystem.registerCommand(new CommandInfo(
"quit",
"Gracefully quits from the IRC network.",
"quit [comment]",
"quit [connection]",
true
), this::handleQuit);
commandSystem.registerCommand(new CommandInfo(
"join",
"Requests that the bot join a channel on the network.",
"join <channel>",
"join <channel> [connection]",
true
), this::handleJoin);
commandSystem.registerCommand(new CommandInfo(
"leave",
"Requests that the bot leave a channel on the network.",
"leave <channel> [comment]",
"leave <channel> [connection]",
true
), this::handleLeave);
commandSystem.registerCommand(new CommandInfo(
......@@ -198,13 +198,12 @@ public class CommandExecutorImpl {
sender.sendChat("Console must provide a connection name.");
return true;
}
sender.sendChat("Disconnecting");
((User) sender).getConnection().disconnect();
} else {
for (Connection connection : bot.getConnectionManager().forHost(args[0])) {
connection.disconnect();
}
sender.sendChat("Disconnecting from " + args[0]);
getConnection(args[0]).disconnect();
}
sender.sendChat("Goodbye!");
return true;
}
......@@ -215,14 +214,12 @@ public class CommandExecutorImpl {
sender.sendChat("Console must provide a connection name.");
return true;
}
sender.sendChat("Joining channel " + args[0]);
((User) sender).getConnection().joinChannel(args[0]);
} else {
for (Connection connection : bot.getConnectionManager().forHost(args[1])) {
connection.joinChannel(args[0]);
}
sender.sendChat("Joining channel " + args[0] + " on " + args[1]);
getConnection(args[1]).joinChannel(args[0]);
}
sender.sendChat("Joining channel " + args[0]);
return true;
}
return false;
......@@ -235,14 +232,12 @@ public class CommandExecutorImpl {
sender.sendChat("Console must provide a connection name.");
return true;
}
sender.sendChat("Leaving channel " + args[0]);
((User) sender).getConnection().leaveChannel(args[0]);
} else {
for (Connection connection : bot.getConnectionManager().forHost(args[1])) {
connection.leaveChannel(args[0]);
}
sender.sendChat("Leaving channel " + args[0] + " on " + args[1]);
getConnection(args[1]).leaveChannel(args[0], "Requested by " + sender.getName());
}
sender.sendChat("Leaving channel " + args[0]);
return true;
}
return false;
......@@ -264,4 +259,8 @@ public class CommandExecutorImpl {
}
return false;
}
private Connection getConnection(String name) {
return bot.getConnectionManager().getConnection(name);
}
}
......@@ -10,11 +10,17 @@ import org.apache.logging.log4j.Logger;
public class ConnectionImpl implements Connection {
private final ninja.nonemu.irc.Connection connection;
private final BotImpl bot;
private final String name;
private final Logger logger;
public ConnectionImpl(BotImpl bot, ninja.nonemu.irc.Connection connection) {
this.connection = connection;
this(bot, connection.getHost(), connection);
}
public ConnectionImpl(BotImpl bot, String name, ninja.nonemu.irc.Connection connection) {
this.bot = bot;
this.name = name;
this.connection = connection;
logger = bot.getLogger();
}
......@@ -130,6 +136,11 @@ public class ConnectionImpl implements Connection {
return connection;
}
@Override
public String getName() {
return name;
}
@Override
public void sendMessage(Message message) {
connection.sendMessage(message);
......
package ninja.nonemu.samurai.connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import ninja.nonemu.irc.ConnectionBuilder;
import ninja.nonemu.samurai.BotImpl;
import org.apache.logging.log4j.Logger;
......@@ -9,12 +9,12 @@ import org.apache.logging.log4j.Logger;
public class ConnectionManagerImpl implements ConnectionManager {
private final BotImpl bot;
private final Logger logger;
private final List<ConnectionImpl> connections;
private final Map<String, ConnectionImpl> connections;
public ConnectionManagerImpl(BotImpl bot) {
this.bot = bot;
logger = bot.getLogger();
connections = new ArrayList<>();
connections = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
}
public void init() {
......@@ -22,7 +22,7 @@ public class ConnectionManagerImpl implements ConnectionManager {
}
public void run() {
connections.forEach(ConnectionImpl::run);
connections.values().forEach(ConnectionImpl::run);
}
public void cleanup() {
......@@ -31,19 +31,16 @@ public class ConnectionManagerImpl implements ConnectionManager {
}
@Override
public List<Connection> forHost(String hostname) {
List<Connection> result = new ArrayList<>(connections);
result.removeIf(connection -> !connection.getHost().equalsIgnoreCase(hostname));
public Connection[] getConnections() {
Connection[] result = new Connection[connections.size()];
connections.values().toArray(result);
return result;
}
@Override
public Connection[] getConnections() {
Connection[] result = new Connection[connections.size()];
connections.toArray(result);
return result;
public Connection getConnection(String name) {
return connections.get(name);
}
@Override
......@@ -51,11 +48,21 @@ public class ConnectionManagerImpl implements ConnectionManager {
builder.username(System.getProperty("user.name"));
builder.realname("Samurai IRC Bot");
connections.add(new ConnectionImpl(bot, builder.build()));
ConnectionImpl connection = new ConnectionImpl(bot, builder.build());
connections.put(connection.getName(), connection);
}
@Override
public void removeConnection(Connection connection) {
public void addConnection(String name, ConnectionBuilder builder) {
builder.username(System.getProperty("user.name"));
builder.realname("Samurai IRC Bot");
connections.put(name, new ConnectionImpl(bot, name, builder.build()));
}
@Override
public void removeConnection(String name) {
connections.remove(name);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment