Add more net code
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
package com.flaremicro.crossjeeves;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Properties;
|
||||
import java.util.Random;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
@@ -21,34 +20,23 @@ import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import com.flaremicro.crossjeeves.net.NetworkHandler;
|
||||
import com.flaremicro.crossjeeves.net.packet.Packet;
|
||||
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
|
||||
|
||||
public class ScriptProcessor {
|
||||
public Properties properties;
|
||||
public Map<String, String> currentEnvironment = new HashMap<String, String>();
|
||||
public Process runningProcess = null;
|
||||
private Properties properties;
|
||||
private Map<String, String> currentEnvironment = new HashMap<String, String>();
|
||||
private Process runningProcess = null;
|
||||
private NetworkHandler netHandler;
|
||||
private Random random = new Random();
|
||||
|
||||
|
||||
public ScriptProcessor(Properties properties)
|
||||
public ScriptProcessor(NetworkHandler netHandler, Properties properties)
|
||||
{
|
||||
this.netHandler = netHandler;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public static void main(String[] token) throws ScriptProcessingException, IOException {
|
||||
|
||||
Properties properties = new Properties();
|
||||
|
||||
String xml = "";
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(ScriptProcessor.class.getResourceAsStream("/test.xml")));
|
||||
String line;
|
||||
while((line = br.readLine()) != null)
|
||||
{
|
||||
xml += line + "\n";
|
||||
}
|
||||
br.close();
|
||||
|
||||
new ScriptProcessor(properties).processScript(xml);
|
||||
}
|
||||
|
||||
public String requireAttribute(Element e, String attribute) throws ScriptProcessingException {
|
||||
if (!e.hasAttribute(attribute))
|
||||
{
|
||||
@@ -163,9 +151,13 @@ public class ScriptProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
private void cloneProcessor(Element e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
private void cloneProcessor(Element e) throws ScriptProcessingException {
|
||||
long id = random.nextLong();
|
||||
Packet packet = new Packet3Clone(id, 0);
|
||||
netHandler.enqueue(packet);
|
||||
File file = netHandler.waitForFile(id);
|
||||
if(file == null)
|
||||
throw new ScriptProcessingException("File failed to transfer");
|
||||
}
|
||||
|
||||
public File writeScriptToTempFile(String script, String type) throws ScriptProcessingException {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.flaremicro.crossjeeves.net;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
|
||||
@@ -12,6 +14,7 @@ import com.flaremicro.crossjeeves.net.packet.Packet2Script;
|
||||
import com.flaremicro.crossjeeves.net.packet.Packet3Clone;
|
||||
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
|
||||
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
|
||||
import com.flaremicro.util.Util;
|
||||
import com.flaremicro.util.ZipUtils;
|
||||
|
||||
public class ClientHandler extends NetworkHandler {
|
||||
@@ -52,17 +55,34 @@ public class ClientHandler extends NetworkHandler {
|
||||
{
|
||||
//disconnect
|
||||
}
|
||||
BufferedInputStream fileStream = null;
|
||||
try
|
||||
{
|
||||
File workspaceZip = File.createTempFile("workspace-"+fileId, ".zip");
|
||||
ZipUtils.zipDirectory(new File(workspace), workspaceZip);
|
||||
|
||||
packet = new Packet3Clone(fileId, workspaceZip.length());
|
||||
enqueue(packet);
|
||||
fileStream = new BufferedInputStream(new FileInputStream(workspaceZip));
|
||||
int read;
|
||||
byte[] buffer = new byte[4096];
|
||||
while((read = fileStream.read(buffer)) > -1)
|
||||
{
|
||||
if(read == 0)
|
||||
continue;
|
||||
Packet4FileData dataPacket = new Packet4FileData(fileId, (short)read, buffer);
|
||||
enqueue(dataPacket);
|
||||
}
|
||||
enqueue(new Packet4FileData(fileId, (short)0, new byte[]{}));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
//disconnect
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Util.cleanClose(fileStream);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,8 +2,10 @@ package com.flaremicro.crossjeeves.net;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
@@ -23,6 +25,8 @@ public abstract class NetworkHandler {
|
||||
private DataInputStream in;
|
||||
private DataOutputStream out;
|
||||
private Thread readThread;
|
||||
private HashMap<Long, File> downloadQueue = new HashMap<Long, File>();
|
||||
private HashMap<Long, File> downloadComplete = new HashMap<Long, File>();
|
||||
|
||||
private BlockingQueue<Packet> outbox = new LinkedBlockingQueue<Packet>();
|
||||
|
||||
@@ -32,6 +36,25 @@ public abstract class NetworkHandler {
|
||||
this.out = new DataOutputStream(socket.getOutputStream());
|
||||
}
|
||||
|
||||
public File waitForFile(long fileId)
|
||||
{
|
||||
try
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
downloadComplete.wait();
|
||||
File file = downloadComplete.get(fileId);
|
||||
if(file != null)
|
||||
return file;
|
||||
}
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
@@ -8,19 +8,21 @@ import com.flaremicro.crossjeeves.net.NetworkHandler;
|
||||
|
||||
public class Packet4FileData extends Packet{
|
||||
private long fileId;
|
||||
private short chunkSize;
|
||||
private byte[] fileChunk;
|
||||
|
||||
public Packet4FileData(){
|
||||
}
|
||||
|
||||
public Packet4FileData(long fileId, byte[] fileChunk){
|
||||
public Packet4FileData(long fileId, short chunkSize, byte[] fileChunk){
|
||||
this.fileId = fileId;
|
||||
this.chunkSize = chunkSize;
|
||||
this.fileChunk = fileChunk;
|
||||
}
|
||||
|
||||
public void recievePacket(DataInputStream in) throws IOException {
|
||||
fileId = in.readLong();
|
||||
int chunkSize = in.readShort();
|
||||
chunkSize = in.readShort();
|
||||
fileChunk = new byte[chunkSize];
|
||||
in.readFully(fileChunk);
|
||||
}
|
||||
@@ -28,8 +30,8 @@ public class Packet4FileData extends Packet{
|
||||
public void sendPacket(DataOutputStream out) throws IOException {
|
||||
super.sendPacket(out);
|
||||
out.writeLong(fileId);
|
||||
out.writeShort(fileChunk.length);
|
||||
out.write(fileChunk);
|
||||
out.writeShort(chunkSize);
|
||||
out.write(fileChunk, 0, chunkSize);
|
||||
}
|
||||
|
||||
public void processPacket(NetworkHandler networkHandler){
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.flaremicro.crossjeeves.net.packet.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -11,7 +13,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import com.flaremicro.crossjeeves.net.NetworkHandler;
|
||||
import com.flaremicro.crossjeeves.net.packet.Packet;
|
||||
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
|
||||
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class Packet4FileDataTest extends PacketTestBase {
|
||||
@@ -22,36 +24,41 @@ public class Packet4FileDataTest extends PacketTestBase {
|
||||
@Test
|
||||
public void testWrite() throws IOException
|
||||
{
|
||||
Packet5Artifact packet = new Packet5Artifact(1, "file.txt");
|
||||
Packet4FileData packet = new Packet4FileData(1, (short)4, new byte[]{1,2,3,4,5});
|
||||
assertEquals(packet.getFileId(), 1);
|
||||
assertEquals("file.txt", packet.getRelativeFile());
|
||||
assertArrayEquals(new byte[]{1,2,3,4,5}, packet.getFileChunk());
|
||||
packet.sendPacket(output());
|
||||
|
||||
assertEquals(5, input().readByte());
|
||||
byte[] buffer = new byte[4];
|
||||
|
||||
assertEquals(4, input().readByte());
|
||||
assertEquals(1, input().readLong());
|
||||
assertEquals("file.txt", input().readUTF());
|
||||
assertEquals(4, input().readShort());
|
||||
input().readFully(buffer);
|
||||
assertArrayEquals(new byte[]{1,2,3,4}, buffer);
|
||||
assertBufferEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRead() throws IOException
|
||||
{
|
||||
Packet5Artifact packet = new Packet5Artifact();
|
||||
Packet4FileData packet = new Packet4FileData();
|
||||
|
||||
output().writeLong(1);
|
||||
output().writeUTF("file.txt");
|
||||
output().writeShort(4);
|
||||
output().write(new byte[]{1,2,3,4});
|
||||
|
||||
packet.recievePacket(input());
|
||||
|
||||
assertEquals(1, packet.getFileId());
|
||||
assertEquals("file.txt", packet.getRelativeFile());
|
||||
assertArrayEquals(new byte[]{1,2,3,4}, packet.getFileChunk());
|
||||
assertBufferEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcess() throws IOException
|
||||
{
|
||||
Packet5Artifact packet = new Packet5Artifact(1, "file.txt");
|
||||
Packet4FileData packet = new Packet4FileData(1, (short) 4, new byte[]{1,2,3,4});
|
||||
packet.processPacket(handler);
|
||||
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
|
||||
}
|
||||
@@ -59,7 +66,7 @@ public class Packet4FileDataTest extends PacketTestBase {
|
||||
@Test
|
||||
public void testTypeCopy()
|
||||
{
|
||||
Packet5Artifact packet = new Packet5Artifact();
|
||||
Packet4FileData packet = new Packet4FileData();
|
||||
Packet clonedPacket = packet.cloneTypeOnly();
|
||||
assertEquals(packet.getClass(), clonedPacket.getClass());
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.flaremicro.crossjeeves.net.packet.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -13,7 +11,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import com.flaremicro.crossjeeves.net.NetworkHandler;
|
||||
import com.flaremicro.crossjeeves.net.packet.Packet;
|
||||
import com.flaremicro.crossjeeves.net.packet.Packet4FileData;
|
||||
import com.flaremicro.crossjeeves.net.packet.Packet5Artifact;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class Packet5ArtifactTest extends PacketTestBase {
|
||||
@@ -24,41 +22,36 @@ public class Packet5ArtifactTest extends PacketTestBase {
|
||||
@Test
|
||||
public void testWrite() throws IOException
|
||||
{
|
||||
Packet4FileData packet = new Packet4FileData(1, new byte[]{1,2,3,4});
|
||||
Packet5Artifact packet = new Packet5Artifact(1, "file.txt");
|
||||
assertEquals(packet.getFileId(), 1);
|
||||
assertArrayEquals(new byte[]{1,2,3,4}, packet.getFileChunk());
|
||||
assertEquals("file.txt", packet.getRelativeFile());
|
||||
packet.sendPacket(output());
|
||||
|
||||
byte[] buffer = new byte[4];
|
||||
|
||||
assertEquals(4, input().readByte());
|
||||
assertEquals(5, input().readByte());
|
||||
assertEquals(1, input().readLong());
|
||||
assertEquals(4, input().readShort());
|
||||
input().readFully(buffer);
|
||||
assertArrayEquals(new byte[]{1,2,3,4}, buffer);
|
||||
assertEquals("file.txt", input().readUTF());
|
||||
assertBufferEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRead() throws IOException
|
||||
{
|
||||
Packet4FileData packet = new Packet4FileData();
|
||||
Packet5Artifact packet = new Packet5Artifact();
|
||||
|
||||
output().writeLong(1);
|
||||
output().writeShort(4);
|
||||
output().write(new byte[]{1,2,3,4});
|
||||
output().writeUTF("file.txt");
|
||||
|
||||
packet.recievePacket(input());
|
||||
|
||||
assertEquals(1, packet.getFileId());
|
||||
assertArrayEquals(new byte[]{1,2,3,4}, packet.getFileChunk());
|
||||
assertEquals("file.txt", packet.getRelativeFile());
|
||||
assertBufferEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcess() throws IOException
|
||||
{
|
||||
Packet4FileData packet = new Packet4FileData(1, new byte[]{1,2,3,4});
|
||||
Packet5Artifact packet = new Packet5Artifact(1, "file.txt");
|
||||
packet.processPacket(handler);
|
||||
Mockito.verify(handler, Mockito.times(1)).handlePacket(packet);
|
||||
}
|
||||
@@ -66,7 +59,7 @@ public class Packet5ArtifactTest extends PacketTestBase {
|
||||
@Test
|
||||
public void testTypeCopy()
|
||||
{
|
||||
Packet4FileData packet = new Packet4FileData();
|
||||
Packet5Artifact packet = new Packet5Artifact();
|
||||
Packet clonedPacket = packet.cloneTypeOnly();
|
||||
assertEquals(packet.getClass(), clonedPacket.getClass());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user