// Path: android/android_ESVMTrainer/src/edu/cmu/cs/cloudlet/application/esvmtrainer/util/ZipUtility.java
// public class ZipUtility {
// 
// 	public static final void zipFiles(File[] fileList, File zip)
// 			throws IOException {
// 		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
// 		zip(fileList, zos);
// 		zos.close();
// 	}
// 
// 	private static final void zip(File[] files, ZipOutputStream zos)
// 			throws IOException {
// 		byte[] buffer = new byte[8192];
// 		int read = 0;
// 		for (int i = 0, n = files.length; i < n; i++) {
// 			FileInputStream in = new FileInputStream(files[i]);
// 			ZipEntry entry = new ZipEntry(files[i].getName());
// 			zos.putNextEntry(entry);
// 			while (-1 != (read = in.read(buffer))) {
// 				zos.write(buffer, 0, read);
// 			}
// 			in.close();
// 		}
// 	}
// 
// 	public static final void unzip(File zip, File extractTo) throws IOException {
// 		ZipFile archive = new ZipFile(zip);
// 		Enumeration e = archive.entries();
// 		while (e.hasMoreElements()) {
// 			ZipEntry entry = (ZipEntry) e.nextElement();
// 			File file = new File(extractTo, entry.getName());
// 			if (entry.isDirectory() && !file.exists()) {
// 				file.mkdirs();
// 			} else {
// 				if (!file.getParentFile().exists()) {
// 					file.getParentFile().mkdirs();
// 				}
// 
// 				InputStream in = archive.getInputStream(entry);
// 				BufferedOutputStream out = new BufferedOutputStream(
// 						new FileOutputStream(file));
// 
// 				byte[] buffer = new byte[8192];
// 				int read;
// 
// 				while (-1 != (read = in.read(buffer))) {
// 					out.write(buffer, 0, read);
// 				}
// 
// 				in.close();
// 				out.close();
// 			}
// 		}
// 	}
// }


import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import org.json.JSONException;
import org.json.JSONObject;
import edu.cmu.cs.cloudlet.application.esvmtrainer.util.ZipUtility;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
	public static final int CALLBACK_FAILED = 0;
	public static final int CALLBACK_SUCCESS = 1;
	public static final int CALLBACK_UPDATE = 2;

	private byte[] binarySendingBuffer = new byte[3 * 1024 * 1024];
	private JSONObject header = null;
	private File[] imageFiles;
	private String serverAddress = null;
	private int serverPort = -1;

	private Handler networkCallbackHandler = null;
	private Socket clientSocket = null;
	private DataOutputStream networkWriter = null;
	private DataInputStream networkReader = null;
	private File outputFile;

	public ESVMNetworkClient(Handler handler, JSONObject header, File[] imageFiles, String address, int port) {
		this.networkCallbackHandler = handler;
		this.header = header;
		this.imageFiles = imageFiles;
		this.serverAddress = address;
		this.serverPort = port;
	}

	public void run() {
		// Zip target files
		this.outputFile = null;
		File zipDir = imageFiles[0].getParentFile();
		try {
			this.outputFile = File.createTempFile("ESVMImages", ".zip", zipDir);
// Path: android/android_ESVMTrainer/src/edu/cmu/cs/cloudlet/application/esvmtrainer/util/ZipUtility.java
// public class ZipUtility {
// 
// 	public static final void zipFiles(File[] fileList, File zip)
// 			throws IOException {
// 		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
// 		zip(fileList, zos);
// 		zos.close();
// 	}
// 
// 	private static final void zip(File[] files, ZipOutputStream zos)
// 			throws IOException {
// 		byte[] buffer = new byte[8192];
// 		int read = 0;
// 		for (int i = 0, n = files.length; i < n; i++) {
// 			FileInputStream in = new FileInputStream(files[i]);
// 			ZipEntry entry = new ZipEntry(files[i].getName());
// 			zos.putNextEntry(entry);
// 			while (-1 != (read = in.read(buffer))) {
// 				zos.write(buffer, 0, read);
// 			}
// 			in.close();
// 		}
// 	}
// 
// 	public static final void unzip(File zip, File extractTo) throws IOException {
// 		ZipFile archive = new ZipFile(zip);
// 		Enumeration e = archive.entries();
// 		while (e.hasMoreElements()) {
// 			ZipEntry entry = (ZipEntry) e.nextElement();
// 			File file = new File(extractTo, entry.getName());
// 			if (entry.isDirectory() && !file.exists()) {
// 				file.mkdirs();
// 			} else {
// 				if (!file.getParentFile().exists()) {
// 					file.getParentFile().mkdirs();
// 				}
// 
// 				InputStream in = archive.getInputStream(entry);
// 				BufferedOutputStream out = new BufferedOutputStream(
// 						new FileOutputStream(file));
// 
// 				byte[] buffer = new byte[8192];
// 				int read;
// 
// 				while (-1 != (read = in.read(buffer))) {
// 					out.write(buffer, 0, read);
// 				}
// 
// 				in.close();
// 				out.close();
// 			}
// 		}
// 	}
// }


// Path: android/android_ESVMTrainer/src/edu/cmu/cs/cloudlet/application/esvmtrainer/network/ESVMNetworkClient.java
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import org.json.JSONException;
import org.json.JSONObject;
import edu.cmu.cs.cloudlet.application.esvmtrainer.util.ZipUtility;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

	public static final int CALLBACK_FAILED = 0;
	public static final int CALLBACK_SUCCESS = 1;
	public static final int CALLBACK_UPDATE = 2;

	private byte[] binarySendingBuffer = new byte[3 * 1024 * 1024];
	private JSONObject header = null;
	private File[] imageFiles;
	private String serverAddress = null;
	private int serverPort = -1;

	private Handler networkCallbackHandler = null;
	private Socket clientSocket = null;
	private DataOutputStream networkWriter = null;
	private DataInputStream networkReader = null;
	private File outputFile;

	public ESVMNetworkClient(Handler handler, JSONObject header, File[] imageFiles, String address, int port) {
		this.networkCallbackHandler = handler;
		this.header = header;
		this.imageFiles = imageFiles;
		this.serverAddress = address;
		this.serverPort = port;
	}

	public void run() {
		// Zip target files
		this.outputFile = null;
		File zipDir = imageFiles[0].getParentFile();
		try {
			this.outputFile = File.createTempFile("ESVMImages", ".zip", zipDir);
			ZipUtility.zipFiles(this.imageFiles, outputFile);