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); File file = new File(msg);
if (file.exists()) { if (file.exists()) {
if (!file.isFile()) { if (!file.isFile()) {
ctx.write("Not a file: " + file + '\n'); ctx.writeAndFlush("Not a file: " + file + '\n');
return; return;
} }
ctx.write(file + " " + file.length() + '\n'); ctx.write(file + " " + file.length() + '\n');
FileRegion region = new DefaultFileRegion(new FileInputStream(file).getChannel(), 0, file.length()); FileRegion region = new DefaultFileRegion(new FileInputStream(file).getChannel(), 0, file.length());
ctx.write(region); ctx.write(region);
ctx.write("\n"); ctx.writeAndFlush("\n");
ctx.flush();
} else { } 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"))); new DefaultCookie("another-cookie", "bar")));
// Send the HTTP request. // Send the HTTP request.
ch.write(request); ch.writeAndFlush(request);
// Wait for the server to close the connection. // Wait for the server to close the connection.
ch.closeFuture().sync(); ch.closeFuture().sync();

View File

@ -277,6 +277,8 @@ public class HttpUploadClient {
// could do either request.isChunked() // could do either request.isChunked()
// either do it through ChunkedWriteHandler // either do it through ChunkedWriteHandler
channel.writeAndFlush(bodyRequestEncoder).awaitUninterruptibly(); channel.writeAndFlush(bodyRequestEncoder).awaitUninterruptibly();
} else {
channel.flush();
} }
// Do not clear here since we will reuse the InterfaceHttpData on the // 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 // test if request was chunked and if so, finish the write
if (bodyRequestEncoder.isChunked()) { if (bodyRequestEncoder.isChunked()) {
channel.writeAndFlush(bodyRequestEncoder).awaitUninterruptibly(); channel.writeAndFlush(bodyRequestEncoder).awaitUninterruptibly();
} else {
channel.flush();
} }
// Now no more use of file representation (and list of HttpData) // 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()); response.headers().set(CONTENT_LENGTH, buf.readableBytes());
// Write the response. // Write the response.
ctx.channel().write(response); ctx.channel().writeAndFlush(response);
} }
@Override @Override

View File

@ -104,16 +104,16 @@ public class WebSocketClient {
// Send 10 messages and wait for responses // Send 10 messages and wait for responses
System.out.println("WebSocket Client sending message"); System.out.println("WebSocket Client sending message");
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
ch.write(new TextWebSocketFrame("Message #" + i)); ch.writeAndFlush(new TextWebSocketFrame("Message #" + i));
} }
// Ping // Ping
System.out.println("WebSocket Client sending 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 // Close
System.out.println("WebSocket Client sending close"); System.out.println("WebSocket Client sending close");
ch.write(new CloseWebSocketFrame()); ch.writeAndFlush(new CloseWebSocketFrame());
// WebSocketClientHandler will close the connection when the server // WebSocketClientHandler will close the connection when the server
// responds to the CloseWebSocketFrame. // 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 { private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
// Handle a bad request. // Handle a bad request.
if (!req.getDecoderResult().isSuccess()) { 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. // 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) { if (!isKeepAlive(req) || res.getStatus().code() != 200) {
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);
} }
} }
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace(); cause.printStackTrace();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -56,7 +56,7 @@ public final class SocksServerConnectHandler extends SimpleChannelInboundHandler
@Override @Override
public void onFailure(ChannelHandlerContext outboundCtx, Throwable cause) { 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()); 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 @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) throws Exception {
throwable.printStackTrace(); throwable.printStackTrace();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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