[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.params.annotations.ParamAnnotation; |
---|
| 10 | import com.framsticks.remote.RemoteTree; |
---|
[105] | 11 | import com.framsticks.structure.Path; |
---|
[102] | 12 | import com.framsticks.util.AutoAttacher; |
---|
| 13 | import com.framsticks.util.Misc; |
---|
| 14 | import com.framsticks.util.dispatching.Dispatcher; |
---|
| 15 | import com.framsticks.util.dispatching.Dispatching; |
---|
[105] | 16 | import com.framsticks.util.dispatching.Future; |
---|
[102] | 17 | import com.framsticks.util.lang.Casting; |
---|
| 18 | import com.framsticks.util.lang.Strings; |
---|
| 19 | |
---|
[105] | 20 | import static com.framsticks.structure.TreeOperations.*; |
---|
[102] | 21 | |
---|
| 22 | @FramsClassAnnotation |
---|
| 23 | public class SimulatorConnector implements AutoAttacher { |
---|
| 24 | private static final Logger log = LogManager.getLogger(SimulatorConnector.class); |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | protected Experiment experiment; |
---|
| 28 | protected Address address; |
---|
| 29 | protected RemoteTree remoteTree; |
---|
| 30 | |
---|
| 31 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
---|
| 32 | @Override |
---|
| 33 | public void attachTo(Object parent) { |
---|
| 34 | experiment = Casting.throwCast(Experiment.class, parent); |
---|
| 35 | |
---|
| 36 | Misc.throwIfNull(address); |
---|
| 37 | log.debug("connecting to simulator at {}", address); |
---|
| 38 | // experiment |
---|
| 39 | remoteTree = new RemoteTree(); |
---|
| 40 | remoteTree.setAddress(address); |
---|
| 41 | remoteTree.setDispatcher((Dispatcher) experiment.getDispatcher()); |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | experiment.getSimulatorCandidates().add(remoteTree); |
---|
| 45 | |
---|
| 46 | Dispatching.dispatchLog(remoteTree, log, Level.DEBUG, "first task in remote tree"); |
---|
| 47 | |
---|
[105] | 48 | tryGet(remoteTree, "/simulator", new Future<Path>(experiment) { |
---|
[102] | 49 | |
---|
| 50 | @Override |
---|
| 51 | protected void result(Path result) { |
---|
| 52 | log.debug("resolved simulator path: {}", result); |
---|
| 53 | Misc.checkEquals(experiment.getExpdef(), bindAccess(result).get("expdef", String.class), "expdef mismatch in connected simulator", this); |
---|
| 54 | |
---|
| 55 | Simulator simulator = new Simulator(experiment, remoteTree, result); |
---|
| 56 | experiment.addSimulator(simulator); |
---|
| 57 | experiment.getSimulatorCandidates().remove(remoteTree); |
---|
| 58 | } |
---|
| 59 | }); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | @ParamAnnotation |
---|
| 63 | public void setAddress(String address) { |
---|
| 64 | this.address = new Address(address); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | @ParamAnnotation |
---|
| 68 | public String getAddress() { |
---|
| 69 | return Strings.toStringNullProof(address, ""); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | @Override |
---|
| 73 | public String toString() { |
---|
| 74 | return "simulator connector to: " + Strings.toStringNullProof(address, "<null>"); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | } |
---|