source: java/main/src/main/java/com/framsticks/core/TreeOperations.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: 7.4 KB
Line 
1package com.framsticks.core;
2
3import java.util.List;
4
5import javax.annotation.Nonnull;
6
7import org.apache.log4j.Logger;
8
9import com.framsticks.communication.File;
10import com.framsticks.params.AccessInterface;
11import com.framsticks.params.CompositeParam;
12import com.framsticks.params.FramsClass;
13import com.framsticks.params.ListAccess;
14import com.framsticks.params.Param;
15import com.framsticks.params.PrimitiveParam;
16import com.framsticks.params.ValueParam;
17import com.framsticks.params.types.ObjectParam;
18import com.framsticks.params.types.ProcedureParam;
19import com.framsticks.parsers.Loaders;
20import com.framsticks.parsers.MultiParamLoader;
21import com.framsticks.util.FramsticksException;
22import com.framsticks.util.dispatching.Future;
23import com.framsticks.util.dispatching.FutureHandler;
24import com.framsticks.util.dispatching.RunAt;
25
26import static com.framsticks.util.dispatching.Dispatching.*;
27
28public final class TreeOperations {
29
30        private static final Logger log = Logger.getLogger(TreeOperations.class.getName());
31
32        private TreeOperations() {
33        }
34
35        public static @Nonnull FramsClass processFetchedInfo(Tree tree, File file) {
36                assert tree.isActive();
37                FramsClass framsClass = Loaders.loadFramsClass(file.getContent());
38                if ("/".equals(file.getPath())) {
39                        if (tree.getRoot().getParam().getContainedTypeName() == null) {
40                                tree.setRoot(new Node(Param.build().name("Tree").id(tree.getName()).type("o " + framsClass.getId()).finish(CompositeParam.class), tree.getRoot().getObject()));
41                        }
42                }
43                tree.putInfoIntoCache(framsClass);
44                return framsClass;
45        }
46
47        public static FramsClass getInfo(Path path) {
48                Tree tree = path.getTree();
49                assert tree.isActive();
50                final String name = path.getTop().getParam().getContainedTypeName();
51                return tree.getInfoFromCache(name);
52        }
53
54        public static void findInfo(final Path path, final Future<FramsClass> future) {
55                try {
56                        Tree tree = path.getTree();
57                        assert tree.isActive();
58                        final FramsClass framsClass = getInfo(path);
59                        if (framsClass != null) {
60                                future.pass(framsClass);
61                                return;
62                        }
63                        tree.info(path, future);
64                } catch (FramsticksException e) {
65                        future.handle(e);
66                }
67        }
68
69        public static void processFetchedValues(Path path, List<File> files) {
70                Tree tree = path.getTree();
71                assert tree.isActive();
72                assert files.size() == 1;
73                assert path.isTheSame(files.get(0).getPath());
74                Node node = path.getTop();
75                MultiParamLoader loader = new MultiParamLoader();
76                loader.setNewSource(files.get(0).getContent());
77                loader.addBreakCondition(MultiParamLoader.Status.AfterObject);
78
79                try {
80                        if (node.getParam() instanceof ObjectParam) {
81                                loader.addAccessInterface(TreeOperations.bindAccess(tree, node));
82                                loader.go();
83                                tree.notifyOfFetch(path);
84                                return;
85                        }
86
87                        ListAccess listAccess = ((ListAccess) TreeOperations.bindAccess(tree, node));
88                        assert listAccess != null;
89                        listAccess.clearValues();
90
91                        AccessInterface elementAccess = listAccess.getElementAccess();
92                        loader.addAccessInterface(elementAccess);
93                        MultiParamLoader.Status status;
94                        while ((status = loader.go()) != MultiParamLoader.Status.Finished) {
95                                if (status == MultiParamLoader.Status.AfterObject) {
96                                        AccessInterface accessInterface = loader.getLastAccessInterface();
97
98                                        String id = listAccess.computeIdentifierFor(accessInterface.getSelected());
99                                        //TODO listAccessParam
100                                        Param param = Param.build().forAccess(accessInterface).id(id).finish();
101                                        Object child = accessInterface.getSelected();
102                                        accessInterface.select(null);
103                                        assert child != null;
104                                        TreeOperations.bindAccess(tree, node).set((ValueParam) param, child);
105                                }
106                        }
107
108                        tree.notifyOfFetch(path);
109                } catch (Exception e) {
110                        log.error("exception occurred while loading: " + e);
111                }
112
113        }
114
115        public static void resolveAndGet(final Tree tree, final String targetPath, final Future<Path> future) {
116                resolve(tree, targetPath, new FutureHandler<Path>(future) {
117                        @Override
118                        protected void result(final Path path) {
119                                assert path.isResolved(targetPath);
120                                //TODO Future
121                                tree.get(path, Mode.FETCH, new FutureHandler<Object>(future) {
122                                        @Override
123                                        protected void result(Object object) {
124                                                future.pass(path);
125                                        }
126                                });
127                        }
128                });
129        }
130
131
132        public static void resolve(final Path path, final Future<Path> future) {
133                final Tree tree = path.getTree();
134                assert tree.isActive();
135                if (path.getTop().getObject() != null) {
136                        if (getInfoFromCache(path) != null) {
137                                future.pass(path);
138                                return;
139                        }
140                        TreeOperations.findInfo(path, new FutureHandler<FramsClass>(future) {
141                                @Override
142                                protected void result(FramsClass result) {
143                                        future.pass(path);
144                                }
145                        });
146                        return;
147                }
148                TreeOperations.findInfo(path, new FutureHandler<FramsClass>(future) {
149                        @Override
150                        protected void result(FramsClass result) {
151                                assert tree.isActive();
152                                assert path.getTop().getParam().isMatchingContainedName(result.getId());
153                                Path p = (path.getTop().getParam().getContainedTypeName() != null ? path : path.tryFindResolution());
154                                future.pass(TreeOperations.createIfNeeded(p));
155                        }
156                });
157        }
158
159        public static Path createIfNeeded(Tree tree, String path) {
160                assert tree.isActive();
161                Path p;
162                while (!(p = Path.to(tree, path)).isResolved(path)) {
163                        tree.create(p);
164                }
165                return p;
166        }
167
168        public static Path createIfNeeded(Path path) {
169                Tree tree = path.getTree();
170                assert tree.isActive();
171                if (path.isResolved()) {
172                        return path;
173                }
174                return tree.create(path);
175        }
176
177        public static @Nonnull AccessInterface bindAccess(Tree tree, String path) {
178                return bindAccess(Path.to(tree, path));
179        }
180
181        public static @Nonnull AccessInterface bindAccess(Tree tree, Node node) {
182                assert tree.isActive();
183                assert node.getObject() != null;
184
185                try {
186                        return tree.prepareAccess(node.getParam()).select(node.getObject());
187                } catch (FramsticksException e) {
188                        throw new FramsticksException().msg("failed to prepare access for param").arg("param", node.getParam()).cause(e);
189                        // log.error("failed to bind access for " + node.getParam() + ": " + e);
190                }
191        }
192
193        public static @Nonnull AccessInterface bindAccess(Path path) {
194                assert path.getTree().isActive();
195                path.assureResolved();
196                return bindAccess(path.getTree(), path.getTop());
197        }
198
199        public static void set(final Path path, final PrimitiveParam<?> param, final Object value, final Future<Integer> future) {
200                final Tree tree = path.getTree();
201
202                dispatchIfNotActive(tree, new RunAt<Tree>(future) {
203                        @Override
204                        protected void runAt() {
205                                tree.set(path, param, value, future);
206                        }
207                });
208        }
209
210        public static void call(final Path path, final ProcedureParam param, final Object[] arguments, final Future<Object> future) {
211                final Tree tree = path.getTree();
212
213                dispatchIfNotActive(tree, new RunAt<Tree>(future) {
214                        @Override
215                        protected void runAt() {
216                                tree.call(path, param, arguments, future);
217                        }
218                });
219        }
220
221        /** This might not be correct. */
222        public static void resolve(final Tree tree, final String targetPath, final Future<Path> future) {
223                dispatchIfNotActive(tree, new RunAt<Tree>(future) {
224
225                        @Override
226                        protected void runAt() {
227                                tree.resolve(Path.to(tree, targetPath), new FutureHandler<Path>(future) {
228                                        @Override
229                                        protected void result(Path result) {
230                                                assert result.getTree().isActive();
231                                                if (result.isResolved(targetPath)) {
232                                                        future.pass(result);
233                                                        return;
234                                                }
235                                                resolve(tree, targetPath, future);
236                                        }
237                                });
238                        }
239                });
240        }
241
242        public static FramsClass getInfoFromCache(Path path) {
243                assert path.getTree().isActive();
244                return path.getTree().getInfoFromCache(path.getTop().getParam().getContainedTypeName());
245        }
246}
Note: See TracBrowser for help on using the repository browser.