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