From 217106d306a4258656d9ab700d2f16982cb48f39 Mon Sep 17 00:00:00 2001 From: Stefan Derkits Date: Wed, 15 May 2013 00:13:07 +0200 Subject: [PATCH] started input handling of SchedulerSimulator --- src/at/ac/tuwien/lsdc/SchedSimulator.java | 80 +++++++++++++++---- .../tuwien/lsdc/sched/AbstractScheduler.java | 6 +- src/at/ac/tuwien/lsdc/types/Application.java | 1 - src/at/ac/tuwien/lsdc/util/CSVParser.java | 15 ++-- 4 files changed, 71 insertions(+), 31 deletions(-) diff --git a/src/at/ac/tuwien/lsdc/SchedSimulator.java b/src/at/ac/tuwien/lsdc/SchedSimulator.java index 9495902..93d2093 100644 --- a/src/at/ac/tuwien/lsdc/SchedSimulator.java +++ b/src/at/ac/tuwien/lsdc/SchedSimulator.java @@ -1,5 +1,7 @@ package at.ac.tuwien.lsdc; +import java.io.File; +import java.io.IOException; import java.util.LinkedList; import org.slf4j.Logger; @@ -9,8 +11,9 @@ import at.ac.tuwien.lsdc.sched.AbstractScheduler; import at.ac.tuwien.lsdc.sched.SchedulerA; import at.ac.tuwien.lsdc.sched.SchedulerB; import at.ac.tuwien.lsdc.sched.SchedulerC; -import at.ac.tuwien.lsdc.types.Application; import at.ac.tuwien.lsdc.types.ScenarioData; +import at.ac.tuwien.lsdc.types.SchedulerEvent; +import at.ac.tuwien.lsdc.types.SchedulerType; import at.ac.tuwien.lsdc.util.CSVParser; import at.ac.tuwien.lsdc.util.CSVScenariosLogger; @@ -18,33 +21,76 @@ import at.ac.tuwien.lsdc.util.CSVScenariosLogger; * Read config (command line properties) and start Scheduler-Simulation. */ public class SchedSimulator { - private static final Logger log = LoggerFactory.getLogger(SchedSimulator.class); - - private static String schedulerType; - private static String inputFile; - private static String scenario; + + private static final String USAGE = "USAGE"; + + private static final Logger log = LoggerFactory + .getLogger(SchedSimulator.class); + + private static SchedulerType schedulerType; + private static File inputFile; + private static File logFile; + private static File scenarioLogFile; public static void main(String[] args) throws Exception { - log.info("Hello World!"); - parseCommandLineArgs(args); - CSVParser parser = new CSVParser(inputFile); - LinkedList apps = parser.parseFile(); + if (args.length == 4) { + parseCommandLineArgs(args); + } else if (args.length == 1) { + String[] newArgs = args[0].split(" "); + if (newArgs.length == 4) { + parseCommandLineArgs(newArgs); + } else { + log.info(USAGE); + System.exit(1); + } + } + LinkedList apps = CSVParser.parseFile(inputFile); AbstractScheduler scheduler; switch (schedulerType) { - case "A": + case A: scheduler = new SchedulerA(); - case "B": + case B: scheduler = new SchedulerB(); default: scheduler = new SchedulerC(); } - + ScenarioData data = scheduler.initAndStart(apps); - CSVScenariosLogger logger = new CSVScenariosLogger("out_scen"+scenario+"sched"+schedulerType+".csv"); - logger.appendScenarioData(data); + // CSVScenariosLogger logger = new + // CSVScenariosLogger("out_scen"+scenario+"sched"+schedulerType+".csv"); + // logger.appendScenarioData(data); } - + private static void parseCommandLineArgs(String[] args) { - + String schedulerTypeStr = args[0]; + String inputFileStr = args[1]; + String logFileStr = args[2]; + String scenarioLogFileStr = args[3]; + try { + schedulerType = SchedulerType.valueOf(schedulerTypeStr); + inputFile = new File(inputFileStr); + if (!inputFile.canRead()) + throw new IOException("Can not read input file"); + logFile = new File(logFileStr); + if (!logFile.canWrite()) + throw new IOException("Can not write to log file"); + boolean fileWriteOK = false; + scenarioLogFile = new File(scenarioLogFileStr); + if (scenarioLogFile.exists()) { + fileWriteOK = scenarioLogFile.canWrite(); + scenarioLogFile.delete(); + scenarioLogFile.createNewFile(); + } else { + scenarioLogFile.createNewFile(); + fileWriteOK = true; + } + if (!fileWriteOK) + throw new IOException("Can not write to Scenario Log File " + + scenarioLogFileStr); + } catch (Exception e) { + e.printStackTrace(); + log.info(USAGE); + System.exit(1); + } } } diff --git a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java index f86a5e8..3e0cec5 100644 --- a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java +++ b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java @@ -33,10 +33,10 @@ public abstract class AbstractScheduler { // Initialize Scheduler with Data from CSV // CSV will be parsed and sent as List to Scheduler - public ScenarioData initAndStart(List apps) { - for (Application a : apps) { + public ScenarioData initAndStart(LinkedList apps) { + for (SchedulerEvent a : apps) { // read start timestamp - // save as Event in Map + // save event in map } startScheduling(); return doEndLogging(); diff --git a/src/at/ac/tuwien/lsdc/types/Application.java b/src/at/ac/tuwien/lsdc/types/Application.java index cc09198..c1bb6c1 100644 --- a/src/at/ac/tuwien/lsdc/types/Application.java +++ b/src/at/ac/tuwien/lsdc/types/Application.java @@ -9,7 +9,6 @@ public class Application { private int duration; public Application(long timestamp, int size, int ram, int cpu, int duration) { - super(); this.timestamp = timestamp; this.size = size; this.ram = ram; diff --git a/src/at/ac/tuwien/lsdc/util/CSVParser.java b/src/at/ac/tuwien/lsdc/util/CSVParser.java index 4d3273e..93dad47 100644 --- a/src/at/ac/tuwien/lsdc/util/CSVParser.java +++ b/src/at/ac/tuwien/lsdc/util/CSVParser.java @@ -1,26 +1,21 @@ package at.ac.tuwien.lsdc.util; +import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.LinkedList; import java.util.List; -import at.ac.tuwien.lsdc.types.Application; +import at.ac.tuwien.lsdc.types.SchedulerEvent; import au.com.bytecode.opencsv.CSVReader; public class CSVParser { - private final String filename; - public CSVParser(String filename) { - this.filename = filename; - } - - public LinkedList parseFile() throws IOException { - CSVReader reader = new CSVReader(new FileReader(filename)); + public static LinkedList parseFile(File csvFile) throws IOException { + CSVReader reader = new CSVReader(new FileReader(csvFile)); List file = reader.readAll(); reader.close(); - return new LinkedList(); + return new LinkedList(); } - } -- 2.43.0