1 | package de.uka.ipd.sdq.simucomframework.simulationdock; |
2 | |
3 | import java.util.Hashtable; |
4 | import java.util.Map; |
5 | |
6 | import org.apache.log4j.Logger; |
7 | import org.osgi.service.event.Event; |
8 | import org.osgi.service.event.EventAdmin; |
9 | |
10 | import de.uka.ipd.sdq.simulation.IStatusObserver; |
11 | |
12 | public class DebugObserver implements IStatusObserver { |
13 | |
14 | protected static Logger logger = |
15 | Logger.getLogger(DebugObserver.class.getName()); |
16 | |
17 | private boolean isStepping; |
18 | private EventAdmin eventAdmin; |
19 | private SimulationDockService myDock; |
20 | |
21 | private Object suspendedBarrier = new Object(); |
22 | private boolean suspended; |
23 | |
24 | public DebugObserver(EventAdmin eventAdmin, SimulationDockService myDock) { |
25 | super(); |
26 | this.eventAdmin = eventAdmin; |
27 | this.myDock = myDock; |
28 | this.suspended = true; |
29 | } |
30 | |
31 | public void suspend() { |
32 | this.suspended = true; |
33 | synchronized (this.suspendedBarrier) { |
34 | this.suspendedBarrier.notifyAll(); |
35 | } |
36 | } |
37 | |
38 | public void resume() { |
39 | this.suspended = false; |
40 | postEvent("de/uka/ipd/sdq/simucomframework/simucomdock/SIM_RESUMED"); |
41 | |
42 | if (this.isStepping) { |
43 | isStepping = false; |
44 | postEvent("de/uka/ipd/sdq/simucomframework/simucomdock/PERFORMED_STEP"); |
45 | } |
46 | |
47 | synchronized (this.suspendedBarrier) { |
48 | this.suspendedBarrier.notifyAll(); |
49 | } |
50 | } |
51 | |
52 | public void step() { |
53 | if (suspended && !isStepping) { |
54 | this.isStepping = true; |
55 | postEvent("de/uka/ipd/sdq/simucomframework/simucomdock/STARTED_STEP"); |
56 | synchronized (this.suspendedBarrier) { |
57 | this.suspendedBarrier.notifyAll(); |
58 | } |
59 | } |
60 | } |
61 | |
62 | public void updateStatus(int percentDone, double currentSimTime, |
63 | long measurementsTaken) { |
64 | |
65 | if (this.isStepping) { |
66 | isStepping = false; |
67 | postEvent("de/uka/ipd/sdq/simucomframework/simucomdock/PERFORMED_STEP"); |
68 | } |
69 | |
70 | synchronized(this.suspendedBarrier) { |
71 | while (this.suspended && !isStepping) { |
72 | try { |
73 | postEvent("de/uka/ipd/sdq/simucomframework/simucomdock/SIM_SUSPENDED"); |
74 | this.suspendedBarrier.wait(); |
75 | } catch (InterruptedException e) { } |
76 | } |
77 | } |
78 | |
79 | if (this.suspended){ |
80 | postEvent("de/uka/ipd/sdq/simucomframework/simucomdock/SIM_RESUMED"); |
81 | logger.debug("------------------------- Simulation Resumed ----------------------------------"); |
82 | } |
83 | } |
84 | |
85 | private void postEvent(String topic) { |
86 | postEvent(topic, new Hashtable<String,Object>()); |
87 | } |
88 | |
89 | private void postEvent(String topic, Hashtable<String,Object> newProperties) { |
90 | Hashtable<String,Object> properties = new Hashtable<String,Object>(); |
91 | properties.put("DOCK_ID", myDock.getDockId()); |
92 | properties.putAll(newProperties); |
93 | Event event = new Event(topic, (Map)properties); |
94 | eventAdmin.sendEvent(event); |
95 | } |
96 | |
97 | } |