Add more net code
This commit is contained in:
@@ -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>();
|
||||
|
||||
@@ -31,6 +35,25 @@ public abstract class NetworkHandler {
|
||||
this.in = new DataInputStream(socket.getInputStream());
|
||||
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){
|
||||
|
||||
Reference in New Issue
Block a user