Fix examples

This commit is contained in:
Norman Maurer 2013-07-11 11:25:40 +02:00
parent 1d577b1b8b
commit c0580cfe71
20 changed files with 50 additions and 21 deletions

View File

@ -97,16 +97,15 @@ public class FileServer {
File file = new File(msg);
if (file.exists()) {
if (!file.isFile()) {
ctx.write("Not a file: " + file + '\n');
ctx.writeAndFlush("Not a file: " + file + '\n');
return;
}
ctx.write(file + " " + file.length() + '\n');
FileRegion region = new DefaultFileRegion(new FileInputStream(file).getChannel(), 0, file.length());
ctx.write(region);
ctx.write("\n");
ctx.flush();
ctx.writeAndFlush("\n");
} else {
ctx.write("File not found: " + file + '\n');
ctx.writeAndFlush("File not found: " + file + '\n');
}
}

View File

@ -87,7 +87,7 @@ public class HttpSnoopClient {
new DefaultCookie("another-cookie", "bar")));
// Send the HTTP request.
ch.write(request);
ch.writeAndFlush(request);
// Wait for the server to close the connection.
ch.closeFuture().sync();

View File

@ -277,6 +277,8 @@ public class HttpUploadClient {
// could do either request.isChunked()
// either do it through ChunkedWriteHandler
channel.writeAndFlush(bodyRequestEncoder).awaitUninterruptibly();
} else {
channel.flush();
}
// Do not clear here since we will reuse the InterfaceHttpData on the
@ -352,6 +354,8 @@ public class HttpUploadClient {
// test if request was chunked and if so, finish the write
if (bodyRequestEncoder.isChunked()) {
channel.writeAndFlush(bodyRequestEncoder).awaitUninterruptibly();
} else {
channel.flush();
}
// Now no more use of file representation (and list of HttpData)

View File

@ -401,7 +401,7 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
response.headers().set(CONTENT_LENGTH, buf.readableBytes());
// Write the response.
ctx.channel().write(response);
ctx.channel().writeAndFlush(response);
}
@Override

View File

@ -104,16 +104,16 @@ public class WebSocketClient {
// Send 10 messages and wait for responses
System.out.println("WebSocket Client sending message");
for (int i = 0; i < 10; i++) {
ch.write(new TextWebSocketFrame("Message #" + i));
ch.writeAndFlush(new TextWebSocketFrame("Message #" + i));
}
// Ping
System.out.println("WebSocket Client sending ping");
ch.write(new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[]{1, 2, 3, 4, 5, 6})));
ch.writeAndFlush(new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[]{1, 2, 3, 4, 5, 6})));
// Close
System.out.println("WebSocket Client sending close");
ch.write(new CloseWebSocketFrame());
ch.writeAndFlush(new CloseWebSocketFrame());
// WebSocketClientHandler will close the connection when the server
// responds to the CloseWebSocketFrame.

View File

@ -61,6 +61,11 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
// Handle a bad request.
if (!req.getDecoderResult().isSuccess()) {

View File

@ -137,12 +137,17 @@ public class WebSocketSslServerHandler extends SimpleChannelInboundHandler<Objec
}
// Send the response and close the connection if necessary.
ChannelFuture f = ctx.channel().writeAndFlush(res);
ChannelFuture f = ctx.channel().write(res);
if (!isKeepAlive(req) || res.getStatus().code() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();

View File

@ -52,7 +52,7 @@ public class ObjectEchoClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// Send the first message if this handler is a client-side handler.
ctx.write(firstMessage);
ctx.writeAndFlush(firstMessage);
}
@Override

View File

@ -52,6 +52,11 @@ public class QuoteOfTheMomentServerHandler extends SimpleChannelInboundHandler<D
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(
ChannelHandlerContext ctx, Throwable cause)

View File

@ -22,7 +22,7 @@ public class RxtxClientHandler extends SimpleChannelInboundHandler<String> {
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.write("AT\n");
ctx.writeAndFlush("AT\n");
}
@Override

View File

@ -51,7 +51,7 @@ public class SctpEchoClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.write(new SctpMessage(0, 0, firstMessage));
ctx.writeAndFlush(new SctpMessage(0, 0, firstMessage));
}
@Override

View File

@ -37,7 +37,7 @@ public final class RelayHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.write(Unpooled.EMPTY_BUFFER);
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
}
@Override

View File

@ -56,7 +56,7 @@ public final class SocksServerConnectHandler extends SimpleChannelInboundHandler
@Override
public void onFailure(ChannelHandlerContext outboundCtx, Throwable cause) {
ctx.channel().write(new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType()));
ctx.channel().writeAndFlush(new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType()));
SocksServerUtils.closeOnFlush(ctx.channel());
}
};

View File

@ -67,6 +67,11 @@ public final class SocksServerHandler extends SimpleChannelInboundHandler<SocksR
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) throws Exception {
throwable.printStackTrace();

View File

@ -40,6 +40,7 @@ public class TelnetServerHandler extends SimpleChannelInboundHandler<String> {
ctx.write(
"Welcome to " + InetAddress.getLocalHost().getHostName() + "!\r\n");
ctx.write("It is " + new Date() + " now.\r\n");
ctx.flush();
}
@Override
@ -59,7 +60,7 @@ public class TelnetServerHandler extends SimpleChannelInboundHandler<String> {
// We do not need to write a ChannelBuffer here.
// We know the encoder inserted at TelnetPipelineFactory will do the conversion.
ChannelFuture future = ctx.writeAndFlush(response);
ChannelFuture future = ctx.write(response);
// Close the connection after sending 'Have a good day!'
// if the client has sent 'bye'.
@ -68,6 +69,11 @@ public class TelnetServerHandler extends SimpleChannelInboundHandler<String> {
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
logger.log(

View File

@ -54,7 +54,7 @@ public class ByteEchoClientHandler extends SimpleChannelInboundHandler<ByteBuf>
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
log.info("ECHO active " + NioUdtProvider.socketUDT(ctx.channel()).toStringOptions());
ctx.write(message);
ctx.writeAndFlush(message);
}
@Override

View File

@ -54,7 +54,7 @@ public class MsgEchoClientHandler extends SimpleChannelInboundHandler<UdtMessage
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
log.info("ECHO active " + NioUdtProvider.socketUDT(ctx.channel()).toStringOptions());
ctx.write(message);
ctx.writeAndFlush(message);
}
@Override

View File

@ -55,7 +55,7 @@ public class MsgEchoPeerHandler extends
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
log.info("ECHO active " + NioUdtProvider.socketUDT(ctx.channel()).toStringOptions());
ctx.write(message);
ctx.writeAndFlush(message);
}
@Override

View File

@ -50,7 +50,7 @@ public class ByteEchoPeerHandler extends SimpleChannelInboundHandler<ByteBuf> {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
log.info("ECHO active " + NioUdtProvider.socketUDT(ctx.channel()).toStringOptions());
ctx.write(message);
ctx.writeAndFlush(message);
}
@Override

View File

@ -59,7 +59,7 @@ public class WorldClockClientHandler extends SimpleChannelInboundHandler<LocalTi
setCity(components[1]).build());
}
channel.write(builder.build());
channel.writeAndFlush(builder.build());
LocalTimes localTimes;
boolean interrupted = false;