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

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

HIGHLIGHTS:

  • loading f0 schema with XmlLoader?
  • use XmlLoader? to load configuration
  • introduce unified fork-join model of various entities

(Instances, Connections, GUI Frames, etc.),
all those entities clean up gracefully on
shutdown, which may be initialized by user
or by some entity

  • basing on above, simplify several organizing classes

(Observer, main class)

(to host native frams server process from Java level)

CHANGELOG:
Remove redundant Observer class.

Clean up in AbstractJoinable?.

Update ExternalProcess? class to changes in joining model.

Another sweep through code with FindBugs?.

Find bug with not joining RemoteInstance?.

Joining almost works.

Much improved joining model.

More improvement to joining model.

Add logging messages around joinable operations.

Rename methods in AbstractJoinable?.

Improve Joinable.

Rewrite of entity structure.

More simplifications with entities.

Further improve joinables.

Let Frame compose from JFrame instead of inheriting.

Add join classes.

Improvements of closing.

Add Builder interface.

Add FramsServerTest?.xml

FramsServer? may be configured through xml.

Make Framsticks main class an Observer of Entities.

Make Observer a generic type.

Remove variables regarding to removed endpoint.

Simplify observer (remove endpoints).

More changes to Observer and Endpoint.

Minor improvements.

Add OutputListener? to ExternalProcess?.

Improve testing of ExternalProcess?.

Add ExternalProcess? runner.

Rename the Program class to Framsticks.

Migrate Program to use XmlLoader? configuration.

First steps with configuration using XmlLoader?.

Fix several bugs.

Move all f0 classes to apriopriate package.

XmlLoader? is able to load Schema.

XmlLoader? is loading classes and props.

Add GroupBuilder?.

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