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

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

HIGHLIGHTS:

  • upgrade to Java 7
    • use try-multi-catch clauses
    • use try-with-resources were appropriate
  • configure FindBugs? (use mvn site and then navigate in browser to the report)
    • remove most bugs found
  • parametrize Dispatching environment (Dispatcher, RunAt?) to enforce more control on the place of closures actual call

CHANGELOG:
Rework FavouritesXMLFactory.

FindBugs?. Thread start.

FindBugs?. Minor change.

FindBugs?. Iterate over entrySet.

FindBugs?. Various.

FindBug?.

FindBug?. Encoding.

FindBug?. Final fields.

FindBug?.

Remove synchronization bug in ClientConnection?.

Experiments with findbugs.

Finish parametrization.

Make RunAt? an abstract class.

More changes in parametrization.

More changes in parametrizing dispatching.

Several changes to parametrize tasks.

Rename Runnable to RunAt?.

Add specific framsticks Runnable.

Add JSR305 (annotations).

Add findbugs reporting.

More improvements to ParamBuilder? wording.

Make FramsClass? accept also ParamBuilder?.

Change wording of ParamBuilder?.

Change wording of Request creation.

Use Java 7 exception catch syntax.

Add ScopeEnd? class.

Upgrade to Java 7.

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