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 | protected String option = "an option"; |
---|
21 | |
---|
22 | protected final Class<NF> netJavaClass; |
---|
23 | |
---|
24 | protected final MessageLogger messages = new MessageLogger(NetLoadSaveLogic.class); |
---|
25 | |
---|
26 | /** |
---|
27 | * @param experiment |
---|
28 | */ |
---|
29 | public NetLoadSaveLogic(Experiment parentExperiment, Class<NF> netJavaClassArg) { |
---|
30 | super(parentExperiment); |
---|
31 | this.netJavaClass = netJavaClassArg; |
---|
32 | |
---|
33 | experiment.addSimulatorsListener(new EventListener<ListChange>() { |
---|
34 | |
---|
35 | @Override |
---|
36 | public void action(final ListChange change) { |
---|
37 | assert experiment.isActive(); |
---|
38 | final Simulator simulator = experiment.getSimulators().get(change.getIdentifier()); |
---|
39 | log.debug("processing list change: {}", change); |
---|
40 | |
---|
41 | if (change.getAction() == ListChange.Action.Add) { |
---|
42 | log.debug("registering in {}", simulator); |
---|
43 | simulator.getRemoteTree().getRegistry().registerAndBuild(netJavaClass); |
---|
44 | } |
---|
45 | |
---|
46 | if (!change.hasHint("stoped")) { |
---|
47 | issueNetloadIfReady(change, simulator); |
---|
48 | return; |
---|
49 | } |
---|
50 | |
---|
51 | log.debug("issuing netsave to: {}", simulator); |
---|
52 | simulator.netsave(netJavaClass, new Future<NF>(simulator) { |
---|
53 | |
---|
54 | @Override |
---|
55 | protected void result(NF net) { |
---|
56 | log.debug("netsave of {} done: {}", simulator, net.getShortDescription()); |
---|
57 | netsave(simulator, net); |
---|
58 | issueNetloadIfReady(change, simulator); |
---|
59 | } |
---|
60 | }); |
---|
61 | } |
---|
62 | }); |
---|
63 | } |
---|
64 | |
---|
65 | protected void issueNetloadIfReady(ListChange change, final Simulator simulator) { |
---|
66 | if (!change.hasHint("ready")) { |
---|
67 | return; |
---|
68 | } |
---|
69 | log.debug("issuing netload to: {}", simulator); |
---|
70 | netload(simulator, new Future<NF>(simulator) { |
---|
71 | |
---|
72 | @Override |
---|
73 | protected void result(final NF net) { |
---|
74 | if (net == null) { |
---|
75 | log.debug("no file for upload provided - leaving simulator idle"); |
---|
76 | return; |
---|
77 | } |
---|
78 | |
---|
79 | simulator.netload(net, new Future<Object>(this) { |
---|
80 | |
---|
81 | @Override |
---|
82 | protected void result(Object result) { |
---|
83 | NetLoadSaveLogic.this.messages.info("netload", "done " + net.getShortDescription()); |
---|
84 | log.debug("netload of {} done", net.getShortDescription()); |
---|
85 | simulator.start(); |
---|
86 | } |
---|
87 | }); |
---|
88 | } |
---|
89 | }); |
---|
90 | } |
---|
91 | |
---|
92 | public abstract void netload(Simulator simulator, FutureHandler<NF> net); |
---|
93 | |
---|
94 | public abstract void netsave(Simulator simulator, NF net); |
---|
95 | |
---|
96 | /** |
---|
97 | * @return the option |
---|
98 | */ |
---|
99 | @ParamAnnotation |
---|
100 | public String getOption() { |
---|
101 | return option; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * @param option the option to set |
---|
106 | */ |
---|
107 | @ParamAnnotation |
---|
108 | public void setOption(String option) { |
---|
109 | this.option = option; |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * @return the netJavaClass |
---|
114 | */ |
---|
115 | @ParamAnnotation(name = "Java class representing netfile") |
---|
116 | public String getNetJavaClassName() { |
---|
117 | return netJavaClass.getCanonicalName(); |
---|
118 | } |
---|
119 | |
---|
120 | @ParamAnnotation(id = "messages") |
---|
121 | public void addMessageListener(EventListener<Message> listener) { |
---|
122 | messages.add(listener); |
---|
123 | } |
---|
124 | |
---|
125 | @ParamAnnotation(id = "messages") |
---|
126 | public void removeMessageListener(EventListener<Message> listener) { |
---|
127 | messages.remove(listener); |
---|
128 | } |
---|
129 | |
---|
130 | } |
---|