1 | package com.framsticks.experiment; |
---|
2 | |
---|
3 | import com.framsticks.communication.File; |
---|
4 | import com.framsticks.communication.queries.NeedFile; |
---|
5 | import com.framsticks.communication.queries.NeedFileAcceptor; |
---|
6 | import com.framsticks.params.AccessOperations; |
---|
7 | import com.framsticks.params.CastFailure; |
---|
8 | import com.framsticks.params.EventListener; |
---|
9 | import com.framsticks.params.FramsClass; |
---|
10 | import com.framsticks.params.UniqueObject; |
---|
11 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
12 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
13 | import com.framsticks.params.types.BooleanParam; |
---|
14 | import com.framsticks.params.types.EventParam; |
---|
15 | import com.framsticks.params.types.ProcedureParam; |
---|
16 | import com.framsticks.remote.RemoteTree; |
---|
17 | import com.framsticks.structure.Path; |
---|
18 | import com.framsticks.structure.Tree; |
---|
19 | import com.framsticks.structure.messages.ListChange; |
---|
20 | import com.framsticks.structure.messages.ValueChange; |
---|
21 | import com.framsticks.util.ExceptionHandler; |
---|
22 | import com.framsticks.util.FramsticksException; |
---|
23 | import com.framsticks.util.dispatching.AbstractJoinable; |
---|
24 | import com.framsticks.util.dispatching.Dispatcher; |
---|
25 | import com.framsticks.util.dispatching.Dispatching; |
---|
26 | import com.framsticks.util.dispatching.FutureHandler; |
---|
27 | import com.framsticks.util.dispatching.Future; |
---|
28 | import com.framsticks.util.dispatching.Joinable; |
---|
29 | import com.framsticks.util.dispatching.JoinableParent; |
---|
30 | import com.framsticks.util.dispatching.JoinableState; |
---|
31 | import com.framsticks.util.dispatching.RunAt; |
---|
32 | import com.framsticks.util.dispatching.ThrowExceptionHandler; |
---|
33 | import com.framsticks.util.lang.Holder; |
---|
34 | import java.util.concurrent.atomic.AtomicInteger; |
---|
35 | import org.apache.logging.log4j.LogManager; |
---|
36 | import org.apache.logging.log4j.Logger; |
---|
37 | import static com.framsticks.params.ParamsUtil.arguments; |
---|
38 | import static com.framsticks.params.ParamsUtil.getParam; |
---|
39 | import static com.framsticks.structure.TreeOperations.*; |
---|
40 | |
---|
41 | @FramsClassAnnotation |
---|
42 | public final class Simulator extends AbstractJoinable implements Dispatcher<Simulator>, JoinableParent, UniqueObject, ExceptionHandler { |
---|
43 | |
---|
44 | private static final Logger log = LogManager.getLogger(Simulator.class); |
---|
45 | |
---|
46 | protected String uid; |
---|
47 | |
---|
48 | protected final RemoteTree remoteTree; |
---|
49 | protected final Path simulatorPath; |
---|
50 | protected final FramsClass simulatorClass; |
---|
51 | protected final Experiment experiment; |
---|
52 | protected final EventListener<ValueChange> runningListener; |
---|
53 | |
---|
54 | /** |
---|
55 | * |
---|
56 | */ |
---|
57 | public Simulator(Experiment experiment, RemoteTree remoteTree, Path simulatorPath) { |
---|
58 | super(); |
---|
59 | this.remoteTree = remoteTree; |
---|
60 | this.simulatorPath = simulatorPath.assureResolved(); |
---|
61 | this.experiment = experiment; |
---|
62 | this.simulatorClass = getFramsClass(simulatorPath); |
---|
63 | |
---|
64 | assert remoteTree.isActive(); |
---|
65 | assert experiment.isActive(); |
---|
66 | |
---|
67 | log.info("simulator ready {}", this); |
---|
68 | |
---|
69 | runningListener = new EventListener<ValueChange>() { |
---|
70 | @Override |
---|
71 | public void action(ValueChange argument) { |
---|
72 | try { |
---|
73 | boolean running = getParam(simulatorClass, "running", BooleanParam.class).reassign(argument.value, null).getValue(); |
---|
74 | log.debug("running state of {} changed: {}", Simulator.this, running); |
---|
75 | if (!running) { |
---|
76 | Simulator.this.experiment.simulators.fireChildrenChange(Simulator.this, ListChange.Action.Modify, "ready", "stoped"); |
---|
77 | } |
---|
78 | } catch (CastFailure e) { |
---|
79 | log.error("failure: ", e); |
---|
80 | } |
---|
81 | } |
---|
82 | }; |
---|
83 | |
---|
84 | addListener(simulatorPath, getParam(simulatorClass, "running_changed", EventParam.class), runningListener, ValueChange.class, new Future<Void>(this) { |
---|
85 | @Override |
---|
86 | protected void result(Void result) { |
---|
87 | log.debug("running listener for {} registered", this); |
---|
88 | } |
---|
89 | }); |
---|
90 | } |
---|
91 | |
---|
92 | @ParamAnnotation |
---|
93 | public String getAddress() { |
---|
94 | return remoteTree.getAddress(); |
---|
95 | } |
---|
96 | |
---|
97 | @Override |
---|
98 | @ParamAnnotation |
---|
99 | public String getName() { |
---|
100 | return getAddress(); |
---|
101 | } |
---|
102 | |
---|
103 | @Override |
---|
104 | @ParamAnnotation |
---|
105 | public String getUid() { |
---|
106 | return uid; |
---|
107 | } |
---|
108 | |
---|
109 | @Override |
---|
110 | public void setUid(String uid) { |
---|
111 | this.uid = uid; |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * @return the tree |
---|
116 | */ |
---|
117 | @ParamAnnotation |
---|
118 | public RemoteTree getRemoteTree() { |
---|
119 | return remoteTree; |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * @return the simulatorPath |
---|
124 | */ |
---|
125 | public Path getSimulatorPath() { |
---|
126 | return simulatorPath; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * @return the simulatorClass |
---|
131 | */ |
---|
132 | public FramsClass getSimulatorClass() { |
---|
133 | return simulatorClass; |
---|
134 | } |
---|
135 | |
---|
136 | @Override |
---|
137 | protected void joinableStart() { |
---|
138 | Dispatching.use(remoteTree, this); |
---|
139 | } |
---|
140 | |
---|
141 | @Override |
---|
142 | protected void joinableInterrupt() { |
---|
143 | Dispatching.drop(remoteTree, this); |
---|
144 | |
---|
145 | } |
---|
146 | |
---|
147 | @Override |
---|
148 | protected void joinableFinish() { |
---|
149 | |
---|
150 | } |
---|
151 | |
---|
152 | @Override |
---|
153 | protected void joinableJoin() throws InterruptedException { |
---|
154 | Dispatching.join(remoteTree); |
---|
155 | } |
---|
156 | |
---|
157 | @ParamAnnotation(paramType = ProcedureParam.class) |
---|
158 | public void init() { |
---|
159 | } |
---|
160 | |
---|
161 | @ParamAnnotation(paramType = ProcedureParam.class) |
---|
162 | public void start() { |
---|
163 | log.debug("starting simulator {}", this); |
---|
164 | call(simulatorPath, "start", arguments(), Object.class, Future.doNothing(Object.class, this)); |
---|
165 | } |
---|
166 | |
---|
167 | @ParamAnnotation(paramType = ProcedureParam.class) |
---|
168 | public void stop() { |
---|
169 | log.debug("stoping simulator {}", this); |
---|
170 | } |
---|
171 | |
---|
172 | @ParamAnnotation(paramType = ProcedureParam.class) |
---|
173 | public void abort() { |
---|
174 | assert isActive(); |
---|
175 | log.info("explicitly aborting {}", this); |
---|
176 | experiment.removeSimulator(this); |
---|
177 | interruptJoinable(); |
---|
178 | } |
---|
179 | |
---|
180 | @Override |
---|
181 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
182 | proceedToState(state); |
---|
183 | } |
---|
184 | |
---|
185 | @Override |
---|
186 | public void handle(FramsticksException exception) { |
---|
187 | experiment.handle(new FramsticksException().msg("exception caught in simulator").arg("simulator", this).cause(exception)); |
---|
188 | } |
---|
189 | |
---|
190 | @Override |
---|
191 | public boolean isActive() { |
---|
192 | return experiment.isActive(); |
---|
193 | } |
---|
194 | |
---|
195 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
---|
196 | @Override |
---|
197 | public void dispatch(RunAt<? extends Simulator> runnable) { |
---|
198 | experiment.dispatch((RunAt) runnable); |
---|
199 | } |
---|
200 | |
---|
201 | protected final AtomicInteger netloadIdCounter = new AtomicInteger(); |
---|
202 | |
---|
203 | public <N> void netload(final N net, final FutureHandler<Object> future) { |
---|
204 | final String netloadId = "NetLoadSaveLogic" + netloadIdCounter.getAndIncrement(); |
---|
205 | |
---|
206 | final File file = AccessOperations.convert(File.class, net, getRemoteTree().getRegistry()); |
---|
207 | log.debug("uploading file {} to {} identified by {}", file, simulatorPath, netloadId); |
---|
208 | |
---|
209 | final Holder<NeedFileAcceptor> acceptor = new Holder<>(); |
---|
210 | final Tree tree = simulatorPath.getTree(); |
---|
211 | |
---|
212 | acceptor.set(new NeedFileAcceptor() { |
---|
213 | |
---|
214 | @Override |
---|
215 | public boolean acceptNeed(NeedFile needFile) { |
---|
216 | if (!needFile.getDescription().equals(netloadId)) { |
---|
217 | return false; |
---|
218 | } |
---|
219 | log.debug("accepting netload {}", netloadId); |
---|
220 | needFile.getFuture().pass(file); |
---|
221 | tree.dispatch(new RunAt<Tree>(ThrowExceptionHandler.getInstance()) { |
---|
222 | |
---|
223 | @Override |
---|
224 | protected void runAt() { |
---|
225 | tree.removeNeedFileAcceptor(acceptor.get()); |
---|
226 | } |
---|
227 | }); |
---|
228 | return true; |
---|
229 | } |
---|
230 | |
---|
231 | }); |
---|
232 | |
---|
233 | simulatorPath.getTree().addNeedFileAcceptor(Integer.MIN_VALUE, acceptor.get()); |
---|
234 | |
---|
235 | call(simulatorPath, getParam(simulatorPath, "netload_id", ProcedureParam.class), arguments(netloadId), Object.class, new Future<Object>(future) { |
---|
236 | |
---|
237 | @Override |
---|
238 | protected void result(Object result) { |
---|
239 | log.debug("netload of {} done", file); |
---|
240 | future.pass(result); |
---|
241 | } |
---|
242 | }); |
---|
243 | |
---|
244 | } |
---|
245 | |
---|
246 | public <N> void netsave(Class<N> netJavaClass, final FutureHandler<N> futureNet) { |
---|
247 | call(simulatorPath, getParam(simulatorPath, "netsave", ProcedureParam.class), arguments(), netJavaClass, new Future<N>(futureNet) { |
---|
248 | |
---|
249 | @Override |
---|
250 | protected void result(N net) { |
---|
251 | log.debug("netsave of {} done", net); |
---|
252 | futureNet.pass(net); |
---|
253 | } |
---|
254 | }); |
---|
255 | } |
---|
256 | } |
---|