tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.cluster.impl; |
| 2 | |
| 3 | import org.onlab.nio.AcceptorLoop; |
| 4 | import org.onlab.packet.IpPrefix; |
| 5 | |
| 6 | import java.io.IOException; |
| 7 | import java.net.InetSocketAddress; |
| 8 | import java.net.Socket; |
| 9 | import java.nio.channels.ServerSocketChannel; |
| 10 | import java.nio.channels.SocketChannel; |
| 11 | |
| 12 | import static java.net.InetAddress.getByAddress; |
| 13 | |
| 14 | /** |
| 15 | * Listens to inbound connection requests and accepts them. |
| 16 | */ |
| 17 | public 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 | |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame^] | 26 | private final ClusterCommunicationManager manager; |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 27 | |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame^] | 28 | ClusterConnectionListener(ClusterCommunicationManager manager, |
| 29 | IpPrefix ip, int tcpPort) throws IOException { |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 30 | super(SELECT_TIMEOUT, new InetSocketAddress(getByAddress(ip.toOctets()), tcpPort)); |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame^] | 31 | this.manager = manager; |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 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 | |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame^] | 44 | manager.findWorker().acceptStream(sc); |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | } |