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         tasks.add(ObjectUtil.checkNotNull(command, "command"));
49     }
50
51     void runTasks() {
52         for (;;) {
53             Runnable task = tasks.poll();
54             if (task == null) {
55                 break;
56             }
57
58             task.run();
59         }
60     }
61
62     long runScheduledTasks() {
63         long time = AbstractScheduledEventExecutor.nanoTime();
64         for (;;) {
65             Runnable task = pollScheduledTask(time);
66             if (task == null) {
67                 return nextScheduledTaskNano();
68             }
69
70             task.run();
71         }
72     }
73
74     long nextScheduledTask() {
75         return nextScheduledTaskNano();
76     }
77
78     @Override
79     protected void cancelScheduledTasks() {
80         super.cancelScheduledTasks();
81     }
82
83     @Override
84     public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
85         throw new UnsupportedOperationException();
86     }
87
88     @Override
89     public Future<?> terminationFuture() {
90         throw new UnsupportedOperationException();
91     }
92
93     @Override
94     @Deprecated
95     public void shutdown() {
96         throw new UnsupportedOperationException();
97     }
98
99     @Override
100     public boolean isShuttingDown() {
101         return false;
102     }
103
104     @Override
105     public boolean isShutdown() {
106         return false;
107     }
108
109     @Override
110     public boolean isTerminated() {
111         return false;
112     }
113
114     @Override
115     public boolean awaitTermination(long timeout, TimeUnit unit) {
116         return false;
117     }
118
119     @Override
120     public ChannelFuture register(Channel channel) {
121         return register(new DefaultChannelPromise(channel, this));
122     }
123
124     @Override
125     public ChannelFuture register(ChannelPromise promise) {
126         ObjectUtil.checkNotNull(promise, "promise");
127         promise.channel().unsafe().register(this, promise);
128         return promise;
129     }
130
131     @Deprecated
132     @Override
133     public ChannelFuture register(Channel channel, ChannelPromise promise) {
134         channel.unsafe().register(this, promise);
135         return promise;
136     }
137
138     @Override
139     public boolean inEventLoop() {
140         return true;
141     }
142
143     @Override
144     public boolean inEventLoop(Thread thread) {
145         return true;
146     }
147 }
148