BUG-632: fix netty binding
[bgpcep.git] / tcp-md5 / nio / src / main / java / org / opendaylight / bgpcep / tcpmd5 / nio / MD5ServerSocketChannel.java
1 /*
2  * Copyright (c) 2013 Robert Varga. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.bgpcep.tcpmd5.nio;
9
10 import java.io.IOException;
11 import java.net.ServerSocket;
12 import java.net.SocketAddress;
13 import java.net.SocketOption;
14 import java.nio.channels.ServerSocketChannel;
15 import java.util.Set;
16
17 import org.opendaylight.bgpcep.tcpmd5.KeyAccessFactory;
18
19 import com.google.common.base.Preconditions;
20
21 /**
22  * {@link ServerSocketChannel} augmented with support for TCP MD5 Signature
23  * option.
24  */
25 public final class MD5ServerSocketChannel extends ServerSocketChannel {
26         private final KeyAccessFactory keyAccessFactory;
27         private final ServerSocketChannel inner;
28         private final MD5ChannelOptions options;
29
30         public MD5ServerSocketChannel(final ServerSocketChannel inner, final KeyAccessFactory keyAccessFactory) {
31                 super(inner.provider());
32                 this.inner = inner;
33                 this.keyAccessFactory = Preconditions.checkNotNull(keyAccessFactory);
34                 this.options = MD5ChannelOptions.create(keyAccessFactory, inner);
35         }
36
37         public MD5ServerSocketChannel(final ServerSocketChannel inner) {
38                 this(inner, DefaultKeyAccessFactoryFactory.getKeyAccessFactory());
39         }
40
41         public static MD5ServerSocketChannel open() throws IOException {
42                 return new MD5ServerSocketChannel(ServerSocketChannel.open());
43         }
44
45         public static MD5ServerSocketChannel open(final KeyAccessFactory keyAccessFactory) throws IOException {
46                 return new MD5ServerSocketChannel(ServerSocketChannel.open(), keyAccessFactory);
47         }
48
49         @Override
50         public SocketAddress getLocalAddress() throws IOException {
51                 return inner.getLocalAddress();
52         }
53
54         @Override
55         public <T> T getOption(final SocketOption<T> name) throws IOException {
56                 return options.getOption(name);
57         }
58
59         @Override
60         public Set<SocketOption<?>> supportedOptions() {
61                 return options.supportedOptions();
62         }
63
64         @Override
65         public ServerSocketChannel bind(final SocketAddress local, final int backlog) throws IOException {
66                 inner.bind(local, backlog);
67                 return this;
68         }
69
70         @Override
71         public <T> ServerSocketChannel setOption(final SocketOption<T> name, final T value) throws IOException {
72                 options.setOption(name, value);
73                 return this;
74         }
75
76         @Override
77         public ServerSocket socket() {
78                 // FIXME: provider a wrapper
79                 return inner.socket();
80         }
81
82         @Override
83         public MD5SocketChannel accept() throws IOException {
84                 return new MD5SocketChannel(inner.accept(), keyAccessFactory);
85         }
86
87         @Override
88         protected void implCloseSelectableChannel() throws IOException {
89                 inner.close();
90         }
91
92         @Override
93         protected void implConfigureBlocking(final boolean block) throws IOException {
94                 inner.configureBlocking(block);
95         }
96 }