View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel;
17  
18  import io.netty.util.internal.CallableEventExecutorAdapter;
19  import io.netty.util.concurrent.EventExecutor;
20  import io.netty.util.concurrent.EventExecutorGroup;
21  import io.netty.util.concurrent.Future;
22  import io.netty.util.concurrent.PausableEventExecutor;
23  import io.netty.util.concurrent.ProgressivePromise;
24  import io.netty.util.concurrent.Promise;
25  import io.netty.util.internal.RunnableEventExecutorAdapter;
26  import io.netty.util.concurrent.ScheduledFuture;
27  
28  import java.net.SocketAddress;
29  import java.util.Collection;
30  import java.util.List;
31  import java.util.Set;
32  import java.util.concurrent.Callable;
33  import java.util.concurrent.ExecutionException;
34  import java.util.concurrent.RejectedExecutionException;
35  import java.util.concurrent.TimeUnit;
36  import java.util.concurrent.TimeoutException;
37  
38  abstract class PausableChannelEventExecutor implements PausableEventExecutor, ChannelHandlerInvoker {
39  
40      abstract Channel channel();
41  
42      abstract ChannelHandlerInvoker unwrapInvoker();
43  
44      @Override
45      public void invokeFlush(ChannelHandlerContext ctx) {
46          unwrapInvoker().invokeFlush(ctx);
47      }
48  
49      @Override
50      public EventExecutor executor() {
51          return this;
52      }
53  
54      @Override
55      public void invokeChannelRegistered(ChannelHandlerContext ctx) {
56          unwrapInvoker().invokeChannelRegistered(ctx);
57      }
58  
59      @Override
60      public void invokeChannelUnregistered(ChannelHandlerContext ctx) {
61          unwrapInvoker().invokeChannelUnregistered(ctx);
62      }
63  
64      @Override
65      public void invokeChannelActive(ChannelHandlerContext ctx) {
66          unwrapInvoker().invokeChannelActive(ctx);
67      }
68  
69      @Override
70      public void invokeChannelInactive(ChannelHandlerContext ctx) {
71          unwrapInvoker().invokeChannelInactive(ctx);
72      }
73  
74      @Override
75      public void invokeExceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
76          unwrapInvoker().invokeExceptionCaught(ctx, cause);
77      }
78  
79      @Override
80      public void invokeUserEventTriggered(ChannelHandlerContext ctx, Object event) {
81          unwrapInvoker().invokeUserEventTriggered(ctx, event);
82      }
83  
84      @Override
85      public void invokeChannelRead(ChannelHandlerContext ctx, Object msg) {
86          unwrapInvoker().invokeChannelRead(ctx, msg);
87      }
88  
89      @Override
90      public void invokeChannelReadComplete(ChannelHandlerContext ctx) {
91          unwrapInvoker().invokeChannelReadComplete(ctx);
92      }
93  
94      @Override
95      public void invokeChannelWritabilityChanged(ChannelHandlerContext ctx) {
96          unwrapInvoker().invokeChannelWritabilityChanged(ctx);
97      }
98  
99      @Override
100     public void invokeBind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) {
101         unwrapInvoker().invokeBind(ctx, localAddress, promise);
102     }
103 
104     @Override
105     public void invokeConnect(
106            ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
107         unwrapInvoker().invokeConnect(ctx, remoteAddress, localAddress, promise);
108     }
109 
110     @Override
111     public void invokeDisconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
112         unwrapInvoker().invokeDisconnect(ctx, promise);
113     }
114 
115     @Override
116     public void invokeClose(ChannelHandlerContext ctx, ChannelPromise promise) {
117         unwrapInvoker().invokeClose(ctx, promise);
118     }
119 
120     @Override
121     public void invokeDeregister(ChannelHandlerContext ctx, ChannelPromise promise) {
122         unwrapInvoker().invokeDeregister(ctx, promise);
123     }
124 
125     @Override
126     public void invokeRead(ChannelHandlerContext ctx) {
127         unwrapInvoker().invokeRead(ctx);
128     }
129 
130     @Override
131     public void invokeWrite(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
132         unwrapInvoker().invokeWrite(ctx, msg, promise);
133     }
134 
135     @Override
136     public EventExecutor next() {
137         return unwrap().next();
138     }
139 
140     @Override
141     public <E extends EventExecutor> Set<E> children() {
142         return unwrap().children();
143     }
144 
145     @Override
146     public EventExecutorGroup parent() {
147         return unwrap().parent();
148     }
149 
150     @Override
151     public boolean inEventLoop() {
152         return unwrap().inEventLoop();
153     }
154 
155     @Override
156     public boolean inEventLoop(Thread thread) {
157         return unwrap().inEventLoop(thread);
158     }
159 
160     @Override
161     public <V> Promise<V> newPromise() {
162         return unwrap().newPromise();
163     }
164 
165     @Override
166     public <V> ProgressivePromise<V> newProgressivePromise() {
167         return unwrap().newProgressivePromise();
168     }
169 
170     @Override
171     public <V> Future<V> newSucceededFuture(V result) {
172         return unwrap().newSucceededFuture(result);
173     }
174 
175     @Override
176     public <V> Future<V> newFailedFuture(Throwable cause) {
177         return unwrap().newFailedFuture(cause);
178     }
179 
180     @Override
181     public boolean isShuttingDown() {
182         return unwrap().isShuttingDown();
183     }
184 
185     @Override
186     public Future<?> shutdownGracefully() {
187         return unwrap().shutdownGracefully();
188     }
189 
190     @Override
191     public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
192         return unwrap().shutdownGracefully(quietPeriod, timeout, unit);
193     }
194 
195     @Override
196     public Future<?> terminationFuture() {
197         return unwrap().terminationFuture();
198     }
199 
200     @Override
201     @Deprecated
202     public void shutdown() {
203         unwrap().shutdown();
204     }
205 
206     @Override
207     @Deprecated
208     public List<Runnable> shutdownNow() {
209         return unwrap().shutdownNow();
210     }
211 
212     @Override
213     public Future<?> submit(Runnable task) {
214         if (!isAcceptingNewTasks()) {
215             throw new RejectedExecutionException();
216         }
217         return unwrap().submit(task);
218     }
219 
220     @Override
221     public <T> Future<T> submit(Runnable task, T result) {
222         if (!isAcceptingNewTasks()) {
223             throw new RejectedExecutionException();
224         }
225         return unwrap().submit(task, result);
226     }
227 
228     @Override
229     public <T> Future<T> submit(Callable<T> task) {
230         if (!isAcceptingNewTasks()) {
231             throw new RejectedExecutionException();
232         }
233         return unwrap().submit(task);
234     }
235 
236     @Override
237     public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
238         if (!isAcceptingNewTasks()) {
239             throw new RejectedExecutionException();
240         }
241 
242         return unwrap().schedule(new ChannelRunnableEventExecutor(channel(), command), delay, unit);
243     }
244 
245     @Override
246     public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
247         if (!isAcceptingNewTasks()) {
248             throw new RejectedExecutionException();
249         }
250         return unwrap().schedule(new ChannelCallableEventExecutor<V>(channel(), callable), delay, unit);
251     }
252 
253     @Override
254     public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
255         if (!isAcceptingNewTasks()) {
256             throw new RejectedExecutionException();
257         }
258         return unwrap().scheduleAtFixedRate(
259                 new ChannelRunnableEventExecutor(channel(), command), initialDelay, period, unit);
260     }
261 
262     @Override
263     public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
264         if (!isAcceptingNewTasks()) {
265             throw new RejectedExecutionException();
266         }
267         return unwrap().scheduleWithFixedDelay(
268                 new ChannelRunnableEventExecutor(channel(), command), initialDelay, delay, unit);
269     }
270 
271     @Override
272     public boolean isShutdown() {
273         return unwrap().isShutdown();
274     }
275 
276     @Override
277     public boolean isTerminated() {
278         return unwrap().isTerminated();
279     }
280 
281     @Override
282     public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
283         return unwrap().awaitTermination(timeout, unit);
284     }
285 
286     @Override
287     public <T> List<java.util.concurrent.Future<T>>
288     invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
289         if (!isAcceptingNewTasks()) {
290             throw new RejectedExecutionException();
291         }
292         return unwrap().invokeAll(tasks);
293     }
294 
295     @Override
296     public <T> List<java.util.concurrent.Future<T>>
297     invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
298         if (!isAcceptingNewTasks()) {
299             throw new RejectedExecutionException();
300         }
301         return unwrap().invokeAll(tasks, timeout, unit);
302     }
303 
304     @Override
305     public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
306         if (!isAcceptingNewTasks()) {
307             throw new RejectedExecutionException();
308         }
309         return unwrap().invokeAny(tasks);
310     }
311 
312     @Override
313     public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
314             throws InterruptedException, ExecutionException, TimeoutException {
315         if (!isAcceptingNewTasks()) {
316             throw new RejectedExecutionException();
317         }
318         return unwrap().invokeAny(tasks, timeout, unit);
319     }
320 
321     @Override
322     public void execute(Runnable command) {
323         if (!isAcceptingNewTasks()) {
324             throw new RejectedExecutionException();
325         }
326         unwrap().execute(command);
327     }
328 
329     @Override
330     public void close() throws Exception {
331         unwrap().close();
332     }
333 
334     private static final class ChannelCallableEventExecutor<V> implements CallableEventExecutorAdapter<V> {
335 
336         final Channel channel;
337         final Callable<V> callable;
338 
339         ChannelCallableEventExecutor(Channel channel, Callable<V> callable) {
340             this.channel = channel;
341             this.callable = callable;
342         }
343 
344         @Override
345         public EventExecutor executor() {
346             return channel.eventLoop();
347         }
348 
349         @Override
350         public Callable unwrap() {
351             return callable;
352         }
353 
354         @Override
355         public V call() throws Exception {
356             return callable.call();
357         }
358     }
359 
360     private static final class ChannelRunnableEventExecutor implements RunnableEventExecutorAdapter {
361 
362         final Channel channel;
363         final Runnable runnable;
364 
365         ChannelRunnableEventExecutor(Channel channel, Runnable runnable) {
366             this.channel = channel;
367             this.runnable = runnable;
368         }
369 
370         @Override
371         public EventExecutor executor() {
372             return channel.eventLoop();
373         }
374 
375         @Override
376         public Runnable unwrap() {
377             return runnable;
378         }
379 
380         @Override
381         public void run() {
382             runnable.run();
383         }
384     }
385 }