1 | package de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework; |
2 | |
3 | import org.apache.log4j.Logger; |
4 | |
5 | import de.uka.ipd.sdq.pipesandfilters.framework.MetaDataInit; |
6 | import de.uka.ipd.sdq.pipesandfilters.framework.PipeData; |
7 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.IRawWriteStrategy; |
8 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.launch.SensorFrameworkConfig; |
9 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.strategies.DemandedTimeWriteDataStrategy; |
10 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.strategies.ExecutionResultWriteDataStrategy; |
11 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.strategies.IWriteDataStrategy; |
12 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.strategies.OverallUtilisationWriteDataStrategy; |
13 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.strategies.ResponseTimeWriteDataStrategy; |
14 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.strategies.UtilisationWriteDataStrategy; |
15 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.strategies.WaitingTimeWriteDataStrategy; |
16 | import de.uka.ipd.sdq.sensorframework.SensorFrameworkDataset; |
17 | import de.uka.ipd.sdq.sensorframework.entities.Experiment; |
18 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
19 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
20 | |
21 | /** |
22 | * Write Strategy for the SensorFramework. |
23 | * |
24 | * @author pmerkle |
25 | * |
26 | */ |
27 | public class SensorFrameworkWriteStrategy implements IRawWriteStrategy { |
28 | |
29 | private static Logger logger = Logger.getLogger(SensorFrameworkWriteStrategy.class.getName()); |
30 | |
31 | private SensorFrameworkMetaDataInit metaData; |
32 | |
33 | private SensorFrameworkConfig sensorFrameworkConfig; |
34 | |
35 | private IDAOFactory daoFactory; |
36 | |
37 | /** |
38 | * All instances of this class uses the same IDAOFactory. Hence it is |
39 | * sufficient when a single writer flushes the data. Several subsequent |
40 | * flushes would be redundant. This variable is used to coordinate the flush |
41 | * between several instances of this writer. It is false when one instance |
42 | * has written data that has not yet been flushed; true else. |
43 | */ |
44 | private static boolean flushed; |
45 | |
46 | private Experiment experiment; |
47 | |
48 | private ExperimentRun run; |
49 | |
50 | private IWriteDataStrategy writeDataStrategy; |
51 | |
52 | @Override |
53 | public void initialize(MetaDataInit metaData) { |
54 | if (metaData instanceof SensorFrameworkMetaDataInit) { |
55 | this.metaData = (SensorFrameworkMetaDataInit) metaData; |
56 | } else { |
57 | throw new IllegalArgumentException("Expected meta data of type " |
58 | + SensorFrameworkMetaDataInit.class.getSimpleName() |
59 | + " but was " + metaData.getClass().getSimpleName()); |
60 | } |
61 | |
62 | if (this.metaData.getRecorderConfiguration() instanceof SensorFrameworkConfig) { |
63 | sensorFrameworkConfig = (SensorFrameworkConfig) this.metaData |
64 | .getRecorderConfiguration(); |
65 | } else { |
66 | throw new IllegalArgumentException("Expected meta data containing " |
67 | + SensorFrameworkConfig.class.getSimpleName() |
68 | + " but was " |
69 | + this.metaData.getRecorderConfiguration().getClass() |
70 | .getSimpleName() + "."); |
71 | } |
72 | |
73 | if (!this.metaData.isRemoteRun()) { |
74 | initialiseNewSensorframework(); |
75 | } else { |
76 | initialiseTempSensorframework(); |
77 | } |
78 | |
79 | // Create sensor |
80 | // TODO Remove hard coded metric names "Response Time", ... Use Enum |
81 | // instead!? |
82 | if (metaData.getMetricName().equals("Response Time")) { |
83 | writeDataStrategy = new ResponseTimeWriteDataStrategy(daoFactory, |
84 | experiment, run); |
85 | } else if (metaData.getMetricName().equals("Waiting Time")) { |
86 | writeDataStrategy = new WaitingTimeWriteDataStrategy(daoFactory, |
87 | experiment, run); |
88 | } else if (metaData.getMetricName().equals("Hold Time")) { |
89 | writeDataStrategy = new WaitingTimeWriteDataStrategy(daoFactory, |
90 | experiment, run); |
91 | } else if (metaData.getMetricName().equals("Demanded Time")) { |
92 | writeDataStrategy = new DemandedTimeWriteDataStrategy(daoFactory, |
93 | experiment, run); |
94 | } else if (metaData.getMetricName().equals("Utilisation")) { |
95 | writeDataStrategy = new UtilisationWriteDataStrategy(daoFactory, |
96 | experiment, run); |
97 | } else if (metaData.getMetricName().equals("Overall Utilisation")) { |
98 | writeDataStrategy = new OverallUtilisationWriteDataStrategy( |
99 | daoFactory, experiment, run); |
100 | } else if (metaData.getMetricName().equals("Execution Time")) { |
101 | writeDataStrategy = new ExecutionResultWriteDataStrategy( |
102 | daoFactory, experiment, run); |
103 | } |
104 | writeDataStrategy.initialise(metaData); |
105 | |
106 | flushed = false; |
107 | } |
108 | |
109 | @Override |
110 | public void writeData(PipeData data) { |
111 | if (!flushed) { |
112 | writeDataStrategy.writeData(data); |
113 | } else { |
114 | logger.warn("Tried to write data, but the pipe has been flushed already"); |
115 | } |
116 | } |
117 | |
118 | @Override |
119 | public synchronized void flush() { |
120 | if (!flushed) { |
121 | flushed = true; |
122 | logger.debug("Flushing SensorFramework data store"); |
123 | daoFactory.store(); |
124 | //do not execute daoFactory.finalizeAndClose() ! This will flush all lists for file-based lists, e.g. experiments, from memory. This should not be done on any DAO requested via the singleton as the lists are not reloaded on next access. |
125 | } |
126 | run = null; |
127 | experiment = null; |
128 | } |
129 | |
130 | private void initialiseNewSensorframework() { |
131 | // Obtain DAOFactory |
132 | daoFactory = SensorFrameworkDataset.singleton().getDataSourceByID( |
133 | sensorFrameworkConfig.getDatasourceID()); |
134 | if (daoFactory == null) |
135 | throw new DatasourceConfigurationInvalidException(); |
136 | |
137 | // Find an existing or create a new Experiment |
138 | if (daoFactory.createExperimentDAO().findByExperimentName( |
139 | metaData.getExperimentName()).size() == 1) { |
140 | experiment = daoFactory.createExperimentDAO().findByExperimentName( |
141 | metaData.getExperimentName()).iterator().next(); |
142 | } else { |
143 | experiment = daoFactory.createExperimentDAO().addExperiment( |
144 | metaData.getExperimentName()); |
145 | } |
146 | |
147 | // Find an existing or create a new ExperimentRun |
148 | for (ExperimentRun r : experiment.getExperimentRuns()) { |
149 | if (r.getExperimentDateTime().equals( |
150 | metaData.getExperimentRunName())) { |
151 | run = r; |
152 | } |
153 | } |
154 | if (run == null) |
155 | run = experiment.addExperimentRun(metaData.getExperimentRunName()); |
156 | |
157 | // run = new SimuComExperimentRunDecorator(this, experiment |
158 | // .addExperimentRun("Run " + new Date())); |
159 | } |
160 | |
161 | // TODO Initialization for R-OSGI |
162 | private void initialiseTempSensorframework() { |
163 | // // TODO!!!! |
164 | // this.daoFactory = new FileDAOFactory("C:/temp/test"); |
165 | // experiment = daoFactory.createExperimentDAO().addExperiment( |
166 | // this.getConfig().getNameExperimentRun()); |
167 | // run = new SimuComExperimentRunDecorator(this, experiment |
168 | // .addExperimentRun("Run " + new Date())); |
169 | } |
170 | |
171 | } |