1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.timeout;
17
18 import org.jboss.netty.bootstrap.ServerBootstrap;
19 import org.jboss.netty.channel.ChannelHandler;
20 import org.jboss.netty.channel.ChannelHandler.Sharable;
21 import org.jboss.netty.channel.ChannelHandlerContext;
22 import org.jboss.netty.channel.ChannelPipeline;
23 import org.jboss.netty.channel.ChannelPipelineFactory;
24 import org.jboss.netty.channel.ChannelStateEvent;
25 import org.jboss.netty.channel.Channels;
26 import org.jboss.netty.channel.LifeCycleAwareChannelHandler;
27 import org.jboss.netty.channel.MessageEvent;
28 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
29 import org.jboss.netty.util.ExternalResourceReleasable;
30 import org.jboss.netty.util.HashedWheelTimer;
31 import org.jboss.netty.util.Timeout;
32 import org.jboss.netty.util.Timer;
33 import org.jboss.netty.util.TimerTask;
34
35 import java.util.concurrent.TimeUnit;
36
37 import static org.jboss.netty.channel.Channels.*;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 @Sharable
80 public class ReadTimeoutHandler extends SimpleChannelUpstreamHandler
81 implements LifeCycleAwareChannelHandler,
82 ExternalResourceReleasable {
83
84 static final ReadTimeoutException EXCEPTION = new ReadTimeoutException();
85
86 final Timer timer;
87 final long timeoutMillis;
88
89
90
91
92
93
94
95
96
97
98 public ReadTimeoutHandler(Timer timer, int timeoutSeconds) {
99 this(timer, timeoutSeconds, TimeUnit.SECONDS);
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113 public ReadTimeoutHandler(Timer timer, long timeout, TimeUnit unit) {
114 if (timer == null) {
115 throw new NullPointerException("timer");
116 }
117 if (unit == null) {
118 throw new NullPointerException("unit");
119 }
120
121 this.timer = timer;
122 if (timeout <= 0) {
123 timeoutMillis = 0;
124 } else {
125 timeoutMillis = Math.max(unit.toMillis(timeout), 1);
126 }
127 }
128
129
130
131
132
133
134 public void releaseExternalResources() {
135 timer.stop();
136 }
137
138 public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
139 if (ctx.getPipeline().isAttached()) {
140
141
142
143 initialize(ctx);
144 } else {
145
146
147 }
148 }
149
150 public void afterAdd(ChannelHandlerContext ctx) throws Exception {
151
152 }
153
154 public void beforeRemove(ChannelHandlerContext ctx) throws Exception {
155 destroy(ctx);
156 }
157
158 public void afterRemove(ChannelHandlerContext ctx) throws Exception {
159
160 }
161
162 @Override
163 public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
164 throws Exception {
165
166
167
168 initialize(ctx);
169 ctx.sendUpstream(e);
170 }
171
172 @Override
173 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
174 throws Exception {
175 destroy(ctx);
176 ctx.sendUpstream(e);
177 }
178
179 @Override
180 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
181 throws Exception {
182 State state = (State) ctx.getAttachment();
183 state.lastReadTime = System.currentTimeMillis();
184 ctx.sendUpstream(e);
185 }
186
187 private void initialize(ChannelHandlerContext ctx) {
188 State state = state(ctx);
189
190
191
192 synchronized (state) {
193 switch (state.state) {
194 case 1:
195 case 2:
196 return;
197 }
198 state.state = 1;
199 }
200
201 if (timeoutMillis > 0) {
202 state.timeout = timer.newTimeout(new ReadTimeoutTask(ctx), timeoutMillis, TimeUnit.MILLISECONDS);
203 }
204 }
205
206 private static void destroy(ChannelHandlerContext ctx) {
207 State state = state(ctx);
208 synchronized (state) {
209 if (state.state != 1) {
210 return;
211 }
212 state.state = 2;
213 }
214
215 if (state.timeout != null) {
216 state.timeout.cancel();
217 state.timeout = null;
218 }
219 }
220
221 private static State state(ChannelHandlerContext ctx) {
222 State state;
223 synchronized (ctx) {
224
225 state = (State) ctx.getAttachment();
226 if (state != null) {
227 return state;
228 }
229 state = new State();
230 ctx.setAttachment(state);
231 }
232 return state;
233 }
234
235 protected void readTimedOut(ChannelHandlerContext ctx) throws Exception {
236 fireExceptionCaught(ctx, EXCEPTION);
237 }
238
239 private final class ReadTimeoutTask implements TimerTask {
240
241 private final ChannelHandlerContext ctx;
242
243 ReadTimeoutTask(ChannelHandlerContext ctx) {
244 this.ctx = ctx;
245 }
246
247 public void run(Timeout timeout) throws Exception {
248 if (timeout.isCancelled()) {
249 return;
250 }
251
252 if (!ctx.getChannel().isOpen()) {
253 return;
254 }
255
256 State state = (State) ctx.getAttachment();
257 long currentTime = System.currentTimeMillis();
258 long nextDelay = timeoutMillis - (currentTime - state.lastReadTime);
259 if (nextDelay <= 0) {
260
261 state.timeout =
262 timer.newTimeout(this, timeoutMillis, TimeUnit.MILLISECONDS);
263 fireReadTimedOut(ctx);
264 } else {
265
266 state.timeout =
267 timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS);
268 }
269 }
270
271 private void fireReadTimedOut(final ChannelHandlerContext ctx) throws Exception {
272 ctx.getPipeline().execute(new Runnable() {
273
274 public void run() {
275 try {
276 readTimedOut(ctx);
277 } catch (Throwable t) {
278 fireExceptionCaught(ctx, t);
279 }
280 }
281 });
282 }
283 }
284
285 private static final class State {
286
287 int state;
288 volatile Timeout timeout;
289 volatile long lastReadTime = System.currentTimeMillis();
290
291 State() {
292 }
293 }
294 }