]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/SchedSimulator.java
added logging to scheduler
[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 = "USAGE";
28
29         private static final Logger log = LoggerFactory
30                         .getLogger(SchedSimulator.class);
31
32         private static SchedulerType schedulerType;
33         private static ScenarioType scenario;
34         private static File inputFile;
35         private static File logfile;
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                 }
50                 LinkedList<Application> apps = CSVParser.parseFile(inputFile);
51                 Collections.sort(apps);
52                 AbstractScheduler scheduler;
53                 switch (schedulerType) {
54                 case A:
55                         scheduler = new SchedulerA(schedulerLog,schedulerType,scenario);
56                 case B:
57                         scheduler = new SchedulerB(schedulerLog,schedulerType,scenario);
58                 default:
59                         scheduler = new SchedulerC(schedulerLog,schedulerType,scenario);
60                 }
61                 ScenarioData data = scheduler.initAndStart(apps);
62                 CSVLogger logger = new CSVLogger(logfile);
63                 logger.logScenarioData(data);
64                 logger.close();
65         }
66
67         private static void parseCommandLineArgs(String[] args) {
68                 String scenarioStr = args[0];
69                 String schedulerTypeStr = args[1];
70                 String inputFileStr = args[2];
71                 String schedulerLogStr = args[3];
72                 String scenarioLogFileStr = args[4];
73                 try {
74                         scenario = ScenarioType.valueOf(scenarioStr);
75                         schedulerType = SchedulerType.valueOf(schedulerTypeStr);
76                         inputFile = new File(inputFileStr);
77                         if (!inputFile.canRead())
78                                 throw new IOException("Can not read input file");
79                         logfile = new File(schedulerLogStr);
80                         boolean  logFileOK = false;
81                         if ( logfile.canWrite() ) {
82                                 logFileOK = true;
83                         }
84                         else if (!logfile.exists()) {
85                                 logfile.createNewFile();
86                                 logFileOK = true;
87                         }
88                         if (!logFileOK)
89                                 throw new IOException("Can not write to scheduler log file");
90                         boolean fileWriteOK = false;
91                         schedulerLog = new File(scenarioLogFileStr);
92                         if (schedulerLog.exists()) {
93                                 fileWriteOK = schedulerLog.canWrite();
94                                 schedulerLog.delete();
95                                 schedulerLog.createNewFile();
96                         } else {
97                                 schedulerLog.createNewFile();
98                                 fileWriteOK = true;
99                         }
100                         if (!fileWriteOK)
101                                 throw new IOException("Can not write to Scenario Log File "
102                                                 + scenarioLogFileStr);
103                 } catch (Exception e) {
104                         e.printStackTrace();
105                         log.info(USAGE);
106                         System.exit(1);
107                 }
108         }
109
110 }