/**
 * Created by IntelliJ IDEA.
 * User: Gautam
 * Date: Jan 15, 2009
 * Time: 9:35:12 AM
 * To change this template use File | Settings | File Templates.
 */

import java.io.*;

public class threadCommand {
    static String fileName;
    static String fileLines[];
    static int numberLinesToCompute;
    static int numberThreads = 16;

    public static void main(String args[]) {
        for (int i = 0; i < args.length; i++)
            if (args[i].equals("-f"))
                fileName = args[++i];
            else if (args[i].equals("-n"))
                numberLinesToCompute = Integer.parseInt(args[++i]);
        try {
            fileLines = new String[numberLinesToCompute];
            DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(new File(fileName))));
            int i = 0;
            while (dis.available() != 0) {
                fileLines[i++] = dis.readLine().trim();
            }
            dis.close();
        } catch (IOException e) {
            System.out.println("java threadCommand -f  -n ");
            System.exit(0);
        }
        for (int i = 0; i < numberThreads; i++) {
            if (i == (numberThreads - 1))
                new threadedExecution("Thread" + i, fileLines, i * (numberLinesToCompute / numberThreads), numberLinesToCompute - 1);
            else
                new threadedExecution("Thread" + i, fileLines, i * (numberLinesToCompute / numberThreads), ((i + 1) * (numberLinesToCompute / numberThreads)) - 1);
        }
    }
}

class threadedExecution extends Thread {
    static String SHELL_SCRIPT_TO_EXECUTE = "./getBGP.sh";
    private static final String ERROR_NODES = "error";
    String threadName;
    String linesForThisThread[];
    int init;
    int finale;

    public threadedExecution(String threadName, String linesForThisThread[], int init, int finale) {
        super(threadName);
        //System.out.println(this);
        this.linesForThisThread = linesForThisThread;
        this.threadName = threadName;
        this.init = init;
        this.finale = finale;
        start();
        File f = new File(ERROR_NODES + threadName);
        if (f.exists())
            f.delete();
    }

    public void run() {
        try {
            for (int i = init; i < finale + 1; i++) {
                runCommand(linesForThisThread[i], threadName);
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        //System.out.println("Exit from Thread " + getName());
    }

    private static void runCommand(String line, String thread) throws IOException {
        //To change body of created methods use File | Settings | File Templates.
        Process process = Runtime.getRuntime().exec(SHELL_SCRIPT_TO_EXECUTE + " " + line);
        System.out.println("Success at:" + line);
        try {
            if (process.waitFor() != 0) {
                FileWriter fw = new FileWriter(ERROR_NODES + thread, true);
                StringBuffer sbuf = new StringBuffer();
                sbuf.append(line + "\n");
                fw.write(sbuf.toString());
                fw.flush();
                fw.close();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}