Merge "Bug 498 - Fixed PCEP and BGP testtools"
[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         @Override
46         public SocketAddress getLocalAddress() throws IOException {
47                 return inner.getLocalAddress();
48         }
49
50         @Override
51         public <T> T getOption(final SocketOption<T> name) throws IOException {
52                 return options.getOption(name);
53         }
54
55         @Override
56         public Set<SocketOption<?>> supportedOptions() {
57                 return options.supportedOptions();
58         }
59
60         @Override
61         public ServerSocketChannel bind(final SocketAddress local, final int backlog) throws IOException {
62                 inner.bind(local, backlog);
63                 return this;
64         }
65
66         @Override
67         public <T> ServerSocketChannel setOption(final SocketOption<T> name, final T value) throws IOException {
68                 options.setOption(name, value);
69                 return this;
70         }
71
72         @Override
73         public ServerSocket socket() {
74                 // FIXME: provider a wrapper
75                 return inner.socket();
76         }
77
78         @Override
79         public MD5SocketChannel accept() throws IOException {
80                 return new MD5SocketChannel(inner.accept(), keyAccessFactory);
81         }
82
83         @Override
84         protected void implCloseSelectableChannel() throws IOException {
85                 inner.close();
86         }
87
88         @Override
89         protected void implConfigureBlocking(final boolean block) throws IOException {
90                 inner.configureBlocking(block);
91         }
92 }