source: java/main/src/main/java/com/framsticks/hosting/ClientAtServer.java @ 102

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

HIGHLIGHTS:

for Joinables running

CHANGELOG:
Add WorkPackageLogic? and classes representing prime experiment state.

Add classes for PrimeExperiment? state.

Extract single netload routine in Simulator.

Working netload with dummy content in PrimeExperiment?.

More development with NetLoadSaveLogic? and PrimeExperiment?.

Improvement around prime.

Improve BufferedDispatcher?.isActive logic.

Add prime-all.xml configuration.

Manual connecting to existing simulators from GUI.

Guard in SimulatorConnector? against expdef mismatch.

Guard against empty target dispatcher in BufferedDispatcher?.

Make BufferedDispatcher? a Dispatcher (and Joinable).

Minor improvements.

Done StackedJoinable?, improve Experiment.

Develop StackedJoinable?.

Add StackedJoinable? utility joinables controller.

Add dependency on apache-commons-lang.

Add ready ListChange? on Simulators.

Improve hints in ListChange?.

Several improvements.

Found bug with dispatching in Experiment.

Minor improvements.

Fix bug with early finishing Server.

Many changes in Dispatching.

Fix bug with connection.

Do not obfuscate log with socket related exceptions.

Add SocketClosedException?.

Add SimulatorConnector?.

Work out conception of experiment composing of logics building blocks.

Rename SinkInterface? to Sink.

Move saving of Accesses into AccessOperations?.

Some improvements to Experiment.

Improve joinables.

Fix issue with joinables closing.

Add direct and managed consoles to popup menu.

File size: 7.7 KB
Line 
1package com.framsticks.hosting;
2
3import static com.framsticks.util.lang.Strings.assureNotEmpty;
4
5import com.framsticks.communication.*;
6import com.framsticks.communication.queries.ApplicationRequest;
7import com.framsticks.communication.queries.CallRequest;
8import com.framsticks.communication.queries.GetRequest;
9import com.framsticks.communication.queries.InfoRequest;
10import com.framsticks.communication.queries.RegisterRequest;
11import com.framsticks.communication.queries.SetRequest;
12import com.framsticks.core.LocalTree;
13import com.framsticks.core.Tree;
14import com.framsticks.core.Path;
15import com.framsticks.params.*;
16import com.framsticks.params.types.EventParam;
17import com.framsticks.params.types.ProcedureParam;
18import com.framsticks.parsers.Savers;
19import com.framsticks.util.FramsticksException;
20import com.framsticks.util.Misc;
21import com.framsticks.util.dispatching.AbstractJoinable;
22import com.framsticks.util.dispatching.Dispatching;
23import com.framsticks.util.dispatching.ExceptionResultHandler;
24import com.framsticks.util.dispatching.FutureHandler;
25import com.framsticks.util.dispatching.Joinable;
26import com.framsticks.util.dispatching.JoinableParent;
27import com.framsticks.util.dispatching.JoinableState;
28import com.framsticks.util.lang.FlagsUtil;
29import com.framsticks.util.lang.Strings;
30
31import static com.framsticks.core.TreeOperations.*;
32import static com.framsticks.params.AccessOperations.*;
33
34import java.net.Socket;
35
36/**
37 * @author Piotr Sniegowski
38 */
39public class ClientAtServer extends AbstractJoinable implements RequestHandler, JoinableParent, ExceptionResultHandler {
40
41        protected final Server server;
42        protected final Tree contentTree;
43        protected final Object treeRootObject;
44        protected final ServerSideManagedConnection connection;
45
46        protected final Cli cliObject;
47        protected final LocalTree rootTree;
48
49
50        protected final FramsClass rootFramsClass;
51        protected final Object root;
52        protected final String contentPrefix;
53
54        public ClientAtServer(Server server, Socket socket) {
55                this.server = server;
56                this.contentTree = server.hosted;
57                this.connection = new ServerSideManagedConnection(socket, this);
58
59                treeRootObject = contentTree.getAssignedRoot().getObject();
60                Misc.throwIfNull(treeRootObject);
61
62                cliObject = new Cli(this);
63                rootTree = new LocalTree();
64                rootTree.setName(server.getName() + " root tree");
65                // rootTree.setDispatcher(new AtOnceDispatcher<Tree>());
66                rootTree.setDispatcher(server.getHosted().getDispatcher());
67                assert rootTree.getDispatcher() != null;
68
69                final FramsClass framsClass = bindAccess(contentTree.getAssignedRoot()).getFramsClass();
70                final String id = Strings.uncapitalize(framsClass.getName());
71                contentPrefix = "/" + id;
72                final String rootFramsClassId = id + "Root";
73
74                rootFramsClass = FramsClass.build()
75                        .idAndName(rootFramsClassId)
76                        .param(Param.build().id(id).name(framsClass.getName()).type("o " + framsClass.getId()))
77                        .param(Param.build().id("cli").name("CLI").type("o Cli"))
78                        .finish();
79
80                // rootTree.putInfoIntoCache(rootFramsClass);
81                rootTree.getRegistry().putFramsClass(rootFramsClass);
82                rootTree.getRegistry().registerAndBuild(Cli.class);
83                rootTree.getRegistry().registerAndBuild(CliEvent.class);
84
85                Access access = new PropertiesAccess(rootFramsClass);
86
87                root = createAccessee(rootTree, access);
88                access.select(root);
89                access.set(id, treeRootObject);
90                access.set("cli", cliObject);
91
92                rootTree.assignRootParam(access.buildParam(new ParamBuilder()).name(rootFramsClassId).finish(CompositeParam.class));
93                rootTree.assignRootObject(root);
94
95        }
96
97        @Override
98        public String getName() {
99                return connection + " to " + server;
100        }
101
102        @Override
103        public void handle(final ApplicationRequest request, final ServerSideResponseFuture responseCallback) {
104                assureNotEmpty(request.getPath());
105
106                if (request.getPath().startsWith(contentPrefix)) {
107                        String p = request.getPath().substring(contentPrefix.length());
108                        request.path(p.equals("") ? "/" : p);
109                        handleInTree(contentTree, request, responseCallback, contentPrefix);
110                        return;
111                }
112
113                handleInTree(rootTree, request, responseCallback, "");
114        }
115
116        public static File printToFile(String path, Access access) {
117                ListSink sink = new ListSink();
118                save(access, sink);
119                return new File(path, new ListSource(sink.getOut()));
120        }
121
122        protected void handleInTree(final Tree tree, final ApplicationRequest request, final ServerSideResponseFuture responseCallback, final String usedPrefix) {
123
124                tryGet(tree, request.getActualPath(), new FutureHandler<Path>(responseCallback) {
125                        @Override
126                        protected void result(final Path path) {
127
128                                if (!path.getTextual().equals(request.getActualPath())) {
129                                        throw new FramsticksException().msg("invalid path").arg("path", request.getActualPath());
130                                }
131
132                                // final Access access = tree.prepareAccess(path);
133                                final Access access = bindAccess(path);
134
135                                if (request instanceof GetRequest) {
136                                        Object result = path.getTopObject();
137                                        if (result != access.getSelected()) {
138                                                throw new FramsticksException().msg("mismatch objects during fetch").arg("path", path);
139                                        }
140                                        responseCallback.pass(new Response(true, "", File.single(printToFile(path.getTextual(), access))));
141
142                                        return;
143                                }
144
145                                if (request instanceof SetRequest) {
146                                        SetRequest setRequest = (SetRequest) request;
147                                        tree.set(path, access.getFramsClass().getParamEntry(setRequest.getField(), PrimitiveParam.class), setRequest.getValue(), new FutureHandler<Integer>(responseCallback) {
148                                                @Override
149                                                protected void result(Integer flag) {
150                                                        responseCallback.pass(new Response(true, FlagsUtil.write(SetStateFlags.class, flag, null), null));
151
152                                                }
153                                        });
154                                        return;
155                                }
156
157                                if (request instanceof CallRequest) {
158                                        final CallRequest callRequest = (CallRequest) request;
159                                        tree.call(path, access.getFramsClass().getParamEntry(callRequest.getProcedure(), ProcedureParam.class), callRequest.getArguments().toArray(), new FutureHandler<Object>(responseCallback) {
160                                                @Override
161                                                protected void result(Object result) {
162                                                        ListSink sink = new ListSink();
163                                                        sink.print("Result:").breakLine();
164                                                        sink.print("value:").print("[");
165                                                        if (result != null) {
166                                                                sink.print(result);
167                                                        }
168                                                        sink.print("]");
169
170                                                        responseCallback.pass(new Response(true, "", File.single(new File("", new ListSource(sink.getOut())))));
171                                                }
172                                        });
173                                        return;
174                                }
175
176                                if (request instanceof InfoRequest) {
177                                        FramsClass framsClass = getInfo(path);
178                                        if (framsClass == null) {
179                                                throw new FramsticksException().msg("info should be available");
180                                        }
181                                        responseCallback.pass(new Response(true, null, File.single(new File(path.getTextual(), new ListSource(Savers.saveFramsClass(new ListSink(), framsClass).getOut())))));
182                                        return;
183                                }
184
185                                if (request instanceof RegisterRequest) {
186                                        RegisterRequest register = (RegisterRequest) request;
187
188                                        cliObject.addListener(path, access.getFramsClass().getParamEntry(register.getEventName(), EventParam.class), usedPrefix, responseCallback);
189                                        return;
190                                }
191
192                                throw new FramsticksException().msg("invalid request type: " + request.getCommand());
193                        }
194                });
195
196        }
197
198        @Override
199        protected void joinableStart() {
200                Dispatching.use(connection, this);
201                Dispatching.use(rootTree, this);
202        }
203
204        @Override
205        protected void joinableInterrupt() {
206                Dispatching.drop(rootTree, this);
207                Dispatching.drop(connection, this);
208        }
209
210        @Override
211        protected void joinableFinish() {
212
213        }
214
215        @Override
216        protected void joinableJoin() throws InterruptedException {
217                Dispatching.join(connection);
218                Dispatching.join(rootTree);
219        }
220
221        @Override
222        public void childChangedState(Joinable joinable, JoinableState state) {
223                proceedToState(state);
224        }
225
226        @Override
227        public void handle(FramsticksException exception) {
228                contentTree.handle(exception);
229        }
230
231        /**
232         * @return the tree
233         */
234        public Tree getTree() {
235                return contentTree;
236        }
237
238}
Note: See TracBrowser for help on using the repository browser.