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