source: java/main/src/main/java/com/framsticks/hosting/ClientAtServer.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: 4.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.SetRequest;
11import com.framsticks.core.Mode;
12import com.framsticks.core.Tree;
13import com.framsticks.core.Path;
14import com.framsticks.params.*;
15import com.framsticks.params.types.ProcedureParam;
16import com.framsticks.parsers.Savers;
17import com.framsticks.util.FramsticksException;
18import com.framsticks.util.dispatching.AbstractJoinable;
19import com.framsticks.util.dispatching.Dispatching;
20import com.framsticks.util.dispatching.FutureHandler;
21import com.framsticks.util.dispatching.Joinable;
22import com.framsticks.util.dispatching.JoinableParent;
23import com.framsticks.util.dispatching.JoinableState;
24import static com.framsticks.core.TreeOperations.*;
25
26import java.net.Socket;
27import java.util.LinkedList;
28import java.util.List;
29
30/**
31 * @author Piotr Sniegowski
32 */
33public class ClientAtServer extends AbstractJoinable implements RequestHandler, JoinableParent {
34
35        protected final Server server;
36        protected final Tree tree;
37        protected final ServerSideManagedConnection connection;
38
39        public ClientAtServer(Server server, Socket socket) {
40                this.server = server;
41                this.tree = server.hosted;
42                this.connection = new ServerSideManagedConnection(socket, this);
43        }
44
45        @Override
46        public String getName() {
47                return connection + " to " + server;
48        }
49
50        @Override
51        public void handle(final ApplicationRequest request, final ServerSideResponseFuture responseCallback) {
52                assureNotEmpty(request.getPath());
53
54                resolve(tree, request.getPath(), new FutureHandler<Path>(responseCallback) {
55                        @Override
56                        protected void result(final Path path) {
57
58                                // final AccessInterface access = tree.prepareAccess(path);
59                                final AccessInterface access = bindAccess(path);
60
61                                if (request instanceof SetRequest) {
62                                        SetRequest setRequest = (SetRequest) request;
63                                        //TODO Primitive Param?
64                                        tree.set(path, access.getFramsClass().getParamEntry(setRequest.getField(), PrimitiveParam.class), setRequest.getValue(), new FutureHandler<Integer>(responseCallback) {
65                                                @Override
66                                                protected void result(Integer flag) {
67                                                        responseCallback.pass(new Response(true, Flags.write(flag, null), null));
68
69                                                }
70                                        });
71                                        return;
72                                }
73
74                                if (request instanceof GetRequest) {
75                                        tree.get(path, Mode.FETCH, new FutureHandler<Object>(responseCallback) {
76
77                                                @Override
78                                                protected void result(Object result) {
79                                                        if (result != access.getSelected()) {
80                                                                throw new FramsticksException().msg("mismatch objects during fetch").arg("path", path);
81                                                        }
82                                                        // TODO Auto-generated method stub
83                                                        List<File> files = new LinkedList<File>();
84                                                        ListSink sink = new ListSink();
85                                                        access.save(sink);
86                                                        files.add(new File(path.getTextual(), new ListSource(sink.getOut())));
87                                                        responseCallback.pass(new Response(true, "", files));
88                                                }
89                                        });
90
91                                        return;
92                                }
93                                if (request instanceof CallRequest) {
94                                        final CallRequest callRequest = (CallRequest) request;
95                                        tree.call(path, access.getFramsClass().getParamEntry(callRequest.getProcedure(), ProcedureParam.class), callRequest.getArguments().toArray(), new FutureHandler<Object>(responseCallback) {
96                                                @Override
97                                                protected void result(Object result) {
98                                                        ListSink sink = new ListSink();
99                                                        sink.print("Result:").breakLine();
100                                                        sink.print("value:").print("[");
101                                                        if (result != null) {
102                                                                sink.print(result);
103                                                        }
104                                                        sink.print("]");
105
106                                                        responseCallback.pass(new Response(true, "", File.single(new File("", new ListSource(sink.getOut())))));
107                                                }
108                                        });
109                                        return;
110                                }
111                                if (request instanceof InfoRequest) {
112                                        FramsClass framsClass = getInfo(path);
113                                        if (framsClass == null) {
114                                                throw new FramsticksException().msg("info should be available");
115                                        }
116                                        responseCallback.pass(new Response(true, null, File.single(new File(path.getTextual(), new ListSource(Savers.saveFramsClass(new ListSink(), framsClass).getOut())))));
117                                        return;
118                                }
119
120                                throw new FramsticksException().msg("invalid request type: " + request.getCommand());
121                        }
122                });
123
124        }
125
126        @Override
127        protected void joinableStart() {
128                Dispatching.use(connection, this);
129        }
130
131        @Override
132        protected void joinableInterrupt() {
133                Dispatching.drop(connection, this);
134        }
135
136        @Override
137        protected void joinableFinish() {
138        }
139
140        @Override
141        protected void joinableJoin() throws InterruptedException {
142                Dispatching.join(connection);
143        }
144
145        @Override
146        public void childChangedState(Joinable joinable, JoinableState state) {
147                proceedToState(state);
148        }
149
150
151}
Note: See TracBrowser for help on using the repository browser.