comparison src/org/eclipse/jetty/io/nio/SelectorManager.java @ 948:f5aefdc4a81a

simplify SelectChannelConnector
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 11 Oct 2016 22:16:29 -0600
parents 1d24b6e422fa
children e9088af3787f
comparison
equal deleted inserted replaced
947:64f3d8dae31d 948:f5aefdc4a81a
90 _selectSets=selectSets; 90 _selectSets=selectSets;
91 _lowResourcesConnections=lrc/_selectSets; 91 _lowResourcesConnections=lrc/_selectSets;
92 } 92 }
93 93
94 /* ------------------------------------------------------------ */ 94 /* ------------------------------------------------------------ */
95 /**
96 * @return the max idle time
97 */
98 public long getMaxIdleTime()
99 {
100 return _maxIdleTime;
101 }
102
103 /* ------------------------------------------------------------ */
104 /**
105 * @return the number of select sets in use
106 */
107 public int getSelectSets()
108 {
109 return _selectSets;
110 }
111
112 /* ------------------------------------------------------------ */
113 /**
114 * @param i
115 * @return The select set
116 */
117 public SelectSet getSelectSet(int i)
118 {
119 return _selectSet[i];
120 }
121
122 /* ------------------------------------------------------------ */
123 /** Register a channel 95 /** Register a channel
124 * @param channel 96 * @param channel
125 */ 97 */
126 public void register(SocketChannel channel) 98 public void register(SocketChannel channel)
127 { 99 {
128 // The ++ increment here is not atomic, but it does not matter. 100 // The ++ increment here is not atomic, but it does not matter.
129 // so long as the value changes sometimes, then connections will 101 // so long as the value changes sometimes, then connections will
130 // be distributed over the available sets. 102 // be distributed over the available sets.
131 103
132 int s=_set++; 104 int s = _set++;
133 if (s<0) 105 if (s<0)
134 s=-s; 106 s=-s;
135 s=s%_selectSets; 107 s=s%_selectSets;
136 SelectSet[] sets=_selectSet; 108 SelectSet[] sets = _selectSet;
137 if (sets!=null) 109 if (sets!=null)
138 { 110 {
139 SelectSet set=sets[s]; 111 SelectSet set=sets[s];
140 set.addChange(channel); 112 set.addChange(channel);
141 set.wakeup(); 113 set.wakeup();
194 _selectSet[i]= new SelectSet(i); 166 _selectSet[i]= new SelectSet(i);
195 167
196 super.doStart(); 168 super.doStart();
197 169
198 // start a thread to Select 170 // start a thread to Select
199 for (int i=0;i<getSelectSets();i++) 171 for (int i=0;i<_selectSets;i++)
200 { 172 {
201 final int id=i; 173 final int id=i;
202 execute(new Runnable() 174 execute(new Runnable()
203 { 175 {
204 public void run() 176 public void run()
256 } 228 }
257 } 229 }
258 super.doStop(); 230 super.doStop();
259 } 231 }
260 232
261 /* ------------------------------------------------------------ */
262 /**
263 * @param endpoint
264 */
265 protected abstract void endPointClosed(SelectChannelEndPoint endpoint);
266
267 /* ------------------------------------------------------------------------------- */ 233 /* ------------------------------------------------------------------------------- */
268 public abstract AsyncConnection newConnection(SocketChannel channel, AsyncEndPoint endpoint, Object attachment); 234 public abstract AsyncConnection newConnection(SocketChannel channel, AsyncEndPoint endpoint, Object attachment);
269 235
270 /* ------------------------------------------------------------ */
271 /**
272 * Create a new end point
273 * @param channel
274 * @param selectSet
275 * @param sKey the selection key
276 * @return the new endpoint {@link SelectChannelEndPoint}
277 * @throws IOException
278 */
279 protected abstract SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectorManager.SelectSet selectSet, SelectionKey sKey) throws IOException;
280
281 /* ------------------------------------------------------------------------------- */
282 protected void connectionFailed(SocketChannel channel,Throwable ex,Object attachment)
283 {
284 LOG.warn(ex+","+channel+","+attachment);
285 LOG.debug("",ex);
286 }
287
288 /* ------------------------------------------------------------ */
289 public String dump() 236 public String dump()
290 { 237 {
291 return AggregateLifeCycle.dump(this); 238 return AggregateLifeCycle.dump(this);
292 } 239 }
293 240
294 /* ------------------------------------------------------------ */
295 public void dump(Appendable out, String indent) throws IOException 241 public void dump(Appendable out, String indent) throws IOException
296 { 242 {
297 AggregateLifeCycle.dumpObject(out,this); 243 AggregateLifeCycle.dumpObject(out,this);
298 AggregateLifeCycle.dump(out,indent,TypeUtil.asList(_selectSet)); 244 AggregateLifeCycle.dump(out,indent,TypeUtil.asList(_selectSet));
299 } 245 }
300 246
301 247
302 /* ------------------------------------------------------------------------------- */
303 /* ------------------------------------------------------------------------------- */
304 /* ------------------------------------------------------------------------------- */
305 public class SelectSet implements Dumpable 248 public class SelectSet implements Dumpable
306 { 249 {
307 private final int _setID; 250 private final int _setID;
308 private volatile long _now = System.currentTimeMillis(); 251 private volatile long _now = System.currentTimeMillis();
309 252
498 { 441 {
499 connected=channel.finishConnect(); 442 connected=channel.finishConnect();
500 } 443 }
501 catch(Exception e) 444 catch(Exception e)
502 { 445 {
503 connectionFailed(channel,e,att); 446 LOG.warn(e+","+channel+","+att);
447 LOG.debug("",e);
504 } 448 }
505 finally 449 finally
506 { 450 {
507 if (connected) 451 if (connected)
508 { 452 {
626 selector.wakeup(); 570 selector.wakeup();
627 } 571 }
628 572
629 private SelectChannelEndPoint createEndPoint(SocketChannel channel, SelectionKey sKey) throws IOException 573 private SelectChannelEndPoint createEndPoint(SocketChannel channel, SelectionKey sKey) throws IOException
630 { 574 {
631 SelectChannelEndPoint endp = newEndPoint(channel,this,sKey); 575 SelectChannelEndPoint endp = new SelectChannelEndPoint(channel,this,sKey, _maxIdleTime);
576 endp.setConnection(getManager().newConnection(channel,endp, sKey.attachment()));
632 LOG.debug("created {}",endp); 577 LOG.debug("created {}",endp);
633 _endPoints.put(endp,this); 578 _endPoints.put(endp,this);
634 return endp; 579 return endp;
635 } 580 }
636 581
637 public void destroyEndPoint(SelectChannelEndPoint endp) 582 public void destroyEndPoint(SelectChannelEndPoint endp)
638 { 583 {
639 LOG.debug("destroyEndPoint {}",endp); 584 LOG.debug("destroyEndPoint {}",endp);
640 _endPoints.remove(endp); 585 _endPoints.remove(endp);
641 endPointClosed(endp); 586 endp.getConnection().onClose();
642 } 587 }
643 588
644 Selector getSelector() 589 Selector getSelector()
645 { 590 {
646 return _selector; 591 return _selector;