Ignore:
Timestamp:
06/22/13 21:51:33 (11 years ago)
Author:
psniegowski
Message:

HIGHLIGHTS:

  • simplification of entities management model
  • cleanup around params (improve hierarchy)
  • migrate from JUnit to TestNG
  • introduce FEST to automatically test GUI
  • improve slider control
  • loosen synchronization between gui tree and backend representation
  • and many other bug fixes

NOTICE:

  • a great many of lines is changed only because of substituting spaces with tabs

CHANGELOG (oldest changes at the bottom):

Some cleaning after fix found.

Fix bug with tree.

More changes with TreeNodes?.

Finally fix issue with tree.

Improve gui tree management.

Decouple update of values from fetch request in gui.

Minor changes.

Minor changes.

Minor change.

Change Path construction wording.

More fixes to SliderControl?.

Fix SliderControl?.

Fix SliderControl?.

Minor improvement.

Several changes.

Make NumberParam? a generic class.

Add robot to the gui test.

Setup common testing logging configuration.

Remove Parameters class.

Remove entityOwner from Parameters.

Move name out from Parameters class.

Move configuration to after the construction.

Simplify observers and endpoints.

Remove superfluous configureEntity overrides.

Add dependency on fest-swing-testng.

Use FEST for final print test.

Use FEST for more concise and readable assertions.

Divide test of F0Parser into multiple methods.

Migrate to TestNG

Minor change.

Change convention from LOGGER to log.

Fix reporting of errors during controls filling.

Bound maximal height of SliderControl?.

Minor improvements.

Improve tooltips for controls.

Also use Delimeted in more places.

Move static control utilities to Gui.

Rename package gui.components to controls.

Some cleaning in controls.

Improve Param classes placing.

Move ValueParam?, PrimitiveParam? and CompositeParam? one package up.

Improve ParamBuilder?.

Move getDef to ValueParam? and PrimitiveParam?.

Move getMax and getDef to ValueParam?.

Move getMin to ValueParam?.

Upgrade to laters apache commons versions.

Use filterInstanceof extensively.

Add instanceof filters.

Make ValueParam? in many places of Param.

Place assertions about ValueParam?.

Add ValueParam?

Rename ValueParam? to PrimitiveParam?

Minor changes.

Several improvements to params types.

Add NumberParam?.

Add TextControl? component.

Add .swp files to .gitignore

Greatly improved slider component.

Some improvements.

Make Param.reassign return also a state.

Add IterableIterator?.

Several changes.

  • Move util classes to better packages.
  • Remove warnings from eclim.

Several improvements.

Fix bug with BooleanParam?.

Some experiments with visualization.

Another fix to panel management.

Improve panel management.

Some refactorization around panels.

Add root class for panel.

Location:
java/main
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • java/main

    • Property svn:ignore set to
      target
  • java/main/src/main/java/com/framsticks/remote/RemoteInstance.java

    r77 r84  
    77import com.framsticks.communication.util.LoggingSubscriptionCallback;
    88import com.framsticks.core.ListChange;
    9 import com.framsticks.core.Parameters;
    109import com.framsticks.core.Path;
    1110import com.framsticks.params.*;
    12 import com.framsticks.params.types.CompositeParam;
    1311import com.framsticks.params.types.EventParam;
    1412import com.framsticks.parsers.MultiParamLoader;
    1513import com.framsticks.core.Instance;
    1614import com.framsticks.util.*;
     15import com.framsticks.util.dispatching.Dispatching;
     16import com.framsticks.util.dispatching.Future;
     17import com.framsticks.util.lang.Casting;
     18import com.framsticks.util.lang.Pair;
     19
     20import org.apache.commons.configuration.Configuration;
    1721import org.apache.log4j.Logger;
    1822
     
    2428public class RemoteInstance extends Instance {
    2529
    26         private final static Logger LOGGER = Logger.getLogger(RemoteInstance.class.getName());
    27 
    28     protected Path simulator;
    29     protected final ClientConnection connection;
    30 
    31     protected final Set<Pair<Path, Subscription>> subscriptions = new HashSet<Pair<Path, Subscription>>();
    32 
    33     public Pair<Path, Subscription> getSubscription(Path path) {
    34         for (Pair<Path, Subscription> s : subscriptions) {
    35             if (s.first.matches(path)) {
    36                 return s;
    37             }
    38         }
    39         return null;
    40     }
    41 
    42     @Override
    43     public void run() {
    44         assert isActive();
    45         super.run();
    46         connection.connect(new StateFunctor() {
     30        private final static Logger log = Logger.getLogger(RemoteInstance.class.getName());
     31
     32        protected Path simulator;
     33        protected ClientConnection connection;
     34
     35        protected final Set<Pair<Path, Subscription>> subscriptions = new HashSet<Pair<Path, Subscription>>();
     36
     37        public Pair<Path, Subscription> getSubscription(Path path) {
     38                for (Pair<Path, Subscription> s : subscriptions) {
     39                        if (s.first.matches(path)) {
     40                                return s;
     41                        }
     42                }
     43                return null;
     44        }
     45
     46        @Override
     47        public void run() {
     48                assert isActive();
     49                super.run();
     50                connection.connect(new StateFunctor() {
    4751                        @Override
    4852                        public void call(Exception e) {
    49                 if (e != null) {
    50                     fireRun(e);
    51                     return;
    52                 }
     53                                if (e != null) {
     54                                        fireRun(e);
     55                                        return;
     56                                }
    5357                                connection.negotiateProtocolVersion(new StateFunctor() {
    5458                                        @Override
    5559                                        public void call(Exception e) {
    5660                                                if (e != null) {
    57                                                         LOGGER.fatal("unsupported protocol version!\n minimal version is: "
     61                                                        log.fatal("unsupported protocol version!\n minimal version is: "
    5862                                                                        + "nmanager protocol is: "
    5963                                                                        + connection.getProtocolVersion());
    6064                                                        connection.close();
    61                             fireRun(e);
     65                                                        fireRun(e);
    6266                                                        return;
    6367                                                }
    6468
    65                         invokeLater(new Runnable() {
    66                             @Override
    67                             public void run() {
    68                                 resolveAndFetch("/simulator", new Future<Path>() {
    69                                     @Override
    70                                     public void result(Path path, Exception e) {
    71                                         if (e != null) {
    72                                             LOGGER.fatal("failed to resolve simulator node");
    73                                             fireRun(e);
    74                                             return;
    75                                         }
    76                                         assert isActive();
    77                                         simulator = path;
    78                                         fireRun(null);
    79                                         LOGGER.info("resolved simulator node");
    80 
    81                                         EventParam param = getParam(simulator, "running_changed", EventParam.class);
    82                                         assert param != null;
    83                                         connection.subscribe(simulator.getTextual() + "/" + param.getId(), new LoggingSubscriptionCallback(LOGGER, "server running state change", new EventCallback() {
    84                                             @Override
    85                                             public void call(List<File> files) {
    86                                                 invokeLater(new Runnable() {
    87                                                     @Override
    88                                                     public void run() {
    89                                                         updateSimulationRunning();
    90                                                     }
    91                                                 });
    92                                             }
    93                                         }));
    94                                         new PeriodicTask(RemoteInstance.this, 1000) {
    95                                             @Override
    96                                             public void run() {
    97                                                 updateSimulationRunning();
    98                                                 again();
    99                                             }
    100                                         };
    101                                     }
    102                                 });
    103                             }
    104                         });
     69                                                invokeLater(new Runnable() {
     70                                                        @Override
     71                                                        public void run() {
     72                                                                resolveAndFetch("/simulator", new Future<Path>() {
     73                                                                        @Override
     74                                                                        public void result(Path path, Exception e) {
     75                                                                                if (e != null) {
     76                                                                                        log.fatal("failed to resolve simulator node");
     77                                                                                        fireRun(e);
     78                                                                                        return;
     79                                                                                }
     80                                                                                assert isActive();
     81                                                                                simulator = path;
     82                                                                                fireRun(null);
     83                                                                                log.info("resolved simulator node");
     84
     85                                                                                EventParam param = getParam(simulator, "running_changed", EventParam.class);
     86                                                                                assert param != null;
     87                                                                                connection.subscribe(simulator.getTextual() + "/" + param.getId(), new LoggingSubscriptionCallback(log, "server running state change", new EventCallback() {
     88                                                                                        @Override
     89                                                                                        public void call(List<File> files) {
     90                                                                                                invokeLater(new Runnable() {
     91                                                                                                        @Override
     92                                                                                                        public void run() {
     93                                                                                                                updateSimulationRunning();
     94                                                                                                        }
     95                                                                                                });
     96                                                                                        }
     97                                                                                }));
     98                                                                                new PeriodicTask(RemoteInstance.this, 1000) {
     99                                                                                        @Override
     100                                                                                        public void run() {
     101                                                                                                updateSimulationRunning();
     102                                                                                                again();
     103                                                                                        }
     104                                                                                };
     105                                                                        }
     106                                                                });
     107                                                        }
     108                                                });
    105109                                        }
    106110                                });
     
    109113        }
    110114
    111         public RemoteInstance(Parameters parameters) {
    112         super(parameters);
    113         connection = new ClientConnection(config.getString("address"));
    114     }
     115        public RemoteInstance() {
     116        }
     117
     118
     119        @Override
     120        public void configure(Configuration config) {
     121                super.configure(config);
     122                connection = new ClientConnection(config.getString("address"));
     123        }
     124
     125
     126        public void setConnection(ClientConnection connection) {
     127                this.connection = connection;
     128        }
    115129
    116130        @Override
    117131        public String toString() {
    118         assert Dispatching.isThreadSafe();
     132                assert Dispatching.isThreadSafe();
    119133                return getConnection().getAddress();
    120134        }
    121135
    122136        public void setRunning(final boolean running) {
    123         assert isActive();
    124         //simulator.call(simulator.getParam(running ? "start" : "stop", ProcedureParam.class), new LoggingStateCallback(LOGGER, (running ? "starting" : "stopping") + " server"));
     137                assert isActive();
     138                //simulator.call(simulator.getParam(running ? "start" : "stop", ProcedureParam.class), new LoggingStateCallback(log, (running ? "starting" : "stopping") + " server"));
    125139        }
    126140
     
    128142
    129143        protected void updateSimulationRunning() {
    130         assert isActive();
    131         /*
    132         fetchValue(simulator, getParam(simulator, "running", Param.class), new StateFunctor() {
    133             @Override
    134             public void call(Exception e) {
    135                 if (e != null) {
    136                     LOGGER.fatal("failed to query simulator running status: " + e);
    137                     return;
    138                 }
    139 
    140                 invokeLater(new Runnable() {
    141                     @Override
    142                     public void run() {
    143                         boolean value = bindAccess(simulator).get("running", Boolean.class);
    144                         LOGGER.trace("server running: " + value);
    145                         simulationRunningListeners.call(value);
    146                     }
    147                 });
    148 
    149             }
    150         });
    151         */
     144                assert isActive();
     145                /*
     146                fetchValue(simulator, getParam(simulator, "running", Param.class), new StateFunctor() {
     147                        @Override
     148                        public void call(Exception e) {
     149                                if (e != null) {
     150                                        log.fatal("failed to query simulator running status: " + e);
     151                                        return;
     152                                }
     153
     154                                invokeLater(new Runnable() {
     155                                        @Override
     156                                        public void run() {
     157                                                boolean value = bindAccess(simulator).get("running", Boolean.class);
     158                                                log.trace("server running: " + value);
     159                                                simulationRunningListeners.call(value);
     160                                        }
     161                                });
     162
     163                        }
     164                });
     165                */
    152166        }
    153167
    154168        public void addRunningStateListener(UnaryFunctor<Boolean, Boolean> listener) {
    155         assert isActive();
    156         simulationRunningListeners.add(listener);
     169                assert isActive();
     170                simulationRunningListeners.add(listener);
    157171        }
    158172
    159173        public void disconnect() {
    160         assert isActive();
    161         if (connection.isConnected()) {
     174                assert isActive();
     175                if (connection.isConnected()) {
    162176                        connection.close();
    163177                }
    164178        }
    165179
    166     public final ClientConnection getConnection() {
    167         return connection;
    168     }
    169 
    170     @Override
    171     public void fetchValue(final Path path, final Param param, final StateFunctor stateFunctor) {
    172         assert isActive();
    173         assert param != null;
    174         assert path.isResolved();
    175         connection.send(new GetRequest().setField(param.getId()).setPath(path.getTextual()), this, new ResponseCallback() {
    176             @Override
    177             public void process(Response response) {
    178                 assert isActive();
    179                 if (!response.getOk()) {
    180                     stateFunctor.call(new Exception(response.getComment()));
    181                     return;
    182                 }
    183                 try {
    184                     processFetchedValues(path, response.getFiles());
    185                     stateFunctor.call(null);
    186                 } catch (Exception ex) {
    187                     stateFunctor.call(ex);
    188                 }
    189             }
    190         });
    191     }
    192 
    193     protected final Map<String, Set<Future<FramsClass>>> infoRequests = new HashMap<String, Set<Future<FramsClass>>>();
    194 
    195     protected void finishInfoRequest(String id, FramsClass result, Exception e) {
    196         assert isActive();
    197         Set<Future<FramsClass>> futures = infoRequests.get(id);
    198         infoRequests.remove(id);
    199         for (Future<FramsClass> f : futures) {
    200             f.result(result, e);
    201         }
    202     }
    203 
    204     @Override
    205     protected void fetchInfo(final Path path, final Future<FramsClass> future) {
    206 
    207         final String name = path.getTop().getParam().getContainedTypeName();
    208 
    209         if (infoRequests.containsKey(name)) {
    210             infoRequests.get(name).add(future);
    211             return;
    212         }
    213 
    214         LOGGER.debug("issuing info request for " + name);
    215         Set<Future<FramsClass>> futures = new HashSet<Future<FramsClass>>();
    216         futures.add(future);
    217         infoRequests.put(name, futures);
    218 
    219         //TODO: if the info is in the cache, then don't communicate
    220         connection.send(new InfoRequest().setPath(path.getTextual()), this, new ResponseCallback() {
    221             @Override
    222             public void process(Response response) {
    223                 assert isActive();
    224                 if (!response.getOk()) {
    225                     finishInfoRequest(name, null, new Exception(response.getComment()));
    226                     return;
    227                 }
    228 
    229                 assert response.getFiles().size() == 1;
    230                 assert path.isTheSame(response.getFiles().get(0).getPath());
    231                 FramsClass framsClass = processFetchedInfo(response.getFiles().get(0));
    232 
    233                 if (framsClass == null) {
    234                     LOGGER.fatal("could not read class info");
    235                     finishInfoRequest(name, null, new Exception("could not read class info"));
    236                     return;
    237                 }
    238                 CompositeParam thisParam = path.getTop().getParam();
    239                 if (!thisParam.isMatchingContainedName(framsClass.getId())) {
    240                     String mismatch = "class name mismatch: param=" + thisParam.getContainedTypeName() + " differs from fetched=" + framsClass.getId();
    241                     LOGGER.error(mismatch);
    242                     finishInfoRequest(name, null, new Exception(mismatch));
    243                     return;
    244                 }
    245                 finishInfoRequest(name, framsClass, null);
    246             }
    247         });
    248     }
    249 
    250     @Override
    251     public void fetchValues(final Path path, final StateFunctor stateFunctor) {
    252         assert isActive();
    253         assert path.getTop().getObject() != null;
    254 
    255         LOGGER.trace("fetching values for " + path);
    256         connection.send(new GetRequest().setPath(path.getTextual()), this, new ResponseCallback() {
    257             @Override
    258             public void process(Response response) {
    259                 assert isActive();
    260                 if (!response.getOk()) {
    261                     stateFunctor.call(new Exception(response.getComment()));
    262                     return;
    263                 }
    264                 try {
    265                     processFetchedValues(path, response.getFiles());
    266                     stateFunctor.call(null);
    267                 } catch (Exception ex) {
    268                     LOGGER.error("an exception occurred while loading: " + ex);
    269                     ex.printStackTrace();
    270                     stateFunctor.call(ex);
    271                 }
    272             }
    273         });
    274     }
    275 
    276     @Override
    277     public void resolve(final Path path, final Future<Path> future) {
    278         assert isActive();
    279         if (path.getTop().getObject() != null) {
    280             if (getInfoFromCache(path) != null) {
    281                 future.result(path, null);
    282                 return;
    283             }
    284             findInfo(path, new Future<FramsClass>() {
    285                 @Override
    286                 public void result(FramsClass result, Exception e) {
    287                     if (e != null) {
    288                         future.result(null, e);
    289                         return;
    290                     }
    291                     future.result(path, null);
    292                 }
    293             });
    294             return;
    295         }
    296         findInfo(path, new Future<FramsClass>() {
    297             @Override
    298             public void result(FramsClass result, Exception e) {
    299                 assert isActive();
    300                 if (e != null) {
    301                     future.result(null, e);
    302                     return;
    303                 }
    304                 assert path.getTop().getParam().isMatchingContainedName(result.getId());
    305                 Path p = (path.getTop().getParam().getContainedTypeName() != null ? path : new Path(path.getInstance(), path.getTextual()));
    306                 future.result(create(p), null);
    307             }
    308         });
    309     }
    310 
    311     @Override
    312     protected void tryRegisterOnChangeEvents(final Path path) {
    313         assert isActive();
    314         AccessInterface access = bindAccess(path);
    315         if (!(access instanceof ListAccess)) {
    316             return;
    317         }
    318 
    319 
    320         assert path.size() >= 2;
    321         FramsClass underFramsClass = getInfoFromCache(path.getUnder().getParam().getContainedTypeName());
    322 
    323         EventParam changedEvent = Casting.tryCast(EventParam.class, underFramsClass.getParamEntry(path.getTop().getParam().getId() + "_changed"));
    324         if (changedEvent == null) {
    325             return;
    326         }
    327 
    328         if (getSubscription(path) != null) {
    329             return;
    330         }
    331 
    332         final Pair<Path, Subscription> temporary = new Pair<Path, Subscription>(path, null);
    333         subscriptions.add(temporary);
    334 
    335         connection.subscribe(path.getTextual() + "_changed", new SubscriptionCallback() {
    336             @Override
    337             public EventCallback subscribed(final Subscription subscription) {
    338                 if (subscription == null) {
    339                     LOGGER.error("failed to subscribe for change event for " + path);
    340                     return null;
    341                 }
    342                 LOGGER.debug("subscribed for change event for " + path);
    343                 subscription.setDispatcher(RemoteInstance.this);
    344                 RemoteInstance.this.invokeLater(new Runnable() {
    345                     @Override
    346                     public void run() {
    347                         subscriptions.remove(temporary);
    348                         subscriptions.add(new Pair<Path, Subscription>(path, subscription));
    349                     }
    350                 });
    351                 return new EventCallback() {
    352                     @Override
    353                     public void call(List<File> files) {
    354                         assert isActive();
    355                         assert files.size() == 1;
    356                         MultiParamLoader loader = new MultiParamLoader();
    357                         loader.setNewSource(files.get(0).getContent());
    358                         loader.addBreakCondition(MultiParamLoader.Status.AfterObject);
    359                         ReflectionAccess access = new ReflectionAccess(ListChange.class, ListChange.getFramsClass());
    360                         loader.addAccessInterface(access);
    361                         MultiParamLoader.Status status;
    362                         try {
    363                             while ((status = loader.go()) != MultiParamLoader.Status.Finished) {
    364                                 if (status == MultiParamLoader.Status.AfterObject) {
    365                                     AccessInterface accessInterface = loader.getLastAccessInterface();
    366                                     reactToChange(path, (ListChange) accessInterface.getSelected());
    367                                 }
    368                             }
    369                         } catch (Exception e) {
    370                             e.printStackTrace();
    371                         }
    372                     }
    373                 };
    374             }
    375         });
    376     }
    377 
    378 
    379     protected void reactToChange(final Path path, final ListChange listChange) {
    380         assert isActive();
    381         LOGGER.debug("reacting to change " + listChange + " in " + path);
     180        public final ClientConnection getConnection() {
     181                return connection;
     182        }
     183
     184        @Override
     185        public void fetchValue(final Path path, final Param param, final StateFunctor stateFunctor) {
     186                assert isActive();
     187                assert param != null;
     188                assert path.isResolved();
     189                connection.send(new GetRequest().setField(param.getId()).setPath(path.getTextual()), this, new ResponseCallback() {
     190                        @Override
     191                        public void process(Response response) {
     192                                assert isActive();
     193                                if (!response.getOk()) {
     194                                        stateFunctor.call(new Exception(response.getComment()));
     195                                        return;
     196                                }
     197                                try {
     198                                        processFetchedValues(path, response.getFiles());
     199                                        stateFunctor.call(null);
     200                                } catch (Exception ex) {
     201                                        stateFunctor.call(ex);
     202                                }
     203                        }
     204                });
     205        }
     206
     207        protected final Map<String, Set<Future<FramsClass>>> infoRequests = new HashMap<String, Set<Future<FramsClass>>>();
     208
     209        protected void finishInfoRequest(String id, FramsClass result, Exception e) {
     210                assert isActive();
     211                Set<Future<FramsClass>> futures = infoRequests.get(id);
     212                infoRequests.remove(id);
     213                for (Future<FramsClass> f : futures) {
     214                        f.result(result, e);
     215                }
     216        }
     217
     218        @Override
     219        protected void fetchInfo(final Path path, final Future<FramsClass> future) {
     220
     221                final String name = path.getTop().getParam().getContainedTypeName();
     222
     223                if (infoRequests.containsKey(name)) {
     224                        infoRequests.get(name).add(future);
     225                        return;
     226                }
     227
     228                log.debug("issuing info request for " + name);
     229                Set<Future<FramsClass>> futures = new HashSet<Future<FramsClass>>();
     230                futures.add(future);
     231                infoRequests.put(name, futures);
     232
     233                //TODO: if the info is in the cache, then don't communicate
     234                connection.send(new InfoRequest().setPath(path.getTextual()), this, new ResponseCallback() {
     235                        @Override
     236                        public void process(Response response) {
     237                                assert isActive();
     238                                if (!response.getOk()) {
     239                                        finishInfoRequest(name, null, new Exception(response.getComment()));
     240                                        return;
     241                                }
     242
     243                                assert response.getFiles().size() == 1;
     244                                assert path.isTheSame(response.getFiles().get(0).getPath());
     245                                FramsClass framsClass = processFetchedInfo(response.getFiles().get(0));
     246
     247                                if (framsClass == null) {
     248                                        log.fatal("could not read class info");
     249                                        finishInfoRequest(name, null, new Exception("could not read class info"));
     250                                        return;
     251                                }
     252                                CompositeParam thisParam = path.getTop().getParam();
     253                                if (!thisParam.isMatchingContainedName(framsClass.getId())) {
     254                                        String mismatch = "class name mismatch: param=" + thisParam.getContainedTypeName() + " differs from fetched=" + framsClass.getId();
     255                                        log.error(mismatch);
     256                                        finishInfoRequest(name, null, new Exception(mismatch));
     257                                        return;
     258                                }
     259                                finishInfoRequest(name, framsClass, null);
     260                        }
     261                });
     262        }
     263
     264        @Override
     265        public void fetchValues(final Path path, final StateFunctor stateFunctor) {
     266                assert isActive();
     267                assert path.getTop().getObject() != null;
     268
     269                log.trace("fetching values for " + path);
     270                connection.send(new GetRequest().setPath(path.getTextual()), this, new ResponseCallback() {
     271                        @Override
     272                        public void process(Response response) {
     273                                assert isActive();
     274                                if (!response.getOk()) {
     275                                        stateFunctor.call(new Exception(response.getComment()));
     276                                        return;
     277                                }
     278                                try {
     279                                        processFetchedValues(path, response.getFiles());
     280                                        stateFunctor.call(null);
     281                                } catch (Exception ex) {
     282                                        log.error("an exception occurred while loading: " + ex);
     283                                        ex.printStackTrace();
     284                                        stateFunctor.call(ex);
     285                                }
     286                        }
     287                });
     288        }
     289
     290        @Override
     291        public void resolve(final Path path, final Future<Path> future) {
     292                assert isActive();
     293                if (path.getTop().getObject() != null) {
     294                        if (getInfoFromCache(path) != null) {
     295                                future.result(path, null);
     296                                return;
     297                        }
     298                        findInfo(path, new Future<FramsClass>() {
     299                                @Override
     300                                public void result(FramsClass result, Exception e) {
     301                                        if (e != null) {
     302                                                future.result(null, e);
     303                                                return;
     304                                        }
     305                                        future.result(path, null);
     306                                }
     307                        });
     308                        return;
     309                }
     310                findInfo(path, new Future<FramsClass>() {
     311                        @Override
     312                        public void result(FramsClass result, Exception e) {
     313                                assert isActive();
     314                                if (e != null) {
     315                                        future.result(null, e);
     316                                        return;
     317                                }
     318                                assert path.getTop().getParam().isMatchingContainedName(result.getId());
     319                                Path p = (path.getTop().getParam().getContainedTypeName() != null ? path : path.tryFindResolution());
     320                                future.result(createIfNeeded(p), null);
     321                        }
     322                });
     323        }
     324
     325        @Override
     326        protected void tryRegisterOnChangeEvents(final Path path) {
     327                assert isActive();
     328                AccessInterface access = bindAccess(path);
     329                if (!(access instanceof ListAccess)) {
     330                        return;
     331                }
     332
     333
     334                assert path.size() >= 2;
     335                FramsClass underFramsClass = getInfoFromCache(path.getUnder().getParam().getContainedTypeName());
     336
     337                EventParam changedEvent = underFramsClass.getParamEntry(path.getTop().getParam().getId() + "_changed", EventParam.class);
     338                if (changedEvent == null) {
     339                        return;
     340                }
     341
     342                if (getSubscription(path) != null) {
     343                        return;
     344                }
     345
     346                final Pair<Path, Subscription> temporary = new Pair<Path, Subscription>(path, null);
     347                subscriptions.add(temporary);
     348
     349                connection.subscribe(path.getTextual() + "_changed", new SubscriptionCallback() {
     350                        @Override
     351                        public EventCallback subscribed(final Subscription subscription) {
     352                                if (subscription == null) {
     353                                        log.error("failed to subscribe for change event for " + path);
     354                                        return null;
     355                                }
     356                                log.debug("subscribed for change event for " + path);
     357                                subscription.setDispatcher(RemoteInstance.this);
     358                                RemoteInstance.this.invokeLater(new Runnable() {
     359                                        @Override
     360                                        public void run() {
     361                                                subscriptions.remove(temporary);
     362                                                subscriptions.add(new Pair<Path, Subscription>(path, subscription));
     363                                        }
     364                                });
     365                                return new EventCallback() {
     366                                        @Override
     367                                        public void call(List<File> files) {
     368                                                assert isActive();
     369                                                assert files.size() == 1;
     370                                                MultiParamLoader loader = new MultiParamLoader();
     371                                                loader.setNewSource(files.get(0).getContent());
     372                                                loader.addBreakCondition(MultiParamLoader.Status.AfterObject);
     373                                                ReflectionAccess access = new ReflectionAccess(ListChange.class, ListChange.getFramsClass());
     374                                                loader.addAccessInterface(access);
     375                                                MultiParamLoader.Status status;
     376                                                try {
     377                                                        while ((status = loader.go()) != MultiParamLoader.Status.Finished) {
     378                                                                if (status == MultiParamLoader.Status.AfterObject) {
     379                                                                        AccessInterface accessInterface = loader.getLastAccessInterface();
     380                                                                        reactToChange(path, (ListChange) accessInterface.getSelected());
     381                                                                }
     382                                                        }
     383                                                } catch (Exception e) {
     384                                                        e.printStackTrace();
     385                                                }
     386                                        }
     387                                };
     388                        }
     389                });
     390        }
     391
     392
     393        protected void reactToChange(final Path path, final ListChange listChange) {
     394                assert isActive();
     395                log.debug("reacting to change " + listChange + " in " + path);
    382396                AccessInterface access = bindAccess(path);
    383397                assert access != null;
     
    389403                                public void result(Path result, Exception e) {
    390404                                        if (e != null) {
    391                                                 LOGGER.error("failed to modify " + p + ": " + e);
     405                                                log.error("failed to modify " + p + ": " + e);
    392406                                                return;
    393407                                        }
     
    399413
    400414
    401         CompositeParam childParam = Casting.tryCast(CompositeParam.class, access.getParam(listChange.getBestIdentifier()));
    402         assert childParam != null;
    403         switch (listChange.getAction()) {
    404             case Add: {
    405                 final String p = path.getTextual() + "/" + childParam.getId();
    406                 resolveAndFetch(p, new Future<Path>() {
    407                     @Override
    408                     public void result(Path result, Exception e) {
    409                         if (e != null) {
    410                             LOGGER.error("failed to add " + p + ": " + e);
    411                             return;
    412                         }
    413                         LOGGER.debug("added: " + result);
    414                         fireListChange(path, listChange);
    415                     }
    416                 });
    417                 break;
    418             }
    419             case Remove: {
    420                 access.set(childParam, null);
    421                 fireListChange(path, listChange);
    422                 break;
    423             }
    424             case Modify: {
    425                 final String p = path.getTextual() + "/" + childParam.getId();
    426                 resolveAndFetch(p, new Future<Path>() {
    427                     @Override
    428                     public void result(Path result, Exception e) {
    429                         if (e != null) {
    430                             LOGGER.error("failed to modify " + p + ": " + e);
    431                             return;
    432                         }
    433                         fireListChange(path, listChange);
    434                     }
    435                 });
    436                 break;
    437             }
    438         }
    439     }
    440 
     415                CompositeParam childParam = Casting.tryCast(CompositeParam.class, access.getParam(listChange.getBestIdentifier()));
     416                assert childParam != null;
     417                switch (listChange.getAction()) {
     418                        case Add: {
     419                                final String p = path.getTextual() + "/" + childParam.getId();
     420                                resolveAndFetch(p, new Future<Path>() {
     421                                        @Override
     422                                        public void result(Path result, Exception e) {
     423                                                if (e != null) {
     424                                                        log.error("failed to add " + p + ": " + e);
     425                                                        return;
     426                                                }
     427                                                log.debug("added: " + result);
     428                                                fireListChange(path, listChange);
     429                                        }
     430                                });
     431                                break;
     432                        }
     433                        case Remove: {
     434                                access.set(childParam, null);
     435                                fireListChange(path, listChange);
     436                                break;
     437                        }
     438                        case Modify: {
     439                                final String p = path.getTextual() + "/" + childParam.getId();
     440                                resolveAndFetch(p, new Future<Path>() {
     441                                        @Override
     442                                        public void result(Path result, Exception e) {
     443                                                if (e != null) {
     444                                                        log.error("failed to modify " + p + ": " + e);
     445                                                        return;
     446                                                }
     447                                                fireListChange(path, listChange);
     448                                        }
     449                                });
     450                                break;
     451                        }
     452                }
     453        }
     454
     455        //TODO ValueParam
    441456        @Override
    442457        public void storeValue(final Path path, final Param param, final Object value, final StateFunctor stateFunctor) {
    443458                assert isActive();
    444459
    445                 LOGGER.trace("storing value " + param + " for " + path);
     460                log.trace("storing value " + param + " for " + path);
    446461                connection.send(new SetRequest().value(value.toString()).setField(param.getId()).setPath(path.getTextual()), this, new StateCallback() {
    447462                        @Override
    448463                        public void call(Exception e) {
    449464                                if (e == null) {
    450                                         bindAccess(path).set(param, value);
     465                                        bindAccess(path).set((ValueParam) param, value);
    451466                                }
    452467                                stateFunctor.call(e);
Note: See TracChangeset for help on using the changeset viewer.