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 java.io.IOException; |
| 19 | import java.net.SocketAddress; |
| 20 | import java.net.StandardSocketOptions; |
| 21 | import java.nio.channels.SelectionKey; |
| 22 | import java.nio.channels.ServerSocketChannel; |
| 23 | import java.util.Iterator; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * Selector loop derivative tailored to acceptConnection inbound connections. |
| 29 | */ |
| 30 | public abstract class AcceptorLoop extends SelectorLoop { |
| 31 | |
| 32 | private SocketAddress listenAddress; |
| 33 | private ServerSocketChannel socketChannel; |
| 34 | |
| 35 | /** |
| 36 | * Creates an acceptor loop with the specified selection timeout and |
| 37 | * accepting connections on the the given address. |
| 38 | * |
| 39 | * @param selectTimeout selection timeout; specified in millis |
| 40 | * @param listenAddress socket address where to listen for connections |
| 41 | * @throws IOException if the backing selector cannot be opened |
| 42 | */ |
| 43 | public AcceptorLoop(long selectTimeout, SocketAddress listenAddress) |
| 44 | throws IOException { |
| 45 | super(selectTimeout); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 46 | this.listenAddress = checkNotNull(listenAddress, "Address cannot be null"); |
tom | 6a62aa2 | 2014-09-25 09:10:12 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Hook to accept an inbound connection on the specified socket channel. |
| 51 | * |
| 52 | * @param channel socketChannel where an accept operation awaits |
| 53 | * @throws IOException if the accept operation cannot be processed |
| 54 | */ |
| 55 | protected abstract void acceptConnection(ServerSocketChannel channel) throws IOException; |
| 56 | |
| 57 | /** |
| 58 | * Opens a new server socket channel configured in non-blocking mode and |
| 59 | * bound to the loop's listen address. |
| 60 | * |
| 61 | * @throws IOException if unable to open or configure the socket channel |
| 62 | */ |
| 63 | protected synchronized void openChannel() throws IOException { |
| 64 | socketChannel = ServerSocketChannel.open(); |
| 65 | socketChannel.configureBlocking(false); |
| 66 | socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); |
| 67 | socketChannel.register(selector, SelectionKey.OP_ACCEPT); |
| 68 | socketChannel.bind(listenAddress); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Closes the server socket channel. |
| 73 | * |
| 74 | * @throws IOException if unable to close the socketChannel |
| 75 | */ |
| 76 | protected synchronized void closechannel() throws IOException { |
| 77 | if (socketChannel != null) { |
| 78 | socketChannel.close(); |
| 79 | socketChannel = null; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void shutdown() { |
| 85 | try { |
| 86 | closechannel(); |
| 87 | } catch (IOException e) { |
| 88 | log.warn("Unable to close the socketChannel", e); |
| 89 | } |
| 90 | super.shutdown(); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | protected void loop() throws IOException { |
| 95 | openChannel(); |
| 96 | notifyReady(); |
| 97 | |
| 98 | // Keep looping until told otherwise. |
| 99 | while (isRunning()) { |
| 100 | // Attempt a selection; if no operations selected or if signalled |
| 101 | // to shutdown, spin through. |
| 102 | int count = selector.select(selectTimeout); |
| 103 | if (count == 0 || !isRunning()) { |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | // Iterate over all keys selected for an operation and process them. |
| 108 | Iterator<SelectionKey> keys = selector.selectedKeys().iterator(); |
| 109 | while (keys.hasNext()) { |
| 110 | // Fetch the key and remove it from the pending list. |
| 111 | SelectionKey key = keys.next(); |
| 112 | keys.remove(); |
| 113 | |
| 114 | // If the key has a pending acceptConnection operation, process it. |
| 115 | if (key.isAcceptable()) { |
| 116 | acceptConnection((ServerSocketChannel) key.channel()); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |