source: java/main/src/main/java/com/framsticks/remote/RemoteTree.java @ 101

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

HIGHLIGHTS:

  • improve tree side notes
  • improve GUI layout
  • add foldable list of occured events to EventControl?
  • improve automatic type conversion in proxy listeners
  • implement several Access functionalities as algorithms independent of Access type
  • introduce draft base classes for distributed experiments
  • automatically register dependant Java classes to FramsClass? registry
  • add testing prime experiment and configuration
  • simplify and improve task dispatching

CHANGELOG:
Improve task dispatching in RemoteTree?.

GUI no longer hangs on connection problems.

Make all dispatchers joinables.

Refactorize Thread dispatcher.

Remove Task and PeriodicTask?.

Use Java utilities in those situations.

Reworking tasks dispatching.

Fix bug in EventControl? listener dispatching.

Minor improvements.

Add testing configuration for ExternalProcess? in GUI.

More improvement to prime.

Support for USERREADONLY in GUI.

Add that flag to various params in Java classes.

Remove redundant register clauses from several FramsClassAnnotations?.

Automatically gather and register dependant classes.

Add configuration for prime.

Improve Simulator class.

Add prime.xml configuration.

Introduce draft Experiment and Simulator classes.

Add prime experiment tests.

Enclose typical map with listeners into SimpleUniqueList?.

Needfile works in GUI.

Improve needfile handling in Browser.

More improvement with NeedFile?.

Implementing needfile.

Update test.

Rename ChangeEvent? to TestChangeEvent?.

Automatic argument type search in RemoteTree? listeners.

MultiParamLoader? uses AccessProvider?. By default old implementation
enclosed in AccessStash? or Registry.

Minor changes.

Rename SourceInterface? to Source.

Also improve toString of File and ListSource?.

Remove unused SimpleSource? class.

Add clearing in HistoryControl?.

Show entries in table at EventControl?.

Improve EventControl?.

Add listeners registration to EventControl?.

Add foldable table to HistoryControl?.

Add control row to Procedure and Event controls.

Improve layout of controls.

Another minor change to gui layout.

Minor improvement in the SliderControl?.

Minor changes.

Move ReflectionAccess?.Backend to separate file.

It was to cluttered.

Cleanup in ReflectionAccess?.

Move setMin, setMax, setDef to AccessOperations?.

Extract loading operation into AccessOperations?.

Append Framsticks to name of UnsupportedOperationException?.

The java.lang.UnsupportedOperationException? was shadowing this class.

Rename params.Util to params.ParamsUtil?.

Several improvements.

Minor changes.

Implement revert functionality.

Improve local changes management.

Minor improvement.

Remove methods rendered superfluous after SideNoteKey? improvement.

Improve SideNoteKey?.

It is now generic type, so explicit type specification at
call site is no more needed.

Introduce SideNoteKey? interface.

Only Objects implementing that key may be used as side note keys.

Minor improvements.

Use strings instead of ValueControls? in several gui mappings.

File size: 11.2 KB
Line 
1package com.framsticks.remote;
2
3import com.framsticks.communication.*;
4import com.framsticks.communication.queries.CallRequest;
5import com.framsticks.communication.queries.GetRequest;
6import com.framsticks.communication.queries.InfoRequest;
7import com.framsticks.communication.queries.SetRequest;
8import com.framsticks.core.AbstractTree;
9import com.framsticks.core.ListChange;
10import com.framsticks.core.Path;
11import com.framsticks.core.ValueChange;
12import com.framsticks.params.*;
13import com.framsticks.params.EventListener;
14import com.framsticks.params.annotations.FramsClassAnnotation;
15import com.framsticks.params.annotations.ParamAnnotation;
16import com.framsticks.params.types.EventParam;
17import com.framsticks.params.types.ProcedureParam;
18import com.framsticks.parsers.Loaders;
19import com.framsticks.parsers.MultiParamLoader;
20import com.framsticks.util.*;
21import com.framsticks.util.dispatching.AtOnceDispatcher;
22import com.framsticks.util.dispatching.Dispatching;
23import com.framsticks.util.dispatching.DispatchingFuture;
24import com.framsticks.util.dispatching.ExceptionResultHandler;
25import com.framsticks.util.dispatching.Future;
26import com.framsticks.util.dispatching.FutureHandler;
27import com.framsticks.util.dispatching.Joinable;
28import com.framsticks.util.dispatching.JoinableParent;
29import com.framsticks.util.dispatching.JoinableState;
30import com.framsticks.util.dispatching.ThrowExceptionHandler;
31import com.framsticks.util.lang.Casting;
32import com.framsticks.util.dispatching.RunAt;
33import static com.framsticks.core.TreeOperations.*;
34
35import java.util.*;
36
37import javax.annotation.Nonnull;
38
39import org.apache.logging.log4j.Logger;
40import org.apache.logging.log4j.LogManager;
41
42/**
43 * @author Piotr Sniegowski
44 */
45@FramsClassAnnotation
46public final class RemoteTree extends AbstractTree implements JoinableParent {
47
48        private final static Logger log = LogManager.getLogger(RemoteTree.class);
49
50        protected ClientSideManagedConnection connection;
51
52        public RemoteTree() {
53                bufferedDispatcher.setBuffer(true);
54                registry.registerAndBuild(ListChange.class);
55                registry.registerAndBuild(ValueChange.class);
56        }
57
58        @ParamAnnotation
59        public void setAddress(String address) {
60                this.connection = Connection.to(new ClientSideManagedConnection(), new Address(address));
61                this.connection.setExceptionHandler(this);
62                this.connection.setNeedFileAcceptor(this);
63        }
64
65        @ParamAnnotation(flags = ParamFlags.USERREADONLY)
66        public String getAddress() {
67                return connection == null ? "<disconnected>" : connection.getAddress().toString();
68        }
69
70        public final ClientSideManagedConnection getConnection() {
71                return connection;
72        }
73
74        @Override
75        public String toString() {
76                assert Dispatching.isThreadSafe();
77                return "remote tree " + getName() + "(" + getAddress() + ")";
78        }
79
80        protected ExceptionResultHandler pathRemoveHandler(final Path path, final ExceptionResultHandler handler) {
81                return new ExceptionResultHandler() {
82
83                        @Override
84                        public void handle(final FramsticksException exception) {
85                                Dispatching.dispatchIfNotActive(RemoteTree.this, new RunAt<RemoteTree>(RemoteTree.this) {
86
87                                        @Override
88                                        protected void runAt() {
89                                                assert path.getTree().isActive();
90                                                log.info("path is invalid (removing): {}", path);
91                                                bindAccess(path.getUnder()).set(path.getTop().getParam(), null);
92                                                handler.handle(exception);
93                                        }
94                                });
95                        }
96                };
97        }
98
99        // @Override
100        // public void get(final Path path, final ValueParam param, final Future<Object> future) {
101        //      assert isActive();
102        //      assert param != null;
103        //      // assert path.isResolved();
104        //      //TODO only do that if needed
105        //      connection.send(new GetRequest().field(param.getId()).path(path.getTextual()), this, new ClientSideResponseFuture(pathRemoveHandler(path, future)) {
106        //              @Override
107        //              protected void processOk(Response response) {
108        //                      assert isActive();
109        //                      processFetchedValues(path, response.getFiles());
110        //                      future.pass(bindAccess(path.tryResolveIfNeeded()).get(param, Object.class));
111        //              }
112        //      });
113        // }
114
115        protected final Map<String, Set<Future<FramsClass>>> infoRequests = new HashMap<String, Set<Future<FramsClass>>>();
116
117
118        @Override
119        public void info(final Path path, final Future<FramsClass> future) {
120
121                final String name = path.getTop().getParam().getContainedTypeName();
122
123                if (infoRequests.containsKey(name)) {
124                        infoRequests.get(name).add(future);
125                        return;
126                }
127
128                log.debug("issuing info request for {}", name);
129                final Set<Future<FramsClass>> futures = new HashSet<Future<FramsClass>>();
130                futures.add(future);
131                infoRequests.put(name, futures);
132
133                final Future<FramsClass> compositeFuture = DispatchingFuture.create(this, new Future<FramsClass>() {
134
135                        @Override
136                        public void handle(FramsticksException exception) {
137                                assert isActive();
138                                infoRequests.remove(name);
139                                for (Future<FramsClass> f : futures) {
140                                        f.handle(exception);
141                                }
142                        }
143
144                        @Override
145                        protected void result(FramsClass framsClass) {
146                                assert isActive();
147                                putInfoIntoCache(framsClass);
148                                infoRequests.remove(name);
149                                for (Future<FramsClass> f : futures) {
150                                        f.pass(framsClass);
151                                }
152                        }
153                });
154
155                //TODO: if the info is in the cache, then don't communicate
156                connection.send(new InfoRequest().path(path.getTextual()), AtOnceDispatcher.getInstance(), new ClientSideResponseFuture(compositeFuture) {
157                        @Override
158                        protected void processOk(Response response) {
159                                assert connection.getReceiverDispatcher().isActive();
160
161                                if (response.getFiles().size() != 1) {
162                                        throw new FramsticksException().msg("invalid number of files in response").arg("files", response.getFiles().size());
163                                }
164                                if (!path.isTheSame(response.getFiles().get(0).getPath())) {
165                                        throw new FramsticksException().msg("path mismatch").arg("returned path", response.getFiles().get(0).getPath());
166                                }
167                                FramsClass framsClass = Loaders.loadFramsClass(response.getFiles().get(0).getContent());
168
169                                CompositeParam thisParam = path.getTop().getParam();
170                                if (!thisParam.isMatchingContainedName(framsClass.getId())) {
171                                        throw new FramsticksException().msg("framsclass id mismatch").arg("request", thisParam.getContainedTypeName()).arg("fetched", framsClass.getId());
172                                }
173                                compositeFuture.pass(framsClass);
174                        }
175                });
176        }
177
178        @Override
179        public void get(final Path path, final Future<Path> future) {
180                assert isActive();
181                final ExceptionResultHandler remover = pathRemoveHandler(path, future);
182
183                log.trace("fetching values for {}", path);
184                findInfo(path, new FutureHandler<FramsClass>(remover) {
185                        @Override
186                        protected void result(FramsClass result) {
187
188                                final Access access = registry.prepareAccess(path.getTop().getParam());
189                                connection.send(new GetRequest().path(path.getTextual()), AtOnceDispatcher.getInstance(), new ClientSideResponseFuture(remover) {
190                                        @Override
191                                        protected void processOk(Response response) {
192                                                processFetchedValues(path, response.getFiles(), access, future);
193                                        }
194                                });
195                        }
196                });
197        }
198
199        @Override
200        public void set(final Path path, final PrimitiveParam<?> param, final Object value, final Future<Integer> future) {
201                assert isActive();
202                final Integer flag = bindAccess(path).set(param, value);
203
204                log.trace("storing value {} for {}", param, path);
205                //TODO break in passing exception handler is here
206                connection.send(new SetRequest().value(value.toString()).field(param.getId()).path(path.getTextual()), this, new ClientSideResponseFuture(future) {
207                        @Override
208                        protected void processOk(Response response) {
209                                future.pass(flag);
210                        }
211                });
212        }
213
214        @Override
215        protected void joinableStart() {
216                Dispatching.use(connection, this);
217                super.joinableStart();
218
219                bufferedDispatcher.getTargetDispatcher().dispatch(new RunAt<RemoteTree>(ThrowExceptionHandler.getInstance()) {
220                        @Override
221                        protected void runAt() {
222
223                                connection.send(new InfoRequest().path("/"), bufferedDispatcher.getTargetDispatcher(), new ClientSideResponseFuture(this) {
224                                        @Override
225                                        protected void processOk(Response response) {
226                                                FramsClass framsClass = processFetchedInfo(RemoteTree.this, response.getFiles().get(0));
227                                                assignRootParam(Param.build().name("Tree").id(RemoteTree.this.getName()).type("o " + framsClass.getId()).finish(CompositeParam.class));
228                                                bufferedDispatcher.setBuffer(false);
229                                        }
230                                });
231
232                        }
233                });
234        }
235
236        @Override
237        protected void joinableInterrupt() {
238                Dispatching.drop(connection, this);
239                super.joinableInterrupt();
240        }
241
242        @Override
243        protected void joinableFinish() {
244                super.joinableFinish();
245
246        }
247
248        @Override
249        public void joinableJoin() throws InterruptedException {
250                Dispatching.join(connection);
251                super.joinableJoin();
252        }
253
254        @Override
255        public void childChangedState(Joinable joinable, JoinableState state) {
256                proceedToState(state);
257        }
258
259        @Override
260        public void call(@Nonnull final Path path, @Nonnull final ProcedureParam procedure, @Nonnull Object[] arguments, final Future<Object> future) {
261                assert isActive();
262                assert path.isResolved();
263
264                //TODO validate arguments type using params
265                connection.send(new CallRequest().procedure(procedure.getId()).arguments(Arrays.asList(arguments)).path(path.getTextual()), this, new ClientSideResponseFuture(future) {
266                        @Override
267                        protected void processOk(Response response) {
268                                assert isActive();
269                                // InstanceUtils.processFetchedValues(path, response.getFiles());
270                                future.pass(null);
271                        }
272                });
273
274        }
275
276        protected final Map<EventListener<?>, EventListener<File>> proxyListeners = new IdentityHashMap<>();
277
278        public <A> void addListener(Path path, EventParam param, final EventListener<A> listener, final Class<A> argumentType, final Future<Void> future) {
279                assert isActive();
280                assert path.isResolved();
281                if ((!argumentType.equals(Object.class)) && (null == registry.getFramsClassForJavaClass(argumentType))) {
282                        registry.registerAndBuild(argumentType);
283                }
284
285                final EventListener<File> proxyListener = new EventListener<File>() {
286
287                        @Override
288                        public void action(final File file) {
289                                Dispatching.dispatchIfNotActive(RemoteTree.this, new RunAt<RemoteTree>(RemoteTree.this) {
290
291                                        @Override
292                                        protected void runAt() {
293                                                assert isActive();
294                                                if (argumentType.equals(File.class)) {
295                                                        listener.action(Casting.tryCast(argumentType, file));
296                                                        return;
297                                                }
298                                                // Access access = registry.createAccess(argumentType);
299
300                                                // log.info("executing event with argument {}", argumentType);
301                                                MultiParamLoader loader = new MultiParamLoader();
302                                                loader.setNewSource(file.getContent());
303                                                loader.addBreakCondition(MultiParamLoader.Status.AfterObject);
304                                                loader.setAccessProvider(registry);
305                                                // loader.addAccess(access);
306                                                loader.go();
307                                                Object argument = loader.getLastAccess().getSelected();
308                                                // Object argument = access.getSelected();
309                                                if (argument == null) {
310                                                        listener.action(null);
311                                                }
312                                                if (!argumentType.isInstance(argument)) {
313                                                        throw new FramsticksException().msg("created argument is of wrong type").arg("expected", argumentType).arg("created", argument.getClass());
314                                                }
315                                                listener.action(argumentType.cast(argument));
316                                        }
317                                });
318                        }
319                };
320
321                proxyListeners.put(listener, proxyListener);
322
323                connection.addListener(Path.appendString(path.getTextual(), param.getId()), proxyListener, this, future);
324        }
325
326        public void removeListener(Path path, EventParam param, EventListener<?> listener, Future<Void> future) {
327                assert isActive();
328                EventListener<File> proxyListener = proxyListeners.get(listener);
329                connection.removeListener(proxyListener, this, future);
330        }
331
332}
Note: See TracBrowser for help on using the repository browser.