Mercurial Hosting > luan
comparison src/org/eclipse/jetty/server/nio/BlockingChannelConnector.java @ 975:53b3f7d9714c
simplify BlockingChannelConnector
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Sun, 16 Oct 2016 01:10:02 -0600 |
| parents | 7422ca1ae146 |
| children | bdb6eb0fbf93 |
comparison
equal
deleted
inserted
replaced
| 974:7422ca1ae146 | 975:53b3f7d9714c |
|---|---|
| 73 | 73 |
| 74 @Override | 74 @Override |
| 75 protected void doStart() throws Exception | 75 protected void doStart() throws Exception |
| 76 { | 76 { |
| 77 // Create a new server socket and set to non blocking mode | 77 // Create a new server socket and set to non blocking mode |
| 78 _acceptChannel= ServerSocketChannel.open(); | 78 _acceptChannel = ServerSocketChannel.open(); |
| 79 _acceptChannel.configureBlocking(true); | 79 _acceptChannel.configureBlocking(true); |
| 80 | 80 |
| 81 // Bind the server socket to the local host and port | 81 // Bind the server socket to the local host and port |
| 82 InetSocketAddress addr = getHost()==null?new InetSocketAddress(port):new InetSocketAddress(getHost(),port); | 82 InetSocketAddress addr = getHost()==null?new InetSocketAddress(port):new InetSocketAddress(getHost(),port); |
| 83 _acceptChannel.bind(addr); | 83 _acceptChannel.bind(addr); |
| 91 while (isRunning()) | 91 while (isRunning()) |
| 92 { | 92 { |
| 93 try | 93 try |
| 94 { | 94 { |
| 95 Thread.sleep(400); | 95 Thread.sleep(400); |
| 96 long now=System.currentTimeMillis(); | 96 long now = System.currentTimeMillis(); |
| 97 for (BlockingChannelEndPoint endp : _endpoints) | 97 for (BlockingChannelEndPoint endp : _endpoints) |
| 98 { | 98 { |
| 99 endp.checkIdleTimestamp(now); | 99 endp.checkIdleTimestamp(now); |
| 100 } | 100 } |
| 101 } | 101 } |
| 121 SocketChannel channel = _acceptChannel.accept(); | 121 SocketChannel channel = _acceptChannel.accept(); |
| 122 channel.configureBlocking(true); | 122 channel.configureBlocking(true); |
| 123 Socket socket = channel.socket(); | 123 Socket socket = channel.socket(); |
| 124 configure(socket); | 124 configure(socket); |
| 125 | 125 |
| 126 BlockingChannelEndPoint connection=new BlockingChannelEndPoint(channel); | 126 BlockingChannelEndPoint connection = new BlockingChannelEndPoint(channel); |
| 127 connection.dispatch(); | 127 try { |
| 128 server.threadPool.execute(connection); | |
| 129 } catch(RejectedExecutionException e) { | |
| 130 LOG.warn("dispatch failed for {}",connection._connection); | |
| 131 connection.close(); | |
| 132 } | |
| 128 } | 133 } |
| 129 | 134 |
| 130 @Override | 135 @Override |
| 131 public void customize(EndPoint endpoint, Request request) | 136 public void customize(EndPoint endpoint, Request request) |
| 132 throws IOException | 137 throws IOException |
| 148 { | 153 { |
| 149 super(channel,BlockingChannelConnector.this._maxIdleTime); | 154 super(channel,BlockingChannelConnector.this._maxIdleTime); |
| 150 _connection = new BlockingHttpConnection(BlockingChannelConnector.this,this); | 155 _connection = new BlockingHttpConnection(BlockingChannelConnector.this,this); |
| 151 } | 156 } |
| 152 | 157 |
| 153 public void checkIdleTimestamp(long now) | 158 private void checkIdleTimestamp(long now) |
| 154 { | 159 { |
| 155 if (_idleTimestamp!=0 && _timeout>0 && now>(_idleTimestamp+_timeout)) | 160 if (_idleTimestamp!=0 && _timeout>0 && now>(_idleTimestamp+_timeout)) |
| 156 { | 161 { |
| 157 idleExpired(); | 162 try |
| 158 } | 163 { |
| 159 } | 164 close(); |
| 160 | 165 } |
| 161 protected void idleExpired() | 166 catch (IOException e) |
| 167 { | |
| 168 LOG.trace("",e); | |
| 169 } | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 @Override | |
| 174 public int fill(Buffer buffer) throws IOException | |
| 175 { | |
| 176 _idleTimestamp = System.currentTimeMillis(); | |
| 177 return super.fill(buffer); | |
| 178 } | |
| 179 | |
| 180 @Override | |
| 181 public int flush(Buffer buffer) throws IOException | |
| 182 { | |
| 183 _idleTimestamp = System.currentTimeMillis(); | |
| 184 return super.flush(buffer); | |
| 185 } | |
| 186 | |
| 187 @Override | |
| 188 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException | |
| 189 { | |
| 190 _idleTimestamp = System.currentTimeMillis(); | |
| 191 return super.flush(header,buffer,trailer); | |
| 192 } | |
| 193 | |
| 194 @Override | |
| 195 public void run() | |
| 162 { | 196 { |
| 163 try | 197 try |
| 164 { | 198 { |
| 165 super.close(); | 199 _timeout = getMaxIdleTime(); |
| 166 } | |
| 167 catch (IOException e) | |
| 168 { | |
| 169 LOG.trace("",e); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 void dispatch() throws IOException | |
| 174 { | |
| 175 try { | |
| 176 server.threadPool.execute(this); | |
| 177 } catch(RejectedExecutionException e) { | |
| 178 LOG.warn("dispatch failed for {}",_connection); | |
| 179 super.close(); | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 /* ------------------------------------------------------------ */ | |
| 184 /** | |
| 185 * @see org.eclipse.jetty.io.nio.ChannelEndPoint#fill(org.eclipse.jetty.io.Buffer) | |
| 186 */ | |
| 187 @Override | |
| 188 public int fill(Buffer buffer) throws IOException | |
| 189 { | |
| 190 _idleTimestamp=System.currentTimeMillis(); | |
| 191 return super.fill(buffer); | |
| 192 } | |
| 193 | |
| 194 /* ------------------------------------------------------------ */ | |
| 195 /** | |
| 196 * @see org.eclipse.jetty.io.nio.ChannelEndPoint#flush(org.eclipse.jetty.io.Buffer) | |
| 197 */ | |
| 198 @Override | |
| 199 public int flush(Buffer buffer) throws IOException | |
| 200 { | |
| 201 _idleTimestamp=System.currentTimeMillis(); | |
| 202 return super.flush(buffer); | |
| 203 } | |
| 204 | |
| 205 /* ------------------------------------------------------------ */ | |
| 206 /** | |
| 207 * @see org.eclipse.jetty.io.nio.ChannelEndPoint#flush(org.eclipse.jetty.io.Buffer, org.eclipse.jetty.io.Buffer, org.eclipse.jetty.io.Buffer) | |
| 208 */ | |
| 209 @Override | |
| 210 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException | |
| 211 { | |
| 212 _idleTimestamp=System.currentTimeMillis(); | |
| 213 return super.flush(header,buffer,trailer); | |
| 214 } | |
| 215 | |
| 216 /* ------------------------------------------------------------ */ | |
| 217 public void run() | |
| 218 { | |
| 219 try | |
| 220 { | |
| 221 _timeout=getMaxIdleTime(); | |
| 222 _endpoints.add(this); | 200 _endpoints.add(this); |
| 223 | 201 |
| 224 while (isOpen()) | 202 while (isOpen()) |
| 225 { | 203 { |
| 226 _idleTimestamp=System.currentTimeMillis(); | 204 _idleTimestamp = System.currentTimeMillis(); |
| 227 _connection.handle(); | 205 _connection.handle(); |
| 228 | 206 |
| 229 } | 207 } |
| 230 } | 208 } |
| 231 catch (EofException e) | 209 catch (EofException e) |
| 235 catch(IOException e2){LOG.trace("",e2);} | 213 catch(IOException e2){LOG.trace("",e2);} |
| 236 } | 214 } |
| 237 catch (HttpException e) | 215 catch (HttpException e) |
| 238 { | 216 { |
| 239 LOG.debug("BAD", e); | 217 LOG.debug("BAD", e); |
| 240 try{super.close();} | 218 try{close();} |
| 241 catch(IOException e2){LOG.trace("",e2);} | 219 catch(IOException e2){LOG.trace("",e2);} |
| 242 } | 220 } |
| 243 catch(Throwable e) | 221 catch(Throwable e) |
| 244 { | 222 { |
| 245 LOG.warn("handle failed",e); | 223 LOG.warn("handle failed",e); |
| 246 try{super.close();} | 224 try{close();} |
| 247 catch(IOException e2){LOG.trace("",e2);} | 225 catch(IOException e2){LOG.trace("",e2);} |
| 248 } | 226 } |
| 249 finally | 227 finally |
| 250 { | 228 { |
| 251 _endpoints.remove(this); | 229 _endpoints.remove(this); |
| 253 // wait for client to close, but if not, close ourselves. | 231 // wait for client to close, but if not, close ourselves. |
| 254 try | 232 try |
| 255 { | 233 { |
| 256 if (!_socket.isClosed()) | 234 if (!_socket.isClosed()) |
| 257 { | 235 { |
| 258 long timestamp=System.currentTimeMillis(); | 236 long timestamp = System.currentTimeMillis(); |
| 259 int max_idle=getMaxIdleTime(); | 237 int max_idle = getMaxIdleTime(); |
| 260 | 238 |
| 261 _socket.setSoTimeout(getMaxIdleTime()); | 239 _socket.setSoTimeout(getMaxIdleTime()); |
| 262 int c=0; | 240 int c=0; |
| 263 do | 241 do |
| 264 { | 242 { |
| 274 LOG.trace("",e); | 252 LOG.trace("",e); |
| 275 } | 253 } |
| 276 } | 254 } |
| 277 } | 255 } |
| 278 | 256 |
| 279 /* ------------------------------------------------------------ */ | |
| 280 @Override | 257 @Override |
| 281 public String toString() | 258 public String toString() |
| 282 { | 259 { |
| 283 return String.format("BCEP@%x{l(%s)<->r(%s),open=%b,ishut=%b,oshut=%b}-{%s}", | 260 return String.format("BCEP@%x{l(%s)<->r(%s),open=%b,ishut=%b,oshut=%b}-{%s}", |
| 284 hashCode(), | 261 hashCode(), |
