source: java/main/src/main/java/com/framsticks/remote/RemoteInstance.java @ 84

Last change on this file since 84 was 84, checked in by psniegowski, 11 years ago

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.

File size: 13.4 KB
Line 
1package com.framsticks.remote;
2
3import com.framsticks.communication.*;
4import com.framsticks.communication.queries.GetRequest;
5import com.framsticks.communication.queries.InfoRequest;
6import com.framsticks.communication.queries.SetRequest;
7import com.framsticks.communication.util.LoggingSubscriptionCallback;
8import com.framsticks.core.ListChange;
9import com.framsticks.core.Path;
10import com.framsticks.params.*;
11import com.framsticks.params.types.EventParam;
12import com.framsticks.parsers.MultiParamLoader;
13import com.framsticks.core.Instance;
14import 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;
21import org.apache.log4j.Logger;
22
23import java.util.*;
24
25/**
26 * @author Piotr Sniegowski
27 */
28public class RemoteInstance extends Instance {
29
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() {
51                        @Override
52                        public void call(Exception e) {
53                                if (e != null) {
54                                        fireRun(e);
55                                        return;
56                                }
57                                connection.negotiateProtocolVersion(new StateFunctor() {
58                                        @Override
59                                        public void call(Exception e) {
60                                                if (e != null) {
61                                                        log.fatal("unsupported protocol version!\n minimal version is: "
62                                                                        + "nmanager protocol is: "
63                                                                        + connection.getProtocolVersion());
64                                                        connection.close();
65                                                        fireRun(e);
66                                                        return;
67                                                }
68
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                                                });
109                                        }
110                                });
111                        }
112                });
113        }
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        }
129
130        @Override
131        public String toString() {
132                assert Dispatching.isThreadSafe();
133                return getConnection().getAddress();
134        }
135
136        public void setRunning(final boolean running) {
137                assert isActive();
138                //simulator.call(simulator.getParam(running ? "start" : "stop", ProcedureParam.class), new LoggingStateCallback(log, (running ? "starting" : "stopping") + " server"));
139        }
140
141        protected final UnaryListenersSet<Boolean> simulationRunningListeners = new UnaryListenersSet<Boolean>();
142
143        protected void updateSimulationRunning() {
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                */
166        }
167
168        public void addRunningStateListener(UnaryFunctor<Boolean, Boolean> listener) {
169                assert isActive();
170                simulationRunningListeners.add(listener);
171        }
172
173        public void disconnect() {
174                assert isActive();
175                if (connection.isConnected()) {
176                        connection.close();
177                }
178        }
179
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);
396                AccessInterface access = bindAccess(path);
397                assert access != null;
398
399                if ((listChange.getAction() == ListChange.Action.Modify) && (listChange.getPosition() == -1)) {
400                        final String p = path.getTextual();
401                        resolveAndFetch(p, new Future<Path>() {
402                                @Override
403                                public void result(Path result, Exception e) {
404                                        if (e != null) {
405                                                log.error("failed to modify " + p + ": " + e);
406                                                return;
407                                        }
408                                        fireListChange(path, listChange);
409                                }
410                        });
411                        return;
412                }
413
414
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
456        @Override
457        public void storeValue(final Path path, final Param param, final Object value, final StateFunctor stateFunctor) {
458                assert isActive();
459
460                log.trace("storing value " + param + " for " + path);
461                connection.send(new SetRequest().value(value.toString()).setField(param.getId()).setPath(path.getTextual()), this, new StateCallback() {
462                        @Override
463                        public void call(Exception e) {
464                                if (e == null) {
465                                        bindAccess(path).set((ValueParam) param, value);
466                                }
467                                stateFunctor.call(e);
468                        }
469                });
470        }
471}
Note: See TracBrowser for help on using the repository browser.