blob: 605e0a92f397f7bbd65a73c132033d6e4e308305 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
tom6a62aa22014-09-25 09:10:12 -070016package org.onlab.nio;
17
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20
21import java.io.IOException;
22import java.nio.channels.Selector;
23
24import static com.google.common.base.Preconditions.checkArgument;
tom74d49652014-09-25 23:48:46 -070025import static java.lang.System.currentTimeMillis;
tom6a62aa22014-09-25 09:10:12 -070026
27/**
28 * Abstraction of an I/O processing loop based on an NIO selector.
29 */
30public abstract class SelectorLoop implements Runnable {
31
32 protected final Logger log = LoggerFactory.getLogger(getClass());
33
34 /**
35 * Selector used by this loop to pace the I/O operations.
36 */
37 protected final Selector selector;
38
39 /**
40 * Selection operations timeout; specified in millis.
41 */
42 protected long selectTimeout;
43
44 /**
45 * Retains the error that caused the loop to exit prematurely.
46 */
47 private Throwable error;
48
49 // State indicator
50 private enum State { STARTING, STARTED, STOPPING, STOPPED };
51 private volatile State state = State.STOPPED;
52
53 /**
54 * Creates a new selector loop with the given selection timeout.
55 *
56 * @param selectTimeout selection timeout; specified in millis
57 * @throws IOException if the backing selector cannot be opened
58 */
59 public SelectorLoop(long selectTimeout) throws IOException {
60 checkArgument(selectTimeout > 0, "Timeout must be positive");
61 this.selectTimeout = selectTimeout;
62 this.selector = openSelector();
63 }
64
65 /**
66 * Opens a new selector for the use by the loop.
67 *
68 * @return newly open selector
69 * @throws IOException if the backing selector cannot be opened
70 */
71 protected Selector openSelector() throws IOException {
72 return Selector.open();
73 }
74
75 /**
76 * Indicates that the loop is marked to run.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080077 * @return true if the loop is marked to run
tom6a62aa22014-09-25 09:10:12 -070078 */
79 protected boolean isRunning() {
80 return state == State.STARTED || state == State.STARTING;
81 }
82
83 /**
84 * Returns the error, if there was one, that caused the loop to terminate
85 * prematurely.
86 *
87 * @return error or null if there was none
88 */
89 public Throwable getError() {
90 return error;
91 }
92
93 /**
94 * Contains the body of the I/O selector loop.
95 *
96 * @throws IOException if an error is encountered while selecting I/O
97 */
98 protected abstract void loop() throws IOException;
99
100 @Override
101 public void run() {
102 error = null;
103 state = State.STARTING;
104 try {
105 loop();
106 } catch (Throwable e) {
107 error = e;
108 log.error("Loop aborted", e);
109 }
110 notifyDone();
111 }
112
113 /**
114 * Notifies observers waiting for loop to become ready.
115 */
116 protected synchronized void notifyReady() {
117 state = State.STARTED;
118 notifyAll();
119 }
120
121 /**
122 * Triggers loop shutdown.
123 */
124 public void shutdown() {
125 // Mark the loop as no longer running and wake up the selector.
126 state = State.STOPPING;
127 selector.wakeup();
128 }
129
130 /**
131 * Notifies observers waiting for loop to fully stop.
132 */
133 private synchronized void notifyDone() {
134 state = State.STOPPED;
135 notifyAll();
136 }
137
tom74d49652014-09-25 23:48:46 -0700138 /**
139 * Waits for the loop execution to start.
140 *
141 * @param timeout number of milliseconds to wait
142 * @return true if loop started in time
143 */
144 public final synchronized boolean awaitStart(long timeout) {
145 long max = currentTimeMillis() + timeout;
146 while (state != State.STARTED && (currentTimeMillis() < max)) {
147 try {
148 wait(timeout);
149 } catch (InterruptedException e) {
150 throw new RuntimeException("Interrupted", e);
151 }
152 }
153 return state == State.STARTED;
154 }
155
156 /**
157 * Waits for the loop execution to stop.
158 *
159 * @param timeout number of milliseconds to wait
160 * @return true if loop finished in time
161 */
162 public final synchronized boolean awaitStop(long timeout) {
163 long max = currentTimeMillis() + timeout;
164 while (state != State.STOPPED && (currentTimeMillis() < max)) {
165 try {
166 wait(timeout);
167 } catch (InterruptedException e) {
168 throw new RuntimeException("Interrupted", e);
169 }
170 }
171 return state == State.STOPPED;
172 }
173
174
tom6a62aa22014-09-25 09:10:12 -0700175}