blob: ae4a76ff9f1f29154daa0fcc485e6be389eb846e [file] [log] [blame]
tom1d416c52014-09-29 20:55:24 -07001package org.onlab.onos.store.cluster.impl;
2
3import org.onlab.nio.AcceptorLoop;
4import org.onlab.packet.IpPrefix;
5
6import java.io.IOException;
7import java.net.InetSocketAddress;
8import java.net.Socket;
9import java.nio.channels.ServerSocketChannel;
10import java.nio.channels.SocketChannel;
11
12import static java.net.InetAddress.getByAddress;
13
14/**
15 * Listens to inbound connection requests and accepts them.
16 */
17public class ClusterConnectionListener extends AcceptorLoop {
18
19 private static final long SELECT_TIMEOUT = 50;
20 private static final int COMM_BUFFER_SIZE = 32 * 1024;
21
22 private static final boolean SO_NO_DELAY = false;
23 private static final int SO_SEND_BUFFER_SIZE = COMM_BUFFER_SIZE;
24 private static final int SO_RCV_BUFFER_SIZE = COMM_BUFFER_SIZE;
25
26 private final WorkerFinder workerFinder;
27
28 ClusterConnectionListener(IpPrefix ip, int tcpPort,
29 WorkerFinder workerFinder) throws IOException {
30 super(SELECT_TIMEOUT, new InetSocketAddress(getByAddress(ip.toOctets()), tcpPort));
31 this.workerFinder = workerFinder;
32 }
33
34 @Override
35 protected void acceptConnection(ServerSocketChannel channel) throws IOException {
36 SocketChannel sc = channel.accept();
37 sc.configureBlocking(false);
38
39 Socket so = sc.socket();
40 so.setTcpNoDelay(SO_NO_DELAY);
41 so.setReceiveBufferSize(SO_RCV_BUFFER_SIZE);
42 so.setSendBufferSize(SO_SEND_BUFFER_SIZE);
43
44 workerFinder.findWorker().acceptStream(sc);
45 }
46
47}