started input handling of SchedulerSimulator
authorStefan Derkits <stefan@derkits.at>
Tue, 14 May 2013 22:13:07 +0000 (00:13 +0200)
committerStefan Derkits <stefan@derkits.at>
Tue, 14 May 2013 22:13:07 +0000 (00:13 +0200)
src/at/ac/tuwien/lsdc/SchedSimulator.java
src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
src/at/ac/tuwien/lsdc/types/Application.java
src/at/ac/tuwien/lsdc/util/CSVParser.java

index 9495902f6224f453c3c1327884d3b6c0deb1c329..93d20935599abde714ce4c73a92dcc580080e948 100644 (file)
@@ -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<Application> 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<SchedulerEvent> 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);
+               }
        }
 }
index f86a5e81711534538a330d8971d11455bdb8c8fe..3e0cec503f2dbc9107415885eeeeb36220c0a6d2 100644 (file)
@@ -33,10 +33,10 @@ public abstract class AbstractScheduler {
 
        // Initialize Scheduler with Data from CSV
        // CSV will be parsed and sent as List<Application> to Scheduler
-       public ScenarioData initAndStart(List<Application> apps) {
-               for (Application a : apps) {
+       public ScenarioData initAndStart(LinkedList<SchedulerEvent> apps) {
+               for (SchedulerEvent a : apps) {
                        // read start timestamp
-                       // save as Event in Map
+                       // save event in map
                }
                startScheduling();
                return doEndLogging();
index cc0919873b244016dd0baab999e104b381c686f2..c1bb6c121d6dfa34250142f71669a9f4aa98ee7a 100644 (file)
@@ -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;
index 4d3273e555273f34f05ddadee60abf00779b4e59..93dad47370388cf973cfc1fa1d7efa628239bac2 100644 (file)
@@ -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<Application> parseFile() throws IOException {
-               CSVReader reader = new CSVReader(new FileReader(filename));
+       public static LinkedList<SchedulerEvent> parseFile(File csvFile) throws IOException {
+               CSVReader reader = new CSVReader(new FileReader(csvFile));
                List<String[]> file = reader.readAll();
                reader.close();
-               return new LinkedList<Application>();
+               return new LinkedList<SchedulerEvent>();
        }
 
-       
 }