I want to control bandwidth for data transfer. According to Netty document, they suggest:
In your handler, you should consider to use the channel.isWritable() and channelWritabilityChanged(ctx) to handle writability, or through future.addListener(new GenericFutureListener()) on the future returned by ctx.write().
Here is my channel initializer source code:
public class MyChannelInitializer extends ChannelInitializer<Channel>
{
private int mode;
private Server server;
private String fileName;
public MyChannelInitializer(Server server, int mode,String fileName)
{
this.mode=mode;
this.server=server;
this.fileName=fileName;
}
@Override
protected void initChannel(Channel ch) throws Exception
{
if (this.mode==MyFtpServer.RECEIVEFILE)
{
ch.pipeline().addLast(new ChannelTrafficShapingHandler(0L,10240L));
ch.pipeline().addLast(new ReceiveFileHandler(this.fileName,server));
}
else
{
ch.pipeline().addLast(new ChannelTrafficShapingHandler(10240L,0L));
ch.pipeline().addLast("streamer", new ChunkedWriteHandler());
ch.pipeline().addLast("handler",new SendFileHandler(this.fileName,server));
}
}
}
Here is Send File Handler source code:
public class SendFileHandler extends SimpleChannelInboundHandler<ByteBuf>
{
String fileName;
PassiveServer txServer=null;
public SendFileHandler(String fileName, PassiveServer txServer)
{
this.fileName=fileName;
this.txServer=txServer;
}
public void channelWritabilityChanged(ChannelHandlerContext ctx)throws IOException
{
System.out.println("isWritable="+ctx.channel().isWritable());
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws IOException
{
System.out.println("SendFileHandler active");
}
}
Would you tell me why "channelWritabilityChanged" method in my SendFileHandler is never trigger, only channelActive method is triggered.?
After I changed SendFileHandler.channelActive method as the following:
public void channelActive(ChannelHandlerContext ctx) throws IOException
{
Calendar startTime;
System.out.println("SendFileHandler active");
startTime=Calendar.getInstance();
ctx.fireChannelWritabilityChanged();
}
The SendFileHandler.channelWritabilityChanged method still not triggered. Would anyone tell me why?