- Timestamp:
- 06/22/13 21:51:33 (11 years ago)
- Location:
- java/main
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main
-
Property
svn:ignore
set to
target
-
Property
svn:ignore
set to
-
java/main/src/main/java/com/framsticks/communication/ServerConnection.java
r77 r84 3 3 import com.framsticks.communication.queries.*; 4 4 import com.framsticks.params.SourceInterface; 5 import com.framsticks.util. Pair;6 import com.framsticks.util. Strings;5 import com.framsticks.util.lang.Pair; 6 import com.framsticks.util.lang.Strings; 7 7 import org.apache.log4j.Logger; 8 8 9 import java.io.BufferedReader;10 import java.io.IOException;11 import java.io.InputStreamReader;12 import java.io.PrintWriter;13 9 import java.net.Socket; 14 10 … … 18 14 public class ServerConnection extends Connection { 19 15 20 private final static Logger LOGGER= Logger.getLogger(ServerConnection.class);16 private final static Logger log = Logger.getLogger(ServerConnection.class); 21 17 22 18 RequestHandler requestHandler; 23 19 24 25 26 27 20 public ServerConnection(Socket socket, RequestHandler requestHandler) { 21 this.socket = socket; 22 this.requestHandler = requestHandler; 23 connected = true; 28 24 29 25 } 30 26 31 27 public void start() { 32 28 33 29 34 35 30 runThreads(); 31 } 36 32 37 38 39 40 33 @Override 34 public String toString() { 35 return socket.getInetAddress().getHostAddress(); 36 } 41 37 42 43 44 45 46 47 38 @Override 39 protected void receiverThreadRoutine() throws Exception { 40 while (connected) { 41 processNextRequest(); 42 } 43 } 48 44 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 } 69 65 70 71 LOGGER.error("unhandled request: " + request);72 73 66 } 67 log.error("unhandled request: " + request); 68 responseCallback.process(new Response(false, "unhandled", null)); 69 } 74 70 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 }); 102 98 103 99 } 104 100 105 106 107 108 109 110 111 112 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 } 113 109 114 115 116 117 118 119 120 if (LOGGER.isTraceEnabled()) {121 LOGGER.trace("read request: " + request);122 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 } 123 119 124 125 126 127 128 129 130 120 handleRequest(request, new ResponseCallback() { 121 @Override 122 public void process(Response response) { 123 respond(response, rest.first); 124 } 125 }); 126 } 131 127 }
Note: See TracChangeset
for help on using the changeset viewer.