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