Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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, |
| 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | 6a62aa2 | 2014-09-25 09:10:12 -0700 | [diff] [blame] | 16 | package org.onlab.nio; |
| 17 | |
| 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.nio.channels.Selector; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 25 | import static java.lang.System.currentTimeMillis; |
tom | 6a62aa2 | 2014-09-25 09:10:12 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Abstraction of an I/O processing loop based on an NIO selector. |
| 29 | */ |
| 30 | public 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 HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame^] | 77 | * @return true if the loop is marked to run |
tom | 6a62aa2 | 2014-09-25 09:10:12 -0700 | [diff] [blame] | 78 | */ |
| 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 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 138 | /** |
| 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 | |
tom | 6a62aa2 | 2014-09-25 09:10:12 -0700 | [diff] [blame] | 175 | } |