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.netty;
10 import io.netty.channel.ChannelException;
11 import io.netty.channel.ChannelMetadata;
12 import io.netty.channel.ChannelOutboundBuffer;
13 import io.netty.channel.nio.AbstractNioMessageChannel;
14 import io.netty.channel.socket.nio.NioServerSocketChannel;
16 import java.io.IOException;
17 import java.net.InetSocketAddress;
18 import java.net.SocketAddress;
19 import java.nio.channels.SelectionKey;
20 import java.nio.channels.ServerSocketChannel;
21 import java.nio.channels.SocketChannel;
22 import java.util.List;
24 import org.opendaylight.bgpcep.tcpmd5.KeyAccessFactory;
26 import com.google.common.base.Preconditions;
29 * {@link NioServerSocketChannel} enabled with support for TCP MD5 Signature
32 public class MD5NioServerSocketChannel extends AbstractNioMessageChannel implements io.netty.channel.socket.ServerSocketChannel {
33 private static final ChannelMetadata METADATA = new ChannelMetadata(false);
35 private final MD5ServerSocketChannelConfig config;
36 private final KeyAccessFactory keyAccessFactory;
38 private static ServerSocketChannel newChannel() {
40 return ServerSocketChannel.open();
41 } catch (IOException e) {
42 throw new ChannelException("Failed to instantiate channel", e);
46 private MD5NioServerSocketChannel(final ServerSocketChannel channel, final KeyAccessFactory keyAccessFactory) {
47 super(null, channel, SelectionKey.OP_ACCEPT);
48 this.config = new DefaultMD5ServerSocketChannelConfig(this, keyAccessFactory);
49 this.keyAccessFactory = Preconditions.checkNotNull(keyAccessFactory);
52 public MD5NioServerSocketChannel(final KeyAccessFactory keyAccessFactory) {
53 this(newChannel(), keyAccessFactory);
57 public MD5ServerSocketChannelConfig config() {
62 public InetSocketAddress localAddress() {
63 return (InetSocketAddress) super.localAddress();
67 public ChannelMetadata metadata() {
72 public boolean isActive() {
73 return javaChannel().socket().isBound();
77 public InetSocketAddress remoteAddress() {
82 protected ServerSocketChannel javaChannel() {
83 return (ServerSocketChannel) super.javaChannel();
87 protected SocketAddress localAddress0() {
88 return javaChannel().socket().getLocalSocketAddress();
92 protected void doBind(final SocketAddress localAddress) throws Exception {
93 javaChannel().socket().bind(localAddress, config.getBacklog());
97 protected void doClose() throws Exception {
98 javaChannel().close();
102 protected int doReadMessages(final List<Object> buf) throws Exception {
103 final SocketChannel jc = javaChannel().accept();
108 final MD5NioSocketChannel ch = new MD5NioSocketChannel(this, jc, keyAccessFactory);
114 protected boolean doWriteMessage(final Object msg, final ChannelOutboundBuffer in)
116 throw new UnsupportedOperationException();
120 protected boolean doConnect(final SocketAddress remoteAddress,
121 final SocketAddress localAddress) throws Exception {
122 throw new UnsupportedOperationException();
126 protected void doFinishConnect() throws Exception {
127 throw new UnsupportedOperationException();
131 protected SocketAddress remoteAddress0() {
136 protected void doDisconnect() throws Exception {
137 throw new UnsupportedOperationException();