- Timestamp:
- 07/18/13 23:52:25 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/experiment/NetLoadSaveLogic.java
r102 r103 5 5 import org.apache.logging.log4j.LogManager; 6 6 7 import com.framsticks.communication.File;8 7 import com.framsticks.core.ListChange; 8 import com.framsticks.core.Message; 9 9 import com.framsticks.params.EventListener; 10 import com.framsticks.params.MessageLogger; 10 11 import com.framsticks.params.annotations.FramsClassAnnotation; 11 12 import com.framsticks.params.annotations.ParamAnnotation; … … 14 15 15 16 @FramsClassAnnotation 16 public abstract class NetLoadSaveLogic extends AbstractExperimentLogic {17 public abstract class NetLoadSaveLogic<NF extends NetFile> extends AbstractExperimentLogic { 17 18 private static final Logger log = LogManager.getLogger(NetLoadSaveLogic.class); 18 19 20 protected String option = "an option"; 19 21 20 protected String option = "an option"; 22 protected final Class<NF> netJavaClass; 23 24 protected final MessageLogger messages = new MessageLogger(NetLoadSaveLogic.class); 21 25 22 26 /** 23 27 * @param experiment 24 28 */ 25 public NetLoadSaveLogic(Experiment parentExperiment ) {29 public NetLoadSaveLogic(Experiment parentExperiment, Class<NF> netJavaClassArg) { 26 30 super(parentExperiment); 31 this.netJavaClass = netJavaClassArg; 27 32 28 33 experiment.addSimulatorsListener(new EventListener<ListChange>() { 29 34 30 35 @Override 31 public void action( ListChange argument) {36 public void action(final ListChange change) { 32 37 assert experiment.isActive(); 38 final Simulator simulator = experiment.getSimulators().get(change.getIdentifier()); 39 log.debug("processing list change: {}", change); 33 40 34 if (argument.hasHint("ready")) { 35 final Simulator simulator = experiment.getSimulators().get(argument.getIdentifier()); 36 log.debug("simulator is ready: {}", simulator); 41 if (change.getAction() == ListChange.Action.Add) { 42 log.debug("registering in {}", simulator); 43 simulator.getRemoteTree().getRegistry().registerAndBuild(netJavaClass); 44 } 37 45 38 netload(simulator, new FutureHandler<File>(simulator) { 46 if (!change.hasHint("stoped")) { 47 issueNetloadIfReady(change, simulator); 48 return; 49 } 39 50 40 @Override 41 protected void result(final File file) { 42 simulator.uploadNet(file, new FutureHandler<Object>(this) { 51 log.debug("issuing netsave to: {}", simulator); 52 simulator.netsave(netJavaClass, new FutureHandler<NF>(simulator) { 43 53 44 @Override 45 protected void result(Object result) { 46 log.debug("netload of {} done", file); 47 } 48 }); 49 } 50 }); 51 } 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 }); 52 61 } 53 62 }); 54 63 } 55 64 56 public abstract void netload(Simulator simulator, Future<File> net); 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 FutureHandler<NF>(simulator) { 57 71 58 public abstract void netsave(Simulator simulator, File net); 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 FutureHandler<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, Future<NF> net); 93 94 public abstract void netsave(Simulator simulator, NF net); 59 95 60 96 /** … … 74 110 } 75 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 76 130 }
Note: See TracChangeset
for help on using the changeset viewer.