blob: 36d5ab49ae9c82a060bb6f4bd50d54115e56a711 [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
tom81583142014-09-30 01:40:29 -070026 private final ClusterCommunicationManager manager;
tom1d416c52014-09-29 20:55:24 -070027
tom81583142014-09-30 01:40:29 -070028 ClusterConnectionListener(ClusterCommunicationManager manager,
29 IpPrefix ip, int tcpPort) throws IOException {
tom1d416c52014-09-29 20:55:24 -070030 super(SELECT_TIMEOUT, new InetSocketAddress(getByAddress(ip.toOctets()), tcpPort));
tom81583142014-09-30 01:40:29 -070031 this.manager = manager;
tom1d416c52014-09-29 20:55:24 -070032 }
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
tom81583142014-09-30 01:40:29 -070044 manager.findWorker().acceptStream(sc);
tom1d416c52014-09-29 20:55:24 -070045 }
46
47}