[102] | 1 | package com.framsticks.experiment; |
---|
| 2 | |
---|
| 3 | import org.apache.logging.log4j.Level; |
---|
| 4 | import org.apache.logging.log4j.Logger; |
---|
| 5 | import org.apache.logging.log4j.LogManager; |
---|
| 6 | |
---|
| 7 | import com.framsticks.communication.Address; |
---|
| 8 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
| 9 | import com.framsticks.remote.RemoteTree; |
---|
[105] | 10 | import com.framsticks.structure.Path; |
---|
[102] | 11 | import com.framsticks.util.Misc; |
---|
| 12 | import com.framsticks.util.dispatching.Dispatcher; |
---|
| 13 | import com.framsticks.util.dispatching.Dispatching; |
---|
[105] | 14 | import com.framsticks.util.dispatching.Future; |
---|
[107] | 15 | import com.framsticks.util.dispatching.FutureHandler; |
---|
| 16 | import com.framsticks.util.dispatching.Joinable; |
---|
| 17 | import com.framsticks.util.dispatching.JoinableParent; |
---|
| 18 | import com.framsticks.util.dispatching.JoinableState; |
---|
[102] | 19 | |
---|
[105] | 20 | import static com.framsticks.structure.TreeOperations.*; |
---|
[102] | 21 | |
---|
| 22 | @FramsClassAnnotation |
---|
[107] | 23 | public class SimulatorConnector extends SingleSimulatorProvider implements JoinableParent { |
---|
[102] | 24 | private static final Logger log = LogManager.getLogger(SimulatorConnector.class); |
---|
| 25 | |
---|
| 26 | protected Experiment experiment; |
---|
[107] | 27 | protected RemoteTree remoteTree; |
---|
| 28 | protected Simulator simulator; |
---|
[102] | 29 | protected Address address; |
---|
[107] | 30 | protected boolean provided = false; |
---|
[102] | 31 | |
---|
[107] | 32 | public boolean hasProvided() { |
---|
| 33 | return provided; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | public void setAddress(Address address) { |
---|
| 37 | this.address = address; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | public Address getAddress() { |
---|
| 41 | return address; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | @Override |
---|
| 46 | public String getName() { |
---|
| 47 | return "simulator connector"; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | @Override |
---|
| 51 | protected void joinableStart() { |
---|
| 52 | |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | @Override |
---|
| 56 | protected void joinableInterrupt() { |
---|
| 57 | Dispatching.drop(remoteTree, this); |
---|
| 58 | finishJoinable(); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | @Override |
---|
| 62 | protected void joinableFinish() { |
---|
| 63 | |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | @Override |
---|
| 67 | protected void joinableJoin() throws InterruptedException { |
---|
| 68 | Dispatching.join(remoteTree); |
---|
| 69 | } |
---|
| 70 | |
---|
[102] | 71 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
---|
| 72 | @Override |
---|
[107] | 73 | public void provideSimulator(final SimulatorSpecification specification, final FutureHandler<Simulator> future) { |
---|
| 74 | if (hasProvided()) { |
---|
| 75 | future.pass(null); |
---|
| 76 | return; |
---|
| 77 | } |
---|
| 78 | provided = true; |
---|
| 79 | experiment = specification.getExperiment(); |
---|
[102] | 80 | |
---|
| 81 | Misc.throwIfNull(address); |
---|
| 82 | log.debug("connecting to simulator at {}", address); |
---|
| 83 | // experiment |
---|
| 84 | remoteTree = new RemoteTree(); |
---|
| 85 | remoteTree.setAddress(address); |
---|
| 86 | remoteTree.setDispatcher((Dispatcher) experiment.getDispatcher()); |
---|
| 87 | |
---|
| 88 | experiment.getSimulatorCandidates().add(remoteTree); |
---|
[107] | 89 | Dispatching.use(remoteTree, this); |
---|
[102] | 90 | |
---|
| 91 | Dispatching.dispatchLog(remoteTree, log, Level.DEBUG, "first task in remote tree"); |
---|
| 92 | |
---|
[105] | 93 | tryGet(remoteTree, "/simulator", new Future<Path>(experiment) { |
---|
[102] | 94 | |
---|
| 95 | @Override |
---|
| 96 | protected void result(Path result) { |
---|
| 97 | log.debug("resolved simulator path: {}", result); |
---|
[107] | 98 | Misc.checkEquals(specification.getDefinition(), bindAccess(result).get("expdef", String.class), "expdef mismatch in connected simulator", SimulatorConnector.this); |
---|
[102] | 99 | |
---|
[107] | 100 | simulator = experiment.createSimulator(remoteTree, result); |
---|
| 101 | |
---|
| 102 | future.pass(simulator); |
---|
[102] | 103 | } |
---|
| 104 | }); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | @Override |
---|
[107] | 108 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
| 109 | log.debug("child {} changed state to {}", joinable, state); |
---|
[102] | 110 | } |
---|
| 111 | } |
---|