1 | package com.framsticks.communication; |
---|
2 | |
---|
3 | import com.framsticks.communication.queries.*; |
---|
4 | import com.framsticks.params.SourceInterface; |
---|
5 | import com.framsticks.util.lang.Pair; |
---|
6 | import com.framsticks.util.lang.Strings; |
---|
7 | import org.apache.log4j.Logger; |
---|
8 | |
---|
9 | import java.net.Socket; |
---|
10 | import com.framsticks.util.dispatching.RunAt; |
---|
11 | |
---|
12 | /** |
---|
13 | * @author Piotr Sniegowski |
---|
14 | */ |
---|
15 | public 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 | } |
---|