annotate src/org/eclipse/jetty/io/nio/SelectorManager.java @ 944:1d24b6e422fa

simplify SelectorManager
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 11 Oct 2016 20:16:03 -0600
parents 96f60ce98949
children f5aefdc4a81a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 // ========================================================================
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 // ------------------------------------------------------------------------
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 // All rights reserved. This program and the accompanying materials
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 // are made available under the terms of the Eclipse Public License v1.0
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 // and Apache License v2.0 which accompanies this distribution.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 // The Eclipse Public License is available at
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 // http://www.eclipse.org/legal/epl-v10.html
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 // The Apache License v2.0 is available at
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 // http://www.opensource.org/licenses/apache2.0.php
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 // You may elect to redistribute this code under either of these licenses.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 // ========================================================================
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 package org.eclipse.jetty.io.nio;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 import java.io.IOException;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 import java.nio.channels.CancelledKeyException;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 import java.nio.channels.Channel;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 import java.nio.channels.ClosedSelectorException;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 import java.nio.channels.SelectableChannel;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 import java.nio.channels.SelectionKey;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 import java.nio.channels.Selector;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 import java.nio.channels.ServerSocketChannel;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 import java.nio.channels.SocketChannel;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 import java.util.ArrayList;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 import java.util.List;
944
1d24b6e422fa simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents: 943
diff changeset
32 import java.util.Collections;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 import java.util.Set;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 import java.util.concurrent.ConcurrentHashMap;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 import java.util.concurrent.ConcurrentLinkedQueue;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 import java.util.concurrent.ConcurrentMap;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 import java.util.concurrent.CountDownLatch;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 import java.util.concurrent.TimeUnit;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 import org.eclipse.jetty.io.AsyncEndPoint;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 import org.eclipse.jetty.io.ConnectedEndPoint;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 import org.eclipse.jetty.io.Connection;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 import org.eclipse.jetty.io.EndPoint;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 import org.eclipse.jetty.util.TypeUtil;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 import org.eclipse.jetty.util.component.AbstractLifeCycle;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 import org.eclipse.jetty.util.component.AggregateLifeCycle;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 import org.eclipse.jetty.util.component.Dumpable;
820
8e9db0bbf4f9 remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
48 import org.slf4j.Logger;
8e9db0bbf4f9 remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
49 import org.slf4j.LoggerFactory;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 /* ------------------------------------------------------------ */
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 /**
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 * The Selector Manager manages and number of SelectSets to allow
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 * NIO scheduling to scale to large numbers of connections.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 * <p>
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 */
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 public abstract class SelectorManager extends AbstractLifeCycle implements Dumpable
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 {
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
60 public static final Logger LOG=LoggerFactory.getLogger("org.eclipse.jetty.io.nio");
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
62 private static final int __MONITOR_PERIOD=Integer.getInteger("org.eclipse.jetty.io.nio.MONITOR_PERIOD",1000).intValue();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
63 private static final int __MAX_SELECTS=Integer.getInteger("org.eclipse.jetty.io.nio.MAX_SELECTS",100000).intValue();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
64 private static final int __BUSY_PAUSE=Integer.getInteger("org.eclipse.jetty.io.nio.BUSY_PAUSE",50).intValue();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
65 private static final int __IDLE_TICK=Integer.getInteger("org.eclipse.jetty.io.nio.IDLE_TICK",400).intValue();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
67 private int _maxIdleTime;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
68 private long _lowResourcesConnections;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
69 private SelectSet[] _selectSet;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
70 private int _selectSets=1;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
71 private volatile int _set=0;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
73 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
74 /**
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
75 * @param maxIdleTime The maximum period in milli seconds that a connection may be idle before it is closed.
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
76 * @see #setLowResourcesMaxIdleTime(long)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
77 */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
78 public void setMaxIdleTime(long maxIdleTime)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
79 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
80 _maxIdleTime=(int)maxIdleTime;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
81 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
83 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
84 /**
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
85 * @param selectSets number of select sets to create
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
86 */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
87 public void setSelectSets(int selectSets)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
88 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
89 long lrc = _lowResourcesConnections * _selectSets;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
90 _selectSets=selectSets;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
91 _lowResourcesConnections=lrc/_selectSets;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
92 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
94 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
95 /**
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
96 * @return the max idle time
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
97 */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
98 public long getMaxIdleTime()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
99 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
100 return _maxIdleTime;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
101 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
103 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
104 /**
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
105 * @return the number of select sets in use
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
106 */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
107 public int getSelectSets()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
108 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
109 return _selectSets;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
110 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
112 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
113 /**
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
114 * @param i
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
115 * @return The select set
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
116 */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
117 public SelectSet getSelectSet(int i)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
118 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
119 return _selectSet[i];
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
120 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
122 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
123 /** Register a channel
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
124 * @param channel
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
125 */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
126 public void register(SocketChannel channel)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
127 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
128 // The ++ increment here is not atomic, but it does not matter.
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
129 // so long as the value changes sometimes, then connections will
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
130 // be distributed over the available sets.
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
132 int s=_set++;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
133 if (s<0)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
134 s=-s;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
135 s=s%_selectSets;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
136 SelectSet[] sets=_selectSet;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
137 if (sets!=null)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
138 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
139 SelectSet set=sets[s];
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
140 set.addChange(channel);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
141 set.wakeup();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
142 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
143 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
145 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
146 /** Register a {@link ServerSocketChannel}
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
147 * @param acceptChannel
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
148 */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
149 public void register(ServerSocketChannel acceptChannel)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
150 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
151 int s=_set++;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
152 if (s<0)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
153 s=-s;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
154 s=s%_selectSets;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
155 SelectSet set=_selectSet[s];
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
156 set.addChange(acceptChannel);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
157 set.wakeup();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
158 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
161 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
162 /**
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
163 * @return the lowResourcesConnections
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
164 */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
165 public long getLowResourcesConnections()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
166 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
167 return _lowResourcesConnections*_selectSets;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
168 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
169
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
170 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
171 /**
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
172 * Set the number of connections, which if exceeded places this manager in low resources state.
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
173 * This is not an exact measure as the connection count is averaged over the select sets.
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
174 * @param lowResourcesConnections the number of connections
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
175 * @see #setLowResourcesMaxIdleTime(long)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
176 */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
177 public void setLowResourcesConnections(long lowResourcesConnections)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
178 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
179 _lowResourcesConnections=(lowResourcesConnections+_selectSets-1)/_selectSets;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
180 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
183 public abstract void execute(Runnable task);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
185 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
186 /* (non-Javadoc)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
187 * @see org.eclipse.component.AbstractLifeCycle#doStart()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
188 */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
189 @Override
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
190 protected void doStart() throws Exception
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
191 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
192 _selectSet = new SelectSet[_selectSets];
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
193 for (int i=0;i<_selectSet.length;i++)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
194 _selectSet[i]= new SelectSet(i);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
196 super.doStart();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
197
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
198 // start a thread to Select
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
199 for (int i=0;i<getSelectSets();i++)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
200 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
201 final int id=i;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
202 execute(new Runnable()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
203 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
204 public void run()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
205 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
206 String name=Thread.currentThread().getName();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
207 try
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
208 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
209 SelectSet[] sets=_selectSet;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
210 if (sets==null)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
211 return;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
212 SelectSet set=sets[id];
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
213
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
214 Thread.currentThread().setName(name+" Selector"+id);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
215 LOG.debug("Starting {} on {}",Thread.currentThread(),this);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
216 while (isRunning())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
217 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
218 try
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
219 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
220 set.doSelect();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
221 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
222 catch(IOException e)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
223 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
224 LOG.trace("",e);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
225 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
226 catch(Exception e)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
227 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
228 LOG.warn("",e);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
229 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
230 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
231 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
232 finally
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
233 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
234 LOG.debug("Stopped {} on {}",Thread.currentThread(),this);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
235 Thread.currentThread().setName(name);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
236 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
237 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
238
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
239 });
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
240 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
241 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
242
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
243
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
244 /* ------------------------------------------------------------------------------- */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
245 @Override
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
246 protected void doStop() throws Exception
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
247 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
248 SelectSet[] sets= _selectSet;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
249 _selectSet=null;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
250 if (sets!=null)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
251 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
252 for (SelectSet set : sets)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
253 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
254 if (set!=null)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
255 set.stop();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
256 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
257 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
258 super.doStop();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
259 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
260
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
261 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
262 /**
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
263 * @param endpoint
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
264 */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
265 protected abstract void endPointClosed(SelectChannelEndPoint endpoint);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
266
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
267 /* ------------------------------------------------------------------------------- */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
268 public abstract AsyncConnection newConnection(SocketChannel channel, AsyncEndPoint endpoint, Object attachment);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
269
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
270 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
271 /**
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
272 * Create a new end point
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
273 * @param channel
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
274 * @param selectSet
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
275 * @param sKey the selection key
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
276 * @return the new endpoint {@link SelectChannelEndPoint}
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
277 * @throws IOException
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
278 */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
279 protected abstract SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectorManager.SelectSet selectSet, SelectionKey sKey) throws IOException;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
280
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
281 /* ------------------------------------------------------------------------------- */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
282 protected void connectionFailed(SocketChannel channel,Throwable ex,Object attachment)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
283 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
284 LOG.warn(ex+","+channel+","+attachment);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
285 LOG.debug("",ex);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
286 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
287
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
288 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
289 public String dump()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
290 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
291 return AggregateLifeCycle.dump(this);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
292 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
293
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
294 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
295 public void dump(Appendable out, String indent) throws IOException
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
296 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
297 AggregateLifeCycle.dumpObject(out,this);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
298 AggregateLifeCycle.dump(out,indent,TypeUtil.asList(_selectSet));
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
299 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
300
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
301
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
302 /* ------------------------------------------------------------------------------- */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
303 /* ------------------------------------------------------------------------------- */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
304 /* ------------------------------------------------------------------------------- */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
305 public class SelectSet implements Dumpable
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
306 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
307 private final int _setID;
943
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
308 private volatile long _now = System.currentTimeMillis();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
309
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
310 private final ConcurrentLinkedQueue<Object> _changes = new ConcurrentLinkedQueue<Object>();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
311
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
312 private volatile Selector _selector;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
313
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
314 private volatile Thread _selecting;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
315 private int _busySelects;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
316 private long _monitorNext;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
317 private boolean _pausing;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
318 private boolean _paused;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
319 private volatile long _idleTick;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
320 private ConcurrentMap<SelectChannelEndPoint,Object> _endPoints = new ConcurrentHashMap<SelectChannelEndPoint, Object>();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
321
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
322 SelectSet(int acceptorID) throws Exception
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
323 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
324 _setID=acceptorID;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
325
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
326 _idleTick = System.currentTimeMillis();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
327
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
328 // create a selector;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
329 _selector = Selector.open();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
330 _monitorNext=System.currentTimeMillis()+__MONITOR_PERIOD;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
331 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
332
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
333 public void addChange(Object change)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
334 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
335 _changes.add(change);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
336 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
337
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
338 /* ------------------------------------------------------------ */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
339 /**
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
340 * Select and dispatch tasks found from changes and the selector.
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
341 *
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
342 * @throws IOException
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
343 */
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
344 public void doSelect() throws IOException
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
345 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
346 try
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
347 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
348 _selecting=Thread.currentThread();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
349 final Selector selector=_selector;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
350 // Stopped concurrently ?
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
351 if (selector == null)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
352 return;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
353
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
354 // Make any key changes required
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
355 Object change;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
356 int changes=_changes.size();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
357 while (changes-->0 && (change=_changes.poll())!=null)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
358 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
359 Channel ch=null;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
360 SelectionKey key=null;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
361
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
362 try
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
363 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
364 if (change instanceof EndPoint)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
365 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
366 // Update the operations for a key.
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
367 SelectChannelEndPoint endpoint = (SelectChannelEndPoint)change;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
368 ch=endpoint.getChannel();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
369 endpoint.doUpdateKey();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
370 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
371 else if (change instanceof SocketChannel)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
372 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
373 // Newly registered channel
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
374 final SocketChannel channel=(SocketChannel)change;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
375 ch=channel;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
376 key = channel.register(selector,SelectionKey.OP_READ,null);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
377 SelectChannelEndPoint endpoint = createEndPoint(channel,key);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
378 key.attach(endpoint);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
379 endpoint.schedule();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
380 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
381 else if (change instanceof Runnable)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
382 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
383 execute((Runnable)change);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
384 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
385 else
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
386 throw new IllegalArgumentException(change.toString());
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
387 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
388 catch (CancelledKeyException e)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
389 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
390 LOG.trace("",e);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
391 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
392 catch (Throwable e)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
393 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
394 if (isRunning())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
395 LOG.warn("",e);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
396 else
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
397 LOG.debug("",e);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
398
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
399 try
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
400 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
401 if (ch!=null)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
402 ch.close();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
403 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
404 catch(IOException e2)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
405 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
406 LOG.debug("",e2);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
407 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
408 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
409 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
410
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
411
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
412 // Do and instant select to see if any connections can be handled.
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
413 int selected=selector.selectNow();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
414
943
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
415 _now = System.currentTimeMillis();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
416
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
417 // if no immediate things to do
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
418 if (selected==0 && selector.selectedKeys().isEmpty())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
419 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
420 // If we are in pausing mode
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
421 if (_pausing)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
422 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
423 try
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
424 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
425 Thread.sleep(__BUSY_PAUSE); // pause to reduce impact of busy loop
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
426 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
427 catch(InterruptedException e)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
428 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
429 LOG.trace("",e);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
430 }
943
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
431 _now = System.currentTimeMillis();
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
432 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
433
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
434 // workout how long to wait in select
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
435 long wait = _changes.size()==0?__IDLE_TICK:0L;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
436
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
437 // If we should wait with a select
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
438 if (wait>0)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
439 {
943
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
440 long before = _now;
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
441 selector.select(wait);
943
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
442 _now = System.currentTimeMillis();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
443
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
444 // If we are monitoring for busy selector
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
445 // and this select did not wait more than 1ms
943
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
446 if (__MONITOR_PERIOD>0 && _now-before <=1)
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
447 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
448 // count this as a busy select and if there have been too many this monitor cycle
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
449 if (++_busySelects>__MAX_SELECTS)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
450 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
451 // Start injecting pauses
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
452 _pausing=true;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
453
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
454 // if this is the first pause
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
455 if (!_paused)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
456 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
457 // Log and dump some status
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
458 _paused=true;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
459 LOG.warn("Selector {} is too busy, pausing!",this);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
460 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
461 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
462 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
463 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
464 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
465
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
466 // have we been destroyed while sleeping
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
467 if (_selector==null || !selector.isOpen())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
468 return;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
469
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
470 // Look for things to do
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
471 for (SelectionKey key: selector.selectedKeys())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
472 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
473 SocketChannel channel=null;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
474
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
475 try
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
476 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
477 if (!key.isValid())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
478 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
479 key.cancel();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
480 SelectChannelEndPoint endpoint = (SelectChannelEndPoint)key.attachment();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
481 if (endpoint != null)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
482 endpoint.doUpdateKey();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
483 continue;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
484 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
485
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
486 Object att = key.attachment();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
487 if (att instanceof SelectChannelEndPoint)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
488 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
489 if (key.isReadable()||key.isWritable())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
490 ((SelectChannelEndPoint)att).schedule();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
491 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
492 else if (key.isConnectable())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
493 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
494 // Complete a connection of a registered channel
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
495 channel = (SocketChannel)key.channel();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
496 boolean connected=false;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
497 try
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
498 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
499 connected=channel.finishConnect();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
500 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
501 catch(Exception e)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
502 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
503 connectionFailed(channel,e,att);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
504 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
505 finally
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
506 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
507 if (connected)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
508 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
509 key.interestOps(SelectionKey.OP_READ);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
510 SelectChannelEndPoint endpoint = createEndPoint(channel,key);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
511 key.attach(endpoint);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
512 endpoint.schedule();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
513 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
514 else
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
515 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
516 key.cancel();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
517 channel.close();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
518 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
519 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
520 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
521 else
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
522 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
523 // Wrap readable registered channel in an endpoint
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
524 channel = (SocketChannel)key.channel();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
525 SelectChannelEndPoint endpoint = createEndPoint(channel,key);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
526 key.attach(endpoint);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
527 if (key.isReadable())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
528 endpoint.schedule();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
529 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
530 key = null;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
531 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
532 catch (CancelledKeyException e)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
533 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
534 LOG.trace("",e);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
535 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
536 catch (Exception e)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
537 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
538 if (isRunning())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
539 LOG.warn("",e);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
540 else
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
541 LOG.trace("",e);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
542
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
543 try
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
544 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
545 if (channel!=null)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
546 channel.close();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
547 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
548 catch(IOException e2)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
549 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
550 LOG.debug("",e2);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
551 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
552
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
553 if (key != null && !(key.channel() instanceof ServerSocketChannel) && key.isValid())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
554 key.cancel();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
555 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
556 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
557
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
558 // Everything always handled
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
559 selector.selectedKeys().clear();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
560
943
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
561 _now = System.currentTimeMillis();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
562
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
563 // Idle tick
943
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
564 if (_now-_idleTick>__IDLE_TICK)
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
565 {
943
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
566 _idleTick = _now;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
567
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
568 final long idle_now=((_lowResourcesConnections>0 && selector.keys().size()>_lowResourcesConnections))
943
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
569 ?(_now+_maxIdleTime)
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
570 :_now;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
571
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
572 execute(new Runnable()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
573 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
574 public void run()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
575 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
576 for (SelectChannelEndPoint endp:_endPoints.keySet())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
577 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
578 endp.checkIdleTimestamp(idle_now);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
579 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
580 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
581 public String toString() {return "Idle-"+super.toString();}
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
582 });
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
583
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
584 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
585
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
586 // Reset busy select monitor counts
943
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
587 if (__MONITOR_PERIOD>0 && _now>_monitorNext)
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
588 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
589 _busySelects=0;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
590 _pausing=false;
943
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
591 _monitorNext=_now+__MONITOR_PERIOD;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
592
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
593 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
594 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
595 catch (ClosedSelectorException e)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
596 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
597 if (isRunning())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
598 LOG.warn("",e);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
599 else
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
600 LOG.trace("",e);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
601 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
602 catch (CancelledKeyException e)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
603 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
604 LOG.trace("",e);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
605 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
606 finally
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
607 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
608 _selecting=null;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
609 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
610 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
611
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
612 public SelectorManager getManager()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
613 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
614 return SelectorManager.this;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
615 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
616
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
617 public long getNow()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
618 {
943
96f60ce98949 remove Timeout
Franklin Schmidt <fschmidt@gmail.com>
parents: 942
diff changeset
619 return _now;
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
620 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
621
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
622 public void wakeup()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
623 {
944
1d24b6e422fa simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents: 943
diff changeset
624 Selector selector = _selector;
1d24b6e422fa simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents: 943
diff changeset
625 if (selector!=null)
1d24b6e422fa simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents: 943
diff changeset
626 selector.wakeup();
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
627 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
628
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
629 private SelectChannelEndPoint createEndPoint(SocketChannel channel, SelectionKey sKey) throws IOException
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
630 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
631 SelectChannelEndPoint endp = newEndPoint(channel,this,sKey);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
632 LOG.debug("created {}",endp);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
633 _endPoints.put(endp,this);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
634 return endp;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
635 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
636
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
637 public void destroyEndPoint(SelectChannelEndPoint endp)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
638 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
639 LOG.debug("destroyEndPoint {}",endp);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
640 _endPoints.remove(endp);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
641 endPointClosed(endp);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
642 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
643
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
644 Selector getSelector()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
645 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
646 return _selector;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
647 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
648
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
649 void stop() throws Exception
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
650 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
651 // Spin for a while waiting for selector to complete
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
652 // to avoid unneccessary closed channel exceptions
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
653 try
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
654 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
655 for (int i=0;i<100 && _selecting!=null;i++)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
656 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
657 wakeup();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
658 Thread.sleep(10);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
659 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
660 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
661 catch(Exception e)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
662 {
944
1d24b6e422fa simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents: 943
diff changeset
663 LOG.warn("",e);
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
664 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
665
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
666 // close endpoints and selector
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
667 synchronized (this)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
668 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
669 Selector selector=_selector;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
670 for (SelectionKey key:selector.keys())
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
671 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
672 if (key==null)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
673 continue;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
674 Object att=key.attachment();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
675 if (att instanceof EndPoint)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
676 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
677 EndPoint endpoint = (EndPoint)att;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
678 try
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
679 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
680 endpoint.close();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
681 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
682 catch(IOException e)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
683 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
684 LOG.trace("",e);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
685 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
686 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
687 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
688
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
689 try
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
690 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
691 selector=_selector;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
692 if (selector != null)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
693 selector.close();
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
694 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
695 catch (IOException e)
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
696 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
697 LOG.trace("",e);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
698 }
944
1d24b6e422fa simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents: 943
diff changeset
699 _selector = null;
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
700 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
701 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
702
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
703 public String dump()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
704 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
705 return AggregateLifeCycle.dump(this);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
706 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
707
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
708 public void dump(Appendable out, String indent) throws IOException
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
709 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
710 out.append(String.valueOf(this)).append(" id=").append(String.valueOf(_setID)).append("\n");
944
1d24b6e422fa simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents: 943
diff changeset
711 AggregateLifeCycle.dump(out,indent,Collections.emptyList());
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
712 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
713
865
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
714 public String toString()
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
715 {
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
716 Selector selector=_selector;
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
717 return String.format("%s keys=%d selected=%d",
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
718 super.toString(),
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
719 selector != null && selector.isOpen() ? selector.keys().size() : -1,
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
720 selector != null && selector.isOpen() ? selector.selectedKeys().size() : -1);
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
721 }
6b210bb66c63 remove ThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
722 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
723
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
724 }