// Path: android/android/src/edu/cmu/cs/cloudlet/android/data/Measure.java
// public class Measure {
// 	private static final String SEPERATOR = ":";
// 	public static final String OVERLAY_TRANSFER_START = "OVERLAY_TRANSFER_START";
// 	public static final String OVERLAY_TRANSFER_END = "OVERLAY_TRANSFER_END";
// 	public static final String VM_LAUNCHED = "VM_LAUNCHED";
// 	public static final String APP_START = "APP_START";
// 	public static final String APP_END = "APP_END";
// 
// 	protected static Vector<String> timeline;
// 	protected static long imageSize = 0, memorySize = 0;
// 
// 	public static void clear() {
// 		timeline = new Vector<String>();
// 	}
// 
// 	public static void record(String timeString) {
// //		timeline.add(timeString + SEPERATOR
// //				+ (System.currentTimeMillis() / 1000));
// 		// Log.d("krha", "Measurement : " + timeString + SEPERATOR +
// 		// System.currentTimeMillis());
// 	}
// 
// 	public static void setOverlaySize(long imgSize, long memSize) {
// 		imageSize = imgSize;
// 		memorySize = memSize;
// 	}
// 
// 	public static String print() {
// 		StringBuffer sb = new StringBuffer();
// 		for (int i = 0; i < timeline.size(); i++) {
// 			sb.append(timeline.get(i) + "\n");
// 		}
// 		return sb.toString();
// 	}
// }
// 
// Path: android/android/src/edu/cmu/cs/cloudlet/android/util/KLog.java
// public class KLog {
// 	public static final String TAG = "krha";
// 	private static final SimpleDateFormat TIMESTAMP_FMT = new SimpleDateFormat("[HH:mm:ss]");
// 	private static Writer mWriter;
// 	
// 	static{
// 		File basePath = CloudletEnv.instance().getFilePath(CloudletEnv.ROOT_DIR);
// 		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-hhmmssSSS");
// 		File f = new File(basePath + df.format(new Date()) + ".log");
// 		String mPath = f.getAbsolutePath();
// 		try {
// 			mWriter = new BufferedWriter(new FileWriter(mPath), 2048);
// 			println("Start logging.");
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		}
// 	}
// 
// 	public static void println(String message) {		
// 		Log.d(TAG, message);
// 		if(mWriter != null){
// 			try {
// 				mWriter.write(TIMESTAMP_FMT.format(new Date()));
// 				mWriter.write(message);
// 				mWriter.write('\n');
// 				mWriter.flush();	
// 			} catch (IOException e) {
// 				Log.e(TAG, e.toString());
// 			}
// 		}
// 	}
// 	
// 	public static void printErr(String message) {
// 		Log.e(TAG, message);
// 		StackTraceElement[] stackTraces = Thread.currentThread().getStackTrace();
// 		for(int i = 0; i < stackTraces.length; i++){
// 		    StackTraceElement stackTraceElement = stackTraces[i];
// 			Log.e(TAG, stackTraceElement.toString());
// 		}
// 		if(mWriter != null){
// 			try {
// 				mWriter.write("[ERROR] " + TIMESTAMP_FMT.format(new Date()));
// 				mWriter.write(message);
// 				mWriter.write('\n');
// 				mWriter.flush();
// 			} catch (IOException e) {
// 				Log.e(TAG, e.toString());
// 			}
// 		}
// 	}
// 
// 	public static void close() throws IOException {
// 		mWriter.close();
// 	}
// }


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Vector;
import edu.cmu.cs.cloudlet.android.data.Measure;
import edu.cmu.cs.cloudlet.android.data.VMInfo;
import edu.cmu.cs.cloudlet.android.util.KLog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

	public void run() {

		while (isThreadRun == true) {
			if (commandQueue.size() == 0) {
				try {
					Thread.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				continue;
			}

			NetworkMsg networkCommand = commandQueue.remove(0);
			try {
				this.sendCommand(this.networkWriter, networkCommand);
				switch (networkCommand.getCommandType()) {
				case NetworkMsg.MESSAGE_COMMAND_SEND_META:
					// Send overlay meta file
					File metaFile = networkCommand.getSelectedFile();
					Measure.record(Measure.OVERLAY_TRANSFER_START);
					this.sendBinaryFile(metaFile, false);
					break;
				case NetworkMsg.MESSAGE_COMMAND_SEND_OVERLAY:
					// Send overlay file
					File overlayFile = networkCommand.getSelectedFile();
					this.sendBinaryFile(overlayFile, true);
					this.connector.updateTransferredOverlay(overlayFile);
					break;
				}
			} catch (IOException e) {

// Path: android/android/src/edu/cmu/cs/cloudlet/android/data/Measure.java
// public class Measure {
// 	private static final String SEPERATOR = ":";
// 	public static final String OVERLAY_TRANSFER_START = "OVERLAY_TRANSFER_START";
// 	public static final String OVERLAY_TRANSFER_END = "OVERLAY_TRANSFER_END";
// 	public static final String VM_LAUNCHED = "VM_LAUNCHED";
// 	public static final String APP_START = "APP_START";
// 	public static final String APP_END = "APP_END";
// 
// 	protected static Vector<String> timeline;
// 	protected static long imageSize = 0, memorySize = 0;
// 
// 	public static void clear() {
// 		timeline = new Vector<String>();
// 	}
// 
// 	public static void record(String timeString) {
// //		timeline.add(timeString + SEPERATOR
// //				+ (System.currentTimeMillis() / 1000));
// 		// Log.d("krha", "Measurement : " + timeString + SEPERATOR +
// 		// System.currentTimeMillis());
// 	}
// 
// 	public static void setOverlaySize(long imgSize, long memSize) {
// 		imageSize = imgSize;
// 		memorySize = memSize;
// 	}
// 
// 	public static String print() {
// 		StringBuffer sb = new StringBuffer();
// 		for (int i = 0; i < timeline.size(); i++) {
// 			sb.append(timeline.get(i) + "\n");
// 		}
// 		return sb.toString();
// 	}
// }
// 
// Path: android/android/src/edu/cmu/cs/cloudlet/android/util/KLog.java
// public class KLog {
// 	public static final String TAG = "krha";
// 	private static final SimpleDateFormat TIMESTAMP_FMT = new SimpleDateFormat("[HH:mm:ss]");
// 	private static Writer mWriter;
// 	
// 	static{
// 		File basePath = CloudletEnv.instance().getFilePath(CloudletEnv.ROOT_DIR);
// 		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-hhmmssSSS");
// 		File f = new File(basePath + df.format(new Date()) + ".log");
// 		String mPath = f.getAbsolutePath();
// 		try {
// 			mWriter = new BufferedWriter(new FileWriter(mPath), 2048);
// 			println("Start logging.");
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		}
// 	}
// 
// 	public static void println(String message) {		
// 		Log.d(TAG, message);
// 		if(mWriter != null){
// 			try {
// 				mWriter.write(TIMESTAMP_FMT.format(new Date()));
// 				mWriter.write(message);
// 				mWriter.write('\n');
// 				mWriter.flush();	
// 			} catch (IOException e) {
// 				Log.e(TAG, e.toString());
// 			}
// 		}
// 	}
// 	
// 	public static void printErr(String message) {
// 		Log.e(TAG, message);
// 		StackTraceElement[] stackTraces = Thread.currentThread().getStackTrace();
// 		for(int i = 0; i < stackTraces.length; i++){
// 		    StackTraceElement stackTraceElement = stackTraces[i];
// 			Log.e(TAG, stackTraceElement.toString());
// 		}
// 		if(mWriter != null){
// 			try {
// 				mWriter.write("[ERROR] " + TIMESTAMP_FMT.format(new Date()));
// 				mWriter.write(message);
// 				mWriter.write('\n');
// 				mWriter.flush();
// 			} catch (IOException e) {
// 				Log.e(TAG, e.toString());
// 			}
// 		}
// 	}
// 
// 	public static void close() throws IOException {
// 		mWriter.close();
// 	}
// }


// Path: android/android/src/edu/cmu/cs/cloudlet/android/network/NetworkClientSender.java
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Vector;
import edu.cmu.cs.cloudlet.android.data.Measure;
import edu.cmu.cs.cloudlet.android.data.VMInfo;
import edu.cmu.cs.cloudlet.android.util.KLog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

	public void run() {

		while (isThreadRun == true) {
			if (commandQueue.size() == 0) {
				try {
					Thread.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				continue;
			}

			NetworkMsg networkCommand = commandQueue.remove(0);
			try {
				this.sendCommand(this.networkWriter, networkCommand);
				switch (networkCommand.getCommandType()) {
				case NetworkMsg.MESSAGE_COMMAND_SEND_META:
					// Send overlay meta file
					File metaFile = networkCommand.getSelectedFile();
					Measure.record(Measure.OVERLAY_TRANSFER_START);
					this.sendBinaryFile(metaFile, false);
					break;
				case NetworkMsg.MESSAGE_COMMAND_SEND_OVERLAY:
					// Send overlay file
					File overlayFile = networkCommand.getSelectedFile();
					this.sendBinaryFile(overlayFile, true);
					this.connector.updateTransferredOverlay(overlayFile);
					break;
				}
			} catch (IOException e) {

				KLog.printErr(e.getMessage());
