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

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

Add new java codebase.

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