Skip to content

Commit

Permalink
Resurrect channel deregistration and constructor changes
Browse files Browse the repository at this point in the history
Motivation:

Due to the complexity of handling deregistration and re-registration of
a channel, we previously decided to remove the deregister() operation
completely to simplify our code.  However, we realized that it shouldn't
be that complicated to implement it during our discussion about making
I/O scheduling more flexible and more customizable [1], and thus the
removal of deregistration and re-registration is unnecessary now.

Modification:

- Revert commit c149f4b
- Revert commit e743a27
- Make some additional adjustments

Result:

- deregister(), fireChannelUnregistered(), and channelRegistered() were
  added back..
- Channel constructors do not require an EventLoop anymore.

[1] netty#2250
  • Loading branch information
Norman Maurer authored and trustin committed Apr 24, 2014
1 parent 797d6d9 commit 48f2e70
Show file tree
Hide file tree
Showing 77 changed files with 890 additions and 920 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
ctx.fireChannelRegistered();
}

@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
if (logger.isEnabled(internalLevel)) {
logger.log(internalLevel, format(ctx, "UNREGISTERED"));
}
ctx.fireChannelUnregistered();
}

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
if (logger.isEnabled(internalLevel)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.oio.OioEventLoopGroup;
Expand Down Expand Up @@ -109,8 +108,8 @@ public List<BootstrapComboFactory<Bootstrap, Bootstrap>> datagram() {
public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel(EventLoop loop) {
return new NioDatagramChannel(loop, InternetProtocolFamily.IPv4);
public Channel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ abstract class AbstractEpollChannel extends AbstractChannel {
volatile int fd;
int id;

AbstractEpollChannel(EventLoop eventLoop, int fd, int flag) {
this(null, eventLoop, fd, flag, false);
AbstractEpollChannel(int fd, int flag) {
this(null, fd, flag, false);
}

AbstractEpollChannel(Channel parent, EventLoop eventLoop, int fd, int flag, boolean active) {
super(parent, eventLoop);
AbstractEpollChannel(Channel parent, int fd, int flag, boolean active) {
super(parent);
this.fd = fd;
readFlag = flag;
flags |= flag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.channel.RecvByteBufAllocator;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.DatagramChannelConfig;
Expand Down Expand Up @@ -51,8 +50,8 @@ public final class EpollDatagramChannel extends AbstractEpollChannel implements
private volatile boolean connected;
private final EpollDatagramChannelConfig config;

public EpollDatagramChannel(EventLoop loop) {
super(loop, Native.socketDgramFd(), Native.EPOLLIN);
public EpollDatagramChannel() {
super(Native.socketDgramFd(), Native.EPOLLIN);
config = new EpollDatagramChannelConfig(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;

import java.net.InetSocketAddress;
Expand All @@ -32,13 +31,11 @@
public final class EpollServerSocketChannel extends AbstractEpollChannel implements ServerSocketChannel {

private final EpollServerSocketChannelConfig config;
private final EventLoopGroup childGroup;
private volatile InetSocketAddress local;

public EpollServerSocketChannel(EventLoop eventLoop, EventLoopGroup childGroup) {
super(eventLoop, Native.socketStreamFd(), Native.EPOLLACCEPT);
public EpollServerSocketChannel() {
super(Native.socketStreamFd(), Native.EPOLLACCEPT);
config = new EpollServerSocketChannelConfig(this);
this.childGroup = childGroup;
}

@Override
Expand Down Expand Up @@ -81,11 +78,6 @@ protected void doWrite(ChannelOutboundBuffer in) {
throw new UnsupportedOperationException();
}

@Override
public EventLoopGroup childEventLoopGroup() {
return childGroup;
}

final class EpollServerSocketUnsafe extends AbstractEpollUnsafe {

@Override
Expand All @@ -109,8 +101,7 @@ void epollInReady() {
}
try {
readPending = false;
pipeline.fireChannelRead(new EpollSocketChannel(EpollServerSocketChannel.this,
childEventLoopGroup().next(), socketFd));
pipeline.fireChannelRead(new EpollSocketChannel(EpollServerSocketChannel.this, socketFd));
} catch (Throwable t) {
// keep on reading as we use epoll ET and need to consume everything from the socket
pipeline.fireChannelReadComplete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ public final class EpollSocketChannel extends AbstractEpollChannel implements So
private volatile boolean inputShutdown;
private volatile boolean outputShutdown;

EpollSocketChannel(Channel parent, EventLoop eventLoop, int fd) {
super(parent, eventLoop, fd, Native.EPOLLIN, true);
EpollSocketChannel(Channel parent, int fd) {
super(parent, fd, Native.EPOLLIN, true);
config = new EpollSocketChannelConfig(this);
// Directly cache the remote and local addresses
// See https://github.com/netty/netty/issues/2359
remote = Native.remoteAddress(fd);
local = Native.localAddress(fd);
}

public EpollSocketChannel(EventLoop eventLoop) {
super(eventLoop, Native.socketStreamFd(), Native.EPOLLIN);
public EpollSocketChannel() {
super(Native.socketStreamFd(), Native.EPOLLIN);
config = new EpollSocketChannelConfig(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.netty.bootstrap.ChannelFactory;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.channel.socket.nio.NioDatagramChannel;
Expand Down Expand Up @@ -100,8 +99,8 @@ public List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> da
public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel(EventLoop loop) {
return new NioDatagramChannel(loop, InternetProtocolFamily.IPv4);
public Channel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.channel.oio.OioByteStreamChannel;

import java.net.SocketAddress;
Expand All @@ -40,8 +39,8 @@ public class RxtxChannel extends OioByteStreamChannel {
private RxtxDeviceAddress deviceAddress;
private SerialPort serialPort;

public RxtxChannel(EventLoop eventLoop) {
super(null, eventLoop);
public RxtxChannel() {
super(null);

config = new DefaultRxtxChannelConfig(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.netty.channel.ChannelMetadata;
import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.channel.RecvByteBufAllocator;
import io.netty.channel.nio.AbstractNioMessageChannel;
import io.netty.channel.sctp.DefaultSctpChannelConfig;
Expand Down Expand Up @@ -82,15 +81,15 @@ private static SctpChannel newSctpChannel() {
/**
* Create a new instance
*/
public NioSctpChannel(EventLoop eventLoop) {
this(eventLoop, newSctpChannel());
public NioSctpChannel() {
this(newSctpChannel());
}

/**
* Create a new instance using {@link SctpChannel}
*/
public NioSctpChannel(EventLoop eventLoop, SctpChannel sctpChannel) {
this(null, eventLoop, sctpChannel);
public NioSctpChannel(SctpChannel sctpChannel) {
this(null, sctpChannel);
}

/**
Expand All @@ -100,8 +99,8 @@ public NioSctpChannel(EventLoop eventLoop, SctpChannel sctpChannel) {
* or {@code null}.
* @param sctpChannel the underlying {@link SctpChannel}
*/
public NioSctpChannel(Channel parent, EventLoop eventLoop, SctpChannel sctpChannel) {
super(parent, eventLoop, sctpChannel, SelectionKey.OP_READ);
public NioSctpChannel(Channel parent, SctpChannel sctpChannel) {
super(parent, sctpChannel, SelectionKey.OP_READ);
try {
sctpChannel.configureBlocking(false);
config = new NioSctpChannelConfig(this, sctpChannel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import io.netty.channel.ChannelMetadata;
import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.AbstractNioMessageServerChannel;
import io.netty.channel.nio.AbstractNioMessageChannel;
import io.netty.channel.sctp.DefaultSctpServerChannelConfig;
import io.netty.channel.sctp.SctpServerChannelConfig;

Expand All @@ -46,9 +44,8 @@
* Be aware that not all operations systems support SCTP. Please refer to the documentation of your operation system,
* to understand what you need to do to use it. Also this feature is only supported on Java 7+.
*/
public class NioSctpServerChannel extends AbstractNioMessageServerChannel
public class NioSctpServerChannel extends AbstractNioMessageChannel
implements io.netty.channel.sctp.SctpServerChannel {

private static final ChannelMetadata METADATA = new ChannelMetadata(false);

private static SctpServerChannel newSocket() {
Expand All @@ -65,9 +62,9 @@ private static SctpServerChannel newSocket() {
/**
* Create a new instance
*/
public NioSctpServerChannel(EventLoop eventLoop, EventLoopGroup childGroup) {
super(null, eventLoop, childGroup, newSocket(), SelectionKey.OP_ACCEPT);
config = new NioSctpServerChannelConfig(this, javaChannel());
public NioSctpServerChannel() {
super(null, newSocket(), SelectionKey.OP_ACCEPT);
config = new DefaultSctpServerChannelConfig(this, javaChannel());
}

@Override
Expand Down Expand Up @@ -143,7 +140,7 @@ protected int doReadMessages(List<Object> buf) throws Exception {
if (ch == null) {
return 0;
}
buf.add(new NioSctpChannel(this, childEventLoopGroup().next(), ch));
buf.add(new NioSctpChannel(this, ch));
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import io.netty.channel.ChannelMetadata;
import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.channel.RecvByteBufAllocator;
import io.netty.channel.oio.AbstractOioMessageChannel;
import io.netty.channel.sctp.DefaultSctpChannelConfig;
Expand Down Expand Up @@ -88,17 +87,17 @@ private static SctpChannel openChannel() {
/**
* Create a new instance with an new {@link SctpChannel}.
*/
public OioSctpChannel(EventLoop eventLoop) {
this(eventLoop, openChannel());
public OioSctpChannel() {
this(openChannel());
}

/**
* Create a new instance from the given {@link SctpChannel}.
*
* @param ch the {@link SctpChannel} which is used by this instance
*/
public OioSctpChannel(EventLoop eventLoop, SctpChannel ch) {
this(null, eventLoop, ch);
public OioSctpChannel(SctpChannel ch) {
this(null, ch);
}

/**
Expand All @@ -108,8 +107,8 @@ public OioSctpChannel(EventLoop eventLoop, SctpChannel ch) {
* {@link} has no parent as it was created by your self.
* @param ch the {@link SctpChannel} which is used by this instance
*/
public OioSctpChannel(Channel parent, EventLoop eventLoop, SctpChannel ch) {
super(parent, eventLoop);
public OioSctpChannel(Channel parent, SctpChannel ch) {
super(parent);
this.ch = ch;
boolean success = false;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import io.netty.channel.ChannelMetadata;
import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.oio.AbstractOioMessageServerChannel;
import io.netty.channel.oio.AbstractOioMessageChannel;
import io.netty.channel.sctp.DefaultSctpServerChannelConfig;
import io.netty.channel.sctp.SctpServerChannelConfig;
import io.netty.util.internal.logging.InternalLogger;
Expand All @@ -49,7 +47,7 @@
* Be aware that not all operations systems support SCTP. Please refer to the documentation of your operation system,
* to understand what you need to do to use it. Also this feature is only supported on Java 7+.
*/
public class OioSctpServerChannel extends AbstractOioMessageServerChannel
public class OioSctpServerChannel extends AbstractOioMessageChannel
implements io.netty.channel.sctp.SctpServerChannel {

private static final InternalLogger logger =
Expand All @@ -72,17 +70,17 @@ private static SctpServerChannel newServerSocket() {
/**
* Create a new instance with an new {@link SctpServerChannel}
*/
public OioSctpServerChannel(EventLoop eventLoop, EventLoopGroup childGroup) {
this(eventLoop, childGroup, newServerSocket());
public OioSctpServerChannel() {
this(newServerSocket());
}

/**
* Create a new instance from the given {@link SctpServerChannel}
*
* @param sch the {@link SctpServerChannel} which is used by this instance
*/
public OioSctpServerChannel(EventLoop eventLoop, EventLoopGroup childGroup, SctpServerChannel sch) {
super(null, eventLoop, childGroup);
public OioSctpServerChannel(SctpServerChannel sch) {
super(null);
if (sch == null) {
throw new NullPointerException("sctp server channel");
}
Expand Down Expand Up @@ -198,7 +196,7 @@ protected int doReadMessages(List<Object> buf) throws Exception {
if (key.isAcceptable()) {
s = sch.accept();
if (s != null) {
buf.add(new OioSctpChannel(this, childEventLoopGroup().next(), s));
buf.add(new OioSctpChannel(this, s));
acceptedChannels ++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import com.barchart.udt.nio.ServerSocketChannelUDT;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.AbstractNioMessageServerChannel;
import io.netty.channel.nio.AbstractNioMessageChannel;
import io.netty.channel.udt.DefaultUdtServerChannelConfig;
import io.netty.channel.udt.UdtServerChannel;
import io.netty.channel.udt.UdtServerChannelConfig;
Expand All @@ -36,16 +34,15 @@
/**
* Common base for Netty Byte/Message UDT Stream/Datagram acceptors.
*/
public abstract class NioUdtAcceptorChannel extends AbstractNioMessageServerChannel implements UdtServerChannel {
public abstract class NioUdtAcceptorChannel extends AbstractNioMessageChannel implements UdtServerChannel {

protected static final InternalLogger logger =
InternalLoggerFactory.getInstance(NioUdtAcceptorChannel.class);

private final UdtServerChannelConfig config;

protected NioUdtAcceptorChannel(EventLoop eventLoop, EventLoopGroup childGroup,
ServerSocketChannelUDT channelUDT) {
super(null, eventLoop, childGroup, channelUDT, OP_ACCEPT);
protected NioUdtAcceptorChannel(final ServerSocketChannelUDT channelUDT) {
super(null, channelUDT, OP_ACCEPT);
try {
channelUDT.configureBlocking(false);
config = new DefaultUdtServerChannelConfig(this, channelUDT, true);
Expand All @@ -61,8 +58,8 @@ protected NioUdtAcceptorChannel(EventLoop eventLoop, EventLoopGroup childGroup,
}
}

protected NioUdtAcceptorChannel(EventLoop eventLoop, EventLoopGroup childGroup, final TypeUDT type) {
this(eventLoop, childGroup, NioUdtProvider.newAcceptorChannelUDT(type));
protected NioUdtAcceptorChannel(final TypeUDT type) {
this(NioUdtProvider.newAcceptorChannelUDT(type));
}

@Override
Expand Down
Loading

0 comments on commit 48f2e70

Please sign in to comment.