Ignore:
Timestamp:
07/16/13 23:31:35 (11 years ago)
Author:
psniegowski
Message:

HIGHLIGHTS:

for Joinables running

CHANGELOG:
Add WorkPackageLogic? and classes representing prime experiment state.

Add classes for PrimeExperiment? state.

Extract single netload routine in Simulator.

Working netload with dummy content in PrimeExperiment?.

More development with NetLoadSaveLogic? and PrimeExperiment?.

Improvement around prime.

Improve BufferedDispatcher?.isActive logic.

Add prime-all.xml configuration.

Manual connecting to existing simulators from GUI.

Guard in SimulatorConnector? against expdef mismatch.

Guard against empty target dispatcher in BufferedDispatcher?.

Make BufferedDispatcher? a Dispatcher (and Joinable).

Minor improvements.

Done StackedJoinable?, improve Experiment.

Develop StackedJoinable?.

Add StackedJoinable? utility joinables controller.

Add dependency on apache-commons-lang.

Add ready ListChange? on Simulators.

Improve hints in ListChange?.

Several improvements.

Found bug with dispatching in Experiment.

Minor improvements.

Fix bug with early finishing Server.

Many changes in Dispatching.

Fix bug with connection.

Do not obfuscate log with socket related exceptions.

Add SocketClosedException?.

Add SimulatorConnector?.

Work out conception of experiment composing of logics building blocks.

Rename SinkInterface? to Sink.

Move saving of Accesses into AccessOperations?.

Some improvements to Experiment.

Improve joinables.

Fix issue with joinables closing.

Add direct and managed consoles to popup menu.

Location:
java/main/src/main/java/com/framsticks/experiment
Files:
6 added
2 edited

Legend:

Unmodified
Added
Removed
  • java/main/src/main/java/com/framsticks/experiment/Experiment.java

    r101 r102  
    22
    33import java.util.Map;
     4
     5import org.apache.logging.log4j.Level;
     6import org.apache.logging.log4j.Logger;
     7import org.apache.logging.log4j.LogManager;
    48
    59import com.framsticks.core.ListChange;
    610import com.framsticks.params.EventListener;
     11import com.framsticks.params.ParamFlags;
    712import com.framsticks.params.SimpleUniqueList;
    813import com.framsticks.params.annotations.AutoAppendAnnotation;
    914import com.framsticks.params.annotations.FramsClassAnnotation;
    1015import com.framsticks.params.annotations.ParamAnnotation;
     16import com.framsticks.params.types.ProcedureParam;
     17import com.framsticks.remote.RemoteTree;
     18import com.framsticks.util.FramsticksException;
     19import com.framsticks.util.dispatching.AbstractJoinable;
     20import com.framsticks.util.dispatching.BufferedDispatcher;
     21import com.framsticks.util.dispatching.Dispatcher;
     22import com.framsticks.util.dispatching.DispatcherSetable;
     23import com.framsticks.util.dispatching.Dispatching;
     24import com.framsticks.util.dispatching.ExceptionResultHandler;
     25import com.framsticks.util.dispatching.Joinable;
    1126import com.framsticks.util.dispatching.JoinableCollection;
     27import com.framsticks.util.dispatching.JoinableParent;
     28import com.framsticks.util.dispatching.JoinableState;
     29import com.framsticks.util.dispatching.RunAt;
    1230
    1331@FramsClassAnnotation
    14 public class Experiment extends JoinableCollection<Simulator> {
     32public class Experiment extends AbstractJoinable implements Dispatcher<Experiment>, DispatcherSetable<Experiment>, JoinableParent, ExceptionResultHandler {
     33        private static final Logger log = LogManager.getLogger(Experiment.class);
     34
     35        protected final JoinableCollection<Simulator> simulatorAsJoinables = new JoinableCollection<Simulator>().setObservableName("simulators");
     36
     37        protected final JoinableCollection<RemoteTree> simulatorCandidates = new JoinableCollection<RemoteTree>().setObservableName("candidates");
    1538
    1639        protected final SimpleUniqueList<Simulator> simulators = new SimpleUniqueList<>(Simulator.class, 's');
     40
     41        protected final SimpleUniqueList<Simulator> oldSimulators = new SimpleUniqueList<>(Simulator.class, 's');
     42
     43        protected final BufferedDispatcher<Experiment> bufferedDispatcher = new BufferedDispatcher<>(this);
     44
     45        protected String expdef;
     46
     47        /**
     48         *
     49         */
     50        public Experiment() {
     51                super();
     52                bufferedDispatcher.setBuffer(false);
     53
     54                Dispatching.dispatchLog(this, log, Level.DEBUG, "first task");
     55
     56
     57                simulators.addListener(new EventListener<ListChange>() {
     58                        @Override
     59                        public void action(ListChange argument) {
     60                                if (argument.getAction() == ListChange.Action.Add) {
     61                                        simulators.fireChildrenChange(argument, ListChange.Action.Modify, "ready");
     62                                }
     63                        }
     64                });
     65
     66        }
     67
     68        /**
     69         * @return the simulatorCandidates
     70         */
     71        public JoinableCollection<RemoteTree> getSimulatorCandidates() {
     72                return simulatorCandidates;
     73        }
    1774
    1875        @ParamAnnotation
     
    2178        }
    2279
     80        @ParamAnnotation(id = "old_simulators")
     81        public Map<String, Simulator> getOldSimulators() {
     82                return oldSimulators.getView();
     83        }
     84
     85        /**
     86         * @return the dispatcher
     87         */
     88        @Override
     89        public Dispatcher<Experiment> getDispatcher() {
     90                return bufferedDispatcher;
     91        }
     92
     93        /**
     94         * @param dispatcher the dispatcher to set
     95         */
     96        @Override
     97        public void setDispatcher(Dispatcher<Experiment> dispatcher) {
     98                bufferedDispatcher.setTargetDispatcher(dispatcher);
     99        }
     100
     101        /**
     102         * @return the expdef
     103         */
     104        @ParamAnnotation(flags = ParamFlags.USERREADONLY)
     105        public String getExpdef() {
     106                return expdef;
     107        }
     108
     109        /**
     110         * @param expdef the expdef to set
     111         */
     112        @ParamAnnotation
     113        public void setExpdef(String expdef) {
     114                this.expdef = expdef;
     115        }
     116
    23117        @ParamAnnotation(id = "simulators_changed")
    24118        public void addSimulatorsListener(EventListener<ListChange> listener) {
     
    34128        public void addSimulator(Simulator simulator) {
    35129                simulators.add(simulator);
    36         }
     130                simulatorAsJoinables.add(simulator);
     131
     132
     133
     134
     135
     136        }
     137
     138        protected void removeSimulator(Simulator simulator) {
     139                simulatorAsJoinables.remove(simulator);
     140                simulators.remove(simulator);
     141                oldSimulators.add(simulator);
     142        }
     143
     144        @ParamAnnotation(id = "old_simulators_changed")
     145        public void addOldSimulatorsListener(EventListener<ListChange> listener) {
     146                oldSimulators.addListener(listener);
     147        }
     148
     149        @ParamAnnotation(id = "old_simulators_changed")
     150        public void removeOldSimulatorsListener(EventListener<ListChange> listener) {
     151                oldSimulators.removeListener(listener);
     152        }
     153
     154        @Override
     155        public String getName() {
     156                return "experiment";
     157        }
     158
     159        @Override
     160        public void childChangedState(Joinable joinable, JoinableState state) {
     161                proceedToState(state);
     162        }
     163
     164        @Override
     165        protected void joinableStart() {
     166                bufferedDispatcher.createThreadIfNeeded();
     167                Dispatching.use(bufferedDispatcher, this);
     168
     169                Dispatching.use(simulatorAsJoinables, this);
     170                Dispatching.use(simulatorCandidates, this);
     171        }
     172
     173        @Override
     174        protected void joinableInterrupt() {
     175
     176                Dispatching.drop(simulatorAsJoinables, this);
     177                Dispatching.drop(simulatorCandidates, this);
     178
     179                finishJoinable();
     180        }
     181
     182        @Override
     183        protected void joinableFinish() {
     184                log.debug("finishing experiment {}", this);
     185        }
     186
     187        @Override
     188        protected void joinableJoin() throws InterruptedException {
     189                Dispatching.drop(bufferedDispatcher, this);
     190
     191                Dispatching.join(simulatorAsJoinables);
     192                Dispatching.join(simulatorCandidates);
     193                Dispatching.join(bufferedDispatcher.getTargetDispatcher());
     194        }
     195
     196        @Override
     197        public void handle(FramsticksException exception) {
     198                log.error("caught exception: ", exception);
     199        }
     200
     201        @Override
     202        public boolean isActive() {
     203                return bufferedDispatcher.isActive();
     204        }
     205
     206        @Override
     207        public void dispatch(RunAt<? extends Experiment> runnable) {
     208                bufferedDispatcher.dispatch(runnable);
     209        }
     210
     211        @ParamAnnotation(paramType = ProcedureParam.class)
     212        public void connectToSimulator(String address) {
     213                SimulatorConnector connector = new SimulatorConnector();
     214                connector.setAddress(address);
     215                connector.attachTo(this);
     216        }
     217
    37218}
  • java/main/src/main/java/com/framsticks/experiment/Simulator.java

    r101 r102  
    11package com.framsticks.experiment;
    22
    3 import com.framsticks.params.ParamFlags;
     3import com.framsticks.communication.File;
     4import com.framsticks.communication.queries.NeedFile;
     5import com.framsticks.communication.queries.NeedFileAcceptor;
     6import com.framsticks.core.Path;
     7import com.framsticks.core.Tree;
     8import com.framsticks.core.ValueChange;
     9import com.framsticks.params.EventListener;
     10import com.framsticks.params.FramsClass;
    411import com.framsticks.params.UniqueObject;
    512import com.framsticks.params.annotations.FramsClassAnnotation;
    613import com.framsticks.params.annotations.ParamAnnotation;
     14import com.framsticks.params.types.EventParam;
    715import com.framsticks.params.types.ProcedureParam;
    816import com.framsticks.remote.RemoteTree;
     17import com.framsticks.util.FramsticksException;
    918import com.framsticks.util.dispatching.AbstractJoinable;
     19import com.framsticks.util.dispatching.Dispatcher;
    1020import com.framsticks.util.dispatching.Dispatching;
     21import com.framsticks.util.dispatching.ExceptionResultHandler;
     22import com.framsticks.util.dispatching.Future;
     23import com.framsticks.util.dispatching.FutureHandler;
    1124import com.framsticks.util.dispatching.Joinable;
    1225import com.framsticks.util.dispatching.JoinableParent;
    1326import com.framsticks.util.dispatching.JoinableState;
     27import com.framsticks.util.dispatching.RunAt;
     28import com.framsticks.util.dispatching.ThrowExceptionHandler;
     29import com.framsticks.util.lang.Holder;
     30import java.util.concurrent.atomic.AtomicInteger;
     31import org.apache.logging.log4j.LogManager;
     32import org.apache.logging.log4j.Logger;
     33import static com.framsticks.core.TreeOperations.*;
    1434
    1535@FramsClassAnnotation
    16 public class Simulator extends AbstractJoinable implements JoinableParent, UniqueObject {
     36public final class Simulator extends AbstractJoinable implements Dispatcher<Simulator>, JoinableParent, UniqueObject, ExceptionResultHandler {
     37
     38        private static final Logger log = LogManager.getLogger(Simulator.class);
     39
    1740        protected String uid;
    1841
    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;
    2047
    2148        /**
    2249         *
    2350         */
    24         public Simulator() {
     51        public Simulator(Experiment experiment, RemoteTree remoteTree, Path simulatorPath) {
    2552                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
    3479        public String getAddress() {
    3580                return remoteTree.getAddress();
     
    3984        @ParamAnnotation
    4085        public String getName() {
    41 
    42                 return "simulator " + uid + " " + getAddress();
     86                return getAddress();
    4387        }
    4488
     
    62106        }
    63107
     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
    64122        @Override
    65123        protected void joinableStart() {
    66124                Dispatching.use(remoteTree, this);
    67 
    68125        }
    69126
     
    96153        }
    97154
     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
    98163        @Override
    99164        public void childChangedState(Joinable joinable, JoinableState state) {
     
    101166        }
    102167
     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        }
    103227}
Note: See TracChangeset for help on using the changeset viewer.