package org.scattport.client.apps;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import java.util.HashMap;

import org.jibble.simpleftp.SimpleFTP;
import org.scattport.client.*;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

public class Sscatt extends App {

	/**
	 * The files that we need to start the calculation.
	 */
	private String[] inputFiles = {
		"default.calc",
		"default.par",
		"default.obj"
	};

	/**
	 * The files we get when the calculation is finished.
	 */
	private String[] outputFiles = {
		"default.out",
		"default.log",
		"default.tma"
	};

	/**
	 * Dummy application that just sleeps for 20 seconds.
	 *
	 * @param job
	 */
	public Sscatt(Job job) {
		super(job);
	}

	@Override
	public void run() {
		try {
			this.setup();
		} catch (IOException e) {
			System.err.println("Environment could not be set up:");
			e.printStackTrace();
		}
		this.spawn();
		this.submitResults();
	}
	
	public void setup() throws IOException {
		super.setup();
		
		for(String file : inputFiles) {
	        try {
	            URL url = new URL(Client.properties.getProperty("server.base") + "uploads/" + job.getExperimentId() + "/" + file);
	            URLConnection con = url.openConnection(); // open the url connection.
	            DataInputStream dis = new DataInputStream(con.getInputStream()); // get a data stream from the url connection.
	            byte[] fileData = new byte[con.getContentLength()]; // determine how many byes the file size is and make array big enough to hold the data
	            for (int x = 0; x < fileData.length; x++) { // fill byte array with bytes from the data input stream
	                fileData[x] = dis.readByte();
	            }
	            dis.close(); // close the data input stream
	            FileOutputStream fos = new FileOutputStream(new File(workingDir + "/" + file));  //create an object representing the file we want to save
	            fos.write(fileData);  // write out the file we want to save.
	            fos.close(); // close the output stream writer
	        }
	        catch(MalformedURLException m) {
	            System.out.println(m);
	        }
	        catch(IOException io) {
	            System.out.println(io);
	        }
		}
	}

	@Override
	public void spawn() {
		try {
			System.out.println("Spawning worker");
			ProcessBuilder builder = new ProcessBuilder("/opt/sscatt/wrapper.sh");
		    builder.directory(this.workingDir); 
		    Process p = builder.start(); 
		    try {
				p.waitFor();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 
		    //Scanner s = new Scanner( p.getInputStream() ).useDelimiter( "\n" ); 
		    //System.out.println( s.next() );
		    System.out.println("Job done");
		    Client.deleteJob(job);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public void kill() {
		throw new NotImplementedException();
	}

	@Override
	public void submitResults() {
		System.out.println("Stopping app and deleting job");
		
		boolean filesUploaded;

		// upload results
		try {

			SimpleFTP ftp = new SimpleFTP();

			// Connect to an FTP server on port 21.
			ftp.connect(
					Client.properties.getProperty("ftp.host"),
					Integer.parseInt(Client.properties.getProperty("ftp.port")),
					Client.properties.getProperty("ftp.user"),
					Client.properties.getProperty("ftp.pass"));

			// Set binary mode.
			ftp.bin();

			// Change to a new working directory on the FTP server.
			ftp.cwd("/tmp");
			if (!ftp.cwd("sp_incoming")) {
				ftp.mkdir("sp_incoming");
				ftp.cwd("sp_incoming");
			}
			
			if (!ftp.cwd(job.getJobId())) {
				ftp.mkdir(job.getJobId());
				ftp.cwd(job.getJobId());
			}

			if (!ftp.pwd().equals("/tmp/sp_incoming/" + job.getJobId())) {
				System.out.println("The server could not change to the correct working directory.");
			}

			// okay, we seem to be in the correct directory. go for it.
			for(String file : outputFiles) {
				try {
					ftp.stor(new File(this.workingDir + "/" + file));
				} catch(FileNotFoundException e) {
					System.err.println("Required output-file " + file + " was not found.");
				}
			}

			// Quit from the FTP server.
			ftp.disconnect();
			filesUploaded = true;
		} catch (IOException e) {
			e.printStackTrace();
			filesUploaded = false;
		}
		
		// tell the server we are done
		HashMap<String, Object> result = Client.exec("job_done", job.getJobId().toString(), filesUploaded ? "true" : "false");

		if (!result.get("success").equals("true")) {
			System.out.println("Job progress could not be stored.");
		}
		
		// delete the job
		Client.deleteJob(job);
	}
}

@@ -607,28 +607,30 @@
 ult.
-par
+obj
 %22,%0A%09%09%22
-default.obj
+param_dsm.dat
 %22%0A%09%7D

Require the correct files