org.zodiac.sdk.simplenetty.handler.DefaultChannelHandlerContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zodiac-sdk-nio Show documentation
Show all versions of zodiac-sdk-nio Show documentation
Zodiac SDK NIO2(New Non-Blocking IO)
package org.zodiac.sdk.simplenetty.handler;
import java.nio.ByteBuffer;
import java.util.ListIterator;
import org.zodiac.sdk.simplenetty.channel.Channel;
import org.zodiac.sdk.simplenetty.core.EventLoop;
import org.zodiac.sdk.simplenetty.core.EventLoopGroup;
import org.zodiac.sdk.simplenetty.enums.ChannelHandlerType;
public class DefaultChannelHandlerContext implements ChannelHandlerContext {
DefaultChannelPipeline pipeline;
Channel channel;
EventLoop eventLoop;
EventLoopGroup group;
public DefaultChannelHandlerContext(Channel channel, EventLoop eventLoop, EventLoopGroup group) {
this.channel = channel;
this.eventLoop = eventLoop;
this.group = group;
}
public void setPipeline(DefaultChannelPipeline pipeline) {
this.pipeline = pipeline;
}
@Override
public EventLoop eventLoop() {
return eventLoop;
}
@Override
public EventLoopGroup eventLoopGroup() {
return group;
}
@Override
public ChannelPipeline pipeline() {
return pipeline;
}
@Override
public Channel channel() {
return channel;
}
@Override
public void handlerAdded(ChannelHandler handler) throws Exception {
pipeline.handlers.add(handler);
}
@Override
public void handlerRemoved(ChannelHandler handler) throws Exception {
pipeline.handlers.remove(handler);
}
@Override
public void fireActive() throws Exception {
ListIterator list = pipeline.handlers.listIterator();
while (list.hasNext()) {
ChannelHandler handler = list.next();
handler.channelActive(this);
}
}
@Override
public void fireInactive() throws Exception {
ListIterator list = pipeline.handlers.listIterator();
while (list.hasNext()) {
ChannelHandler handler = list.next();
handler.channelInactive(this);
}
}
@Override
public void fireRead(Object var2) throws Exception {
ListIterator list = pipeline.handlers.listIterator();
Object res = var2;
while (list.hasNext()) {
ChannelHandler handler = list.next();
if (handler.type() == ChannelHandlerType.IN_BOUND) {
res = handler.channelRead(this, res);
}
}
}
@Override
public void fireReadComplete() throws Exception {
ListIterator list = pipeline.handlers.listIterator();
while (list.hasNext()) {
ChannelHandler handler = list.next();
if (handler.type() == ChannelHandlerType.IN_BOUND) {
handler.channelReadComplete(this);
}
}
}
@Override
public void fireWrite(Object var2) throws Exception {
ListIterator list = pipeline.handlers.listIterator();
Object res = var2;
while (list.hasNext()) {
ChannelHandler handler = list.next();
if (handler.type() == ChannelHandlerType.OUT_BOUND) {
res = handler.channelWrite(this, res);
}
}
}
@Override
public void fireWriteComplete() throws Exception {
ListIterator list = pipeline.handlers.listIterator();
while (list.hasNext()) {
ChannelHandler handler = list.next();
if (handler.type() == ChannelHandlerType.OUT_BOUND) {
handler.channelWriteComplete(this);
}
}
}
@Override
public void fireExceptionCaught(Throwable var2) {
ListIterator list = pipeline.handlers.listIterator();
while (list.hasNext()) {
ChannelHandler handler = list.next();
handler.exceptionCaught(this, var2);
}
}
@Override
public void writeAndFlush(ByteBuffer buffer) {
channel.unsafe().write(buffer);
channel.unsafe().flush();
}
}