comparison src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java @ 947:64f3d8dae31d

simplify SelectChannelEndPoint
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 11 Oct 2016 21:33:40 -0600
parents 901dcfa05c32
children f5aefdc4a81a
comparison
equal deleted inserted replaced
946:901dcfa05c32 947:64f3d8dae31d
65 private volatile AsyncConnection _connection; 65 private volatile AsyncConnection _connection;
66 66
67 private static final int STATE_NEEDS_DISPATCH=-1; 67 private static final int STATE_NEEDS_DISPATCH=-1;
68 private static final int STATE_UNDISPATCHED=0; 68 private static final int STATE_UNDISPATCHED=0;
69 private static final int STATE_DISPATCHED=1; 69 private static final int STATE_DISPATCHED=1;
70 private static final int STATE_ASYNC=2;
71 private int _state; 70 private int _state;
72 71
73 private boolean _onIdle; 72 private boolean _onIdle;
74 73
75 /** true if the last write operation succeed and wrote all offered bytes */ 74 /** true if the last write operation succeed and wrote all offered bytes */
184 dispatch(); 183 dispatch();
185 } 184 }
186 } 185 }
187 } 186 }
188 187
189 /* ------------------------------------------------------------ */ 188 public synchronized void dispatch()
190 public void dispatch() 189 {
191 { 190 if (_state<=STATE_UNDISPATCHED)
192 synchronized(this) 191 {
193 { 192 if (_onIdle)
194 if (_state<=STATE_UNDISPATCHED) 193 _state = STATE_NEEDS_DISPATCH;
195 { 194 else
196 if (_onIdle) 195 {
196 _state = STATE_DISPATCHED;
197 try {
198 _manager.execute(_handler);
199 } catch(RejectedExecutionException e) {
197 _state = STATE_NEEDS_DISPATCH; 200 _state = STATE_NEEDS_DISPATCH;
198 else 201 LOG.warn("Dispatched Failed! "+this+" to "+_manager);
199 { 202 updateKey();
200 _state = STATE_DISPATCHED;
201 try {
202 _manager.execute(_handler);
203 } catch(RejectedExecutionException e) {
204 _state = STATE_NEEDS_DISPATCH;
205 LOG.warn("Dispatched Failed! "+this+" to "+_manager);
206 updateKey();
207 }
208 } 203 }
209 } 204 }
210 } 205 }
211 } 206 }
212 207
215 * Called when a dispatched thread is no longer handling the endpoint. 210 * Called when a dispatched thread is no longer handling the endpoint.
216 * The selection key operations are updated. 211 * The selection key operations are updated.
217 * @return If false is returned, the endpoint has been redispatched and 212 * @return If false is returned, the endpoint has been redispatched and
218 * thread must keep handling the endpoint. 213 * thread must keep handling the endpoint.
219 */ 214 */
220 protected boolean undispatch() 215 protected synchronized void undispatch()
221 { 216 {
222 synchronized (this) 217 _state = STATE_UNDISPATCHED;
223 { 218 updateKey();
224 switch(_state) 219 // return true;
225 { 220 }
226 case STATE_ASYNC: 221
227 _state=STATE_DISPATCHED;
228 return false;
229
230 default:
231 _state=STATE_UNDISPATCHED;
232 updateKey();
233 return true;
234 }
235 }
236 }
237
238 /* ------------------------------------------------------------ */
239 public void setCheckForIdle(boolean check) 222 public void setCheckForIdle(boolean check)
240 { 223 {
241 if (check) 224 if (check)
242 { 225 {
243 _idleTimestamp=System.currentTimeMillis(); 226 _idleTimestamp=System.currentTimeMillis();
245 } 228 }
246 else 229 else
247 _checkIdle=false; 230 _checkIdle=false;
248 } 231 }
249 232
250 /* ------------------------------------------------------------ */
251 public boolean isCheckForIdle() 233 public boolean isCheckForIdle()
252 { 234 {
253 return _checkIdle; 235 return _checkIdle;
254 } 236 }
255 237
256 /* ------------------------------------------------------------ */
257 protected void notIdle() 238 protected void notIdle()
258 { 239 {
259 _idleTimestamp=System.currentTimeMillis(); 240 _idleTimestamp=System.currentTimeMillis();
260 } 241 }
261 242
262 /* ------------------------------------------------------------ */
263 public void checkIdleTimestamp(long now) 243 public void checkIdleTimestamp(long now)
264 { 244 {
265 if (isCheckForIdle() && _maxIdleTime>0) 245 if (isCheckForIdle() && _maxIdleTime>0)
266 { 246 {
267 final long idleForMs=now-_idleTimestamp; 247 final long idleForMs=now-_idleTimestamp;
286 }); 266 });
287 } 267 }
288 } 268 }
289 } 269 }
290 270
291 /* ------------------------------------------------------------ */
292 public void onIdleExpired(long idleForMs) 271 public void onIdleExpired(long idleForMs)
293 { 272 {
294 try 273 try
295 { 274 {
296 synchronized (this) 275 synchronized (this)
309 dispatch(); 288 dispatch();
310 } 289 }
311 } 290 }
312 } 291 }
313 292
314 /* ------------------------------------------------------------ */
315 @Override 293 @Override
316 public int fill(Buffer buffer) throws IOException 294 public int fill(Buffer buffer) throws IOException
317 { 295 {
318 int fill=super.fill(buffer); 296 int fill=super.fill(buffer);
319 if (fill>0) 297 if (fill>0)
320 notIdle(); 298 notIdle();
321 return fill; 299 return fill;
322 } 300 }
323 301
324 /* ------------------------------------------------------------ */
325 @Override 302 @Override
326 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException 303 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
327 { 304 {
328 int l = super.flush(header, buffer, trailer); 305 int l = super.flush(header, buffer, trailer);
329 306
343 notIdle(); 320 notIdle();
344 } 321 }
345 return l; 322 return l;
346 } 323 }
347 324
348 /* ------------------------------------------------------------ */
349 /*
350 */
351 @Override 325 @Override
352 public int flush(Buffer buffer) throws IOException 326 public int flush(Buffer buffer) throws IOException
353 { 327 {
354 int l = super.flush(buffer); 328 int l = super.flush(buffer);
355 329
606 /* ------------------------------------------------------------ */ 580 /* ------------------------------------------------------------ */
607 /* 581 /*
608 */ 582 */
609 protected void handle() 583 protected void handle()
610 { 584 {
611 boolean dispatched=true; 585 boolean dispatched = true;
612 try 586 try
613 { 587 {
614 while(dispatched) 588 try
615 { 589 {
616 try 590 while(true)
617 { 591 {
618 while(true) 592 final AsyncConnection next = (AsyncConnection)_connection.handle();
619 { 593 if (next!=_connection)
620 final AsyncConnection next = (AsyncConnection)_connection.handle(); 594 {
621 if (next!=_connection) 595 LOG.debug("{} replaced {}",next,_connection);
622 { 596 _connection=next;
623 LOG.debug("{} replaced {}",next,_connection); 597 continue;
624 _connection=next; 598 }
625 continue; 599 break;
626 } 600 }
627 break; 601 }
628 } 602 catch (ClosedChannelException e)
629 } 603 {
630 catch (ClosedChannelException e) 604 LOG.trace("",e);
631 { 605 }
632 LOG.trace("",e); 606 catch (EofException e)
633 } 607 {
634 catch (EofException e) 608 LOG.debug("EOF", e);
635 { 609 try{close();}
636 LOG.debug("EOF", e); 610 catch(IOException e2){LOG.trace("",e2);}
637 try{close();} 611 }
638 catch(IOException e2){LOG.trace("",e2);} 612 catch (IOException e)
639 } 613 {
640 catch (IOException e) 614 LOG.warn(e.toString());
641 { 615 try{close();}
642 LOG.warn(e.toString()); 616 catch(IOException e2){LOG.trace("",e2);}
643 try{close();} 617 }
644 catch(IOException e2){LOG.trace("",e2);} 618 catch (Throwable e)
645 } 619 {
646 catch (Throwable e) 620 LOG.warn("handle failed", e);
647 { 621 try{close();}
648 LOG.warn("handle failed", e); 622 catch(IOException e2){LOG.trace("",e2);}
649 try{close();} 623 }
650 catch(IOException e2){LOG.trace("",e2);} 624 finally
651 } 625 {
652 finally 626 if (!_ishut && isInputShutdown() && isOpen())
653 { 627 {
654 if (!_ishut && isInputShutdown() && isOpen()) 628 _ishut = true;
655 { 629 try
656 _ishut=true; 630 {
657 try 631 _connection.onInputShutdown();
658 { 632 }
659 _connection.onInputShutdown(); 633 catch(Throwable x)
660 } 634 {
661 catch(Throwable x) 635 LOG.warn("onInputShutdown failed", x);
662 { 636 try{close();}
663 LOG.warn("onInputShutdown failed", x); 637 catch(IOException e2){LOG.trace("",e2);}
664 try{close();} 638 }
665 catch(IOException e2){LOG.trace("",e2);} 639 finally
666 } 640 {
667 finally 641 updateKey();
668 { 642 }
669 updateKey(); 643 }
670 } 644 undispatch();
671 } 645 dispatched = false;
672 dispatched=!undispatch();
673 }
674 } 646 }
675 } 647 }
676 finally 648 finally
677 { 649 {
678 if (dispatched) 650 if (dispatched)
679 { 651 {
680 dispatched=!undispatch(); 652 undispatch();
681 while (dispatched)
682 {
683 LOG.warn("SCEP.run() finally DISPATCHED");
684 dispatched=!undispatch();
685 }
686 } 653 }
687 } 654 }
688 } 655 }
689 656
690 /* ------------------------------------------------------------ */ 657 /* ------------------------------------------------------------ */