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