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