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

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

HIGHLIGHTS:

  • add proper exception passing between communication sides:

if exception occur during handling client request, it is
automatically passed as comment to error response.

it may be used to snoop communication between peers

  • fix algorithm choosing text controls in GUI
  • allow GUI testing in virtual frame buffer (xvfb)

FEST had some problem with xvfb but workaround was found

supports tab-completion based on requests history

CHANGELOG:
Further improve handling of exceptions in GUI.

Add StatusBar? implementing ExceptionResultHandler?.

Make completion processing asynchronous.

Minor changes.

Improve completion in console.

Improve history in InteractiveConsole?.

First working version of DirectConsole?.

Minor changes.

Make Connection.address non final.

It is more suitable to use in configuration.

Improvement of consoles.

Improve PopupMenu? and closing of FrameJoinable?.

Fix BrowserTest?.

Found bug with FEST running under xvfb.

JButtonFixture.click() is not working under xvfb.
GuiTest? has wrapper which uses JButton.doClick() directly.

Store CompositeParam? param in TreeNode?.

Simplify ClientSideManagedConnection? connecting.

There is now connectedFunctor needed, ApplicationRequests? can be
send right after creation. They are buffered until the version
and features are negotiated.

Narow down interface of ClientSideManagedConnection?.

Allow that connection specialization send only
ApplicationRequests?.

Improve policy of text control choosing.

Change name of Genotype in BrowserTest?.

Make BrowserTest? change name of Genotype.

Minor change.

First working draft of TrackConsole?.

Simplify Consoles.

More improvements with gui joinables.

Unify initialization on gui joinables.

More rework of Frame based entities.

Refactorize structure of JFrames based entities.

Extract GuiTest? from BrowserBaseTest?.

Reorganize Console classes structure.

Add Collection view to JoinableCollection?.

Configure timeout in testing.

Minor changes.

Rework connections hierarchy.

Add Mode to the get operation.

Make get and set in Tree take PrimitiveParam?.

Unify naming of operations.

Make RunAt? use the given ExceptionHandler?.

It wraps the virtual runAt() method call with
try-catch passing exception to handler.

Force RunAt? to include ExceptionHandler?.

Improve ClientAtServer?.

Minor change.

Another sweep with FindBugs?.

Rename Instance to Tree.

Minor changes.

Minor changes.

Further clarify semantics of Futures.

Add FutureHandler?.

FutureHandler? is refinement of Future, that proxifies
exception handling to ExceptionResultHandler? given
at construction time.

Remove StateFunctor? (use Future<Void> instead).

Make Connection use Future<Void>.

Unparametrize *ResponseFuture?.

Remove StateCallback? not needed anymore.

Distinguish between sides of ResponseFuture?.

Base ResponseCallback? on Future (now ResponseFuture?).

Make asynchronous store taking Future for flags.

Implement storeValue in ObjectInstance?.

File size: 12.6 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.Mode;
10import com.framsticks.core.TreeOperations;
11import com.framsticks.core.ListChange;
12import com.framsticks.core.Node;
13import com.framsticks.core.Path;
14import com.framsticks.params.*;
15import com.framsticks.params.annotations.AutoAppendAnnotation;
16import com.framsticks.params.annotations.FramsClassAnnotation;
17import com.framsticks.params.annotations.ParamAnnotation;
18import com.framsticks.params.types.EventParam;
19import com.framsticks.params.types.ProcedureParam;
20import com.framsticks.parsers.MultiParamLoader;
21import com.framsticks.core.Tree;
22import com.framsticks.util.*;
23import com.framsticks.util.dispatching.Dispatching;
24import com.framsticks.util.dispatching.Future;
25import com.framsticks.util.dispatching.FutureHandler;
26import com.framsticks.util.dispatching.Joinable;
27import com.framsticks.util.dispatching.JoinableParent;
28import com.framsticks.util.dispatching.JoinableState;
29import com.framsticks.util.lang.Casting;
30import com.framsticks.util.lang.Pair;
31import com.framsticks.util.dispatching.RunAt;
32import static com.framsticks.core.TreeOperations.*;
33
34import java.util.*;
35
36import javax.annotation.Nonnull;
37
38import org.apache.log4j.Logger;
39
40/**
41 * @author Piotr Sniegowski
42 */
43@FramsClassAnnotation
44public class RemoteTree extends AbstractTree implements JoinableParent {
45
46        private final static Logger log = Logger.getLogger(RemoteTree.class);
47
48        protected ClientSideManagedConnection connection;
49
50        protected final Set<Pair<Path, Subscription<?>>> subscriptions = new HashSet<>();
51
52        public Pair<Path, Subscription<?>> getSubscription(Path path) {
53                for (Pair<Path, Subscription<?>> s : subscriptions) {
54                        if (s.first.matches(path)) {
55                                return s;
56                        }
57                }
58                return null;
59        }
60
61        public RemoteTree() {
62        }
63
64        @ParamAnnotation
65        public void setAddress(String address) {
66                setConnection(Connection.to(new ClientSideManagedConnection(), new Address(address)));
67        }
68
69        @ParamAnnotation
70        public String getAddress() {
71                return connection == null ? "<disconnected>" : connection.getAddress().toString();
72        }
73
74        @AutoAppendAnnotation
75        public void setConnection(final ClientSideManagedConnection connection) {
76                this.connection = connection;
77                // final ExceptionResultHandler failure = new ExceptionResultHandler() {
78                //      @Override
79                //      public void handle(FramsticksException exception) {
80                //              log.fatal("failed to establish connection: ", exception);
81                //              // log.fatal("unsupported protocol version!\n minimal version is: " + "\nmanager protocol is: " + connection.getProtocolVersion());
82                //              Dispatching.drop(connection, RemoteTree.this);
83                //              fireRun(exception);
84                //      }
85                // };
86
87        }
88
89        @Override
90        public String toString() {
91                assert Dispatching.isThreadSafe();
92                return "remote tree " + getName() + "(" + getAddress() + ")";
93        }
94
95        public final ClientSideManagedConnection getConnection() {
96                return connection;
97        }
98
99        @Override
100        public void get(final Path path, final PrimitiveParam<?> param, Mode mode, 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(future) {
106                        @Override
107                        protected void processOk(Response response) {
108                                assert isActive();
109                                TreeOperations.processFetchedValues(path, response.getFiles());
110                                future.pass(bindAccess(path).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 = 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                                infoRequests.remove(name);
148                                for (Future<FramsClass> f : futures) {
149                                        f.pass(framsClass);
150                                }
151                        }
152                };
153
154                //TODO: if the info is in the cache, then don't communicate
155                connection.send(new InfoRequest().path(path.getTextual()), this, new ClientSideResponseFuture(compositeFuture) {
156                        @Override
157                        protected void processOk(Response response) {
158                                assert isActive();
159
160                                if (response.getFiles().size() != 1) {
161                                        throw new FramsticksException().msg("invalid number of files in response").arg("files", response.getFiles().size());
162                                }
163                                if (!path.isTheSame(response.getFiles().get(0).getPath())) {
164                                        throw new FramsticksException().msg("path mismatch").arg("returned path", response.getFiles().get(0).getPath());
165                                }
166                                FramsClass framsClass = TreeOperations.processFetchedInfo(RemoteTree.this, response.getFiles().get(0));
167
168                                CompositeParam thisParam = path.getTop().getParam();
169                                if (!thisParam.isMatchingContainedName(framsClass.getId())) {
170                                        throw new FramsticksException().msg("framsclass id mismatch").arg("request", thisParam.getContainedTypeName()).arg("fetched", framsClass.getId());
171                                }
172                                compositeFuture.pass(framsClass);
173                        }
174                });
175        }
176
177        @Override
178        public void get(final Path path, Mode mode, final Future<Object> future) {
179                assert isActive();
180                assert path.getTop().getObject() != null;
181
182                log.trace("fetching values for " + path);
183                connection.send(new GetRequest().path(path.getTextual()), this, new ClientSideResponseFuture(future) {
184                        @Override
185                        protected void processOk(Response response) {
186                                assert isActive();
187                                TreeOperations.processFetchedValues(path, response.getFiles());
188                                future.pass(path.getTopObject());
189                        }
190                });
191        }
192
193        @Override
194        public void resolve(final Path path, final Future<Path> future) {
195                TreeOperations.resolve(path, future);
196        }
197
198        @Override
199        protected void tryRegisterOnChangeEvents(final Path path) {
200                assert isActive();
201                AccessInterface access = TreeOperations.bindAccess(path);
202                if (!(access instanceof ListAccess)) {
203                        return;
204                }
205
206                assert path.size() >= 2;
207                FramsClass underFramsClass = getInfoFromCache(path.getUnder().getParam().getContainedTypeName());
208
209                EventParam changedEvent;
210                try {
211                        changedEvent = underFramsClass.getParamEntry(path.getTop().getParam().getId() + "_changed", EventParam.class);
212                } catch (FramsticksException e) {
213                        return;
214                }
215
216                log.debug("registering for " + changedEvent);
217                if (getSubscription(path) != null) {
218                        return;
219                }
220
221                final Pair<Path, Subscription<?>> temporary = new Pair<>(path, null);
222                subscriptions.add(temporary);
223
224                connection.subscribe(path.getTextual() + "_changed", this, new SubscriptionCallback<Tree>() {
225                        @Override
226                        public EventCallback subscribed(final Subscription<? super Tree> subscription) {
227                                if (subscription == null) {
228                                        log.error("failed to subscribe for change event for " + path);
229                                        return null;
230                                }
231                                log.debug("subscribed for change event for " + path);
232                                // subscription.setDispatcher(RemoteInstance.this);
233                                RemoteTree.this.dispatch(new RunAt<Tree>(this) {
234                                        @Override
235                                        protected void runAt() {
236                                                subscriptions.remove(temporary);
237                                                subscriptions.add(new Pair<Path, Subscription<?>>(path, subscription));
238                                        }
239                                });
240                                return new EventCallback() {
241                                        @Override
242                                        public void call(List<File> files) {
243                                                assert isActive();
244                                                assert files.size() == 1;
245                                                final MultiParamLoader loader = new MultiParamLoader();
246                                                loader.setNewSource(files.get(0).getContent());
247                                                loader.addBreakCondition(MultiParamLoader.Status.AfterObject);
248                                                loader.addListener(MultiParamLoader.Status.OnComment, new MultiParamLoader.StatusListener() {
249
250                                                        @Override
251                                                        public void onStatusChange() {
252                                                                throw new FramsticksException().msg("multi param loader error").arg("line", loader.getCurrentLine());
253                                                        }
254                                                });
255                                                ReflectionAccess access = new ReflectionAccess(ListChange.class, FramsClass.build().forClass(ListChange.class));
256                                                loader.addAccessInterface(access);
257
258                                                MultiParamLoader.Status status;
259                                                while ((status = loader.go()) != MultiParamLoader.Status.Finished) {
260                                                        if (status == MultiParamLoader.Status.AfterObject) {
261                                                                AccessInterface accessInterface = loader.getLastAccessInterface();
262                                                                reactToChange(path, (ListChange) accessInterface.getSelected());
263                                                        }
264                                                }
265                                        }
266                                };
267                        }
268                });
269        }
270
271        protected Future<Path> futureListChanger(final ListChange listChange, final String path) {
272                return new FutureHandler<Path>(Logging.logger(log, "failed to " + listChange, path)) {
273                        @Override
274                        protected void result(Path result) {
275                                log.debug(listChange + ": " + result);
276                                fireListChange(result, listChange);
277                        }
278                };
279        }
280
281        protected void reactToChange(final Path path, final ListChange listChange) {
282                assert isActive();
283                log.debug("reacting to change " + listChange + " in " + path);
284                AccessInterface access = TreeOperations.bindAccess(path);
285                assert access != null;
286
287                if ((listChange.getAction() == ListChange.Action.Modify) && (listChange.getPosition() == -1)) {
288                        final String p = path.getTextual();
289                        TreeOperations.resolveAndGet(this, p, futureListChanger(listChange, p));
290                        return;
291                }
292
293                CompositeParam childParam = Casting.tryCast(CompositeParam.class, access.getParam(listChange.getBestIdentifier()));
294                assert childParam != null;
295                switch (listChange.getAction()) {
296                        case Add: {
297                                final String p = path.getTextual() + "/" + childParam.getId();
298                                TreeOperations.resolveAndGet(this, p, futureListChanger(listChange, p));
299                                break;
300                        }
301                        case Remove: {
302                                access.set(childParam, null);
303                                fireListChange(path, listChange);
304                                break;
305                        }
306                        case Modify: {
307                                final String p = path.getTextual() + "/" + childParam.getId();
308                                TreeOperations.resolveAndGet(this, p, futureListChanger(listChange, p));
309                                break;
310                        }
311                }
312        }
313
314        @Override
315        public void set(final Path path, final PrimitiveParam<?> param, final Object value, final Future<Integer> future) {
316                assert isActive();
317                final Integer flag = TreeOperations.bindAccess(path).set(param, value);
318
319                log.trace("storing value " + param + " for " + path);
320                //TODO break in passing exception handler is here
321                connection.send(new SetRequest().value(value.toString()).field(param.getId()).path(path.getTextual()), this, new ClientSideResponseFuture(future) {
322                        @Override
323                        protected void processOk(Response response) {
324                                future.pass(flag);
325                        }
326                });
327        }
328
329        @Override
330        protected void joinableStart() {
331                Dispatching.use(connection, this);
332                super.joinableStart();
333        }
334
335        @Override
336        protected void joinableInterrupt() {
337                Dispatching.drop(connection, this);
338                super.joinableInterrupt();
339        }
340
341        @Override
342        protected void joinableFinish() {
343                super.joinableFinish();
344
345        }
346
347        @Override
348        public void joinableJoin() throws InterruptedException {
349                Dispatching.join(connection);
350                super.joinableJoin();
351        }
352
353        @Override
354        public void childChangedState(Joinable joinable, JoinableState state) {
355                proceedToState(state);
356        }
357
358        @Override
359        public void call(@Nonnull final Path path, @Nonnull final ProcedureParam procedure, @Nonnull Object[] arguments, final Future<Object> future) {
360                assert isActive();
361                assert path.isResolved();
362
363                //TODO validate arguments type using params
364                connection.send(new CallRequest().procedure(procedure.getId()).arguments(Arrays.asList(arguments)).path(path.getTextual()), this, new ClientSideResponseFuture(future) {
365                        @Override
366                        protected void processOk(Response response) {
367                                assert isActive();
368                                // InstanceUtils.processFetchedValues(path, response.getFiles());
369                                future.pass(null);
370                        }
371                });
372
373        }
374
375        @Override
376        public Path create(Path path) {
377                assert isActive();
378                assert !path.isResolved();
379                Path resolved = path.tryFindResolution();
380                if (!resolved.isResolved()) {
381                        log.debug("creating: " + path);
382                        AccessInterface access = registry.prepareAccess(path.getTop().getParam());
383                        assert access != null;
384                        Object child = access.createAccessee();
385                        assert child != null;
386                        if (path.size() == 1) {
387                                setRoot(new Node(getRoot().getParam(), child));
388                        } else {
389                                bindAccess(this, path.getUnder()).set(path.getTop().getParam(), child);
390                        }
391                        resolved = path.appendResolution(child);
392                }
393                tryRegisterOnChangeEvents(resolved);
394                return resolved;
395        }
396
397}
Note: See TracBrowser for help on using the repository browser.