- 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/Connection.java
r77 r84 1 1 package com.framsticks.communication; 2 2 3 import com.framsticks.util. Pair;3 import com.framsticks.util.lang.Pair; 4 4 import org.apache.log4j.Logger; 5 5 import java.io.BufferedReader; … … 15 15 public abstract class Connection { 16 16 17 protected final static Logger LOGGER= Logger.getLogger(Connection.class);17 protected final static Logger log = Logger.getLogger(Connection.class); 18 18 19 19 protected PrintWriter output = null; … … 28 28 protected int protocolVersion = -1; 29 29 30 protected final com.framsticks.util. Thread senderThread = new com.framsticks.util.Thread();30 protected final com.framsticks.util.dispatching.Thread senderThread = new com.framsticks.util.dispatching.Thread(); 31 31 protected Thread receiverThread; 32 32 … … 40 40 connected = false; 41 41 42 43 42 senderThread.interrupt(); 43 senderThread.join(); 44 44 if (receiverThread != null) { 45 45 receiverThread.interrupt(); … … 64 64 } 65 65 66 LOGGER.info("connection closed");66 log.info("connection closed"); 67 67 } catch (Exception e) { 68 LOGGER.error(e);68 log.error(e); 69 69 } 70 70 … … 77 77 78 78 79 80 81 82 LOGGER.fatal("unmatched first line of input: " + rest);83 84 85 86 79 protected final Pair<Integer, String> parseRest(String rest) { 80 Matcher matcher = (requestIdEnabled ? requestIdEnabledPattern : requestIDisabledPattern).matcher(rest); 81 if (!matcher.matches()) { 82 log.fatal("unmatched first line of input: " + rest); 83 return null; 84 } 85 return new Pair<Integer, String>(requestIdEnabled ? Integer.parseInt(matcher.group(1)) : null, matcher.group(requestIdEnabled ? 2 : 1)); 86 } 87 87 88 88 static final int BUFFER_LENGTH = 1024; 89 89 90 91 92 93 90 int readChars = 0; 91 int iterator = 0; 92 int bufferStart = 0; 93 char[] readBuffer = new char[BUFFER_LENGTH]; 94 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 95 protected String getLine() throws Exception { 96 StringBuilder lineBuffer = new StringBuilder(); 97 while (!Thread.interrupted()) { 98 while (iterator < readChars) { 99 if (readBuffer[iterator] != '\n') { 100 ++iterator; 101 continue; 102 } 103 lineBuffer.append(readBuffer, bufferStart, iterator - bufferStart + 1); 104 ++iterator; 105 bufferStart = iterator; 106 return lineBuffer.toString(); 107 } 108 lineBuffer.append(readBuffer, bufferStart, readChars - bufferStart); 109 109 110 111 112 113 114 115 116 117 118 119 120 121 122 110 readChars = 0; 111 while (readChars == 0) { 112 try { 113 readChars = input.read(readBuffer); 114 } catch (SocketTimeoutException ignored) { 115 //timeout - continue 116 } 117 } 118 iterator = 0; 119 bufferStart = 0; 120 } 121 throw new InterruptedException(); 122 } 123 123 124 124 protected abstract void receiverThreadRoutine() throws Exception; 125 125 126 127 128 129 130 131 LOGGER.error("buffer creation failure");132 133 134 126 protected void runThreads() { 127 try { 128 output = new PrintWriter(socket.getOutputStream(), true); 129 input = new BufferedReader(new InputStreamReader(socket.getInputStream())); 130 } catch (IOException e) { 131 log.error("buffer creation failure"); 132 close(); 133 return; 134 } 135 135 136 137 138 139 140 141 142 143 LOGGER.debug("receiver thread interrupted");144 145 LOGGER.error("error: " + e);146 147 148 149 150 136 senderThread.setName(this + "-sender"); 137 receiverThread = new Thread(new Runnable() { 138 @Override 139 public void run() { 140 try { 141 receiverThreadRoutine(); 142 } catch (InterruptedException ignored) { 143 log.debug("receiver thread interrupted"); 144 } catch (Exception e) { 145 log.error("error: " + e); 146 close(); 147 } 148 } 149 }); 150 receiverThread.setName(this + "-receiver"); 151 151 152 153 154 152 senderThread.start(); 153 receiverThread.start(); 154 } 155 155 156 156
Note: See TracChangeset
for help on using the changeset viewer.