Add more net code
This commit is contained in:
@@ -30,9 +30,9 @@ public class ScriptProcessor {
|
|||||||
private Process runningProcess = null;
|
private Process runningProcess = null;
|
||||||
private NetworkHandler netHandler;
|
private NetworkHandler netHandler;
|
||||||
private Random random = new Random();
|
private Random random = new Random();
|
||||||
|
private Thread executionThread = null;
|
||||||
|
|
||||||
public ScriptProcessor(NetworkHandler netHandler, Properties properties)
|
public ScriptProcessor(NetworkHandler netHandler, Properties properties) {
|
||||||
{
|
|
||||||
this.netHandler = netHandler;
|
this.netHandler = netHandler;
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
}
|
}
|
||||||
@@ -191,8 +191,7 @@ public class ScriptProcessor {
|
|||||||
return tempFile;
|
return tempFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int beginProcess(String... args) throws ScriptProcessingException
|
public int beginProcess(String... args) throws ScriptProcessingException {
|
||||||
{
|
|
||||||
ProcessBuilder processBuilder = new ProcessBuilder(args);
|
ProcessBuilder processBuilder = new ProcessBuilder(args);
|
||||||
Map<String, String> environment = processBuilder.environment();
|
Map<String, String> environment = processBuilder.environment();
|
||||||
|
|
||||||
@@ -222,7 +221,6 @@ public class ScriptProcessor {
|
|||||||
throw new ScriptProcessingException("Process returned exit code " + returnCode);
|
throw new ScriptProcessingException("Process returned exit code " + returnCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void pythonProcessor(Element e) throws ScriptProcessingException {
|
private void pythonProcessor(Element e) throws ScriptProcessingException {
|
||||||
File file = writeScriptToTempFile(e.getTextContent(), ".py");
|
File file = writeScriptToTempFile(e.getTextContent(), ".py");
|
||||||
int returnCode = beginProcess(properties.getProperty("python", "python3"), file.getAbsolutePath());
|
int returnCode = beginProcess(properties.getProperty("python", "python3"), file.getAbsolutePath());
|
||||||
@@ -249,4 +247,28 @@ public class ScriptProcessor {
|
|||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void processAsync(final String script) {
|
||||||
|
if (executionThread == null)
|
||||||
|
{
|
||||||
|
executionThread = new Thread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
processScript(script);
|
||||||
|
}
|
||||||
|
catch (ScriptProcessingException e)
|
||||||
|
{
|
||||||
|
//TODO Disconnect
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
executionThread = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
executionThread.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.flaremicro.crossjeeves.net;
|
package com.flaremicro.crossjeeves.net;
|
||||||
|
|
||||||
|
import static com.flaremicro.crossjeeves.net.ErrorCodes.FILE_DOWNLOAD_FAILURE;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
@@ -12,8 +14,9 @@ import com.flaremicro.crossjeeves.net.packet.Packet127KeepAlive;
|
|||||||
import com.flaremicro.crossjeeves.net.packet.Packet1Status;
|
import com.flaremicro.crossjeeves.net.packet.Packet1Status;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet2Script;
|
import com.flaremicro.crossjeeves.net.packet.Packet2Script;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
|
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
|
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
|
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
|
||||||
|
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
|
||||||
|
import com.flaremicro.crossjeeves.net.packet.Packet6Disconnect;
|
||||||
import com.flaremicro.util.Util;
|
import com.flaremicro.util.Util;
|
||||||
import com.flaremicro.util.ZipUtils;
|
import com.flaremicro.util.ZipUtils;
|
||||||
|
|
||||||
@@ -87,8 +90,15 @@ public class ClientHandler extends NetworkHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlePacket(Packet4FileData packet) {
|
public void handlePacket(Packet4FileData packet) {
|
||||||
// TODO Auto-generated method stub
|
try
|
||||||
|
{
|
||||||
|
this.appendFile(packet.getFileId(), packet.getFileChunk());
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
disconnect(FILE_DOWNLOAD_FAILURE, "Failed to download transferred file");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -103,4 +113,10 @@ public class ClientHandler extends NetworkHandler {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handlePacket(Packet6Disconnect packet) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package com.flaremicro.crossjeeves.net;
|
package com.flaremicro.crossjeeves.net;
|
||||||
|
|
||||||
|
import java.io.BufferedOutputStream;
|
||||||
|
import java.io.Closeable;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
@@ -17,15 +21,18 @@ import com.flaremicro.crossjeeves.net.packet.Packet2Script;
|
|||||||
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
|
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
|
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
|
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
|
||||||
|
import com.flaremicro.crossjeeves.net.packet.Packet6Disconnect;
|
||||||
import com.flaremicro.util.Util;
|
import com.flaremicro.util.Util;
|
||||||
|
|
||||||
|
import static com.flaremicro.crossjeeves.net.ErrorCodes.*;
|
||||||
|
|
||||||
public abstract class NetworkHandler {
|
public abstract class NetworkHandler {
|
||||||
private boolean connected;
|
private boolean connected;
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private DataInputStream in;
|
private DataInputStream in;
|
||||||
private DataOutputStream out;
|
private DataOutputStream out;
|
||||||
private Thread readThread;
|
private Thread readThread;
|
||||||
private HashMap<Long, File> downloadQueue = new HashMap<Long, File>();
|
private HashMap<Long, FileTransferInfo> downloadQueue = new HashMap<Long, FileTransferInfo>();
|
||||||
private HashMap<Long, File> downloadComplete = new HashMap<Long, File>();
|
private HashMap<Long, File> downloadComplete = new HashMap<Long, File>();
|
||||||
|
|
||||||
private BlockingQueue<Packet> outbox = new LinkedBlockingQueue<Packet>();
|
private BlockingQueue<Packet> outbox = new LinkedBlockingQueue<Packet>();
|
||||||
@@ -36,8 +43,7 @@ public abstract class NetworkHandler {
|
|||||||
this.out = new DataOutputStream(socket.getOutputStream());
|
this.out = new DataOutputStream(socket.getOutputStream());
|
||||||
}
|
}
|
||||||
|
|
||||||
public File waitForFile(long fileId)
|
public File waitForFile(long fileId) {
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
@@ -67,8 +73,12 @@ public abstract class NetworkHandler {
|
|||||||
packet.processPacket(this);
|
packet.processPacket(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO error code?
|
public void disconnect(int code, String message) {
|
||||||
protected void disconnect() {
|
System.out.println("Disconnect code " + code + ": " + message);
|
||||||
|
doDisconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void doDisconnect() {
|
||||||
connected = false;
|
connected = false;
|
||||||
readThread.interrupt();
|
readThread.interrupt();
|
||||||
Util.cleanClose(in);
|
Util.cleanClose(in);
|
||||||
@@ -93,7 +103,7 @@ public abstract class NetworkHandler {
|
|||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
disconnect();
|
disconnect(READ_FAILED, e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,14 +124,14 @@ public abstract class NetworkHandler {
|
|||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
disconnect();
|
disconnect(WRITE_FAILED, e.toString());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (InterruptedException e)
|
catch (InterruptedException e)
|
||||||
{
|
{
|
||||||
disconnect();
|
disconnect(THREAD_INTERRUPTED, e.toString());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -144,6 +154,74 @@ public abstract class NetworkHandler {
|
|||||||
|
|
||||||
public abstract void handlePacket(Packet5Artifact packet);
|
public abstract void handlePacket(Packet5Artifact packet);
|
||||||
|
|
||||||
|
public abstract void handlePacket(Packet6Disconnect packet);
|
||||||
|
|
||||||
public abstract void handlePacket(Packet127KeepAlive packet);
|
public abstract void handlePacket(Packet127KeepAlive packet);
|
||||||
|
|
||||||
|
public void clearFile(long fileId)
|
||||||
|
{
|
||||||
|
FileTransferInfo fileTransferInfo = downloadQueue.get(fileId);
|
||||||
|
if(fileTransferInfo != null)
|
||||||
|
{
|
||||||
|
Util.cleanClose(fileTransferInfo);
|
||||||
|
fileTransferInfo.file.delete();
|
||||||
|
}
|
||||||
|
File file = downloadComplete.get(fileId);
|
||||||
|
if(file != null)
|
||||||
|
{
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void beginFile(long fileId, long expectedSize) throws IOException
|
||||||
|
{
|
||||||
|
File file = File.createTempFile(fileId+"-cj-tmp", ".zip");
|
||||||
|
file.deleteOnExit();
|
||||||
|
OutputStream is = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
is = new BufferedOutputStream(new FileOutputStream(file));
|
||||||
|
FileTransferInfo fti = new FileTransferInfo(file, is, expectedSize);
|
||||||
|
this.downloadQueue.put(fileId, fti);
|
||||||
|
}
|
||||||
|
catch(IOException ex)
|
||||||
|
{
|
||||||
|
Util.cleanClose(is);
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean appendFile(long fileId, byte[] chunk) throws IOException
|
||||||
|
{
|
||||||
|
FileTransferInfo fileTransferInfo = downloadQueue.get(fileId);
|
||||||
|
if(fileTransferInfo == null)
|
||||||
|
return false;
|
||||||
|
if(chunk.length == 0)
|
||||||
|
{
|
||||||
|
Util.cleanClose(fileTransferInfo);
|
||||||
|
this.downloadComplete.put(fileId, fileTransferInfo.file);
|
||||||
|
this.downloadComplete.notifyAll();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fileTransferInfo.outputStream.write(chunk);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
class FileTransferInfo implements Closeable{
|
||||||
|
public final File file;
|
||||||
|
public final OutputStream outputStream;
|
||||||
|
public final long expectedSize;
|
||||||
|
FileTransferInfo(File file, OutputStream outputStream, long expectedSize)
|
||||||
|
{
|
||||||
|
this.file = file;
|
||||||
|
this.outputStream = outputStream;
|
||||||
|
this.expectedSize = expectedSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() throws IOException {
|
||||||
|
outputStream.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
package com.flaremicro.crossjeeves.net;
|
package com.flaremicro.crossjeeves.net;
|
||||||
|
|
||||||
|
import static com.flaremicro.crossjeeves.net.ErrorCodes.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import com.flaremicro.crossjeeves.ScriptProcessor;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet;
|
import com.flaremicro.crossjeeves.net.packet.Packet;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet0Identify;
|
import com.flaremicro.crossjeeves.net.packet.Packet0Identify;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet127KeepAlive;
|
import com.flaremicro.crossjeeves.net.packet.Packet127KeepAlive;
|
||||||
@@ -11,17 +15,18 @@ import com.flaremicro.crossjeeves.net.packet.Packet2Script;
|
|||||||
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
|
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
|
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
|
||||||
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
|
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
|
||||||
|
import com.flaremicro.crossjeeves.net.packet.Packet6Disconnect;
|
||||||
|
|
||||||
public class ServerHandler extends NetworkHandler {
|
public class ServerHandler extends NetworkHandler {
|
||||||
|
Properties properties;
|
||||||
public ServerHandler(Socket socket) throws IOException {
|
public ServerHandler(Socket socket, Properties properties) throws IOException {
|
||||||
super(socket);
|
super(socket);
|
||||||
|
this.properties = properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlePacket(Packet packet) {
|
public void handlePacket(Packet packet) {
|
||||||
// TODO Auto-generated method stub
|
disconnect(INVALID_PACKET_RECIEVED, "Recieved invalid packet " + packet.getId() + " (Unknown)");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -37,28 +42,39 @@ public class ServerHandler extends NetworkHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlePacket(Packet2Script packet) {
|
public void handlePacket(Packet2Script packet) {
|
||||||
// TODO Auto-generated method stub
|
ScriptProcessor scriptProcessor = new ScriptProcessor(this, properties);
|
||||||
|
scriptProcessor.processAsync(packet.script);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlePacket(Packet3Clone packet) {
|
public void handlePacket(Packet3Clone packet) {
|
||||||
|
disconnect(INVALID_PACKET_RECIEVED, "Recieved invalid packet " + packet.getId() + " (Clone)");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlePacket(Packet4FileData packet) {
|
public void handlePacket(Packet4FileData packet) {
|
||||||
// TODO Auto-generated method stub
|
try
|
||||||
|
{
|
||||||
|
this.appendFile(packet.getFileId(), packet.getFileChunk());
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
disconnect(FILE_DOWNLOAD_FAILURE, "Failed to download transferred file");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlePacket(Packet5Artifact packet) {
|
public void handlePacket(Packet5Artifact packet) {
|
||||||
// TODO Auto-generated method stub
|
disconnect(INVALID_PACKET_RECIEVED, "Recieved invalid packet " + packet.getId() + " (Artifact)");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlePacket(Packet127KeepAlive packet) {
|
public void handlePacket(Packet127KeepAlive packet) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handlePacket(Packet6Disconnect packet) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ public abstract class Packet {
|
|||||||
registerPacket((byte) 3, new Packet3Clone());
|
registerPacket((byte) 3, new Packet3Clone());
|
||||||
registerPacket((byte) 4, new Packet4FileData());
|
registerPacket((byte) 4, new Packet4FileData());
|
||||||
registerPacket((byte) 5, new Packet5Artifact());
|
registerPacket((byte) 5, new Packet5Artifact());
|
||||||
|
registerPacket((byte) 6, new Packet6Disconnect());
|
||||||
|
registerPacket((byte) 7, new Packet7LogEntry());
|
||||||
|
|
||||||
registerPacket((byte) 127, new Packet127KeepAlive());
|
registerPacket((byte) 127, new Packet127KeepAlive());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ public class Packet0IdentifyTest extends PacketTestBase {
|
|||||||
assertEquals(0, input().readByte());
|
assertEquals(0, input().readByte());
|
||||||
assertEquals(Packet.PROTOCOL_VERSION, input().readInt());
|
assertEquals(Packet.PROTOCOL_VERSION, input().readInt());
|
||||||
assertEquals(1337, input().readInt());
|
assertEquals(1337, input().readInt());
|
||||||
assertBufferEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -46,7 +45,6 @@ public class Packet0IdentifyTest extends PacketTestBase {
|
|||||||
|
|
||||||
assertEquals(Packet.PROTOCOL_VERSION, packet.getProtocolVersion());
|
assertEquals(Packet.PROTOCOL_VERSION, packet.getProtocolVersion());
|
||||||
assertEquals(1337, packet.getFlags());
|
assertEquals(1337, packet.getFlags());
|
||||||
assertBufferEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ public class Packet1StatusTest extends PacketTestBase {
|
|||||||
|
|
||||||
assertEquals(1, input().readByte());
|
assertEquals(1, input().readByte());
|
||||||
assertEquals(1337, input().readInt());
|
assertEquals(1337, input().readInt());
|
||||||
assertBufferEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -42,7 +41,6 @@ public class Packet1StatusTest extends PacketTestBase {
|
|||||||
packet.recievePacket(input());
|
packet.recievePacket(input());
|
||||||
|
|
||||||
assertEquals(1337, packet.getFlags());
|
assertEquals(1337, packet.getFlags());
|
||||||
assertBufferEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ public class Packet2ScriptTest extends PacketTestBase {
|
|||||||
|
|
||||||
assertEquals(2, input().readByte());
|
assertEquals(2, input().readByte());
|
||||||
assertEquals("1337", input().readUTF());
|
assertEquals("1337", input().readUTF());
|
||||||
assertBufferEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -44,7 +43,6 @@ public class Packet2ScriptTest extends PacketTestBase {
|
|||||||
packet.recievePacket(input());
|
packet.recievePacket(input());
|
||||||
|
|
||||||
assertEquals("1337", packet.script);
|
assertEquals("1337", packet.script);
|
||||||
assertBufferEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ public class Packet3CloneTest extends PacketTestBase {
|
|||||||
assertEquals(3, input().readByte());
|
assertEquals(3, input().readByte());
|
||||||
assertEquals(1, input().readLong());
|
assertEquals(1, input().readLong());
|
||||||
assertEquals(2, input().readLong());
|
assertEquals(2, input().readLong());
|
||||||
assertBufferEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -46,7 +45,6 @@ public class Packet3CloneTest extends PacketTestBase {
|
|||||||
|
|
||||||
assertEquals(1, packet.getFileID());
|
assertEquals(1, packet.getFileID());
|
||||||
assertEquals(2, packet.getFileSize());
|
assertEquals(2, packet.getFileSize());
|
||||||
assertBufferEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ public class Packet4FileDataTest extends PacketTestBase {
|
|||||||
assertEquals(4, input().readShort());
|
assertEquals(4, input().readShort());
|
||||||
input().readFully(buffer);
|
input().readFully(buffer);
|
||||||
assertArrayEquals(new byte[]{1,2,3,4}, buffer);
|
assertArrayEquals(new byte[]{1,2,3,4}, buffer);
|
||||||
assertBufferEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -52,7 +51,6 @@ public class Packet4FileDataTest extends PacketTestBase {
|
|||||||
|
|
||||||
assertEquals(1, packet.getFileId());
|
assertEquals(1, packet.getFileId());
|
||||||
assertArrayEquals(new byte[]{1,2,3,4}, packet.getFileChunk());
|
assertArrayEquals(new byte[]{1,2,3,4}, packet.getFileChunk());
|
||||||
assertBufferEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ public class Packet5ArtifactTest extends PacketTestBase {
|
|||||||
assertEquals(5, input().readByte());
|
assertEquals(5, input().readByte());
|
||||||
assertEquals(1, input().readLong());
|
assertEquals(1, input().readLong());
|
||||||
assertEquals("file.txt", input().readUTF());
|
assertEquals("file.txt", input().readUTF());
|
||||||
assertBufferEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -45,7 +44,6 @@ public class Packet5ArtifactTest extends PacketTestBase {
|
|||||||
|
|
||||||
assertEquals(1, packet.getFileId());
|
assertEquals(1, packet.getFileId());
|
||||||
assertEquals("file.txt", packet.getRelativeFile());
|
assertEquals("file.txt", packet.getRelativeFile());
|
||||||
assertBufferEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import java.io.IOException;
|
|||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
|
||||||
@@ -30,8 +31,9 @@ public abstract class PacketTestBase {
|
|||||||
buffer.empty();
|
buffer.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void assertBufferEmpty() {
|
@After
|
||||||
assertEquals(0, buffer.size());
|
public void assertBufferEmpty() {
|
||||||
|
assertEquals("Buffer should be empty:" ,0, buffer.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected DataInputStream input() {
|
protected DataInputStream input() {
|
||||||
|
|||||||
Reference in New Issue
Block a user