1 | package de.ugoe.cs.eventbench;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileOutputStream;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.OutputStreamWriter;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import de.ugoe.cs.eventbench.data.IReplayable;
|
---|
10 | import de.ugoe.cs.eventbench.data.ReplayableEvent;
|
---|
11 | import de.ugoe.cs.util.StringTools;
|
---|
12 | import de.ugoe.cs.util.console.Console;
|
---|
13 |
|
---|
14 | public class ReplayGenerator {
|
---|
15 |
|
---|
16 | private IReplayDecorator decorator = null;
|
---|
17 |
|
---|
18 | int sessionId = 1;
|
---|
19 |
|
---|
20 | public void createLogfileMultipleSessions(List<List<ReplayableEvent<?>>> sequences, String filename) {
|
---|
21 | OutputStreamWriter writer = openReplayFile(filename);
|
---|
22 | if( writer!=null ) {
|
---|
23 | try {
|
---|
24 | decorator = sequences.get(0).get(0).getReplayDecorator();
|
---|
25 | if( decorator!=null ) {
|
---|
26 | writer.write(decorator.getHeader());
|
---|
27 | }
|
---|
28 | for( List<ReplayableEvent<?>> actions : sequences ) {
|
---|
29 | writeSession(actions, writer);
|
---|
30 | }
|
---|
31 | if( decorator!=null ) {
|
---|
32 | writer.write(decorator.getFooter());
|
---|
33 | }
|
---|
34 | decorator = null;
|
---|
35 | writer.close();
|
---|
36 | } catch (IOException e) {
|
---|
37 | Console.printerrln("Unable to write replay file " + filename);
|
---|
38 | }
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void createLogfileSingleSession(List<ReplayableEvent<?>> actions, String filename) {
|
---|
43 | OutputStreamWriter writer = openReplayFile(filename);
|
---|
44 | if( writer!=null ) {
|
---|
45 | try {
|
---|
46 | actions.get(0).getReplayDecorator();
|
---|
47 | if( decorator!=null ) {
|
---|
48 | writer.write(decorator.getHeader());
|
---|
49 | }
|
---|
50 | writeSession(actions, writer);
|
---|
51 | if( decorator!=null ) {
|
---|
52 | writer.write(decorator.getFooter());
|
---|
53 | }
|
---|
54 | decorator = null;
|
---|
55 | writer.close();
|
---|
56 | } catch (IOException e) {
|
---|
57 | Console.printerrln("Unable to write replay file " + filename);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | private OutputStreamWriter openReplayFile(String filename) {
|
---|
63 | File file = new File(filename);
|
---|
64 | boolean fileCreated;
|
---|
65 | try {
|
---|
66 | fileCreated = file.createNewFile();
|
---|
67 | if( !fileCreated ) {
|
---|
68 | Console.traceln("Created logfile " + filename);
|
---|
69 | } else {
|
---|
70 | Console.traceln("Overwrote existing logfile " + filename);
|
---|
71 | }
|
---|
72 | } catch (IOException e) {
|
---|
73 | Console.printerrln("Unable to create file " + filename);
|
---|
74 | Console.printStacktrace(e);
|
---|
75 | }
|
---|
76 | OutputStreamWriter writer = null;
|
---|
77 | try {
|
---|
78 | writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-16");
|
---|
79 | } catch (IOException e) {
|
---|
80 | Console.printerrln("Unable to open file for writing (read-only file):" + filename);
|
---|
81 | Console.printStacktrace(e);
|
---|
82 | }
|
---|
83 | return writer;
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void writeSession(List<ReplayableEvent<?>> actions, OutputStreamWriter writer)
|
---|
87 | throws IOException {
|
---|
88 | if( decorator!=null ) {
|
---|
89 | writer.write(decorator.getSessionHeader(sessionId));
|
---|
90 | }
|
---|
91 | for( ReplayableEvent<?> currentAction : actions ) {
|
---|
92 |
|
---|
93 | List<? extends IReplayable> replayables = currentAction.getReplayMessages();
|
---|
94 | for( IReplayable replayble : replayables ) {
|
---|
95 | writer.write(replayble.getReplay()+StringTools.ENDLINE);
|
---|
96 | writer.flush();
|
---|
97 | }
|
---|
98 | }
|
---|
99 | if( decorator!=null ) {
|
---|
100 | writer.write(decorator.getSessionFooter(sessionId));
|
---|
101 | }
|
---|
102 | sessionId++;
|
---|
103 | }
|
---|
104 |
|
---|
105 | }
|
---|