source: java/main/src/main/java/com/framsticks/communication/ServerSideManagedConnection.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: 3.7 KB
Line 
1package com.framsticks.communication;
2
3import com.framsticks.communication.queries.*;
4import com.framsticks.params.SourceInterface;
5import com.framsticks.util.FramsticksException;
6import com.framsticks.util.lang.Holder;
7import com.framsticks.util.lang.Pair;
8import com.framsticks.util.lang.Strings;
9
10import org.apache.log4j.Logger;
11
12import java.net.Socket;
13import com.framsticks.util.dispatching.RunAt;
14import com.framsticks.util.dispatching.ThrowExceptionHandler;
15
16/**
17 * @author Piotr Sniegowski
18 */
19public class ServerSideManagedConnection extends ManagedConnection {
20
21        private final static Logger log = Logger.getLogger(ServerSideManagedConnection.class);
22
23        RequestHandler requestHandler;
24
25        public ServerSideManagedConnection(Socket socket, RequestHandler requestHandler) {
26                setAddress(new Address(socket.getInetAddress().getHostAddress(), socket.getPort()));
27                setDescription("server connection");
28                this.socket = socket;
29                this.requestHandler = requestHandler;
30                // socket.setSoTimeout(500);
31                setupStreams();
32        }
33
34
35        protected void processNextInputBatch() {
36                processNextRequest();
37        }
38
39        @Override
40        protected void receiverThreadRoutine() {
41
42                processInputBatchesUntilClosed();
43        }
44
45        protected void handleRequest(Request request, ServerSideResponseFuture responseCallback) {
46                if (request instanceof ApplicationRequest) {
47                        requestHandler.handle((ApplicationRequest) request, responseCallback);
48                        return;
49                }
50                if (request instanceof ProtocolRequest) {
51                        if (request instanceof VersionRequest) {
52                                responseCallback.pass(new Response(true, null, null));
53                                return;
54                        }
55                        if (request instanceof UseRequest) {
56                                String feature = ((UseRequest)request).getFeature();
57                                if (feature.equals("request_id")) {
58                                        requestIdEnabled = true;
59                                        responseCallback.pass(new Response(true, null, null));
60                                        return;
61                                }
62                                responseCallback.pass(new Response(false, "unknown feature: " + feature, null));
63                                return;
64                        }
65
66                }
67                log.error("unhandled request: " + request);
68                responseCallback.pass(new Response(false, "unhandled", null));
69        }
70
71        protected final void respond(final Response response, final Integer id) {
72                //TODO TEH: pass it the hosted tree
73                senderThread.dispatch(new RunAt<Connection>(ThrowExceptionHandler.getInstance()) {
74                        @Override
75                        protected void runAt() {
76                                String outId = id != null ? " " + id : "";
77                                if (response.getFiles() != null) {
78                                        for (File f : response.getFiles()) {
79                                                putLine("file" + outId/* + " " + f.getPath()*/);
80                                                SourceInterface content = f.getContent();
81                                                String line;
82                                                while ((line = content.readLine()) != null) {
83                                                        putLine(line);
84                                                }
85                                                putLine("eof");
86                                        }
87                                }
88                                StringBuilder statusLine = new StringBuilder();
89                                statusLine.append(response.getOk() ? "ok" : "error").append(outId);
90                                if (Strings.notEmpty(response.getComment())) {
91                                        statusLine.append(" \"").append(response.getComment()).append('"');
92                                }
93                                putLine(statusLine.toString());
94                                flushOut();
95                        }
96                });
97
98        }
99
100
101        protected void processNextRequest() {
102                final Holder<Integer> id = new Holder<>();
103                final String line = getLine();
104                try {
105                        Pair<CharSequence, CharSequence> command = Request.takeIdentifier(line);
106                        final Pair<Integer, CharSequence> rest = takeRequestId(command.second);
107                        id.set(rest.first);
108
109                        final Request request = Request.parse(command.first, rest.second);
110
111                        if (log.isTraceEnabled()) {
112                                log.trace("read request: " + request);
113                        }
114
115                        //TODO what to do here?
116                        handleRequest(request, new ServerSideResponseFuture() {
117                                @Override
118                                protected void result(Response response) {
119                                        respond(response, rest.first);
120                                }
121                        });
122                } catch (FramsticksException e) {
123                        e.arg("id", id.get()).arg("line", line);
124                        log.error("error: ", e);
125                        respond(new Response(false, "invalid input: " + e.getMsg(), null), id.get());
126                        return;
127                }
128
129        }
130}
Note: See TracBrowser for help on using the repository browser.