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