2 * Copyright (c) 2013 Robert Varga. All rights reserved.
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
8 package org.opendaylight.bgpcep.tcpmd5.nio;
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;
17 import org.opendaylight.bgpcep.tcpmd5.KeyAccessFactory;
19 import com.google.common.base.Preconditions;
22 * {@link ServerSocketChannel} augmented with support for TCP MD5 Signature
25 public final class MD5ServerSocketChannel extends ServerSocketChannel {
26 private final KeyAccessFactory keyAccessFactory;
27 private final ServerSocketChannel inner;
28 private final MD5ChannelOptions options;
30 public MD5ServerSocketChannel(final ServerSocketChannel inner, final KeyAccessFactory keyAccessFactory) {
31 super(inner.provider());
33 this.keyAccessFactory = Preconditions.checkNotNull(keyAccessFactory);
34 this.options = MD5ChannelOptions.create(keyAccessFactory, inner);
37 public MD5ServerSocketChannel(final ServerSocketChannel inner) {
38 this(inner, DefaultKeyAccessFactoryFactory.getKeyAccessFactory());
41 public static MD5ServerSocketChannel open() throws IOException {
42 return new MD5ServerSocketChannel(ServerSocketChannel.open());
45 public static MD5ServerSocketChannel open(final KeyAccessFactory keyAccessFactory) throws IOException {
46 return new MD5ServerSocketChannel(ServerSocketChannel.open(), keyAccessFactory);
50 public SocketAddress getLocalAddress() throws IOException {
51 return inner.getLocalAddress();
55 public <T> T getOption(final SocketOption<T> name) throws IOException {
56 return options.getOption(name);
60 public Set<SocketOption<?>> supportedOptions() {
61 return options.supportedOptions();
65 public ServerSocketChannel bind(final SocketAddress local, final int backlog) throws IOException {
66 inner.bind(local, backlog);
71 public <T> ServerSocketChannel setOption(final SocketOption<T> name, final T value) throws IOException {
72 options.setOption(name, value);
77 public ServerSocket socket() {
78 // FIXME: provider a wrapper
79 return inner.socket();
83 public MD5SocketChannel accept() throws IOException {
84 return new MD5SocketChannel(inner.accept(), keyAccessFactory);
88 protected void implCloseSelectableChannel() throws IOException {
93 protected void implConfigureBlocking(final boolean block) throws IOException {
94 inner.configureBlocking(block);