source: java/main/src/main/java/com/framsticks/communication/ServerConnection.java @ 84

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

HIGHLIGHTS:

  • simplification of entities management model
  • cleanup around params (improve hierarchy)
  • migrate from JUnit to TestNG
  • introduce FEST to automatically test GUI
  • improve slider control
  • loosen synchronization between gui tree and backend representation
  • and many other bug fixes

NOTICE:

  • a great many of lines is changed only because of substituting spaces with tabs

CHANGELOG (oldest changes at the bottom):

Some cleaning after fix found.

Fix bug with tree.

More changes with TreeNodes?.

Finally fix issue with tree.

Improve gui tree management.

Decouple update of values from fetch request in gui.

Minor changes.

Minor changes.

Minor change.

Change Path construction wording.

More fixes to SliderControl?.

Fix SliderControl?.

Fix SliderControl?.

Minor improvement.

Several changes.

Make NumberParam? a generic class.

Add robot to the gui test.

Setup common testing logging configuration.

Remove Parameters class.

Remove entityOwner from Parameters.

Move name out from Parameters class.

Move configuration to after the construction.

Simplify observers and endpoints.

Remove superfluous configureEntity overrides.

Add dependency on fest-swing-testng.

Use FEST for final print test.

Use FEST for more concise and readable assertions.

Divide test of F0Parser into multiple methods.

Migrate to TestNG

Minor change.

Change convention from LOGGER to log.

Fix reporting of errors during controls filling.

Bound maximal height of SliderControl?.

Minor improvements.

Improve tooltips for controls.

Also use Delimeted in more places.

Move static control utilities to Gui.

Rename package gui.components to controls.

Some cleaning in controls.

Improve Param classes placing.

Move ValueParam?, PrimitiveParam? and CompositeParam? one package up.

Improve ParamBuilder?.

Move getDef to ValueParam? and PrimitiveParam?.

Move getMax and getDef to ValueParam?.

Move getMin to ValueParam?.

Upgrade to laters apache commons versions.

Use filterInstanceof extensively.

Add instanceof filters.

Make ValueParam? in many places of Param.

Place assertions about ValueParam?.

Add ValueParam?

Rename ValueParam? to PrimitiveParam?

Minor changes.

Several improvements to params types.

Add NumberParam?.

Add TextControl? component.

Add .swp files to .gitignore

Greatly improved slider component.

Some improvements.

Make Param.reassign return also a state.

Add IterableIterator?.

Several changes.

  • Move util classes to better packages.
  • Remove warnings from eclim.

Several improvements.

Fix bug with BooleanParam?.

Some experiments with visualization.

Another fix to panel management.

Improve panel management.

Some refactorization around panels.

Add root class for panel.

File size: 3.3 KB
Line 
1package com.framsticks.communication;
2
3import com.framsticks.communication.queries.*;
4import com.framsticks.params.SourceInterface;
5import com.framsticks.util.lang.Pair;
6import com.framsticks.util.lang.Strings;
7import org.apache.log4j.Logger;
8
9import java.net.Socket;
10
11/**
12 * @author Piotr Sniegowski
13 */
14public class ServerConnection extends Connection {
15
16        private final static Logger log = Logger.getLogger(ServerConnection.class);
17
18        RequestHandler requestHandler;
19
20        public ServerConnection(Socket socket, RequestHandler requestHandler) {
21                this.socket = socket;
22                this.requestHandler = requestHandler;
23                connected = true;
24
25        }
26
27        public void start() {
28
29
30                runThreads();
31        }
32
33        @Override
34        public String toString() {
35                return socket.getInetAddress().getHostAddress();
36        }
37
38        @Override
39        protected void receiverThreadRoutine() throws Exception {
40                while (connected) {
41                        processNextRequest();
42                }
43        }
44
45        protected void handleRequest(Request request, ResponseCallback 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.process(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.process(new Response(true, null, null));
60                                        return;
61                                }
62                                responseCallback.process(new Response(false, "\"unknown feature: " + feature + "\"", null));
63                                return;
64                        }
65
66                }
67                log.error("unhandled request: " + request);
68                responseCallback.process(new Response(false, "unhandled", null));
69        }
70
71        protected final void respond(final Response response, final Integer id) {
72                senderThread.invokeLater(new Runnable() {
73                        @Override
74                        public void run() {
75                                String outId = id != null ? " " + id : "";
76                                if (response.getFiles() != null) {
77                                        for (File f : response.getFiles()) {
78                                                output.print("file" + outId/* + " " + f.getPath()*/ + "\n");
79                                                SourceInterface content = f.getContent();
80                                                String line;
81                                                while ((line = content.readLine()) != null) {
82                                                        output.print(line);
83                                                        output.print('\n');
84                                                }
85                                                output.print("eof\n");
86                                        }
87                                }
88                                output.print(response.getOk() ? "ok" : "error");
89                                output.print(outId);
90                                if (Strings.notEmpty(response.getComment())) {
91                                        output.print(' ');
92                                        output.print(response.getComment());
93                                }
94                                output.print('\n');
95                                output.flush();
96                        }
97                });
98
99        }
100
101        protected void processNextRequest() throws Exception {
102                String line = getLine();
103                Pair<String, String> command = Strings.splitIntoPair(line, ' ', "\n");
104                final Pair<Integer, String> rest = parseRest(command.second);
105                if (rest == null) {
106                        respond(new Response(false, "\"invalid input\"", null), null);
107                        return;
108                }
109
110                final Request request = Request.createRequestByTypeString(command.first);
111                if (request == null) {
112                        respond(new Response(false, "\"invalid input\"", null), null);
113                        return;
114                }
115                request.parseRest(rest.second);
116                if (log.isTraceEnabled()) {
117                        log.trace("read request: " + request);
118                }
119
120                handleRequest(request, new ResponseCallback() {
121                        @Override
122                        public void process(Response response) {
123                                respond(response, rest.first);
124                        }
125                });
126        }
127}
Note: See TracBrowser for help on using the repository browser.