1 | package simulator.utils.workload; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.IOException; |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.Calendar; |
---|
7 | import java.util.HashMap; |
---|
8 | import java.util.Iterator; |
---|
9 | import java.util.List; |
---|
10 | import java.util.Set; |
---|
11 | |
---|
12 | import simulator.workload.reader.archive.swf.SWFParser; |
---|
13 | import simulator.workload.writer.swf.Grms3SWFJobWriter; |
---|
14 | |
---|
15 | public class SwfManageTool { |
---|
16 | |
---|
17 | protected List<TaskManagerInterface> managersList; |
---|
18 | protected Grms3SWFJobWriter writer; |
---|
19 | protected SWFParser parser; |
---|
20 | protected File readFromFile; |
---|
21 | |
---|
22 | protected List<TaskManagerInterface> createTaskManageToolsList() throws IOException{ |
---|
23 | List<TaskManagerInterface> list = new ArrayList<TaskManagerInterface>(); |
---|
24 | |
---|
25 | list.add(new IncorrectTaskRemover()); |
---|
26 | list.add(new SubmitTimeNormalizer()); |
---|
27 | list.add(new StartTimeCalcultor()); |
---|
28 | |
---|
29 | return list; |
---|
30 | } |
---|
31 | |
---|
32 | protected void prepareTools(String swfFileName) throws IOException{ |
---|
33 | File file = new File(swfFileName); |
---|
34 | String dirName = file.getParent(); |
---|
35 | String fileName = file.getName(); |
---|
36 | |
---|
37 | this.readFromFile = new File(dirName, Calendar.getInstance().getTimeInMillis()+"_"+fileName); |
---|
38 | file.renameTo(this.readFromFile); |
---|
39 | parser = new SWFParser(this.readFromFile.getAbsolutePath()); |
---|
40 | |
---|
41 | |
---|
42 | this.writer = new Grms3SWFJobWriter(dirName, fileName); |
---|
43 | this.writer.writeFieldDescriptionHeader(); |
---|
44 | |
---|
45 | |
---|
46 | // rewrite swf header |
---|
47 | HashMap<String, String> header = parser.loadHeader(); |
---|
48 | Set<String> keys = header.keySet(); |
---|
49 | Iterator<String> itr = keys.iterator(); |
---|
50 | while(itr.hasNext()){ |
---|
51 | String key = itr.next(); |
---|
52 | this.writer.writeComment(key, header.get(key)); |
---|
53 | } |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | protected void close() throws IOException{ |
---|
58 | if(this.parser != null) |
---|
59 | this.parser.close(); |
---|
60 | if(this.writer != null) |
---|
61 | this.writer.close(); |
---|
62 | if(this.readFromFile != null) |
---|
63 | this.readFromFile.delete(); |
---|
64 | } |
---|
65 | |
---|
66 | public void run(String swfFileName){ |
---|
67 | |
---|
68 | try { |
---|
69 | prepareTools(swfFileName); |
---|
70 | this.managersList = createTaskManageToolsList(); |
---|
71 | |
---|
72 | String task[] = null; |
---|
73 | |
---|
74 | while((task = parser.readTask()) != null){ |
---|
75 | for(int i = 0; i < managersList.size(); i++){ |
---|
76 | TaskManagerInterface taskManager = managersList.get(i); |
---|
77 | task = taskManager.manageTask(task); |
---|
78 | } |
---|
79 | |
---|
80 | if(task != null) |
---|
81 | writer.writeLine(task); |
---|
82 | } |
---|
83 | |
---|
84 | } catch (IOException e) { |
---|
85 | e.printStackTrace(); |
---|
86 | } finally { |
---|
87 | try { |
---|
88 | close(); |
---|
89 | } catch (IOException e) { |
---|
90 | e.printStackTrace(); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | } |
---|
95 | |
---|
96 | public static void main(String args[]){ |
---|
97 | SwfManageTool swfManager = new SwfManageTool(); |
---|
98 | //swfManager.run("src/simulator/utils/workload/DAS.swf"); |
---|
99 | swfManager.run("src/test/ariel/workload/SDSC/SDSC-SP2-1998-3.1-cln-1000_cp.swf"); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | } |
---|