Remove duplicated code in AcceptorChannel classes.

Motivation:

NioUdtByteAcceptorChannel and NioUdtMessageAcceptorChannel have almost same code.
For maintainability, it's better to remove it.

Motification:

- Pulled a member(METADATA) and methods(doReadMessage() and metadata() up.
- Added newConnectorChannel().

Result:

Cleaner code.
This commit is contained in:
JongYoon Lim 2015-05-06 15:43:54 +09:00 committed by Norman Maurer
parent 36061c50b1
commit a8839cc20d
3 changed files with 29 additions and 37 deletions

View File

@ -17,10 +17,13 @@ package io.netty.channel.udt.nio;
import com.barchart.udt.TypeUDT; import com.barchart.udt.TypeUDT;
import com.barchart.udt.nio.ServerSocketChannelUDT; import com.barchart.udt.nio.ServerSocketChannelUDT;
import com.barchart.udt.nio.SocketChannelUDT;
import io.netty.channel.ChannelException; import io.netty.channel.ChannelException;
import io.netty.channel.ChannelMetadata;
import io.netty.channel.ChannelOutboundBuffer; import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.nio.AbstractNioMessageChannel; import io.netty.channel.nio.AbstractNioMessageChannel;
import io.netty.channel.udt.DefaultUdtServerChannelConfig; import io.netty.channel.udt.DefaultUdtServerChannelConfig;
import io.netty.channel.udt.UdtChannel;
import io.netty.channel.udt.UdtServerChannel; import io.netty.channel.udt.UdtServerChannel;
import io.netty.channel.udt.UdtServerChannelConfig; import io.netty.channel.udt.UdtServerChannelConfig;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
@ -28,6 +31,7 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.List;
import static java.nio.channels.SelectionKey.*; import static java.nio.channels.SelectionKey.*;
@ -39,6 +43,8 @@ public abstract class NioUdtAcceptorChannel extends AbstractNioMessageChannel im
protected static final InternalLogger logger = protected static final InternalLogger logger =
InternalLoggerFactory.getInstance(NioUdtAcceptorChannel.class); InternalLoggerFactory.getInstance(NioUdtAcceptorChannel.class);
private static final ChannelMetadata METADATA = new ChannelMetadata(false);
private final UdtServerChannelConfig config; private final UdtServerChannelConfig config;
protected NioUdtAcceptorChannel(final ServerSocketChannelUDT channelUDT) { protected NioUdtAcceptorChannel(final ServerSocketChannelUDT channelUDT) {
@ -132,4 +138,21 @@ public abstract class NioUdtAcceptorChannel extends AbstractNioMessageChannel im
return null; return null;
} }
@Override
public ChannelMetadata metadata() {
return METADATA;
}
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
final SocketChannelUDT channelUDT = javaChannel().accept();
if (channelUDT == null) {
return 0;
} else {
buf.add(newConnectorChannel(channelUDT));
return 1;
}
}
protected abstract UdtChannel newConnectorChannel(SocketChannelUDT channelUDT);
} }

View File

@ -17,34 +17,19 @@ package io.netty.channel.udt.nio;
import com.barchart.udt.TypeUDT; import com.barchart.udt.TypeUDT;
import com.barchart.udt.nio.SocketChannelUDT; import com.barchart.udt.nio.SocketChannelUDT;
import io.netty.channel.ChannelMetadata; import io.netty.channel.udt.UdtChannel;
import java.util.List;
/** /**
* Byte Channel Acceptor for UDT Streams. * Byte Channel Acceptor for UDT Streams.
*/ */
public class NioUdtByteAcceptorChannel extends NioUdtAcceptorChannel { public class NioUdtByteAcceptorChannel extends NioUdtAcceptorChannel {
private static final ChannelMetadata METADATA = new ChannelMetadata(false);
public NioUdtByteAcceptorChannel() { public NioUdtByteAcceptorChannel() {
super(TypeUDT.STREAM); super(TypeUDT.STREAM);
} }
@Override @Override
protected int doReadMessages(List<Object> buf) throws Exception { protected UdtChannel newConnectorChannel(SocketChannelUDT channelUDT) {
final SocketChannelUDT channelUDT = javaChannel().accept(); return new NioUdtByteConnectorChannel(this, channelUDT);
if (channelUDT == null) {
return 0;
} else {
buf.add(new NioUdtByteConnectorChannel(this, channelUDT));
return 1;
}
}
@Override
public ChannelMetadata metadata() {
return METADATA;
} }
} }

View File

@ -17,35 +17,19 @@ package io.netty.channel.udt.nio;
import com.barchart.udt.TypeUDT; import com.barchart.udt.TypeUDT;
import com.barchart.udt.nio.SocketChannelUDT; import com.barchart.udt.nio.SocketChannelUDT;
import io.netty.channel.ChannelMetadata; import io.netty.channel.udt.UdtChannel;
import java.util.List;
/** /**
* Message Channel Acceptor for UDT Datagrams. * Message Channel Acceptor for UDT Datagrams.
*/ */
public class NioUdtMessageAcceptorChannel extends NioUdtAcceptorChannel { public class NioUdtMessageAcceptorChannel extends NioUdtAcceptorChannel {
private static final ChannelMetadata METADATA = new ChannelMetadata(false);
public NioUdtMessageAcceptorChannel() { public NioUdtMessageAcceptorChannel() {
super(TypeUDT.DATAGRAM); super(TypeUDT.DATAGRAM);
} }
@Override @Override
protected int doReadMessages(List<Object> buf) throws Exception { protected UdtChannel newConnectorChannel(SocketChannelUDT channelUDT) {
final SocketChannelUDT channelUDT = javaChannel().accept(); return new NioUdtMessageConnectorChannel(this, channelUDT);
if (channelUDT == null) {
return 0;
} else {
buf.add(new NioUdtMessageConnectorChannel(this, channelUDT));
return 1;
} }
}
@Override
public ChannelMetadata metadata() {
return METADATA;
}
} }