1 | package com.framsticks.experiment; |
---|
2 | |
---|
3 | |
---|
4 | import org.apache.logging.log4j.Logger; |
---|
5 | import org.apache.logging.log4j.LogManager; |
---|
6 | |
---|
7 | import com.framsticks.params.EventListener; |
---|
8 | import com.framsticks.params.MessageLogger; |
---|
9 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
10 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
11 | import com.framsticks.structure.messages.ListChange; |
---|
12 | import com.framsticks.structure.messages.Message; |
---|
13 | import com.framsticks.util.dispatching.FutureHandler; |
---|
14 | import com.framsticks.util.dispatching.Future; |
---|
15 | |
---|
16 | @FramsClassAnnotation |
---|
17 | public abstract class NetLoadSaveLogic<NF extends NetFile> extends AbstractExperimentLogic { |
---|
18 | private static final Logger log = LogManager.getLogger(NetLoadSaveLogic.class); |
---|
19 | |
---|
20 | |
---|
21 | protected final Class<NF> netJavaClass; |
---|
22 | |
---|
23 | protected final MessageLogger messages = new MessageLogger(NetLoadSaveLogic.class); |
---|
24 | |
---|
25 | /** |
---|
26 | * @param experiment |
---|
27 | */ |
---|
28 | public NetLoadSaveLogic(Experiment parentExperiment, Class<NF> netJavaClassArg) { |
---|
29 | super(parentExperiment); |
---|
30 | this.netJavaClass = netJavaClassArg; |
---|
31 | |
---|
32 | experiment.addSimulatorsListener(new EventListener<ListChange>() { |
---|
33 | |
---|
34 | @Override |
---|
35 | public void action(final ListChange change) { |
---|
36 | assert experiment.isActive(); |
---|
37 | final Simulator simulator = experiment.getSimulators().get(change.getIdentifier()); |
---|
38 | log.debug("processing list change: {}", change); |
---|
39 | |
---|
40 | if (change.getAction() == ListChange.Action.Add) { |
---|
41 | log.debug("registering in {}", simulator); |
---|
42 | simulator.getRemoteTree().getRegistry().registerAndBuild(netJavaClass); |
---|
43 | } |
---|
44 | |
---|
45 | if (!change.hasHint("stoped")) { |
---|
46 | issueNetloadIfReady(change, simulator); |
---|
47 | return; |
---|
48 | } |
---|
49 | |
---|
50 | log.debug("issuing netsave to: {}", simulator); |
---|
51 | simulator.netsave(netJavaClass, new Future<NF>(simulator) { |
---|
52 | |
---|
53 | @Override |
---|
54 | protected void result(NF net) { |
---|
55 | log.debug("netsave of {} done: {}", simulator, net.getShortDescription()); |
---|
56 | netsave(simulator, net); |
---|
57 | issueNetloadIfReady(change, simulator); |
---|
58 | } |
---|
59 | }); |
---|
60 | } |
---|
61 | }); |
---|
62 | } |
---|
63 | |
---|
64 | protected void issueNetloadIfReady(ListChange change, final Simulator simulator) { |
---|
65 | if (!change.hasHint("ready")) { |
---|
66 | return; |
---|
67 | } |
---|
68 | log.debug("issuing netload to: {}", simulator); |
---|
69 | netload(simulator, new Future<NF>(simulator) { |
---|
70 | |
---|
71 | @Override |
---|
72 | protected void result(final NF net) { |
---|
73 | if (net == null) { |
---|
74 | log.debug("no file for upload provided - leaving simulator idle"); |
---|
75 | return; |
---|
76 | } |
---|
77 | |
---|
78 | simulator.netload(net, new Future<Object>(this) { |
---|
79 | |
---|
80 | @Override |
---|
81 | protected void result(Object result) { |
---|
82 | NetLoadSaveLogic.this.messages.info("netload", "done " + net.getShortDescription()); |
---|
83 | log.debug("netload of {} done", net.getShortDescription()); |
---|
84 | simulator.start(); |
---|
85 | } |
---|
86 | }); |
---|
87 | } |
---|
88 | }); |
---|
89 | } |
---|
90 | |
---|
91 | public abstract void netload(Simulator simulator, FutureHandler<NF> net); |
---|
92 | |
---|
93 | public abstract void netsave(Simulator simulator, NF net); |
---|
94 | |
---|
95 | /** |
---|
96 | * @return the netJavaClass |
---|
97 | */ |
---|
98 | @ParamAnnotation(name = "Java class representing netfile") |
---|
99 | public String getNetJavaClassName() { |
---|
100 | return netJavaClass.getCanonicalName(); |
---|
101 | } |
---|
102 | |
---|
103 | @ParamAnnotation(id = "messages") |
---|
104 | public void addMessageListener(EventListener<Message> listener) { |
---|
105 | messages.add(listener); |
---|
106 | } |
---|
107 | |
---|
108 | @ParamAnnotation(id = "messages") |
---|
109 | public void removeMessageListener(EventListener<Message> listener) { |
---|
110 | messages.remove(listener); |
---|
111 | } |
---|
112 | |
---|
113 | } |
---|