- Timestamp:
- 07/16/13 23:31:35 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/experiment/Simulator.java
r101 r102 1 1 package com.framsticks.experiment; 2 2 3 import com.framsticks.params.ParamFlags; 3 import com.framsticks.communication.File; 4 import com.framsticks.communication.queries.NeedFile; 5 import com.framsticks.communication.queries.NeedFileAcceptor; 6 import com.framsticks.core.Path; 7 import com.framsticks.core.Tree; 8 import com.framsticks.core.ValueChange; 9 import com.framsticks.params.EventListener; 10 import com.framsticks.params.FramsClass; 4 11 import com.framsticks.params.UniqueObject; 5 12 import com.framsticks.params.annotations.FramsClassAnnotation; 6 13 import com.framsticks.params.annotations.ParamAnnotation; 14 import com.framsticks.params.types.EventParam; 7 15 import com.framsticks.params.types.ProcedureParam; 8 16 import com.framsticks.remote.RemoteTree; 17 import com.framsticks.util.FramsticksException; 9 18 import com.framsticks.util.dispatching.AbstractJoinable; 19 import com.framsticks.util.dispatching.Dispatcher; 10 20 import com.framsticks.util.dispatching.Dispatching; 21 import com.framsticks.util.dispatching.ExceptionResultHandler; 22 import com.framsticks.util.dispatching.Future; 23 import com.framsticks.util.dispatching.FutureHandler; 11 24 import com.framsticks.util.dispatching.Joinable; 12 25 import com.framsticks.util.dispatching.JoinableParent; 13 26 import com.framsticks.util.dispatching.JoinableState; 27 import com.framsticks.util.dispatching.RunAt; 28 import com.framsticks.util.dispatching.ThrowExceptionHandler; 29 import com.framsticks.util.lang.Holder; 30 import java.util.concurrent.atomic.AtomicInteger; 31 import org.apache.logging.log4j.LogManager; 32 import org.apache.logging.log4j.Logger; 33 import static com.framsticks.core.TreeOperations.*; 14 34 15 35 @FramsClassAnnotation 16 public class Simulator extends AbstractJoinable implements JoinableParent, UniqueObject { 36 public final class Simulator extends AbstractJoinable implements Dispatcher<Simulator>, JoinableParent, UniqueObject, ExceptionResultHandler { 37 38 private static final Logger log = LogManager.getLogger(Simulator.class); 39 17 40 protected String uid; 18 41 19 protected final RemoteTree remoteTree = new RemoteTree(); 42 protected final RemoteTree remoteTree; 43 protected final Path simulatorPath; 44 protected final FramsClass simulatorClass; 45 protected final Experiment experiment; 46 protected final EventListener<ValueChange> runningListener; 20 47 21 48 /** 22 49 * 23 50 */ 24 public Simulator( ) {51 public Simulator(Experiment experiment, RemoteTree remoteTree, Path simulatorPath) { 25 52 super(); 26 } 27 28 @ParamAnnotation 29 public void setAddress(String address) { 30 remoteTree.setAddress(address); 31 } 32 33 @ParamAnnotation(flags = ParamFlags.USERREADONLY) 53 this.remoteTree = remoteTree; 54 this.simulatorPath = simulatorPath.assureResolved(); 55 this.experiment = experiment; 56 this.simulatorClass = getFramsClass(simulatorPath); 57 58 assert remoteTree.isActive(); 59 assert experiment.isActive(); 60 61 log.debug("simulator ready {}", this); 62 63 runningListener = new EventListener<ValueChange>() { 64 @Override 65 public void action(ValueChange argument) { 66 log.debug("running state of {} changed: {}", this, argument); 67 } 68 }; 69 70 addListener(simulatorPath, simulatorClass.getParamEntry("running_changed", EventParam.class), runningListener, ValueChange.class, new FutureHandler<Void>(this) { 71 @Override 72 protected void result(Void result) { 73 log.debug("running listener for {} registered", this); 74 } 75 }); 76 } 77 78 @ParamAnnotation 34 79 public String getAddress() { 35 80 return remoteTree.getAddress(); … … 39 84 @ParamAnnotation 40 85 public String getName() { 41 42 return "simulator " + uid + " " + getAddress(); 86 return getAddress(); 43 87 } 44 88 … … 62 106 } 63 107 108 /** 109 * @return the simulatorPath 110 */ 111 public Path getSimulatorPath() { 112 return simulatorPath; 113 } 114 115 /** 116 * @return the simulatorClass 117 */ 118 public FramsClass getSimulatorClass() { 119 return simulatorClass; 120 } 121 64 122 @Override 65 123 protected void joinableStart() { 66 124 Dispatching.use(remoteTree, this); 67 68 125 } 69 126 … … 96 153 } 97 154 155 @ParamAnnotation(paramType = ProcedureParam.class) 156 public void abort() { 157 assert isActive(); 158 log.debug("explicitly aborting {}", this); 159 experiment.removeSimulator(this); 160 interruptJoinable(); 161 } 162 98 163 @Override 99 164 public void childChangedState(Joinable joinable, JoinableState state) { … … 101 166 } 102 167 168 @Override 169 public void handle(FramsticksException exception) { 170 experiment.handle(new FramsticksException().msg("exception caught in simulator").arg("simulator", this).cause(exception)); 171 } 172 173 @Override 174 public boolean isActive() { 175 return experiment.isActive(); 176 } 177 178 @SuppressWarnings({ "rawtypes", "unchecked" }) 179 @Override 180 public void dispatch(RunAt<? extends Simulator> runnable) { 181 experiment.dispatch((RunAt) runnable); 182 } 183 184 protected final AtomicInteger netloadIdCounter = new AtomicInteger(); 185 186 public void uploadNet(final File file, final Future<Object> future) { 187 final String netloadId = "NetLoadSaveLogic" + netloadIdCounter.getAndIncrement(); 188 189 log.debug("uploading file {} to {} identified by {}", file, simulatorPath, netloadId); 190 191 final Holder<NeedFileAcceptor> acceptor = new Holder<>(); 192 final Tree tree = simulatorPath.getTree(); 193 194 acceptor.set(new NeedFileAcceptor() { 195 196 @Override 197 public boolean acceptNeed(NeedFile needFile) { 198 if (!needFile.getDescription().equals(netloadId)) { 199 return false; 200 } 201 log.debug("accepting netload {}", netloadId); 202 needFile.getFuture().pass(file); 203 tree.dispatch(new RunAt<Tree>(ThrowExceptionHandler.getInstance()) { 204 205 @Override 206 protected void runAt() { 207 tree.removeNeedFileAcceptor(acceptor.get()); 208 } 209 }); 210 return true; 211 } 212 213 }); 214 215 simulatorPath.getTree().addNeedFileAcceptor(Integer.MIN_VALUE, acceptor.get()); 216 217 call(simulatorPath, getFramsClass(simulatorPath).getParamEntry("netload_id", ProcedureParam.class), new Object[] { netloadId }, new FutureHandler<Object>(future) { 218 219 @Override 220 protected void result(Object result) { 221 log.debug("upload of {} done", file); 222 future.pass(result); 223 } 224 }); 225 226 } 103 227 }
Note: See TracChangeset
for help on using the changeset viewer.