]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/SchedSimulator.java
Small adjustments
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / SchedSimulator.java
1 package at.ac.tuwien.lsdc;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Collections;
6 import java.util.LinkedList;
7
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import at.ac.tuwien.lsdc.sched.AbstractScheduler;
12 import at.ac.tuwien.lsdc.sched.SchedulerA;
13 import at.ac.tuwien.lsdc.sched.SchedulerB;
14 import at.ac.tuwien.lsdc.sched.SchedulerC;
15 import at.ac.tuwien.lsdc.types.Application;
16 import at.ac.tuwien.lsdc.types.ScenarioData;
17 import at.ac.tuwien.lsdc.types.ScenarioType;
18 import at.ac.tuwien.lsdc.types.SchedulerType;
19 import at.ac.tuwien.lsdc.util.CSVLogger;
20 import at.ac.tuwien.lsdc.util.CSVParser;
21
22 /**
23  * Read config (command line properties) and start Scheduler-Simulation.
24  */
25 public class SchedSimulator {
26
27         private static final String USAGE = "SchedSimulator needs exactly 5 parameters: scenario, scheduler, inputFile, logFilePath, schedulerLogPath";
28
29         private static final Logger log = LoggerFactory
30                         .getLogger(SchedSimulator.class);
31
32         private static ScenarioType scenario;
33         private static SchedulerType schedulerType;
34         private static File inputFile;
35         private static File generalLog;
36         private static File schedulerLog;
37
38         public static void main(String[] args) throws Exception {
39                 if (args.length == 5) {
40                         parseCommandLineArgs(args);
41                 } else if (args.length == 1) {
42                         String[] newArgs = args[0].split(" ");
43                         if (newArgs.length == 5) {
44                                 parseCommandLineArgs(newArgs);
45                         } else {
46                                 log.info(USAGE);
47                                 System.exit(1);
48                         }
49                 } else {
50                         log.info(USAGE);
51                         System.exit(1);
52                 }
53                 LinkedList<Application> apps = CSVParser.parseFile(inputFile);
54                 Collections.sort(apps);
55                 AbstractScheduler scheduler;
56                 switch (schedulerType) {
57                 case A:
58                         scheduler = new SchedulerA(schedulerLog,schedulerType,scenario);
59                 case B:
60                         scheduler = new SchedulerB(schedulerLog,schedulerType,scenario);
61                 default:
62                         scheduler = new SchedulerC(schedulerLog,schedulerType,scenario);
63                 }
64                 ScenarioData data = scheduler.initAndStart(apps);
65                 CSVLogger logger = new CSVLogger(generalLog);
66                 logger.logScenarioData(data);
67                 logger.close();
68         }
69
70         /**
71          * Commandline arguments parsed (mandatory):
72          * - type of scenario
73          * - type of scheduler
74          * - scenario input file with applications
75          * - general output file for all schedulers
76          * - specific output file for this scheduler/scenario combination
77          * @param args the command line arguments to parse
78          */
79         private static void parseCommandLineArgs(String[] args) {
80                 String scenarioStr = args[0];
81                 String schedulerTypeStr = args[1];
82                 String inputFileStr = args[2];
83                 String schedulerLogStr = args[3];
84                 String scenarioLogFileStr = args[4];
85                 try {
86                         scenario = ScenarioType.valueOf(scenarioStr);
87                         schedulerType = SchedulerType.valueOf(schedulerTypeStr);
88                         inputFile = new File(inputFileStr);
89                         if (!inputFile.canRead())
90                                 throw new IOException("Can not read input file");
91                         generalLog = new File(schedulerLogStr);
92                         boolean  logFileOK = false;
93                         if ( generalLog.canWrite() ) {
94                                 logFileOK = true;
95                         }
96                         else if (!generalLog.exists()) {
97                                 generalLog.createNewFile();
98                                 logFileOK = true;
99                         }
100                         if (!logFileOK)
101                                 throw new IOException("Can not write to scheduler log file");
102                         boolean fileWriteOK = false;
103                         schedulerLog = new File(scenarioLogFileStr);
104                         if (schedulerLog.exists()) {
105                                 fileWriteOK = schedulerLog.canWrite();
106                                 schedulerLog.delete();
107                                 schedulerLog.createNewFile();
108                         } else {
109                                 schedulerLog.createNewFile();
110                                 fileWriteOK = true;
111                         }
112                         if (!fileWriteOK)
113                                 throw new IOException("Can not write to Scenario Log File "
114                                                 + scenarioLogFileStr);
115                 } catch (Exception e) {
116                         e.printStackTrace();
117                         log.info(USAGE);
118                         System.exit(1);
119                 }
120         }
121
122 }