View Javadoc

1   /*
2    * Copyright 2012 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.embedded;
17  
18  import io.netty.channel.Channel;
19  import io.netty.channel.ChannelFuture;
20  import io.netty.channel.ChannelPromise;
21  import io.netty.channel.DefaultChannelPromise;
22  import io.netty.channel.EventLoop;
23  import io.netty.channel.EventLoopGroup;
24  import io.netty.util.concurrent.AbstractScheduledEventExecutor;
25  import io.netty.util.concurrent.Future;
26  
27  import java.util.ArrayDeque;
28  import java.util.Queue;
29  import java.util.concurrent.TimeUnit;
30  
31  final class EmbeddedEventLoop extends AbstractScheduledEventExecutor implements EventLoop {
32  
33      private final Queue<Runnable> tasks = new ArrayDeque<Runnable>(2);
34  
35      @Override
36      public void execute(Runnable command) {
37          if (command == null) {
38              throw new NullPointerException("command");
39          }
40          tasks.add(command);
41      }
42  
43      void runTasks() {
44          for (;;) {
45              Runnable task = tasks.poll();
46              if (task == null) {
47                  break;
48              }
49  
50              task.run();
51          }
52      }
53  
54      long runScheduledTasks() {
55          long time = AbstractScheduledEventExecutor.nanoTime();
56          for (;;) {
57              Runnable task = pollScheduledTask(time);
58              if (task == null) {
59                  return nextScheduledTaskNano();
60              }
61  
62              task.run();
63          }
64      }
65  
66      long nextScheduledTask() {
67          return nextScheduledTaskNano();
68      }
69  
70      @Override
71      protected void cancelScheduledTasks() {
72          super.cancelScheduledTasks();
73      }
74  
75      @Override
76      public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
77          throw new UnsupportedOperationException();
78      }
79  
80      @Override
81      public Future<?> terminationFuture() {
82          throw new UnsupportedOperationException();
83      }
84  
85      @Override
86      @Deprecated
87      public void shutdown() {
88          throw new UnsupportedOperationException();
89      }
90  
91      @Override
92      public boolean isShuttingDown() {
93          return false;
94      }
95  
96      @Override
97      public boolean isShutdown() {
98          return false;
99      }
100 
101     @Override
102     public boolean isTerminated() {
103         return false;
104     }
105 
106     @Override
107     public boolean awaitTermination(long timeout, TimeUnit unit)
108             throws InterruptedException {
109         Thread.sleep(unit.toMillis(timeout));
110         return false;
111     }
112 
113     @Override
114     public ChannelFuture register(Channel channel) {
115         return register(channel, new DefaultChannelPromise(channel, this));
116     }
117 
118     @Override
119     public ChannelFuture register(Channel channel, ChannelPromise promise) {
120         channel.unsafe().register(this, promise);
121         return promise;
122     }
123 
124     @Override
125     public boolean inEventLoop() {
126         return true;
127     }
128 
129     @Override
130     public boolean inEventLoop(Thread thread) {
131         return true;
132     }
133 
134     @Override
135     public EventLoop next() {
136         return this;
137     }
138 
139     @Override
140     public EventLoopGroup parent() {
141         return this;
142     }
143 }