Selector selector;
if (firstChannel) {
try {
this.selector = selector = Selector.open();
} catch (IOException e) {
throw new ChannelException(
"Failed to create a selector.", e);
}
} else {
selector = this.selector;
if (selector == null) {
do {
Thread.yield();
selector = this.selector;
} while (selector == null);
}
}
if (firstChannel) {
try {
channel.socket.register(selector, SelectionKey.OP_READ, channel);
if (future != null) {
future.setSuccess();
}
} catch (ClosedChannelException e) {
future.setFailure(e);
throw new ChannelException(
"Failed to register a socket to the selector.", e);
}
boolean server = !(channel instanceof NioClientSocketChannel);
if (server) {
fireChannelOpen(channel);
fireChannelBound(channel, channel.getLocalAddress());
} else if (!((NioClientSocketChannel) channel).boundManually) {
fireChannelBound(channel, channel.getLocalAddress());
}
fireChannelConnected(channel, channel.getRemoteAddress());
String threadName =
(server ? "New I/O server worker #"
: "New I/O client worker #") + bossId + '-' + id;
executor.execute(new ThreadRenamingRunnable(this, threadName));
} else {
selectorGuard.readLock().lock();
try {
if (wakenUp.compareAndSet(false, true)) {
selector.wakeup();
}
try {
channel.socket.register(selector, SelectionKey.OP_READ, channel);
if (future != null) {
future.setSuccess();
}
} catch (ClosedChannelException e) {
future.setFailure(e);
throw new ChannelException(
"Failed to register a socket to the selector.", e);
}
boolean server = !(channel instanceof NioClientSocketChannel);
if (server) {