From a5ccef89ed3e1a6dace601f6d0f7b95384d54765 Mon Sep 17 00:00:00 2001 From: likaijian Date: Tue, 22 Oct 2019 19:17:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=B6=E9=87=8C=E7=9A=84=E7=94=B5=E8=84=91?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=9A=84=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EchoClient/pom.xml | 1 + EchoServer/pom.xml | 4 +- .../polarlights/raincloud/EchoServer.java | 8 +- .../raincloud/EchoServerHandler.java | 22 +- .../polarlights/raincloud/NettyOioServer.java | 2 +- .../plort/polarlights/raincloud/Server.java | 6 + .../polarlights/raincloud/ServerStart.java | 23 +- pom.xml | 8 +- .../netty-in-action-2.0-SNAPSHOT/.gitignore" | 9 + .../netty-in-action-2.0-SNAPSHOT/README.md" | 26 ++ .../chapter1/pom.xml" | 21 ++ .../java/nia/chapter1/BlockingIoExample.java" | 40 +++ .../java/nia/chapter1/ConnectExample.java" | 50 ++++ .../java/nia/chapter1/ConnectHandler.java" | 18 ++ .../main/java/nia/chapter1/package-info.java" | 12 + .../chapter10/pom.xml" | 15 + .../nia/chapter10/ByteToCharDecoder.java" | 23 ++ .../nia/chapter10/CharToByteEncoder.java" | 20 ++ .../nia/chapter10/CombinedByteCharCodec.java" | 16 ++ .../chapter10/IntegerToStringDecoder.java" | 21 ++ .../chapter10/IntegerToStringEncoder.java" | 21 ++ .../chapter10/SafeByteToMessageDecoder.java" | 29 ++ .../nia/chapter10/ShortToByteEncoder.java" | 18 ++ .../java/nia/chapter10/ToIntegerDecoder.java" | 23 ++ .../nia/chapter10/ToIntegerDecoder2.java" | 22 ++ .../chapter10/WebSocketConvertHandler.java" | 107 +++++++ .../java/nia/chapter10/package-info.java" | 24 ++ .../chapter11/pom.xml" | 25 ++ .../ChunkedWriteHandlerInitializer.java" | 45 +++ .../nia/chapter11/CmdHandlerInitializer.java" | 66 +++++ .../chapter11/FileRegionWriteHandler.java" | 38 +++ .../chapter11/HttpAggregatorInitializer.java" | 33 +++ .../HttpCompressionInitializer.java" | 36 +++ .../chapter11/HttpPipelineInitializer.java" | 35 +++ .../nia/chapter11/HttpsCodecInitializer.java" | 39 +++ .../IdleStateHandlerInitializer.java" | 44 +++ .../chapter11/LengthBasedInitializer.java" | 29 ++ .../LineBasedHandlerInitializer.java" | 29 ++ .../chapter11/MarshallingInitializer.java" | 44 +++ .../nia/chapter11/ProtoBufInitializer.java" | 38 +++ .../nia/chapter11/SslChannelInitializer.java" | 30 ++ .../WebSocketServerInitializer.java" | 57 ++++ .../java/nia/chapter11/package-info.java" | 32 +++ .../chapter12/pom.xml" | 76 +++++ .../main/java/nia/chapter12/ChatServer.java" | 68 +++++ .../nia/chapter12/ChatServerInitializer.java" | 34 +++ .../nia/chapter12/HttpRequestHandler.java" | 88 ++++++ .../java/nia/chapter12/SecureChatServer.java" | 49 ++++ .../SecureChatServerInitializer.java" | 31 ++ .../chapter12/TextWebSocketFrameHandler.java" | 41 +++ .../java/nia/chapter12/package-info.java" | 17 ++ .../chapter12/src/main/resources/index.html" | 115 ++++++++ .../chapter13/pom.xml" | 69 +++++ .../main/java/nia/chapter13/LogEvent.java" | 44 +++ .../nia/chapter13/LogEventBroadcaster.java" | 80 ++++++ .../java/nia/chapter13/LogEventDecoder.java" | 33 +++ .../java/nia/chapter13/LogEventEncoder.java" | 36 +++ .../java/nia/chapter13/LogEventHandler.java" | 34 +++ .../java/nia/chapter13/LogEventMonitor.java" | 60 ++++ .../java/nia/chapter13/package-info.java" | 16 ++ .../chapter2/Client/pom.xml" | 39 +++ .../nia/chapter2/echoclient/EchoClient.java" | 64 +++++ .../echoclient/EchoClientHandler.java" | 36 +++ .../main/java/nia/chapter2/package-info.java" | 8 + .../Client/src/main/resources/.gitignore" | 4 + .../chapter2/Server/pom.xml" | 38 +++ .../nia/chapter2/echoserver/EchoServer.java" | 60 ++++ .../echoserver/EchoServerHandler.java" | 39 +++ .../main/java/nia/chapter2/package-info.java" | 8 + .../Server/src/main/resources/.gitignore" | 4 + .../chapter2/out" | 80 ++++++ .../chapter2/pom.xml" | 48 ++++ .../chapter4/pom.xml" | 18 ++ .../chapter4/ChannelOperationExamples.java" | 65 +++++ .../java/nia/chapter4/NettyNioServer.java" | 53 ++++ .../java/nia/chapter4/NettyOioServer.java" | 54 ++++ .../java/nia/chapter4/PlainNioServer.java" | 77 +++++ .../java/nia/chapter4/PlainOioServer.java" | 48 ++++ .../main/java/nia/chapter4/package-info.java" | 16 ++ .../chapter5/pom.xml" | 21 ++ .../channel/DummyChannelHandlerContext.java" | 26 ++ .../java/nia.chapter5/ByteBufExamples.java" | 264 ++++++++++++++++++ .../main/java/nia.chapter5/package-info.java" | 36 +++ .../chapter6/pom.xml" | 19 ++ .../channel/DummyChannelHandlerContext.java" | 26 ++ .../netty/channel/DummyChannelPipeline.java" | 11 + .../java/nia/chapter6/ChannelFutures.java" | 38 +++ .../java/nia/chapter6/DiscardHandler.java" | 22 ++ .../nia/chapter6/DiscardInboundHandler.java" | 19 ++ .../nia/chapter6/DiscardOutboundHandler.java" | 24 ++ .../chapter6/InboundExceptionHandler.java" | 18 ++ .../nia/chapter6/ModifyChannelPipeline.java" | 51 ++++ .../chapter6/OutboundExceptionHandler.java" | 24 ++ .../java/nia/chapter6/SharableHandler.java" | 20 ++ .../nia/chapter6/SimpleDiscardHandler.java" | 20 ++ .../java/nia/chapter6/UnsharableHandler.java" | 23 ++ .../main/java/nia/chapter6/WriteHandler.java" | 21 ++ .../java/nia/chapter6/WriteHandlers.java" | 55 ++++ .../main/java/nia/chapter6/package-info.java" | 32 +++ .../chapter7/pom.xml" | 14 + .../java/nia/chapter7/EventLoopExamples.java" | 38 +++ .../java/nia/chapter7/ScheduleExamples.java" | 87 ++++++ .../main/java/nia/chapter7/package-info.java" | 14 + .../chapter8/pom.xml" | 15 + .../java/nia/chapter8/BootstrapClient.java" | 55 ++++ .../BootstrapClientWithOptionsAndAttrs.java" | 54 ++++ .../chapter8/BootstrapDatagramChannel.java" | 51 ++++ .../java/nia/chapter8/BootstrapServer.java" | 51 ++++ .../BootstrapSharingEventLoopGroup.java" | 75 +++++ .../chapter8/BootstrapWithInitializer.java" | 40 +++ .../java/nia/chapter8/GracefulShutdown.java" | 49 ++++ .../nia/chapter8/InvalidBootstrapClient.java" | 45 +++ .../main/java/nia/chapter8/package-info.java" | 20 ++ .../chapter9/pom.xml" | 15 + .../java/nia/chapter9/AbsIntegerEncoder.java" | 24 ++ .../chapter9/FixedLengthFrameDecoder.java" | 33 +++ .../java/nia/chapter9/FrameChunkDecoder.java" | 35 +++ .../main/java/nia/chapter9/package-info.java" | 10 + .../test/chapter9/AbsIntegerEncoderTest.java" | 35 +++ .../FixedLengthFrameDecoderTest.java" | 76 +++++ .../test/chapter9/FrameChunkDecoderTest.java" | 51 ++++ .../java/nia/test/chapter9/package-info.java" | 10 + .../netty-in-action-2.0-SNAPSHOT/pom.xml" | 102 +++++++ .../utils/pom.xml" | 24 ++ .../main/java/nia/util/BogusKeyStore.java" | 150 ++++++++++ .../nia/util/BogusSslContextFactory.java" | 47 ++++ .../nia/util/BogusTrustManagerFactory.java" | 59 ++++ 127 files changed, 4895 insertions(+), 13 deletions(-) create mode 100644 EchoServer/src/main/java/com/plort/polarlights/raincloud/Server.java create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/.gitignore" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/README.md" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/BlockingIoExample.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/ConnectExample.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/ConnectHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ByteToCharDecoder.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/CharToByteEncoder.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/CombinedByteCharCodec.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/IntegerToStringDecoder.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/IntegerToStringEncoder.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/SafeByteToMessageDecoder.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ShortToByteEncoder.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ToIntegerDecoder.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ToIntegerDecoder2.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/WebSocketConvertHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/ChunkedWriteHandlerInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/CmdHandlerInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/FileRegionWriteHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpAggregatorInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpCompressionInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpPipelineInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpsCodecInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/IdleStateHandlerInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/LengthBasedInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/LineBasedHandlerInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/MarshallingInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/ProtoBufInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/SslChannelInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/WebSocketServerInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/ChatServer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/ChatServerInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/HttpRequestHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/SecureChatServer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/SecureChatServerInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/TextWebSocketFrameHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/resources/index.html" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEvent.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventBroadcaster.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventDecoder.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventEncoder.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventMonitor.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/java/nia/chapter2/echoclient/EchoClient.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/java/nia/chapter2/echoclient/EchoClientHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/java/nia/chapter2/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/resources/.gitignore" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/java/nia/chapter2/echoserver/EchoServer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/java/nia/chapter2/echoserver/EchoServerHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/java/nia/chapter2/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/resources/.gitignore" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/out" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/ChannelOperationExamples.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/NettyNioServer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/NettyOioServer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/PlainNioServer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/PlainOioServer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/src/main/java/io/netty/channel/DummyChannelHandlerContext.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/src/main/java/nia.chapter5/ByteBufExamples.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/src/main/java/nia.chapter5/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/io/netty/channel/DummyChannelHandlerContext.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/io/netty/channel/DummyChannelPipeline.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/ChannelFutures.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/DiscardHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/DiscardInboundHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/DiscardOutboundHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/InboundExceptionHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/ModifyChannelPipeline.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/OutboundExceptionHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/SharableHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/SimpleDiscardHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/UnsharableHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/WriteHandler.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/WriteHandlers.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/src/main/java/nia/chapter7/EventLoopExamples.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/src/main/java/nia/chapter7/ScheduleExamples.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/src/main/java/nia/chapter7/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapClient.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapClientWithOptionsAndAttrs.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapDatagramChannel.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapServer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapSharingEventLoopGroup.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapWithInitializer.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/GracefulShutdown.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/InvalidBootstrapClient.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/AbsIntegerEncoder.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/FixedLengthFrameDecoder.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/FrameChunkDecoder.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/AbsIntegerEncoderTest.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/FixedLengthFrameDecoderTest.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/FrameChunkDecoderTest.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/package-info.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/pom.xml" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/src/main/java/nia/util/BogusKeyStore.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/src/main/java/nia/util/BogusSslContextFactory.java" create mode 100644 "\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/src/main/java/nia/util/BogusTrustManagerFactory.java" diff --git a/EchoClient/pom.xml b/EchoClient/pom.xml index 02cbe9d..b9f6de3 100644 --- a/EchoClient/pom.xml +++ b/EchoClient/pom.xml @@ -20,6 +20,7 @@ org.codehaus.mojo exec-maven-plugin + 1.6.0 run-server diff --git a/EchoServer/pom.xml b/EchoServer/pom.xml index 2c5337c..e1b7eb4 100644 --- a/EchoServer/pom.xml +++ b/EchoServer/pom.xml @@ -13,6 +13,7 @@ 1.8 1.8 UTF-8 + 3.1.1 @@ -20,6 +21,7 @@ org.codehaus.mojo exec-maven-plugin + 1.6.0 run-server @@ -29,7 +31,7 @@ - com.plort.polarlights.raincloud.EchoServer + com.plort.polarlights.raincloud.ServerStart diff --git a/EchoServer/src/main/java/com/plort/polarlights/raincloud/EchoServer.java b/EchoServer/src/main/java/com/plort/polarlights/raincloud/EchoServer.java index 49fe12a..749b602 100644 --- a/EchoServer/src/main/java/com/plort/polarlights/raincloud/EchoServer.java +++ b/EchoServer/src/main/java/com/plort/polarlights/raincloud/EchoServer.java @@ -11,9 +11,13 @@ import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; -public class EchoServer { +public class EchoServer implements Server{ - private final int port; + private int port = 5555; + + public EchoServer() { + + } public EchoServer(int port) { this.port = port; diff --git a/EchoServer/src/main/java/com/plort/polarlights/raincloud/EchoServerHandler.java b/EchoServer/src/main/java/com/plort/polarlights/raincloud/EchoServerHandler.java index ef23733..dd7ca4c 100644 --- a/EchoServer/src/main/java/com/plort/polarlights/raincloud/EchoServerHandler.java +++ b/EchoServer/src/main/java/com/plort/polarlights/raincloud/EchoServerHandler.java @@ -2,6 +2,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; @@ -19,13 +20,28 @@ public class EchoServerHandler extends ChannelInboundHandlerAdapter{ public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf in = (ByteBuf) msg; - System.out.println("Server received" + in.toString(CharsetUtil.UTF_8)); + System.out.println("Server received:" + in.toString(CharsetUtil.UTF_8)); ctx.write(in); } public void channelReadComplete(ChannelHandlerContext ctx) { - ChannelFuture writeAndFlush = ctx.writeAndFlush(Unpooled.EMPTY_BUFFER); //继续写入 - writeAndFlush.addListener(ChannelFutureListener.CLOSE); + Channel channel = ctx.channel(); //可以使用ctx里的channel写,同样的效果 +// ChannelFuture writeAndFlush = ctx.writeAndFlush(Unpooled.EMPTY_BUFFER); //继续写入 +// ChannelFuture writeAndFlush = channel.writeAndFlush(Unpooled.copiedBuffer("write end",CharsetUtil.UTF_8)); //继续写入 + ChannelFuture writeAndFlush = channel.writeAndFlush(Unpooled.EMPTY_BUFFER); //继续写入 +// writeAndFlush.addListener(ChannelFutureListener.CLOSE); + writeAndFlush.addListener(new ChannelFutureListener() { + + public void operationComplete(ChannelFuture future) throws Exception { + if(future.isSuccess()) { + System.out.println("write successfully!"); + }else { + System.out.println("write failed!!"); + future.cause().printStackTrace(); + } + } + + }); } public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause) { diff --git a/EchoServer/src/main/java/com/plort/polarlights/raincloud/NettyOioServer.java b/EchoServer/src/main/java/com/plort/polarlights/raincloud/NettyOioServer.java index fb3df13..b9a5c1f 100644 --- a/EchoServer/src/main/java/com/plort/polarlights/raincloud/NettyOioServer.java +++ b/EchoServer/src/main/java/com/plort/polarlights/raincloud/NettyOioServer.java @@ -18,7 +18,7 @@ import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.oio.OioServerSocketChannel; -public class NettyOioServer { +public class NettyOioServer implements Server{ int port = 5556; diff --git a/EchoServer/src/main/java/com/plort/polarlights/raincloud/Server.java b/EchoServer/src/main/java/com/plort/polarlights/raincloud/Server.java new file mode 100644 index 0000000..85e381a --- /dev/null +++ b/EchoServer/src/main/java/com/plort/polarlights/raincloud/Server.java @@ -0,0 +1,6 @@ +package com.plort.polarlights.raincloud; + +public interface Server { + + void start() throws Exception; +} diff --git a/EchoServer/src/main/java/com/plort/polarlights/raincloud/ServerStart.java b/EchoServer/src/main/java/com/plort/polarlights/raincloud/ServerStart.java index e0de3be..b29b9a9 100644 --- a/EchoServer/src/main/java/com/plort/polarlights/raincloud/ServerStart.java +++ b/EchoServer/src/main/java/com/plort/polarlights/raincloud/ServerStart.java @@ -4,13 +4,32 @@ public class ServerStart { public static void main(String[] args) { try { + // 开始EchoServer -// new EchoServer(5555).start(); + new ServerStart().start(EchoServer.class); + //开始一个OioServer - new NettyOioServer(5556).start(); +// new ServerStart().start(NettyOioServer.class); }catch(Exception e) { e.printStackTrace(); System.out.println("main exception."); } } + + public void start(Class serverClass) { + try { + serverClass.newInstance().start(); + }catch(Exception e) { + e.printStackTrace(); + } + } + + public void start(Class serverClass,int port) { + + try { + serverClass.newInstance().start(); + } catch (Exception e) { + e.printStackTrace(); + } + } } diff --git a/pom.xml b/pom.xml index 56dd22e..f925abd 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,7 @@ - + + \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/.gitignore" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/.gitignore" new file mode 100644 index 0000000..c230269 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/.gitignore" @@ -0,0 +1,9 @@ +target +bin +.settings +.project +.classpath +.idea +.DS_Store +*.iml +/.idea diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/README.md" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/README.md" new file mode 100644 index 0000000..c134145 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/README.md" @@ -0,0 +1,26 @@ +This Repository contains the source-code for all chapters of the book [Netty in Action](http://manning.com/maurer) +by Norman Maurer and Marvin Allen Wolfthal. + +Latest version: https://github.com/normanmaurer/netty-in-action/tree/2.0-SNAPSHOT + +Enjoy! Feedback and PR's welcome! + + +Prerequisites + + JDK 1.7.0u71 or better + + Maven 3.3.9 or better + + +If you want to build everything at once, from the top directory run + + mvn install + + +If you want to build only single projects then from the top directory first run + + mvn install -pl utils + + +This will make the utils jar available to all the projects. diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/pom.xml" new file mode 100644 index 0000000..d4d65d3 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/pom.xml" @@ -0,0 +1,21 @@ + + + 4.0.0 + + + nia + nia-samples-parent + 2.0-SNAPSHOT + + + chapter1 + 2.0-SNAPSHOT + + jar + Chapter 1. Netty—asynchronous and event-driven + + Build an Echo Server and Client + + \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/BlockingIoExample.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/BlockingIoExample.java" new file mode 100644 index 0000000..6ac3dc1 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/BlockingIoExample.java" @@ -0,0 +1,40 @@ +package nia.chapter1; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * Created by kerr. + * + * Listing 1.1 Blocking I/O example + */ +public class BlockingIoExample { + + /** + * Listing 1.1 Blocking I/O example + * */ + public void serve(int portNumber) throws IOException { + ServerSocket serverSocket = new ServerSocket(portNumber); + Socket clientSocket = serverSocket.accept(); + BufferedReader in = new BufferedReader( + new InputStreamReader(clientSocket.getInputStream())); + PrintWriter out = + new PrintWriter(clientSocket.getOutputStream(), true); + String request, response; + while ((request = in.readLine()) != null) { + if ("Done".equals(request)) { + break; + } + response = processRequest(request); + out.println(response); + } + } + + private String processRequest(String request){ + return "Processed"; + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/ConnectExample.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/ConnectExample.java" new file mode 100644 index 0000000..fa3ab7c --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/ConnectExample.java" @@ -0,0 +1,50 @@ +package nia.chapter1; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.socket.nio.NioSocketChannel; + +import java.net.InetSocketAddress; +import java.nio.charset.Charset; + +/** + * Created by kerr. + * + * Listing 1.3 Asynchronous connect + * + * Listing 1.4 Callback in action + */ +public class ConnectExample { + private static final Channel CHANNEL_FROM_SOMEWHERE = new NioSocketChannel(); + + /** + * Listing 1.3 Asynchronous connect + * + * Listing 1.4 Callback in action + * */ + public static void connect() { + Channel channel = CHANNEL_FROM_SOMEWHERE; //reference form somewhere + // Does not block + ChannelFuture future = channel.connect( + new InetSocketAddress("192.168.0.1", 25)); + future.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) { + if (future.isSuccess()) { + ByteBuf buffer = Unpooled.copiedBuffer( + "Hello", Charset.defaultCharset()); + ChannelFuture wf = future.channel() + .writeAndFlush(buffer); + // ... + } else { + Throwable cause = future.cause(); + cause.printStackTrace(); + } + } + }); + + } +} \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/ConnectHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/ConnectHandler.java" new file mode 100644 index 0000000..01313d7 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/ConnectHandler.java" @@ -0,0 +1,18 @@ +package nia.chapter1; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +/** + * Created by kerr. + * + * Listing 1.2 ChannelHandler triggered by a callback + */ +public class ConnectHandler extends ChannelInboundHandlerAdapter { + @Override + public void channelActive(ChannelHandlerContext ctx) + throws Exception { + System.out.println( + "Client " + ctx.channel().remoteAddress() + " connected"); + } +} \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/package-info.java" new file mode 100644 index 0000000..8b659ac --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter1/src/main/java/nia/chapter1/package-info.java" @@ -0,0 +1,12 @@ +/** + * kerr. + * + * Listing 1.1 Blocking I/O example {@link nia.chapter1.BlockingIoExample#serve(int)} + * + * Listing 1.2 ChannelHandler triggered by a callback {@link nia.chapter1.ConnectHandler} + * + * Listing 1.3 Asynchronous connect {@link nia.chapter1.ConnectExample#connect()} + * + * Listing 1.4 Callback in action {@link nia.chapter1.ConnectExample#connect()} + */ +package nia.chapter1; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/pom.xml" new file mode 100644 index 0000000..2a6b888 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/pom.xml" @@ -0,0 +1,15 @@ + + + 4.0.0 + + nia + nia-samples-parent + 2.0-SNAPSHOT + ../pom.xml + + + chapter10 + Chapter 10: The codec framework + An overview of decoders, encoders and codecs + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ByteToCharDecoder.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ByteToCharDecoder.java" new file mode 100644 index 0000000..6a9793f --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ByteToCharDecoder.java" @@ -0,0 +1,23 @@ +package nia.chapter10; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + +import java.util.List; + +/** + * Listing 10.8 Class ByteToCharDecoder + * + * @author Norman Maurer + */ +public class ByteToCharDecoder extends ByteToMessageDecoder { + @Override + public void decode(ChannelHandlerContext ctx, ByteBuf in, + List out) throws Exception { + if (in.readableBytes() >= 2) { + out.add(in.readChar()); + } + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/CharToByteEncoder.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/CharToByteEncoder.java" new file mode 100644 index 0000000..966b658 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/CharToByteEncoder.java" @@ -0,0 +1,20 @@ +package nia.chapter10; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; + +/** + * Listing 10.9 Class CharToByteEncoder + * + * @author Norman Maurer + */ +public class CharToByteEncoder extends + MessageToByteEncoder { + @Override + public void encode(ChannelHandlerContext ctx, Character msg, + ByteBuf out) throws Exception { + out.writeChar(msg); + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/CombinedByteCharCodec.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/CombinedByteCharCodec.java" new file mode 100644 index 0000000..5068df5 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/CombinedByteCharCodec.java" @@ -0,0 +1,16 @@ +package nia.chapter10; + +import io.netty.channel.CombinedChannelDuplexHandler; + +/** + * Listing 10.10 CombinedChannelDuplexHandler + * + * @author Norman Maurer + */ + +public class CombinedByteCharCodec extends + CombinedChannelDuplexHandler { + public CombinedByteCharCodec() { + super(new ByteToCharDecoder(), new CharToByteEncoder()); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/IntegerToStringDecoder.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/IntegerToStringDecoder.java" new file mode 100644 index 0000000..bccbf69 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/IntegerToStringDecoder.java" @@ -0,0 +1,21 @@ +package nia.chapter10; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageDecoder; + +import java.util.List; + +/** + * Listing 10.3 Class IntegerToStringDecoder + * + * @author Norman Maurer + */ +public class IntegerToStringDecoder extends + MessageToMessageDecoder { + @Override + public void decode(ChannelHandlerContext ctx, Integer msg, + List out) throws Exception { + out.add(String.valueOf(msg)); + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/IntegerToStringEncoder.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/IntegerToStringEncoder.java" new file mode 100644 index 0000000..6c6390d --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/IntegerToStringEncoder.java" @@ -0,0 +1,21 @@ +package nia.chapter10; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageEncoder; + +import java.util.List; + +/** + * Listing 10.6 Class IntegerToStringEncoder + * + * @author Norman Maurer + */ +public class IntegerToStringEncoder + extends MessageToMessageEncoder { + @Override + public void encode(ChannelHandlerContext ctx, Integer msg, + List out) throws Exception { + out.add(String.valueOf(msg)); + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/SafeByteToMessageDecoder.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/SafeByteToMessageDecoder.java" new file mode 100644 index 0000000..6328cd2 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/SafeByteToMessageDecoder.java" @@ -0,0 +1,29 @@ +package nia.chapter10; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; +import io.netty.handler.codec.TooLongFrameException; + +import java.util.List; + +/** + * Listing 10.4 TooLongFrameException + * + * @author Norman Maurer + */ + +public class SafeByteToMessageDecoder extends ByteToMessageDecoder { + private static final int MAX_FRAME_SIZE = 1024; + @Override + public void decode(ChannelHandlerContext ctx, ByteBuf in, + List out) throws Exception { + int readable = in.readableBytes(); + if (readable > MAX_FRAME_SIZE) { + in.skipBytes(readable); + throw new TooLongFrameException("Frame too big!"); + } + // do something + // ... + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ShortToByteEncoder.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ShortToByteEncoder.java" new file mode 100644 index 0000000..51e5b80 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ShortToByteEncoder.java" @@ -0,0 +1,18 @@ +package nia.chapter10; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; + +/** + * Listing 10.5 Class ShortToByteEncoder + * + * @author Norman Maurer + */ +public class ShortToByteEncoder extends MessageToByteEncoder { + @Override + public void encode(ChannelHandlerContext ctx, Short msg, ByteBuf out) + throws Exception { + out.writeShort(msg); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ToIntegerDecoder.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ToIntegerDecoder.java" new file mode 100644 index 0000000..48235e7 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ToIntegerDecoder.java" @@ -0,0 +1,23 @@ +package nia.chapter10; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + +import java.util.List; + +/** + * Listing 10.1 Class ToIntegerDecoder extends ByteToMessageDecoder + * + * @author Norman Maurer + */ +public class ToIntegerDecoder extends ByteToMessageDecoder { + @Override + public void decode(ChannelHandlerContext ctx, ByteBuf in, + List out) throws Exception { + if (in.readableBytes() >= 4) { + out.add(in.readInt()); + } + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ToIntegerDecoder2.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ToIntegerDecoder2.java" new file mode 100644 index 0000000..52a0157 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/ToIntegerDecoder2.java" @@ -0,0 +1,22 @@ +package nia.chapter10; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ReplayingDecoder; + +import java.util.List; + +/** + * Listing 10.2 Class ToIntegerDecoder2 extends ReplayingDecoder + * + * @author Norman Maurer + */ +public class ToIntegerDecoder2 extends ReplayingDecoder { + + @Override + public void decode(ChannelHandlerContext ctx, ByteBuf in, + List out) throws Exception { + out.add(in.readInt()); + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/WebSocketConvertHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/WebSocketConvertHandler.java" new file mode 100644 index 0000000..dc5a3f5 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/WebSocketConvertHandler.java" @@ -0,0 +1,107 @@ +package nia.chapter10; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageCodec; +import io.netty.handler.codec.http.websocketx.*; + +import java.util.List; + +/** + * Listing 10.7 Using MessageToMessageCodec + * + * @author Norman Maurer + */ +@Sharable +public class WebSocketConvertHandler extends + MessageToMessageCodec { + @Override + protected void encode(ChannelHandlerContext ctx, + WebSocketConvertHandler.MyWebSocketFrame msg, + List out) throws Exception { + ByteBuf payload = msg.getData().duplicate().retain(); + switch (msg.getType()) { + case BINARY: + out.add(new BinaryWebSocketFrame(payload)); + break; + case TEXT: + out.add(new TextWebSocketFrame(payload)); + break; + case CLOSE: + out.add(new CloseWebSocketFrame(true, 0, payload)); + break; + case CONTINUATION: + out.add(new ContinuationWebSocketFrame(payload)); + break; + case PONG: + out.add(new PongWebSocketFrame(payload)); + break; + case PING: + out.add(new PingWebSocketFrame(payload)); + break; + default: + throw new IllegalStateException( + "Unsupported websocket msg " + msg);} + } + + @Override + protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg, + List out) throws Exception { + ByteBuf payload = msg.content().duplicate().retain(); + if (msg instanceof BinaryWebSocketFrame) { + out.add(new MyWebSocketFrame( + MyWebSocketFrame.FrameType.BINARY, payload)); + } else + if (msg instanceof CloseWebSocketFrame) { + out.add(new MyWebSocketFrame ( + MyWebSocketFrame.FrameType.CLOSE, payload)); + } else + if (msg instanceof PingWebSocketFrame) { + out.add(new MyWebSocketFrame ( + MyWebSocketFrame.FrameType.PING, payload)); + } else + if (msg instanceof PongWebSocketFrame) { + out.add(new MyWebSocketFrame ( + MyWebSocketFrame.FrameType.PONG, payload)); + } else + if (msg instanceof TextWebSocketFrame) { + out.add(new MyWebSocketFrame ( + MyWebSocketFrame.FrameType.TEXT, payload)); + } else + if (msg instanceof ContinuationWebSocketFrame) { + out.add(new MyWebSocketFrame ( + MyWebSocketFrame.FrameType.CONTINUATION, payload)); + } else + { + throw new IllegalStateException( + "Unsupported websocket msg " + msg); + } + } + + public static final class MyWebSocketFrame { + public enum FrameType { + BINARY, + CLOSE, + PING, + PONG, + TEXT, + CONTINUATION + } + private final FrameType type; + private final ByteBuf data; + + public MyWebSocketFrame(FrameType type, ByteBuf data) { + this.type = type; + this.data = data; + } + public FrameType getType() { + return type; + } + public ByteBuf getData() { + return data; + } + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/package-info.java" new file mode 100644 index 0000000..d5f47fa --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter10/src/main/java/nia/chapter10/package-info.java" @@ -0,0 +1,24 @@ +/** + * Created by kerr. + * + * Listing 10.1 Class ToIntegerDecoder extends ByteToMessageDecoder {@link nia.chapter10.ToIntegerDecoder} + * + * Listing 10.2 Class ToIntegerDecoder2 extends ReplayingDecoder {@link nia.chapter10.ToIntegerDecoder2} + * + * Listing 10.3 Class IntegerToStringDecoder {@link nia.chapter10.IntegerToStringDecoder} + * + * Listing 10.4 TooLongFrameException {@link nia.chapter10.SafeByteToMessageDecoder} + * + * Listing 10.5 Class ShortToByteEncoder {@link nia.chapter10.ShortToByteEncoder} + * + * Listing 10.6 Class IntegerToStringEncoder {@link nia.chapter10.IntegerToStringEncoder} + * + * Listing 10.7 Using MessageToMessageCodec {@link nia.chapter10.WebSocketConvertHandler} + * + * Listing 10.8 Class ByteToCharDecoder {@link nia.chapter10.ByteToCharDecoder} + * + * Listing 10.9 Class CharToByteEncoder {@link nia.chapter10.CharToByteEncoder} + * + * Listing 10.10 CombinedChannelDuplexHandler {@link nia.chapter10.CombinedByteCharCodec} + */ +package nia.chapter10; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/pom.xml" new file mode 100644 index 0000000..fd51c61 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/pom.xml" @@ -0,0 +1,25 @@ + + + 4.0.0 + + nia + nia-samples-parent + 2.0-SNAPSHOT + ../pom.xml + + + chapter11 + Chapter 11. Provided ChannelHandlers and codecs + + 2.5.0 + + + + com.google.protobuf + protobuf-java + ${protobuf-java.version} + + + + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/ChunkedWriteHandlerInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/ChunkedWriteHandlerInitializer.java" new file mode 100644 index 0000000..fcde7c2 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/ChunkedWriteHandlerInitializer.java" @@ -0,0 +1,45 @@ +package nia.chapter11; + +import io.netty.channel.*; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslHandler; +import io.netty.handler.stream.ChunkedStream; +import io.netty.handler.stream.ChunkedWriteHandler; + +import java.io.File; +import java.io.FileInputStream; + +/** + * Listing 11.12 of Netty in Action + * + * @author Norman Maurer + */ +public class ChunkedWriteHandlerInitializer + extends ChannelInitializer { + private final File file; + private final SslContext sslCtx; + public ChunkedWriteHandlerInitializer(File file, SslContext sslCtx) { + this.file = file; + this.sslCtx = sslCtx; + } + + @Override + protected void initChannel(Channel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + pipeline.addLast(new SslHandler(sslCtx.newEngine(ch.alloc()))); + pipeline.addLast(new ChunkedWriteHandler()); + pipeline.addLast(new WriteStreamHandler()); + } + + public final class WriteStreamHandler + extends ChannelInboundHandlerAdapter { + + @Override + public void channelActive(ChannelHandlerContext ctx) + throws Exception { + super.channelActive(ctx); + ctx.writeAndFlush( + new ChunkedStream(new FileInputStream(file))); + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/CmdHandlerInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/CmdHandlerInitializer.java" new file mode 100644 index 0000000..1ab893f --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/CmdHandlerInitializer.java" @@ -0,0 +1,66 @@ +package nia.chapter11; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.*; +import io.netty.handler.codec.LineBasedFrameDecoder; + +/** + * Listing 11.9 Using a ChannelInitializer as a decoder installer + * + * @author Norman Maurer + */ +public class CmdHandlerInitializer extends ChannelInitializer { + private static final byte SPACE = (byte)' '; + @Override + protected void initChannel(Channel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + pipeline.addLast(new CmdDecoder(64 * 1024)); + pipeline.addLast(new CmdHandler()); + } + + public static final class Cmd { + private final ByteBuf name; + private final ByteBuf args; + + public Cmd(ByteBuf name, ByteBuf args) { + this.name = name; + this.args = args; + } + + public ByteBuf name() { + return name; + } + + public ByteBuf args() { + return args; + } + } + + public static final class CmdDecoder extends LineBasedFrameDecoder { + public CmdDecoder(int maxLength) { + super(maxLength); + } + + @Override + protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) + throws Exception { + ByteBuf frame = (ByteBuf) super.decode(ctx, buffer); + if (frame == null) { + return null; + } + int index = frame.indexOf(frame.readerIndex(), + frame.writerIndex(), SPACE); + return new Cmd(frame.slice(frame.readerIndex(), index), + frame.slice(index + 1, frame.writerIndex())); + } + } + + public static final class CmdHandler + extends SimpleChannelInboundHandler { + @Override + public void channelRead0(ChannelHandlerContext ctx, Cmd msg) + throws Exception { + // Do something with the command + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/FileRegionWriteHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/FileRegionWriteHandler.java" new file mode 100644 index 0000000..13f65b3 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/FileRegionWriteHandler.java" @@ -0,0 +1,38 @@ +package nia.chapter11; + +import io.netty.channel.*; +import io.netty.channel.socket.nio.NioSocketChannel; + +import java.io.File; +import java.io.FileInputStream; + +/** + * Created by kerr. + * + * Listing 11.11 Transferring file contents with FileRegion + */ +public class FileRegionWriteHandler extends ChannelInboundHandlerAdapter { + private static final Channel CHANNEL_FROM_SOMEWHERE = new NioSocketChannel(); + private static final File FILE_FROM_SOMEWHERE = new File(""); + + @Override + public void channelActive(final ChannelHandlerContext ctx) throws Exception { + File file = FILE_FROM_SOMEWHERE; //get reference from somewhere + Channel channel = CHANNEL_FROM_SOMEWHERE; //get reference from somewhere + //... + FileInputStream in = new FileInputStream(file); + FileRegion region = new DefaultFileRegion( + in.getChannel(), 0, file.length()); + channel.writeAndFlush(region).addListener( + new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) + throws Exception { + if (!future.isSuccess()) { + Throwable cause = future.cause(); + // Do something + } + } + }); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpAggregatorInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpAggregatorInitializer.java" new file mode 100644 index 0000000..8394419 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpAggregatorInitializer.java" @@ -0,0 +1,33 @@ +package nia.chapter11; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.codec.http.HttpClientCodec; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpServerCodec; + +/** + * Listing 11.3 Automatically aggregating HTTP message fragments + * + * @author Norman Maurer + */ +public class HttpAggregatorInitializer extends ChannelInitializer { + private final boolean isClient; + + public HttpAggregatorInitializer(boolean isClient) { + this.isClient = isClient; + } + + @Override + protected void initChannel(Channel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + if (isClient) { + pipeline.addLast("codec", new HttpClientCodec()); + } else { + pipeline.addLast("codec", new HttpServerCodec()); + } + pipeline.addLast("aggregator", + new HttpObjectAggregator(512 * 1024)); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpCompressionInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpCompressionInitializer.java" new file mode 100644 index 0000000..9c6ece2 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpCompressionInitializer.java" @@ -0,0 +1,36 @@ +package nia.chapter11; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.codec.http.HttpClientCodec; +import io.netty.handler.codec.http.HttpContentCompressor; +import io.netty.handler.codec.http.HttpContentDecompressor; +import io.netty.handler.codec.http.HttpServerCodec; + +/** + * Listing 11.4 Automatically compressing HTTP messages + * + * @author Norman Maurer + */ +public class HttpCompressionInitializer extends ChannelInitializer { + private final boolean isClient; + + public HttpCompressionInitializer(boolean isClient) { + this.isClient = isClient; + } + + @Override + protected void initChannel(Channel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + if (isClient) { + pipeline.addLast("codec", new HttpClientCodec()); + pipeline.addLast("decompressor", + new HttpContentDecompressor()); + } else { + pipeline.addLast("codec", new HttpServerCodec()); + pipeline.addLast("compressor", + new HttpContentCompressor()); + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpPipelineInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpPipelineInitializer.java" new file mode 100644 index 0000000..aa9afd9 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpPipelineInitializer.java" @@ -0,0 +1,35 @@ +package nia.chapter11; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.codec.http.HttpRequestDecoder; +import io.netty.handler.codec.http.HttpRequestEncoder; +import io.netty.handler.codec.http.HttpResponseDecoder; +import io.netty.handler.codec.http.HttpResponseEncoder; + +/** + * Listing 11.2 Adding support for HTTP + * + * @author Norman Maurer + */ + +public class HttpPipelineInitializer extends ChannelInitializer { + private final boolean client; + + public HttpPipelineInitializer(boolean client) { + this.client = client; + } + + @Override + protected void initChannel(Channel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + if (client) { + pipeline.addLast("decoder", new HttpResponseDecoder()); + pipeline.addLast("encoder", new HttpRequestEncoder()); + } else { + pipeline.addLast("decoder", new HttpRequestDecoder()); + pipeline.addLast("encoder", new HttpResponseEncoder()); + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpsCodecInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpsCodecInitializer.java" new file mode 100644 index 0000000..78e1f7a --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/HttpsCodecInitializer.java" @@ -0,0 +1,39 @@ +package nia.chapter11; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.codec.http.HttpClientCodec; +import io.netty.handler.codec.http.HttpServerCodec; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslHandler; + +import javax.net.ssl.SSLEngine; + +/** + * Listing 11.5 Using HTTPS + * + * @author Norman Maurer + */ +public class HttpsCodecInitializer extends ChannelInitializer { + private final SslContext context; + private final boolean isClient; + + public HttpsCodecInitializer(SslContext context, boolean isClient) { + this.context = context; + this.isClient = isClient; + } + + @Override + protected void initChannel(Channel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + SSLEngine engine = context.newEngine(ch.alloc()); + pipeline.addFirst("ssl", new SslHandler(engine)); + + if (isClient) { + pipeline.addLast("codec", new HttpClientCodec()); + } else { + pipeline.addLast("codec", new HttpServerCodec()); + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/IdleStateHandlerInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/IdleStateHandlerInitializer.java" new file mode 100644 index 0000000..f00b8e0 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/IdleStateHandlerInitializer.java" @@ -0,0 +1,44 @@ +package nia.chapter11; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.*; +import io.netty.handler.timeout.IdleStateEvent; +import io.netty.handler.timeout.IdleStateHandler; +import io.netty.util.CharsetUtil; + +import java.util.concurrent.TimeUnit; + +/** + * Listing 11.7 Sending heartbeats + * + * @author Norman Maurer + */ +public class IdleStateHandlerInitializer extends ChannelInitializer + { + @Override + protected void initChannel(Channel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + pipeline.addLast( + new IdleStateHandler(0, 0, 60, TimeUnit.SECONDS)); + pipeline.addLast(new HeartbeatHandler()); + } + + public static final class HeartbeatHandler + extends ChannelInboundHandlerAdapter { + private static final ByteBuf HEARTBEAT_SEQUENCE = + Unpooled.unreleasableBuffer(Unpooled.copiedBuffer( + "HEARTBEAT", CharsetUtil.ISO_8859_1)); + @Override + public void userEventTriggered(ChannelHandlerContext ctx, + Object evt) throws Exception { + if (evt instanceof IdleStateEvent) { + ctx.writeAndFlush(HEARTBEAT_SEQUENCE.duplicate()) + .addListener( + ChannelFutureListener.CLOSE_ON_FAILURE); + } else { + super.userEventTriggered(ctx, evt); + } + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/LengthBasedInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/LengthBasedInitializer.java" new file mode 100644 index 0000000..a1d0fb1 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/LengthBasedInitializer.java" @@ -0,0 +1,29 @@ +package nia.chapter11; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.*; +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; + +/** + * Listing 11.10 Decoder for the command and the handler + * + * @author Norman Maurer + */ +public class LengthBasedInitializer extends ChannelInitializer { + @Override + protected void initChannel(Channel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + pipeline.addLast( + new LengthFieldBasedFrameDecoder(64 * 1024, 0, 8)); + pipeline.addLast(new FrameHandler()); + } + + public static final class FrameHandler + extends SimpleChannelInboundHandler { + @Override + public void channelRead0(ChannelHandlerContext ctx, + ByteBuf msg) throws Exception { + // Do something with the frame + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/LineBasedHandlerInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/LineBasedHandlerInitializer.java" new file mode 100644 index 0000000..652d020 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/LineBasedHandlerInitializer.java" @@ -0,0 +1,29 @@ +package nia.chapter11; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.*; +import io.netty.handler.codec.LineBasedFrameDecoder; + +/** + * Listing 11.8 Handling line-delimited frames + * + * @author Norman Maurer + */ +public class LineBasedHandlerInitializer extends ChannelInitializer + { + @Override + protected void initChannel(Channel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + pipeline.addLast(new LineBasedFrameDecoder(64 * 1024)); + pipeline.addLast(new FrameHandler()); + } + + public static final class FrameHandler + extends SimpleChannelInboundHandler { + @Override + public void channelRead0(ChannelHandlerContext ctx, + ByteBuf msg) throws Exception { + // Do something with the data extracted from the frame + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/MarshallingInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/MarshallingInitializer.java" new file mode 100644 index 0000000..c027a62 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/MarshallingInitializer.java" @@ -0,0 +1,44 @@ +package nia.chapter11; + +import io.netty.channel.*; +import io.netty.handler.codec.marshalling.MarshallerProvider; +import io.netty.handler.codec.marshalling.MarshallingDecoder; +import io.netty.handler.codec.marshalling.MarshallingEncoder; +import io.netty.handler.codec.marshalling.UnmarshallerProvider; + +import java.io.Serializable; + +/** + * Listing 11.13 Using JBoss Marshalling + * + * @author Norman Maurer + */ +public class MarshallingInitializer extends ChannelInitializer { + private final MarshallerProvider marshallerProvider; + private final UnmarshallerProvider unmarshallerProvider; + + public MarshallingInitializer( + UnmarshallerProvider unmarshallerProvider, + MarshallerProvider marshallerProvider) { + this.marshallerProvider = marshallerProvider; + this.unmarshallerProvider = unmarshallerProvider; + } + + @Override + protected void initChannel(Channel channel) throws Exception { + ChannelPipeline pipeline = channel.pipeline(); + pipeline.addLast(new MarshallingDecoder(unmarshallerProvider)); + pipeline.addLast(new MarshallingEncoder(marshallerProvider)); + pipeline.addLast(new ObjectHandler()); + } + + public static final class ObjectHandler + extends SimpleChannelInboundHandler { + @Override + public void channelRead0( + ChannelHandlerContext channelHandlerContext, + Serializable serializable) throws Exception { + // Do something + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/ProtoBufInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/ProtoBufInitializer.java" new file mode 100644 index 0000000..e5b77e0 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/ProtoBufInitializer.java" @@ -0,0 +1,38 @@ +package nia.chapter11; + +import com.google.protobuf.MessageLite; +import io.netty.channel.*; +import io.netty.handler.codec.protobuf.ProtobufDecoder; +import io.netty.handler.codec.protobuf.ProtobufEncoder; +import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder; + +/** + * Listing 11.14 Using protobuf + * + * @author Norman Maurer + */ +public class ProtoBufInitializer extends ChannelInitializer { + private final MessageLite lite; + + public ProtoBufInitializer(MessageLite lite) { + this.lite = lite; + } + + @Override + protected void initChannel(Channel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + pipeline.addLast(new ProtobufVarint32FrameDecoder()); + pipeline.addLast(new ProtobufEncoder()); + pipeline.addLast(new ProtobufDecoder(lite)); + pipeline.addLast(new ObjectHandler()); + } + + public static final class ObjectHandler + extends SimpleChannelInboundHandler { + @Override + public void channelRead0(ChannelHandlerContext ctx, Object msg) + throws Exception { + // Do something with the object + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/SslChannelInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/SslChannelInitializer.java" new file mode 100644 index 0000000..55f95aa --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/SslChannelInitializer.java" @@ -0,0 +1,30 @@ +package nia.chapter11; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslHandler; + +import javax.net.ssl.SSLEngine; + +/** + * Listing 11.1 Adding SSL/TLS support + * + * @author Norman Maurer + */ +public class SslChannelInitializer extends ChannelInitializer { + private final SslContext context; + private final boolean startTls; + + public SslChannelInitializer(SslContext context, + boolean startTls) { + this.context = context; + this.startTls = startTls; + } + @Override + protected void initChannel(Channel ch) throws Exception { + SSLEngine engine = context.newEngine(ch.alloc()); + ch.pipeline().addFirst("ssl", + new SslHandler(engine, startTls)); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/WebSocketServerInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/WebSocketServerInitializer.java" new file mode 100644 index 0000000..cbced5e --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/WebSocketServerInitializer.java" @@ -0,0 +1,57 @@ +package nia.chapter11; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpServerCodec; +import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame; +import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame; +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; +import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; + +/** + * Listing 11.6 Supporting WebSocket on the server + * + * @author Norman Maurer + */ +public class WebSocketServerInitializer extends ChannelInitializer { + @Override + protected void initChannel(Channel ch) throws Exception { + ch.pipeline().addLast( + new HttpServerCodec(), + new HttpObjectAggregator(65536), + new WebSocketServerProtocolHandler("/websocket"), + new TextFrameHandler(), + new BinaryFrameHandler(), + new ContinuationFrameHandler()); + } + + public static final class TextFrameHandler extends + SimpleChannelInboundHandler { + @Override + public void channelRead0(ChannelHandlerContext ctx, + TextWebSocketFrame msg) throws Exception { + // Handle text frame + } + } + + public static final class BinaryFrameHandler extends + SimpleChannelInboundHandler { + @Override + public void channelRead0(ChannelHandlerContext ctx, + BinaryWebSocketFrame msg) throws Exception { + // Handle binary frame + } + } + + public static final class ContinuationFrameHandler extends + SimpleChannelInboundHandler { + @Override + public void channelRead0(ChannelHandlerContext ctx, + ContinuationWebSocketFrame msg) throws Exception { + // Handle continuation frame + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/package-info.java" new file mode 100644 index 0000000..1f9c3a4 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter11/src/main/java/nia/chapter11/package-info.java" @@ -0,0 +1,32 @@ +/** + * Created by kerr. + * + * Listing 11.1 Adding SSL/TLS support {@link nia.chapter11.SslChannelInitializer} + * + * Listing 11.2 Adding support for HTTP {@link nia.chapter11.HttpPipelineInitializer} + * + * Listing 11.3 Automatically aggregating HTTP message fragments {@link nia.chapter11.HttpAggregatorInitializer} + * + * Listing 11.4 Automatically compressing HTTP messages {@link nia.chapter11.HttpCompressionInitializer} + * + * Listing 11.5 Using HTTPS {@link nia.chapter11.HttpsCodecInitializer} + * + * Listing 11.6 Supporting WebSocket on the server {@link nia.chapter11.WebSocketServerInitializer} + * + * Listing 11.7 Sending heartbeats {@link nia.chapter11.IdleStateHandlerInitializer} + * + * Listing 11.8 Handling line-delimited frames {@link nia.chapter11.LineBasedHandlerInitializer} + * + * Listing 11.9 Using a ChannelInitializer as a decoder installer {@link nia.chapter11.CmdHandlerInitializer} + * + * Listing 11.10 Decoder for the command and the handler {@link nia.chapter11.LengthBasedInitializer} + * + * Listing 11.11 Transferring file contents with FileRegion {@link nia.chapter11.FileRegionWriteHandler} + * + * Listing 11.12 Transferring file contents with ChunkedStream {@link nia.chapter11.ChunkedWriteHandlerInitializer} + * + * Listing 11.13 Using JBoss Marshalling {@link nia.chapter11.MarshallingInitializer} + * + * Listing 11.14 Using protobuf {@link nia.chapter11.ProtoBufInitializer} + */ +package nia.chapter11; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/pom.xml" new file mode 100644 index 0000000..f22a818 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/pom.xml" @@ -0,0 +1,76 @@ + + + 4.0.0 + + nia + nia-samples-parent + 2.0-SNAPSHOT + ../pom.xml + + + chapter12 + Chapter 12. WebSocket + A Chat Server to demonstrate WebSocket protocol + + + + + maven-compiler-plugin + + + + + + + ChatServer + + nia.chapter12.ChatServer + 8888 + + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + java + + -classpath + + ${mainClass} + ${port} + + + + + + + + SecureChatServer + + nia.chapter12.SecureChatServer + 8888 + + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + java + + -classpath + + ${mainClass} + ${port} + + + + + + + + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/ChatServer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/ChatServer.java" new file mode 100644 index 0000000..837e581 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/ChatServer.java" @@ -0,0 +1,68 @@ +package nia.chapter12; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.group.ChannelGroup; +import io.netty.channel.group.DefaultChannelGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.util.concurrent.ImmediateEventExecutor; + +import java.net.InetSocketAddress; + +/** + * Listing 12.4 Bootstrapping the server + * + * @author Norman Maurer + */ +public class ChatServer { + private final ChannelGroup channelGroup = + new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE); + private final EventLoopGroup group = new NioEventLoopGroup(); + private Channel channel; + + public ChannelFuture start(InetSocketAddress address) { + ServerBootstrap bootstrap = new ServerBootstrap(); + bootstrap.group(group) + .channel(NioServerSocketChannel.class) + .childHandler(createInitializer(channelGroup)); + ChannelFuture future = bootstrap.bind(address); + future.syncUninterruptibly(); + channel = future.channel(); + return future; + } + + protected ChannelInitializer createInitializer( + ChannelGroup group) { + return new ChatServerInitializer(group); + } + + public void destroy() { + if (channel != null) { + channel.close(); + } + channelGroup.close(); + group.shutdownGracefully(); + } + + public static void main(String[] args) throws Exception { + if (args.length != 1) { + System.err.println("Please give port as argument"); + System.exit(1); + } + int port = Integer.parseInt(args[0]); + final ChatServer endpoint = new ChatServer(); + ChannelFuture future = endpoint.start( + new InetSocketAddress(port)); + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + endpoint.destroy(); + } + }); + future.channel().closeFuture().syncUninterruptibly(); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/ChatServerInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/ChatServerInitializer.java" new file mode 100644 index 0000000..3316a8d --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/ChatServerInitializer.java" @@ -0,0 +1,34 @@ +package nia.chapter12; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.group.ChannelGroup; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpServerCodec; +import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; +import io.netty.handler.stream.ChunkedWriteHandler; + +/** + * Listing 12.3 Initializing the ChannelPipeline + * + * @author Norman Maurer + */ +public class ChatServerInitializer extends ChannelInitializer { + private final ChannelGroup group; + + public ChatServerInitializer(ChannelGroup group) { + this.group = group; + } + + @Override + protected void initChannel(Channel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + pipeline.addLast(new HttpServerCodec()); + pipeline.addLast(new ChunkedWriteHandler()); + pipeline.addLast(new HttpObjectAggregator(64 * 1024)); + pipeline.addLast(new HttpRequestHandler("/ws")); + pipeline.addLast(new WebSocketServerProtocolHandler("/ws")); + pipeline.addLast(new TextWebSocketFrameHandler(group)); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/HttpRequestHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/HttpRequestHandler.java" new file mode 100644 index 0000000..cd67de6 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/HttpRequestHandler.java" @@ -0,0 +1,88 @@ +package nia.chapter12; + +import io.netty.channel.*; +import io.netty.handler.codec.http.*; +import io.netty.handler.ssl.SslHandler; +import io.netty.handler.stream.ChunkedNioFile; + +import java.io.File; +import java.io.RandomAccessFile; +import java.net.URISyntaxException; +import java.net.URL; + +/** + * Listing 12.1 HTTPRequestHandler + * + * @author Norman Maurer + */ +public class HttpRequestHandler extends SimpleChannelInboundHandler { + private final String wsUri; + private static final File INDEX; + + static { + URL location = HttpRequestHandler.class + .getProtectionDomain() + .getCodeSource().getLocation(); + try { + String path = location.toURI() + "index.html"; + path = !path.contains("file:") ? path : path.substring(5); + INDEX = new File(path); + } catch (URISyntaxException e) { + throw new IllegalStateException( + "Unable to locate index.html", e); + } + } + + public HttpRequestHandler(String wsUri) { + this.wsUri = wsUri; + } + + @Override + public void channelRead0(ChannelHandlerContext ctx, + FullHttpRequest request) throws Exception { + if (wsUri.equalsIgnoreCase(request.getUri())) { + ctx.fireChannelRead(request.retain()); + } else { + if (HttpHeaders.is100ContinueExpected(request)) { + send100Continue(ctx); + } + RandomAccessFile file = new RandomAccessFile(INDEX, "r"); + HttpResponse response = new DefaultHttpResponse( + request.getProtocolVersion(), HttpResponseStatus.OK); + response.headers().set( + HttpHeaders.Names.CONTENT_TYPE, + "text/html; charset=UTF-8"); + boolean keepAlive = HttpHeaders.isKeepAlive(request); + if (keepAlive) { + response.headers().set( + HttpHeaders.Names.CONTENT_LENGTH, file.length()); + response.headers().set( HttpHeaders.Names.CONNECTION, + HttpHeaders.Values.KEEP_ALIVE); + } + ctx.write(response); + if (ctx.pipeline().get(SslHandler.class) == null) { + ctx.write(new DefaultFileRegion( + file.getChannel(), 0, file.length())); + } else { + ctx.write(new ChunkedNioFile(file.getChannel())); + } + ChannelFuture future = ctx.writeAndFlush( + LastHttpContent.EMPTY_LAST_CONTENT); + if (!keepAlive) { + future.addListener(ChannelFutureListener.CLOSE); + } + } + } + + private static void send100Continue(ChannelHandlerContext ctx) { + FullHttpResponse response = new DefaultFullHttpResponse( + HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE); + ctx.writeAndFlush(response); + } + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) + throws Exception { + cause.printStackTrace(); + ctx.close(); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/SecureChatServer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/SecureChatServer.java" new file mode 100644 index 0000000..a7779ed --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/SecureChatServer.java" @@ -0,0 +1,49 @@ +package nia.chapter12; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.group.ChannelGroup; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.util.SelfSignedCertificate; + +import java.net.InetSocketAddress; + +/** + * Listing 12.7 Adding encryption to the ChatServer + * + * @author Norman Maurer + */ +public class SecureChatServer extends ChatServer { + private final SslContext context; + + public SecureChatServer(SslContext context) { + this.context = context; + } + + @Override + protected ChannelInitializer createInitializer( + ChannelGroup group) { + return new SecureChatServerInitializer(group, context); + } + + public static void main(String[] args) throws Exception { + if (args.length != 1) { + System.err.println("Please give port as argument"); + System.exit(1); + } + int port = Integer.parseInt(args[0]); + SelfSignedCertificate cert = new SelfSignedCertificate(); + SslContext context = SslContext.newServerContext( + cert.certificate(), cert.privateKey()); + final SecureChatServer endpoint = new SecureChatServer(context); + ChannelFuture future = endpoint.start(new InetSocketAddress(port)); + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + endpoint.destroy(); + } + }); + future.channel().closeFuture().syncUninterruptibly(); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/SecureChatServerInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/SecureChatServerInitializer.java" new file mode 100644 index 0000000..a4b669a --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/SecureChatServerInitializer.java" @@ -0,0 +1,31 @@ +package nia.chapter12; + +import io.netty.channel.Channel; +import io.netty.channel.group.ChannelGroup; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslHandler; + +import javax.net.ssl.SSLEngine; + +/** + * Listing 12.6 Adding encryption to the ChannelPipeline + * + * @author Norman Maurer + */ +public class SecureChatServerInitializer extends ChatServerInitializer { + private final SslContext context; + + public SecureChatServerInitializer(ChannelGroup group, + SslContext context) { + super(group); + this.context = context; + } + + @Override + protected void initChannel(Channel ch) throws Exception { + super.initChannel(ch); + SSLEngine engine = context.newEngine(ch.alloc()); + engine.setUseClientMode(false); + ch.pipeline().addFirst(new SslHandler(engine)); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/TextWebSocketFrameHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/TextWebSocketFrameHandler.java" new file mode 100644 index 0000000..e781e9c --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/TextWebSocketFrameHandler.java" @@ -0,0 +1,41 @@ +package nia.chapter12; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.group.ChannelGroup; +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; +import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; + +/** + * Listing 12.2 Handling text frames + * + * @author Norman Maurer + */ +public class TextWebSocketFrameHandler + extends SimpleChannelInboundHandler { + private final ChannelGroup group; + + public TextWebSocketFrameHandler(ChannelGroup group) { + this.group = group; + } + + @Override + public void userEventTriggered(ChannelHandlerContext ctx, + Object evt) throws Exception { + if (evt == WebSocketServerProtocolHandler + .ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) { + ctx.pipeline().remove(HttpRequestHandler.class); + group.writeAndFlush(new TextWebSocketFrame( + "Client " + ctx.channel() + " joined")); + group.add(ctx.channel()); + } else { + super.userEventTriggered(ctx, evt); + } + } + + @Override + public void channelRead0(ChannelHandlerContext ctx, + TextWebSocketFrame msg) throws Exception { + group.writeAndFlush(msg.retain()); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/package-info.java" new file mode 100644 index 0000000..439b8f0 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/java/nia/chapter12/package-info.java" @@ -0,0 +1,17 @@ +/** + * Created by kerr. + * + * + * Listing 12.1 HTTPRequestHandler {@link nia.chapter12.HttpRequestHandler} + * + * Listing 12.2 Handling text frames {@link nia.chapter12.TextWebSocketFrameHandler} + * + * Listing 12.3 Initializing the ChannelPipeline {@link nia.chapter12.ChatServerInitializer} + * + * Listing 12.4 Bootstrapping the server {@link nia.chapter12.ChatServer} + * + * Listing 12.6 Adding encryption to the ChannelPipeline {@link nia.chapter12.SecureChatServerInitializer} + * + * Listing 12.7 Adding encryption to the ChatServer {@link nia.chapter12.SecureChatServer} + */ +package nia.chapter12; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/resources/index.html" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/resources/index.html" new file mode 100644 index 0000000..65ad646 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter12/src/main/resources/index.html" @@ -0,0 +1,115 @@ + + + + + + + + WebSocket ChatServer + + + + + + + + + + + + + + +
+
Enter a message below to send
+ + +
+ + + +
+
+


Instructions:

+ + + + + + + + + +
Step 1:  Press the Connect button.
Step 2:  Once connected, enter a message and press the Send button. The server's + response will + appear in the Log section. You can send as many messages as you like +
+
+ + + \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/pom.xml" new file mode 100644 index 0000000..9c378db --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/pom.xml" @@ -0,0 +1,69 @@ + + + 4.0.0 + + nia + nia-samples-parent + 2.0-SNAPSHOT + ../pom.xml + + + chapter13 + Chapter 13. Broadcasting Events with UDP + + + + LogEventBroadcaster + + nia.chapter13.LogEventBroadcaster + 9999 + /var/log/messages + + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + java + + -classpath + + ${mainClass} + ${port} + ${logfile} + + + + + + + + LogEventMonitor + + nia.chapter13.LogEventMonitor + 9999 + + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + java + + -classpath + + ${mainClass} + ${port} + + + + + + + + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEvent.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEvent.java" new file mode 100644 index 0000000..6b375d3 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEvent.java" @@ -0,0 +1,44 @@ +package nia.chapter13; + +import java.net.InetSocketAddress; + +/** + * Listing 13.1 LogEvent message + * + * @author Norman Maurer + */ +public final class LogEvent { + public static final byte SEPARATOR = (byte) ':'; + private final InetSocketAddress source; + private final String logfile; + private final String msg; + private final long received; + + public LogEvent(String logfile, String msg) { + this(null, -1, logfile, msg); + } + + public LogEvent(InetSocketAddress source, long received, + String logfile, String msg) { + this.source = source; + this.logfile = logfile; + this.msg = msg; + this.received = received; + } + + public InetSocketAddress getSource() { + return source; + } + + public String getLogfile() { + return logfile; + } + + public String getMsg() { + return msg; + } + + public long getReceivedTimestamp() { + return received; + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventBroadcaster.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventBroadcaster.java" new file mode 100644 index 0000000..8ba1a01 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventBroadcaster.java" @@ -0,0 +1,80 @@ +package nia.chapter13; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioDatagramChannel; + +import java.io.File; +import java.io.RandomAccessFile; +import java.net.InetSocketAddress; + +/** + * Listing 13.3 LogEventBroadcaster + * + * @author Norman Maurer + */ +public class LogEventBroadcaster { + private final EventLoopGroup group; + private final Bootstrap bootstrap; + private final File file; + + public LogEventBroadcaster(InetSocketAddress address, File file) { + group = new NioEventLoopGroup(); + bootstrap = new Bootstrap(); + bootstrap.group(group).channel(NioDatagramChannel.class) + .option(ChannelOption.SO_BROADCAST, true) + .handler(new LogEventEncoder(address)); + this.file = file; + } + + public void run() throws Exception { + Channel ch = bootstrap.bind(0).sync().channel(); + long pointer = 0; + for (;;) { + long len = file.length(); + if (len < pointer) { + // file was reset + pointer = len; + } else if (len > pointer) { + // Content was added + RandomAccessFile raf = new RandomAccessFile(file, "r"); + raf.seek(pointer); + String line; + while ((line = raf.readLine()) != null) { + ch.writeAndFlush(new LogEvent(null, -1, + file.getAbsolutePath(), line)); + } + pointer = raf.getFilePointer(); + raf.close(); + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + Thread.interrupted(); + break; + } + } + } + + public void stop() { + group.shutdownGracefully(); + } + + public static void main(String[] args) throws Exception { + if (args.length != 2) { + throw new IllegalArgumentException(); + } + LogEventBroadcaster broadcaster = new LogEventBroadcaster( + new InetSocketAddress("255.255.255.255", + Integer.parseInt(args[0])), new File(args[1])); + try { + broadcaster.run(); + } + finally { + broadcaster.stop(); + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventDecoder.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventDecoder.java" new file mode 100644 index 0000000..7abaaad --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventDecoder.java" @@ -0,0 +1,33 @@ +package nia.chapter13; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.socket.DatagramPacket; +import io.netty.handler.codec.MessageToMessageDecoder; +import io.netty.util.CharsetUtil; + +import java.util.List; + +/** + * Listing 13.6 LogEventDecoder + * + * @author Norman Maurer + */ +public class LogEventDecoder extends MessageToMessageDecoder { + + @Override + protected void decode(ChannelHandlerContext ctx, + DatagramPacket datagramPacket, List out) + throws Exception { + ByteBuf data = datagramPacket.content(); + int idx = data.indexOf(0, data.readableBytes(), + LogEvent.SEPARATOR); + String filename = data.slice(0, idx) + .toString(CharsetUtil.UTF_8); + String logMsg = data.slice(idx + 1, + data.readableBytes()).toString(CharsetUtil.UTF_8); + LogEvent event = new LogEvent(datagramPacket.sender(), + System.currentTimeMillis(), filename, logMsg); + out.add(event); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventEncoder.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventEncoder.java" new file mode 100644 index 0000000..ef02bb5 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventEncoder.java" @@ -0,0 +1,36 @@ +package nia.chapter13; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.socket.DatagramPacket; +import io.netty.handler.codec.MessageToMessageEncoder; +import io.netty.util.CharsetUtil; + +import java.net.InetSocketAddress; +import java.util.List; + +/** + * Listing 13.2 LogEventEncoder + * + * @author Norman Maurer + */ +public class LogEventEncoder extends MessageToMessageEncoder { + private final InetSocketAddress remoteAddress; + + public LogEventEncoder(InetSocketAddress remoteAddress) { + this.remoteAddress = remoteAddress; + } + + @Override + protected void encode(ChannelHandlerContext channelHandlerContext, + LogEvent logEvent, List out) throws Exception { + byte[] file = logEvent.getLogfile().getBytes(CharsetUtil.UTF_8); + byte[] msg = logEvent.getMsg().getBytes(CharsetUtil.UTF_8); + ByteBuf buf = channelHandlerContext.alloc() + .buffer(file.length + msg.length + 1); + buf.writeBytes(file); + buf.writeByte(LogEvent.SEPARATOR); + buf.writeBytes(msg); + out.add(new DatagramPacket(buf, remoteAddress)); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventHandler.java" new file mode 100644 index 0000000..cd447d8 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventHandler.java" @@ -0,0 +1,34 @@ +package nia.chapter13; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; + +/** + * Listing 13.7 LogEventHandler + * + * @author Norman Maurer + */ +public class LogEventHandler + extends SimpleChannelInboundHandler { + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, + Throwable cause) throws Exception { + cause.printStackTrace(); + ctx.close(); + } + + @Override + public void channelRead0(ChannelHandlerContext ctx, + LogEvent event) throws Exception { + StringBuilder builder = new StringBuilder(); + builder.append(event.getReceivedTimestamp()); + builder.append(" ["); + builder.append(event.getSource().toString()); + builder.append("] ["); + builder.append(event.getLogfile()); + builder.append("] : "); + builder.append(event.getMsg()); + System.out.println(builder.toString()); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventMonitor.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventMonitor.java" new file mode 100644 index 0000000..76ea2b5 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/LogEventMonitor.java" @@ -0,0 +1,60 @@ +package nia.chapter13; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioDatagramChannel; + +import java.net.InetSocketAddress; + +/** + * Listing 13.8 LogEventMonitor + * + * @author Norman Maurer + */ +public class LogEventMonitor { + private final EventLoopGroup group; + private final Bootstrap bootstrap; + + public LogEventMonitor(InetSocketAddress address) { + group = new NioEventLoopGroup(); + bootstrap = new Bootstrap(); + bootstrap.group(group) + .channel(NioDatagramChannel.class) + .option(ChannelOption.SO_BROADCAST, true) + .handler( new ChannelInitializer() { + @Override + protected void initChannel(Channel channel) + throws Exception { + ChannelPipeline pipeline = channel.pipeline(); + pipeline.addLast(new LogEventDecoder()); + pipeline.addLast(new LogEventHandler()); + } + } ) + .localAddress(address); + } + + public Channel bind() { + return bootstrap.bind().syncUninterruptibly().channel(); + } + + public void stop() { + group.shutdownGracefully(); + } + + public static void main(String[] args) throws Exception { + if (args.length != 1) { + throw new IllegalArgumentException( + "Usage: LogEventMonitor "); + } + LogEventMonitor monitor = new LogEventMonitor( + new InetSocketAddress(Integer.parseInt(args[0]))); + try { + Channel channel = monitor.bind(); + System.out.println("LogEventMonitor running"); + channel.closeFuture().sync(); + } finally { + monitor.stop(); + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/package-info.java" new file mode 100644 index 0000000..714ce19 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter13/src/main/java/nia/chapter13/package-info.java" @@ -0,0 +1,16 @@ +/** + * Created by kerr. + * + * Listing 13.1 LogEvent message {@link nia.chapter13.LogEvent} + * + * Listing 13.2 LogEventEncoder {@link nia.chapter13.LogEventEncoder} + * + * Listing 13.3 LogEventBroadcaster {@link nia.chapter13.LogEventBroadcaster} + * + * Listing 13.6 LogEventDecoder {@link nia.chapter13.LogEventDecoder} + * + * Listing 13.7 LogEventHandler {@link nia.chapter13.LogEventHandler} + * + * Listing 13.8 LogEventMonitor {@link nia.chapter13.LogEventMonitor} + */ +package nia.chapter13; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/pom.xml" new file mode 100644 index 0000000..0812e7d --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/pom.xml" @@ -0,0 +1,39 @@ + + + 4.0.0 + + + nia + chapter2 + 2.0-SNAPSHOT + + + echo-client + + Chapter 2. Echo Client + + + + + org.codehaus.mojo + exec-maven-plugin + + + run-client + + java + + + + + nia.chapter2.echoclient.EchoClient + + ${echo-server.hostname} + ${echo-server.port} + + + + + + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/java/nia/chapter2/echoclient/EchoClient.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/java/nia/chapter2/echoclient/EchoClient.java" new file mode 100644 index 0000000..82eaae9 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/java/nia/chapter2/echoclient/EchoClient.java" @@ -0,0 +1,64 @@ +package nia.chapter2.echoclient; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; + +import java.net.InetSocketAddress; + +/** + * Listing 2.4 Main class for the client + * + * @author Norman Maurer + */ +public class EchoClient { + private final String host; + private final int port; + + public EchoClient(String host, int port) { + this.host = host; + this.port = port; + } + + public void start() + throws Exception { + EventLoopGroup group = new NioEventLoopGroup(); + try { + Bootstrap b = new Bootstrap(); + b.group(group) + .channel(NioSocketChannel.class) + .remoteAddress(new InetSocketAddress(host, port)) + .handler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) + throws Exception { + ch.pipeline().addLast( + new EchoClientHandler()); + } + }); + ChannelFuture f = b.connect().sync(); + f.channel().closeFuture().sync(); + } finally { + group.shutdownGracefully().sync(); + } + } + + public static void main(String[] args) + throws Exception { + if (args.length != 2) { + System.err.println("Usage: " + EchoClient.class.getSimpleName() + + " " + ); + return; + } + + final String host = args[0]; + final int port = Integer.parseInt(args[1]); + new EchoClient(host, port).start(); + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/java/nia/chapter2/echoclient/EchoClientHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/java/nia/chapter2/echoclient/EchoClientHandler.java" new file mode 100644 index 0000000..687a431 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/java/nia/chapter2/echoclient/EchoClientHandler.java" @@ -0,0 +1,36 @@ +package nia.chapter2.echoclient; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.util.CharsetUtil; + +/** + * Listing 2.3 ChannelHandler for the client + * + * @author Norman Maurer + */ +@Sharable +public class EchoClientHandler + extends SimpleChannelInboundHandler { + @Override + public void channelActive(ChannelHandlerContext ctx) { + ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", + CharsetUtil.UTF_8)); + } + + @Override + public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) { + System.out.println( + "Client received: " + in.toString(CharsetUtil.UTF_8)); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, + Throwable cause) { + cause.printStackTrace(); + ctx.close(); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/java/nia/chapter2/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/java/nia/chapter2/package-info.java" new file mode 100644 index 0000000..6138c69 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/java/nia/chapter2/package-info.java" @@ -0,0 +1,8 @@ +/** + * Created by kerr. + * + * Listing 2.3 ChannelHandler for the client {@link nia.chapter2.echoclient.EchoClientHandler} + * + * Listing 2.4 Main class for the client {@link nia.chapter2.echoclient.EchoClient} + */ +package nia.chapter2; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/resources/.gitignore" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/resources/.gitignore" new file mode 100644 index 0000000..86d0cb2 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Client/src/main/resources/.gitignore" @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/pom.xml" new file mode 100644 index 0000000..c195f41 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/pom.xml" @@ -0,0 +1,38 @@ + + + 4.0.0 + + + nia + chapter2 + 2.0-SNAPSHOT + + + echo-server + + Chapter 2. Echo Server + + + + + org.codehaus.mojo + exec-maven-plugin + + + run-server + + java + + + + + nia.chapter2.echoserver.EchoServer + + ${echo-server.port} + + + + + + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/java/nia/chapter2/echoserver/EchoServer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/java/nia/chapter2/echoserver/EchoServer.java" new file mode 100644 index 0000000..398c6bc --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/java/nia/chapter2/echoserver/EchoServer.java" @@ -0,0 +1,60 @@ +package nia.chapter2.echoserver; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; + +import java.net.InetSocketAddress; + +/** + * Listing 2.2 EchoServer class + * + * @author Norman Maurer + */ +public class EchoServer { + private final int port; + + public EchoServer(int port) { + this.port = port; + } + + public static void main(String[] args) + throws Exception { + if (args.length != 1) { + System.err.println("Usage: " + EchoServer.class.getSimpleName() + + " " + ); + return; + } + int port = Integer.parseInt(args[0]); + new EchoServer(port).start(); + } + + public void start() throws Exception { + final EchoServerHandler serverHandler = new EchoServerHandler(); + EventLoopGroup group = new NioEventLoopGroup(); + try { + ServerBootstrap b = new ServerBootstrap(); + b.group(group) + .channel(NioServerSocketChannel.class) + .localAddress(new InetSocketAddress(port)) + .childHandler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(serverHandler); + } + }); + + ChannelFuture f = b.bind().sync(); + System.out.println(EchoServer.class.getName() + + " started and listening for connections on " + f.channel().localAddress()); + f.channel().closeFuture().sync(); + } finally { + group.shutdownGracefully().sync(); + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/java/nia/chapter2/echoserver/EchoServerHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/java/nia/chapter2/echoserver/EchoServerHandler.java" new file mode 100644 index 0000000..b528e95 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/java/nia/chapter2/echoserver/EchoServerHandler.java" @@ -0,0 +1,39 @@ +package nia.chapter2.echoserver; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.util.CharsetUtil; + +/** + * Listing 2.1 EchoServerHandler + * + * @author Norman Maurer + */ +@Sharable +public class EchoServerHandler extends ChannelInboundHandlerAdapter { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + ByteBuf in = (ByteBuf) msg; + System.out.println( + "Server received: " + in.toString(CharsetUtil.UTF_8)); + ctx.write(in); + } + + @Override + public void channelReadComplete(ChannelHandlerContext ctx) + throws Exception { + ctx.writeAndFlush(Unpooled.EMPTY_BUFFER) + .addListener(ChannelFutureListener.CLOSE); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, + Throwable cause) { + cause.printStackTrace(); + ctx.close(); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/java/nia/chapter2/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/java/nia/chapter2/package-info.java" new file mode 100644 index 0000000..e07ee2b --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/java/nia/chapter2/package-info.java" @@ -0,0 +1,8 @@ +/** + * Created by kerr. + * + * Listing 2.1 EchoServerHandler {@link nia.chapter2.echoserver.EchoServerHandler} + * + * Listing 2.2 EchoServer class {@link nia.chapter2.echoserver.EchoServer} + */ +package nia.chapter2; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/resources/.gitignore" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/resources/.gitignore" new file mode 100644 index 0000000..86d0cb2 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/Server/src/main/resources/.gitignore" @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/out" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/out" new file mode 100644 index 0000000..b5e1bf9 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/out" @@ -0,0 +1,80 @@ +[INFO] Scanning for projects... +[INFO] ------------------------------------------------------------------------ +[INFO] Reactor Build Order: +[INFO] +[INFO] Chapter 2. Your First Netty Application - Echo App +[INFO] Chapter 2. Echo Client +[INFO] Chapter 2. Echo Server +[INFO] +[INFO] ------------------------------------------------------------------------ +[INFO] Building Chapter 2. Your First Netty Application - Echo App 2.0-SNAPSHOT +[INFO] ------------------------------------------------------------------------ +[INFO] +[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ chapter2 --- +[INFO] +[INFO] ------------------------------------------------------------------------ +[INFO] Building Chapter 2. Echo Client 2.0-SNAPSHOT +[INFO] ------------------------------------------------------------------------ +[INFO] +[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ echo-client --- +[INFO] +[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ echo-client --- +[INFO] Using 'UTF-8' encoding to copy filtered resources. +[INFO] Copying 1 resource +[INFO] +[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ echo-client --- +[INFO] Changes detected - recompiling the module! +[INFO] Compiling 2 source files to D:\opt\netty\netty-in-action\chapter2\Client\target\classes +[INFO] +[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ echo-client --- +[INFO] Using 'UTF-8' encoding to copy filtered resources. +[INFO] skip non existing resourceDirectory D:\opt\netty\netty-in-action\chapter2\Client\src\test\resources +[INFO] +[INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) @ echo-client --- +[INFO] No sources to compile +[INFO] +[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ echo-client --- +[INFO] No tests to run. +[INFO] +[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ echo-client --- +[INFO] Building jar: D:\opt\netty\netty-in-action\chapter2\Client\target\echo-client-2.0-SNAPSHOT.jar +[INFO] +[INFO] ------------------------------------------------------------------------ +[INFO] Building Chapter 2. Echo Server 2.0-SNAPSHOT +[INFO] ------------------------------------------------------------------------ +[INFO] +[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ echo-server --- +[INFO] +[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ echo-server --- +[INFO] Using 'UTF-8' encoding to copy filtered resources. +[INFO] Copying 1 resource +[INFO] +[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ echo-server --- +[INFO] Changes detected - recompiling the module! +[INFO] Compiling 2 source files to D:\opt\netty\netty-in-action\chapter2\Server\target\classes +[INFO] +[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ echo-server --- +[INFO] Using 'UTF-8' encoding to copy filtered resources. +[INFO] skip non existing resourceDirectory D:\opt\netty\netty-in-action\chapter2\Server\src\test\resources +[INFO] +[INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) @ echo-server --- +[INFO] No sources to compile +[INFO] +[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ echo-server --- +[INFO] No tests to run. +[INFO] +[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ echo-server --- +[INFO] Building jar: D:\opt\netty\netty-in-action\chapter2\Server\target\echo-server-2.0-SNAPSHOT.jar +[INFO] ------------------------------------------------------------------------ +[INFO] Reactor Summary: +[INFO] +[INFO] Chapter 2. Your First Netty Application - Echo App . SUCCESS [ 0.134 s] +[INFO] Chapter 2. Echo Client ............................. SUCCESS [ 1.509 s] +[INFO] Chapter 2. Echo Server ............................. SUCCESS [ 0.139 s] +[INFO] ------------------------------------------------------------------------ +[INFO] BUILD SUCCESS +[INFO] ------------------------------------------------------------------------ +[INFO] Total time: 1.886 s +[INFO] Finished at: 2015-11-18T17:14:10-05:00 +[INFO] Final Memory: 18M/216M +[INFO] ------------------------------------------------------------------------ diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/pom.xml" new file mode 100644 index 0000000..948d073 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter2/pom.xml" @@ -0,0 +1,48 @@ + + + 4.0.0 + + + nia + nia-samples-parent + 2.0-SNAPSHOT + ../pom.xml + + + chapter2 + pom + Chapter 2. Your First Netty Application - Echo App + + Build an Echo Server and Client + + + + Client + Server + + + + localhost + 8888 + + + + + + maven-compiler-plugin + + + maven-failsafe-plugin + + + maven-surefire-plugin + + + org.codehaus.mojo + exec-maven-plugin + + + + + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/pom.xml" new file mode 100644 index 0000000..3257565 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/pom.xml" @@ -0,0 +1,18 @@ + + + 4.0.0 + + nia + nia-samples-parent + 2.0-SNAPSHOT + ../pom.xml + + + chapter4 + + Chapter 4. Transports + + OIO, NIO, Local and Embedded Transports + + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/ChannelOperationExamples.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/ChannelOperationExamples.java" new file mode 100644 index 0000000..1308050 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/ChannelOperationExamples.java" @@ -0,0 +1,65 @@ +package nia.chapter4; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.util.CharsetUtil; + +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +/** + * Listing 4.5 Writing to a Channel + * + * Listing 4.6 Using a Channel from many threads + * + * @author Norman Maurer + */ +public class ChannelOperationExamples { + private static final Channel CHANNEL_FROM_SOMEWHERE = new NioSocketChannel(); + /** + * Listing 4.5 Writing to a Channel + */ + public static void writingToChannel() { + Channel channel = CHANNEL_FROM_SOMEWHERE; // Get the channel reference from somewhere + ByteBuf buf = Unpooled.copiedBuffer("your data", CharsetUtil.UTF_8); + ChannelFuture cf = channel.writeAndFlush(buf); + cf.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) { + if (future.isSuccess()) { + System.out.println("Write successful"); + } else { + System.err.println("Write error"); + future.cause().printStackTrace(); + } + } + }); + } + + /** + * Listing 4.6 Using a Channel from many threads + */ + public static void writingToChannelFromManyThreads() { + final Channel channel = CHANNEL_FROM_SOMEWHERE; // Get the channel reference from somewhere + final ByteBuf buf = Unpooled.copiedBuffer("your data", + CharsetUtil.UTF_8); + Runnable writer = new Runnable() { + @Override + public void run() { + channel.write(buf.duplicate()); + } + }; + Executor executor = Executors.newCachedThreadPool(); + + // write in one thread + executor.execute(writer); + + // write in another thread + executor.execute(writer); + //... + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/NettyNioServer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/NettyNioServer.java" new file mode 100644 index 0000000..19ff745 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/NettyNioServer.java" @@ -0,0 +1,53 @@ +package nia.chapter4; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; + +import java.net.InetSocketAddress; +import java.nio.charset.Charset; + +/** + * Listing 4.4 Asynchronous networking with Netty + * + * @author Norman Maurer + */ +public class NettyNioServer { + public void server(int port) throws Exception { + final ByteBuf buf = + Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi!\r\n", + Charset.forName("UTF-8"))); + NioEventLoopGroup group = new NioEventLoopGroup(); + try { + ServerBootstrap b = new ServerBootstrap(); + b.group(group).channel(NioServerSocketChannel.class) + .localAddress(new InetSocketAddress(port)) + .childHandler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) + throws Exception { + ch.pipeline().addLast( + new ChannelInboundHandlerAdapter() { + @Override + public void channelActive( + ChannelHandlerContext ctx) throws Exception { + ctx.writeAndFlush(buf.duplicate()) + .addListener( + ChannelFutureListener.CLOSE); + } + }); + } + } + ); + ChannelFuture f = b.bind().sync(); + f.channel().closeFuture().sync(); + } finally { + group.shutdownGracefully().sync(); + } + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/NettyOioServer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/NettyOioServer.java" new file mode 100644 index 0000000..b82b4a0 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/NettyOioServer.java" @@ -0,0 +1,54 @@ +package nia.chapter4; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.*; +import io.netty.channel.oio.OioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.oio.OioServerSocketChannel; + +import java.net.InetSocketAddress; +import java.nio.charset.Charset; + +/** + * Listing 4.3 Blocking networking with Netty + * + * @author Norman Maurer + */ +public class NettyOioServer { + public void server(int port) + throws Exception { + final ByteBuf buf = + Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi!\r\n", Charset.forName("UTF-8"))); + EventLoopGroup group = new OioEventLoopGroup(); + try { + ServerBootstrap b = new ServerBootstrap(); + b.group(group) + .channel(OioServerSocketChannel.class) + .localAddress(new InetSocketAddress(port)) + .childHandler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) + throws Exception { + ch.pipeline().addLast( + new ChannelInboundHandlerAdapter() { + @Override + public void channelActive( + ChannelHandlerContext ctx) + throws Exception { + ctx.writeAndFlush(buf.duplicate()) + .addListener( + ChannelFutureListener.CLOSE); + } + }); + } + }); + ChannelFuture f = b.bind().sync(); + f.channel().closeFuture().sync(); + } finally { + group.shutdownGracefully().sync(); + } + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/PlainNioServer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/PlainNioServer.java" new file mode 100644 index 0000000..54fce8c --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/PlainNioServer.java" @@ -0,0 +1,77 @@ +package nia.chapter4; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.nio.ByteBuffer; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.util.Iterator; +import java.util.Set; + +/** + * Listing 4.2 Asynchronous networking without Netty + * + * @author Norman Maurer + */ +public class PlainNioServer { + public void serve(int port) throws IOException { + ServerSocketChannel serverChannel = ServerSocketChannel.open(); + serverChannel.configureBlocking(false); + ServerSocket ss = serverChannel.socket(); + InetSocketAddress address = new InetSocketAddress(port); + ss.bind(address); + Selector selector = Selector.open(); + serverChannel.register(selector, SelectionKey.OP_ACCEPT); + final ByteBuffer msg = ByteBuffer.wrap("Hi!\r\n".getBytes()); + for (;;){ + try { + selector.select(); + } catch (IOException ex) { + ex.printStackTrace(); + //handle exception + break; + } + Set readyKeys = selector.selectedKeys(); + Iterator iterator = readyKeys.iterator(); + while (iterator.hasNext()) { + SelectionKey key = iterator.next(); + iterator.remove(); + try { + if (key.isAcceptable()) { + ServerSocketChannel server = + (ServerSocketChannel) key.channel(); + SocketChannel client = server.accept(); + client.configureBlocking(false); + client.register(selector, SelectionKey.OP_WRITE | + SelectionKey.OP_READ, msg.duplicate()); + System.out.println( + "Accepted connection from " + client); + } + if (key.isWritable()) { + SocketChannel client = + (SocketChannel) key.channel(); + ByteBuffer buffer = + (ByteBuffer) key.attachment(); + while (buffer.hasRemaining()) { + if (client.write(buffer) == 0) { + break; + } + } + client.close(); + } + } catch (IOException ex) { + key.cancel(); + try { + key.channel().close(); + } catch (IOException cex) { + // ignore on close + } + } + } + } + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/PlainOioServer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/PlainOioServer.java" new file mode 100644 index 0000000..c91f306 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/PlainOioServer.java" @@ -0,0 +1,48 @@ +package nia.chapter4; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.charset.Charset; + +/** + * Listing 4.1 Blocking networking without Netty + * + * @author Norman Maurer + */ +public class PlainOioServer { + public void serve(int port) throws IOException { + final ServerSocket socket = new ServerSocket(port); + try { + for(;;) { + final Socket clientSocket = socket.accept(); + System.out.println( + "Accepted connection from " + clientSocket); + new Thread(new Runnable() { + @Override + public void run() { + OutputStream out; + try { + out = clientSocket.getOutputStream(); + out.write("Hi!\r\n".getBytes( + Charset.forName("UTF-8"))); + out.flush(); + clientSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + clientSocket.close(); + } catch (IOException ex) { + // ignore on close + } + } + } + }).start(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/package-info.java" new file mode 100644 index 0000000..fa771de --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter4/src/main/java/nia/chapter4/package-info.java" @@ -0,0 +1,16 @@ +/** + * Created by kerr. + * + * Listing 4.1 Blocking networking without Netty {@link nia.chapter4.PlainOioServer} + * + * Listing 4.2 Asynchronous networking without Netty {@link nia.chapter4.PlainNioServer} + * + * Listing 4.3 Blocking networking with Netty {@link nia.chapter4.NettyOioServer} + * + * Listing 4.4 Asynchronous networking with Netty {@link nia.chapter4.NettyNioServer} + * + * Listing 4.5 Writing to a Channel {@link nia.chapter4.ChannelOperationExamples#writingToChannel()} + * + * Listing 4.6 Using a Channel from many threads {@link nia.chapter4.ChannelOperationExamples#writingToChannelFromManyThreads()} + */ +package nia.chapter4; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/pom.xml" new file mode 100644 index 0000000..c2f0cbc --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/pom.xml" @@ -0,0 +1,21 @@ + + + 4.0.0 + + + nia + nia-samples-parent + 2.0-SNAPSHOT + + + chapter5 + 2.0-SNAPSHOT + + jar + Chapter 5. ByteBuf + + Netty’s data container + + \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/src/main/java/io/netty/channel/DummyChannelHandlerContext.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/src/main/java/io/netty/channel/DummyChannelHandlerContext.java" new file mode 100644 index 0000000..d20a0c1 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/src/main/java/io/netty/channel/DummyChannelHandlerContext.java" @@ -0,0 +1,26 @@ +package io.netty.channel; + +import io.netty.util.concurrent.EventExecutor; + +/** + * Created by kerr. + */ +public class DummyChannelHandlerContext extends AbstractChannelHandlerContext { + public static ChannelHandlerContext DUMMY_INSTANCE = new DummyChannelHandlerContext( + null, + null, + null, + true, + true + ); + public DummyChannelHandlerContext(DefaultChannelPipeline pipeline, + EventExecutor executor, + String name, boolean inbound, boolean outbound) { + super(pipeline, executor, name, inbound, outbound); + } + + @Override + public ChannelHandler handler() { + return null; + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/src/main/java/nia.chapter5/ByteBufExamples.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/src/main/java/nia.chapter5/ByteBufExamples.java" new file mode 100644 index 0000000..143aab4 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/src/main/java/nia.chapter5/ByteBufExamples.java" @@ -0,0 +1,264 @@ +package nia.chapter5; + +import io.netty.buffer.*; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.util.ByteProcessor; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.util.Random; + +import static io.netty.channel.DummyChannelHandlerContext.DUMMY_INSTANCE; + +/** + * Created by kerr. + * + * Listing 5.1 Backing array + * + * Listing 5.2 Direct buffer data access + * + * Listing 5.3 Composite buffer pattern using ByteBuffer + * + * Listing 5.4 Composite buffer pattern using CompositeByteBuf + * + * Listing 5.5 Accessing the data in a CompositeByteBuf + * + * Listing 5.6 Access data + * + * Listing 5.7 Read all data + * + * Listing 5.8 Write data + * + * Listing 5.9 Using ByteBufProcessor to find \r + * + * Listing 5.10 Slice a ByteBuf + * + * Listing 5.11 Copying a ByteBuf + * + * Listing 5.12 get() and set() usage + * + * Listing 5.13 read() and write() operations on the ByteBuf + * + * Listing 5.14 Obtaining a ByteBufAllocator reference + * + * Listing 5.15 Reference counting + * + * Listing 5.16 Release reference-counted object + */ +public class ByteBufExamples { + private final static Random random = new Random(); + private static final ByteBuf BYTE_BUF_FROM_SOMEWHERE = Unpooled.buffer(1024); + private static final Channel CHANNEL_FROM_SOMEWHERE = new NioSocketChannel(); + private static final ChannelHandlerContext CHANNEL_HANDLER_CONTEXT_FROM_SOMEWHERE = DUMMY_INSTANCE; + /** + * Listing 5.1 Backing array + */ + public static void heapBuffer() { + ByteBuf heapBuf = BYTE_BUF_FROM_SOMEWHERE; //get reference form somewhere + if (heapBuf.hasArray()) { + byte[] array = heapBuf.array(); + int offset = heapBuf.arrayOffset() + heapBuf.readerIndex(); + int length = heapBuf.readableBytes(); + handleArray(array, offset, length); + } + } + + /** + * Listing 5.2 Direct buffer data access + */ + public static void directBuffer() { + ByteBuf directBuf = BYTE_BUF_FROM_SOMEWHERE; //get reference form somewhere + if (!directBuf.hasArray()) { + int length = directBuf.readableBytes(); + byte[] array = new byte[length]; + directBuf.getBytes(directBuf.readerIndex(), array); + handleArray(array, 0, length); + } + } + + /** + * Listing 5.3 Composite buffer pattern using ByteBuffer + */ + public static void byteBufferComposite(ByteBuffer header, ByteBuffer body) { + // Use an array to hold the message parts + ByteBuffer[] message = new ByteBuffer[]{ header, body }; + + // Create a new ByteBuffer and use copy to merge the header and body + ByteBuffer message2 = + ByteBuffer.allocate(header.remaining() + body.remaining()); + message2.put(header); + message2.put(body); + message2.flip(); + } + + + /** + * Listing 5.4 Composite buffer pattern using CompositeByteBuf + */ + public static void byteBufComposite() { + CompositeByteBuf messageBuf = Unpooled.compositeBuffer(); + ByteBuf headerBuf = BYTE_BUF_FROM_SOMEWHERE; // can be backing or direct + ByteBuf bodyBuf = BYTE_BUF_FROM_SOMEWHERE; // can be backing or direct + messageBuf.addComponents(headerBuf, bodyBuf); + //... + messageBuf.removeComponent(0); // remove the header + for (ByteBuf buf : messageBuf) { + System.out.println(buf.toString()); + } + } + + /** + * Listing 5.5 Accessing the data in a CompositeByteBuf + */ + public static void byteBufCompositeArray() { + CompositeByteBuf compBuf = Unpooled.compositeBuffer(); + int length = compBuf.readableBytes(); + byte[] array = new byte[length]; + compBuf.getBytes(compBuf.readerIndex(), array); + handleArray(array, 0, array.length); + } + + /** + * Listing 5.6 Access data + */ + public static void byteBufRelativeAccess() { + ByteBuf buffer = BYTE_BUF_FROM_SOMEWHERE; //get reference form somewhere + for (int i = 0; i < buffer.capacity(); i++) { + byte b = buffer.getByte(i); + System.out.println((char) b); + } + } + + /** + * Listing 5.7 Read all data + */ + public static void readAllData() { + ByteBuf buffer = BYTE_BUF_FROM_SOMEWHERE; //get reference form somewhere + while (buffer.isReadable()) { + System.out.println(buffer.readByte()); + } + } + + /** + * Listing 5.8 Write data + */ + public static void write() { + // Fills the writable bytes of a buffer with random integers. + ByteBuf buffer = BYTE_BUF_FROM_SOMEWHERE; //get reference form somewhere + while (buffer.writableBytes() >= 4) { + buffer.writeInt(random.nextInt()); + } + } + + /** + * Listing 5.9 Using ByteProcessor to find \r + * + * use {@link io.netty.buffer.ByteBufProcessor in Netty 4.0.x} + */ + public static void byteProcessor() { + ByteBuf buffer = BYTE_BUF_FROM_SOMEWHERE; //get reference form somewhere + int index = buffer.forEachByte(ByteProcessor.FIND_CR); + } + + /** + * Listing 5.9 Using ByteBufProcessor to find \r + * + * use {@link io.netty.util.ByteProcessor in Netty 4.1.x} + */ + public static void byteBufProcessor() { + ByteBuf buffer = BYTE_BUF_FROM_SOMEWHERE; //get reference form somewhere + int index = buffer.forEachByte(ByteBufProcessor.FIND_CR); + } + + /** + * Listing 5.10 Slice a ByteBuf + */ + public static void byteBufSlice() { + Charset utf8 = Charset.forName("UTF-8"); + ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!", utf8); + ByteBuf sliced = buf.slice(0, 15); + System.out.println(sliced.toString(utf8)); + buf.setByte(0, (byte)'J'); + assert buf.getByte(0) == sliced.getByte(0); + } + + /** + * Listing 5.11 Copying a ByteBuf + */ + public static void byteBufCopy() { + Charset utf8 = Charset.forName("UTF-8"); + ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!", utf8); + ByteBuf copy = buf.copy(0, 15); + System.out.println(copy.toString(utf8)); + buf.setByte(0, (byte)'J'); + assert buf.getByte(0) != copy.getByte(0); + } + + /** + * Listing 5.12 get() and set() usage + */ + public static void byteBufSetGet() { + Charset utf8 = Charset.forName("UTF-8"); + ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!", utf8); + System.out.println((char)buf.getByte(0)); + int readerIndex = buf.readerIndex(); + int writerIndex = buf.writerIndex(); + buf.setByte(0, (byte)'B'); + System.out.println((char)buf.getByte(0)); + assert readerIndex == buf.readerIndex(); + assert writerIndex == buf.writerIndex(); + } + + /** + * Listing 5.13 read() and write() operations on the ByteBuf + */ + public static void byteBufWriteRead() { + Charset utf8 = Charset.forName("UTF-8"); + ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!", utf8); + System.out.println((char)buf.readByte()); + int readerIndex = buf.readerIndex(); + int writerIndex = buf.writerIndex(); + buf.writeByte((byte)'?'); + assert readerIndex == buf.readerIndex(); + assert writerIndex != buf.writerIndex(); + } + + private static void handleArray(byte[] array, int offset, int len) {} + + /** + * Listing 5.14 Obtaining a ByteBufAllocator reference + */ + public static void obtainingByteBufAllocatorReference(){ + Channel channel = CHANNEL_FROM_SOMEWHERE; //get reference form somewhere + ByteBufAllocator allocator = channel.alloc(); + //... + ChannelHandlerContext ctx = CHANNEL_HANDLER_CONTEXT_FROM_SOMEWHERE; //get reference form somewhere + ByteBufAllocator allocator2 = ctx.alloc(); + //... + } + + /** + * Listing 5.15 Reference counting + * */ + public static void referenceCounting(){ + Channel channel = CHANNEL_FROM_SOMEWHERE; //get reference form somewhere + ByteBufAllocator allocator = channel.alloc(); + //... + ByteBuf buffer = allocator.directBuffer(); + assert buffer.refCnt() == 1; + //... + } + + /** + * Listing 5.16 Release reference-counted object + */ + public static void releaseReferenceCountedObject(){ + ByteBuf buffer = BYTE_BUF_FROM_SOMEWHERE; //get reference form somewhere + boolean released = buffer.release(); + //... + } + + +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/src/main/java/nia.chapter5/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/src/main/java/nia.chapter5/package-info.java" new file mode 100644 index 0000000..f60420b --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter5/src/main/java/nia.chapter5/package-info.java" @@ -0,0 +1,36 @@ +/** + * Created by kerr. + * + * Listing 5.1 Backing array {@link nia.chapter5.ByteBufExamples#heapBuffer()} + * + * Listing 5.2 Direct buffer data access {@link nia.chapter5.ByteBufExamples#directBuffer()} + * + * Listing 5.3 Composite buffer pattern using ByteBuffer {@link nia.chapter5.ByteBufExamples#byteBufferComposite(java.nio.ByteBuffer, java.nio.ByteBuffer)} + * + * Listing 5.4 Composite buffer pattern using CompositeByteBuf {@link nia.chapter5.ByteBufExamples#byteBufComposite()} + * + * Listing 5.5 Accessing the data in a CompositeByteBuf {@link nia.chapter5.ByteBufExamples#byteBufCompositeArray()} + * + * Listing 5.6 Access data {@link nia.chapter5.ByteBufExamples#byteBufRelativeAccess()} + * + * Listing 5.7 Read all data {@link nia.chapter5.ByteBufExamples#readAllData()} + * + * Listing 5.8 Write data {@link nia.chapter5.ByteBufExamples#write()} + * + * Listing 5.9 Using ByteBufProcessor to find \r {@link nia.chapter5.ByteBufExamples#byteBufProcessor()} + * + * Listing 5.10 Slice a ByteBuf {@link nia.chapter5.ByteBufExamples#byteBufSlice()} + * + * Listing 5.11 Copying a ByteBuf {@link nia.chapter5.ByteBufExamples#byteBufCopy()} + * + * Listing 5.12 get() and set() usage {@link nia.chapter5.ByteBufExamples#byteBufSetGet()} + * + * Listing 5.13 read() and write() operations on the ByteBuf {@link nia.chapter5.ByteBufExamples#byteBufWriteRead()} + * + * Listing 5.14 Obtaining a ByteBufAllocator reference {@link nia.chapter5.ByteBufExamples#obtainingByteBufAllocatorReference()} + * + * Listing 5.15 Reference counting {@link nia.chapter5.ByteBufExamples#referenceCounting()} + * + * Listing 5.16 Release reference-counted object {@link nia.chapter5.ByteBufExamples#releaseReferenceCountedObject()} + */ +package nia.chapter5; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/pom.xml" new file mode 100644 index 0000000..4abb119 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/pom.xml" @@ -0,0 +1,19 @@ + + + 4.0.0 + + nia + nia-samples-parent + 2.0-SNAPSHOT + ../pom.xml + + + chapter6 + + Chapter 6. ChannelHandler and ChannelPipeline + + + The ChannelHandler and ChannelPipeline APIs + + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/io/netty/channel/DummyChannelHandlerContext.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/io/netty/channel/DummyChannelHandlerContext.java" new file mode 100644 index 0000000..d20a0c1 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/io/netty/channel/DummyChannelHandlerContext.java" @@ -0,0 +1,26 @@ +package io.netty.channel; + +import io.netty.util.concurrent.EventExecutor; + +/** + * Created by kerr. + */ +public class DummyChannelHandlerContext extends AbstractChannelHandlerContext { + public static ChannelHandlerContext DUMMY_INSTANCE = new DummyChannelHandlerContext( + null, + null, + null, + true, + true + ); + public DummyChannelHandlerContext(DefaultChannelPipeline pipeline, + EventExecutor executor, + String name, boolean inbound, boolean outbound) { + super(pipeline, executor, name, inbound, outbound); + } + + @Override + public ChannelHandler handler() { + return null; + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/io/netty/channel/DummyChannelPipeline.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/io/netty/channel/DummyChannelPipeline.java" new file mode 100644 index 0000000..1266dc2 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/io/netty/channel/DummyChannelPipeline.java" @@ -0,0 +1,11 @@ +package io.netty.channel; + +/** + * Created by kerr. + */ +public class DummyChannelPipeline extends DefaultChannelPipeline { + public static final ChannelPipeline DUMMY_INSTANCE = new DummyChannelPipeline(null); + public DummyChannelPipeline(Channel channel) { + super(channel); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/ChannelFutures.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/ChannelFutures.java" new file mode 100644 index 0000000..5593fae --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/ChannelFutures.java" @@ -0,0 +1,38 @@ +package nia.chapter6; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.socket.nio.NioSocketChannel; + + +/** + * Created by kerr. + * + * Listing 6.13 Adding a ChannelFutureListener to a ChannelFuture + */ +public class ChannelFutures { + private static final Channel CHANNEL_FROM_SOMEWHERE = new NioSocketChannel(); + private static final ByteBuf SOME_MSG_FROM_SOMEWHERE = Unpooled.buffer(1024); + + /** + * Listing 6.13 Adding a ChannelFutureListener to a ChannelFuture + * */ + public static void addingChannelFutureListener(){ + Channel channel = CHANNEL_FROM_SOMEWHERE; // get reference to pipeline; + ByteBuf someMessage = SOME_MSG_FROM_SOMEWHERE; // get reference to pipeline; + //... + io.netty.channel.ChannelFuture future = channel.write(someMessage); + future.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(io.netty.channel.ChannelFuture f) { + if (!f.isSuccess()) { + f.cause().printStackTrace(); + f.channel().close(); + } + } + }); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/DiscardHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/DiscardHandler.java" new file mode 100644 index 0000000..44fc040 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/DiscardHandler.java" @@ -0,0 +1,22 @@ +package nia.chapter6; + +import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.util.ReferenceCountUtil; + +/** + * Listing 6.1 Releasing message resources + * + * @author Norman Maurer + */ +@Sharable +public class DiscardHandler extends ChannelInboundHandlerAdapter { + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + ReferenceCountUtil.release(msg); + } + +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/DiscardInboundHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/DiscardInboundHandler.java" new file mode 100644 index 0000000..985bf35 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/DiscardInboundHandler.java" @@ -0,0 +1,19 @@ +package nia.chapter6; + +import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.util.ReferenceCountUtil; + +/** + * Listing 6.3 Consuming and releasing an inbound message + * + * @author Norman Maurer + */ +@Sharable +public class DiscardInboundHandler extends ChannelInboundHandlerAdapter { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + ReferenceCountUtil.release(msg); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/DiscardOutboundHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/DiscardOutboundHandler.java" new file mode 100644 index 0000000..1043bce --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/DiscardOutboundHandler.java" @@ -0,0 +1,24 @@ +package nia.chapter6; + +import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelOutboundHandlerAdapter; +import io.netty.channel.ChannelPromise; +import io.netty.util.ReferenceCountUtil; + +/** + * Listing 6.4 Discarding and releasing outbound data + * + * @author Norman Maurer + */ +@Sharable +public class DiscardOutboundHandler + extends ChannelOutboundHandlerAdapter { + @Override + public void write(ChannelHandlerContext ctx, + Object msg, ChannelPromise promise) { + ReferenceCountUtil.release(msg); + promise.setSuccess(); + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/InboundExceptionHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/InboundExceptionHandler.java" new file mode 100644 index 0000000..d684dba --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/InboundExceptionHandler.java" @@ -0,0 +1,18 @@ +package nia.chapter6; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +/** + * Listing 6.12 Basic inbound exception handling + * + * @author Norman Maurer + */ +public class InboundExceptionHandler extends ChannelInboundHandlerAdapter { + @Override + public void exceptionCaught(ChannelHandlerContext ctx, + Throwable cause) { + cause.printStackTrace(); + ctx.close(); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/ModifyChannelPipeline.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/ModifyChannelPipeline.java" new file mode 100644 index 0000000..e9ddf56 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/ModifyChannelPipeline.java" @@ -0,0 +1,51 @@ +package nia.chapter6; + +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelPipeline; + +import static io.netty.channel.DummyChannelPipeline.DUMMY_INSTANCE; + +/** + * Listing 6.5 Modify the ChannelPipeline + * + * @author Norman Maurer + */ +public class ModifyChannelPipeline { + private static final ChannelPipeline CHANNEL_PIPELINE_FROM_SOMEWHERE = DUMMY_INSTANCE; + + /** + * Listing 6.5 Modify the ChannelPipeline + * */ + public static void modifyPipeline() { + ChannelPipeline pipeline = CHANNEL_PIPELINE_FROM_SOMEWHERE; // get reference to pipeline; + FirstHandler firstHandler = new FirstHandler(); + pipeline.addLast("handler1", firstHandler); + pipeline.addFirst("handler2", new SecondHandler()); + pipeline.addLast("handler3", new ThirdHandler()); + //... + pipeline.remove("handler3"); + pipeline.remove(firstHandler); + pipeline.replace("handler2", "handler4", new FourthHandler()); + + } + + private static final class FirstHandler + extends ChannelHandlerAdapter { + + } + + private static final class SecondHandler + extends ChannelHandlerAdapter { + + } + + private static final class ThirdHandler + extends ChannelHandlerAdapter { + + } + + private static final class FourthHandler + extends ChannelHandlerAdapter { + + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/OutboundExceptionHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/OutboundExceptionHandler.java" new file mode 100644 index 0000000..0afa819 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/OutboundExceptionHandler.java" @@ -0,0 +1,24 @@ +package nia.chapter6; + +import io.netty.channel.*; + +/** + * Listing 6.14 Adding a ChannelFutureListener to a ChannelPromise + * + * @author Norman Maurer + */ +public class OutboundExceptionHandler extends ChannelOutboundHandlerAdapter { + @Override + public void write(ChannelHandlerContext ctx, Object msg, + ChannelPromise promise) { + promise.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture f) { + if (!f.isSuccess()) { + f.cause().printStackTrace(); + f.channel().close(); + } + } + }); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/SharableHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/SharableHandler.java" new file mode 100644 index 0000000..46be482 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/SharableHandler.java" @@ -0,0 +1,20 @@ +package nia.chapter6; + +import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +/** + * Listing 6.10 A sharable ChannelHandler + * + * @author Norman Maurer + */ +@Sharable +public class SharableHandler extends ChannelInboundHandlerAdapter { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + System.out.println("channel read message " + msg); + ctx.fireChannelRead(msg); + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/SimpleDiscardHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/SimpleDiscardHandler.java" new file mode 100644 index 0000000..4292419 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/SimpleDiscardHandler.java" @@ -0,0 +1,20 @@ +package nia.chapter6; + +import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; + +/** + * Listing 6.2 Using SimpleChannelInboundHandler + * + * @author Norman Maurer + */ +@Sharable +public class SimpleDiscardHandler + extends SimpleChannelInboundHandler { + @Override + public void channelRead0(ChannelHandlerContext ctx, + Object msg) { + // No need to do anything special + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/UnsharableHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/UnsharableHandler.java" new file mode 100644 index 0000000..3bb6145 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/UnsharableHandler.java" @@ -0,0 +1,23 @@ +package nia.chapter6; + +import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +/** + * Listing 6.11 Invalid usage of @Sharable + * + * @author Norman Maurer + */ +@Sharable +public class UnsharableHandler extends ChannelInboundHandlerAdapter { + private int count; + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + count++; + System.out.println("inboundBufferUpdated(...) called the " + + count + " time"); + ctx.fireChannelRead(msg); + } +} + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/WriteHandler.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/WriteHandler.java" new file mode 100644 index 0000000..f853b16 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/WriteHandler.java" @@ -0,0 +1,21 @@ +package nia.chapter6; + + +import io.netty.channel.ChannelHandlerAdapter; +import io.netty.channel.ChannelHandlerContext; + +/** + * Listing 6.9 Caching a ChannelHandlerContext + * + * @author Norman Maurer + */ +public class WriteHandler extends ChannelHandlerAdapter { + private ChannelHandlerContext ctx; + @Override + public void handlerAdded(ChannelHandlerContext ctx) { + this.ctx = ctx; + } + public void send(String msg) { + ctx.writeAndFlush(msg); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/WriteHandlers.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/WriteHandlers.java" new file mode 100644 index 0000000..24296a5 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/WriteHandlers.java" @@ -0,0 +1,55 @@ +package nia.chapter6; + +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.DummyChannelPipeline; +import io.netty.util.CharsetUtil; + +import static io.netty.channel.DummyChannelHandlerContext.DUMMY_INSTANCE; + +/** + * Created by kerr. + * + * Listing 6.6 Accessing the Channel from a ChannelHandlerContext + * + * Listing 6.7 Accessing the ChannelPipeline from a ChannelHandlerContext + * + * Listing 6.8 Calling ChannelHandlerContext write() + */ +public class WriteHandlers { + private static final ChannelHandlerContext CHANNEL_HANDLER_CONTEXT_FROM_SOMEWHERE = DUMMY_INSTANCE; + private static final ChannelPipeline CHANNEL_PIPELINE_FROM_SOMEWHERE = DummyChannelPipeline.DUMMY_INSTANCE; + + /** + * Listing 6.6 Accessing the Channel from a ChannelHandlerContext + * */ + public static void writeViaChannel() { + ChannelHandlerContext ctx = CHANNEL_HANDLER_CONTEXT_FROM_SOMEWHERE; //get reference form somewhere + Channel channel = ctx.channel(); + channel.write(Unpooled.copiedBuffer("Netty in Action", + CharsetUtil.UTF_8)); + + } + + /** + * Listing 6.7 Accessing the ChannelPipeline from a ChannelHandlerContext + * */ + public static void writeViaChannelPipeline() { + ChannelHandlerContext ctx = CHANNEL_HANDLER_CONTEXT_FROM_SOMEWHERE; //get reference form somewhere + ChannelPipeline pipeline = ctx.pipeline(); //get reference form somewhere + pipeline.write(Unpooled.copiedBuffer("Netty in Action", + CharsetUtil.UTF_8)); + + } + + /** + * Listing 6.8 Calling ChannelHandlerContext write() + * */ + public static void writeViaChannelHandlerContext() { + ChannelHandlerContext ctx = CHANNEL_HANDLER_CONTEXT_FROM_SOMEWHERE; //get reference form somewhere; + ctx.write(Unpooled.copiedBuffer("Netty in Action", CharsetUtil.UTF_8)); + } + +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/package-info.java" new file mode 100644 index 0000000..9fc4ea8 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter6/src/main/java/nia/chapter6/package-info.java" @@ -0,0 +1,32 @@ +/** + * Created by kerr. + * + * Listing 6.1 Releasing message resources {@link nia.chapter6.DiscardHandler} + * + * Listing 6.2 Using SimpleChannelInboundHandler {@link nia.chapter6.SimpleDiscardHandler} + * + * Listing 6.3 Consuming and releasing an inbound message {@link nia.chapter6.DiscardInboundHandler} + * + * Listing 6.4 Discarding and releasing outbound data {@link nia.chapter6.DiscardOutboundHandler} + * + * Listing 6.5 Modify the ChannelPipeline {@link nia.chapter6.ModifyChannelPipeline#modifyPipeline()} + * + * Listing 6.6 Accessing the Channel from a ChannelHandlerContext {@link nia.chapter6.WriteHandlers#writeViaChannel()} + * + * Listing 6.7 Accessing the ChannelPipeline from a ChannelHandlerContext {@link nia.chapter6.WriteHandlers#writeViaChannelPipeline()} + * + * Listing 6.8 Calling ChannelHandlerContext write() {@link nia.chapter6.WriteHandlers#writeViaChannelHandlerContext()} + * + * Listing 6.9 Caching a ChannelHandlerContext {@link nia.chapter6.WriteHandler} + * + * Listing 6.10 A sharable ChannelHandler {@link nia.chapter6.SharableHandler} + * + * Listing 6.11 Invalid usage of @Sharable {@link nia.chapter6.UnsharableHandler} + * + * Listing 6.12 Basic inbound exception handling {@link nia.chapter6.InboundExceptionHandler} + * + * Listing 6.13 Adding a ChannelFutureListener to a ChannelFuture {@link nia.chapter6.ChannelFutures#addingChannelFutureListener()} + * + * Listing 6.14 Adding a ChannelFutureListener to a ChannelPromise {@link nia.chapter6.OutboundExceptionHandler} + */ +package nia.chapter6; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/pom.xml" new file mode 100644 index 0000000..b32860d --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/pom.xml" @@ -0,0 +1,14 @@ + + + 4.0.0 + + nia + nia-samples-parent + 2.0-SNAPSHOT + ../pom.xml + + + chapter7 + Chapter 7. EEventLoop and threading model + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/src/main/java/nia/chapter7/EventLoopExamples.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/src/main/java/nia/chapter7/EventLoopExamples.java" new file mode 100644 index 0000000..df172b0 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/src/main/java/nia/chapter7/EventLoopExamples.java" @@ -0,0 +1,38 @@ +package nia.chapter7; + +import java.util.Collections; +import java.util.List; + +/** + * Listing 7.1 Executing tasks in an event loop + * + * @author Norman Maurer + */ +public class EventLoopExamples { + /** + * Listing 7.1 Executing tasks in an event loop + * */ + public static void executeTaskInEventLoop() { + boolean terminated = true; + //... + while (!terminated) { + List readyEvents = blockUntilEventsReady(); + for (Runnable ev: readyEvents) { + ev.run(); + } + } + } + + private static final List blockUntilEventsReady() { + return Collections.singletonList(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/src/main/java/nia/chapter7/ScheduleExamples.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/src/main/java/nia/chapter7/ScheduleExamples.java" new file mode 100644 index 0000000..84147ec --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/src/main/java/nia/chapter7/ScheduleExamples.java" @@ -0,0 +1,87 @@ +package nia.chapter7; + +import io.netty.channel.Channel; +import io.netty.channel.socket.nio.NioSocketChannel; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +/** + * Listing 7.2 Scheduling a task with a ScheduledExecutorService + * + * Listing 7.3 Scheduling a task with EventLoop + * + * Listing 7.4 Scheduling a recurring task with EventLoop + * + * Listing 7.5 Canceling a task using ScheduledFuture + * + * @author Norman Maurer + */ +public class ScheduleExamples { + private static final Channel CHANNEL_FROM_SOMEWHERE = new NioSocketChannel(); + + /** + * Listing 7.2 Scheduling a task with a ScheduledExecutorService + * */ + public static void schedule() { + ScheduledExecutorService executor = + Executors.newScheduledThreadPool(10); + + ScheduledFuture future = executor.schedule( + new Runnable() { + @Override + public void run() { + System.out.println("Now it is 60 seconds later"); + } + }, 60, TimeUnit.SECONDS); + //... + executor.shutdown(); + } + + /** + * Listing 7.3 Scheduling a task with EventLoop + * */ + public static void scheduleViaEventLoop() { + Channel ch = CHANNEL_FROM_SOMEWHERE; // get reference from somewhere + ScheduledFuture future = ch.eventLoop().schedule( + new Runnable() { + @Override + public void run() { + System.out.println("60 seconds later"); + } + }, 60, TimeUnit.SECONDS); + } + + /** + * Listing 7.4 Scheduling a recurring task with EventLoop + * */ + public static void scheduleFixedViaEventLoop() { + Channel ch = CHANNEL_FROM_SOMEWHERE; // get reference from somewhere + ScheduledFuture future = ch.eventLoop().scheduleAtFixedRate( + new Runnable() { + @Override + public void run() { + System.out.println("Run every 60 seconds"); + } + }, 60, 60, TimeUnit.SECONDS); + } + + /** + * Listing 7.5 Canceling a task using ScheduledFuture + * */ + public static void cancelingTaskUsingScheduledFuture(){ + Channel ch = CHANNEL_FROM_SOMEWHERE; // get reference from somewhere + ScheduledFuture future = ch.eventLoop().scheduleAtFixedRate( + new Runnable() { + @Override + public void run() { + System.out.println("Run every 60 seconds"); + } + }, 60, 60, TimeUnit.SECONDS); + // Some other code that runs... + boolean mayInterruptIfRunning = false; + future.cancel(mayInterruptIfRunning); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/src/main/java/nia/chapter7/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/src/main/java/nia/chapter7/package-info.java" new file mode 100644 index 0000000..5bbc479 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter7/src/main/java/nia/chapter7/package-info.java" @@ -0,0 +1,14 @@ +/** + * Created by kerr. + * + * Listing 7.1 Executing tasks in an event loop {@link nia.chapter7.EventLoopExamples#executeTaskInEventLoop()} + * + * Listing 7.2 Scheduling a task with a ScheduledExecutorService {@link nia.chapter7.ScheduleExamples#schedule()} + * + * Listing 7.3 Scheduling a task with EventLoop {@link nia.chapter7.ScheduleExamples#scheduleViaEventLoop()} + * + * Listing 7.4 Scheduling a recurring task with EventLoop {@link nia.chapter7.ScheduleExamples#scheduleFixedViaEventLoop()} + * + * Listing 7.5 Canceling a task using ScheduledFuture {@link nia.chapter7.ScheduleExamples#cancelingTaskUsingScheduledFuture()} + */ +package nia.chapter7; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/pom.xml" new file mode 100644 index 0000000..c79467c --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/pom.xml" @@ -0,0 +1,15 @@ + + + 4.0.0 + + nia + nia-samples-parent + 2.0-SNAPSHOT + ../pom.xml + + + chapter8 + Chapter 8. Bootstrapping + Bootstrapping clients and servers + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapClient.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapClient.java" new file mode 100644 index 0000000..223a15a --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapClient.java" @@ -0,0 +1,55 @@ +package nia.chapter8; + +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; + +import java.net.InetSocketAddress; + +/** + * Listing 8.1 Bootstrapping a client + * + * @author Norman Maurer + * @author Marvin Wolfthal + */ +public class BootstrapClient { + public static void main(String args[]) { + BootstrapClient client = new BootstrapClient(); + client.bootstrap(); + } + + /** + * Listing 8.1 Bootstrapping a client + * */ + public void bootstrap() { + EventLoopGroup group = new NioEventLoopGroup(); + Bootstrap bootstrap = new Bootstrap(); + bootstrap.group(group) + .channel(NioSocketChannel.class) + .handler(new SimpleChannelInboundHandler() { + @Override + protected void channelRead0( + ChannelHandlerContext channelHandlerContext, + ByteBuf byteBuf) throws Exception { + System.out.println("Received data"); + } + }); + ChannelFuture future = + bootstrap.connect( + new InetSocketAddress("www.manning.com", 80)); + future.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture channelFuture) + throws Exception { + if (channelFuture.isSuccess()) { + System.out.println("Connection established"); + } else { + System.err.println("Connection attempt failed"); + channelFuture.cause().printStackTrace(); + } + } + }); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapClientWithOptionsAndAttrs.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapClientWithOptionsAndAttrs.java" new file mode 100644 index 0000000..0ccbafe --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapClientWithOptionsAndAttrs.java" @@ -0,0 +1,54 @@ +package nia.chapter8; + +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelOption; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.util.AttributeKey; + +import java.net.InetSocketAddress; + +/** + * Listing 8.7 Using attributes + * + * @author Norman Maurer + */ +public class BootstrapClientWithOptionsAndAttrs { + + /** + * Listing 8.7 Using attributes + * */ + public void bootstrap() { + final AttributeKey id = AttributeKey.newInstance("ID"); + Bootstrap bootstrap = new Bootstrap(); + bootstrap.group(new NioEventLoopGroup()) + .channel(NioSocketChannel.class) + .handler( + new SimpleChannelInboundHandler() { + @Override + public void channelRegistered(ChannelHandlerContext ctx) + throws Exception { + Integer idValue = ctx.channel().attr(id).get(); + // do something with the idValue + } + + @Override + protected void channelRead0( + ChannelHandlerContext channelHandlerContext, + ByteBuf byteBuf) throws Exception { + System.out.println("Received data"); + } + } + ); + bootstrap.option(ChannelOption.SO_KEEPALIVE, true) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000); + bootstrap.attr(id, 123456); + ChannelFuture future = bootstrap.connect( + new InetSocketAddress("www.manning.com", 80)); + future.syncUninterruptibly(); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapDatagramChannel.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapDatagramChannel.java" new file mode 100644 index 0000000..89acc2f --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapDatagramChannel.java" @@ -0,0 +1,51 @@ +package nia.chapter8; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.oio.OioEventLoopGroup; +import io.netty.channel.socket.DatagramPacket; +import io.netty.channel.socket.oio.OioDatagramChannel; + +import java.net.InetSocketAddress; + +/** + * Listing 8.8 Using Bootstrap with DatagramChannel + * + * @author Norman Maurer + * @author Marvin Wolfthal + */ +public class BootstrapDatagramChannel { + + /** + * Listing 8.8 Using Bootstrap with DatagramChannel + */ + public void bootstrap() { + Bootstrap bootstrap = new Bootstrap(); + bootstrap.group(new OioEventLoopGroup()).channel( + OioDatagramChannel.class).handler( + new SimpleChannelInboundHandler() { + @Override + public void channelRead0(ChannelHandlerContext ctx, + DatagramPacket msg) throws Exception { + // Do something with the packet + } + } + ); + ChannelFuture future = bootstrap.bind(new InetSocketAddress(0)); + future.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture channelFuture) + throws Exception { + if (channelFuture.isSuccess()) { + System.out.println("Channel bound"); + } else { + System.err.println("Bind attempt failed"); + channelFuture.cause().printStackTrace(); + } + } + }); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapServer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapServer.java" new file mode 100644 index 0000000..d88b20a --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapServer.java" @@ -0,0 +1,51 @@ +package nia.chapter8; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; + +import java.net.InetSocketAddress; + +/** + * Listing 8.4 Bootstrapping a server + * + * @author Norman Maurer + * @author Marvin Wolfthal + */ +public class BootstrapServer { + + /** + * Listing 8.4 Bootstrapping a server + * */ + public void bootstrap() { + NioEventLoopGroup group = new NioEventLoopGroup(); + ServerBootstrap bootstrap = new ServerBootstrap(); + bootstrap.group(group) + .channel(NioServerSocketChannel.class) + .childHandler(new SimpleChannelInboundHandler() { + @Override + protected void channelRead0(ChannelHandlerContext channelHandlerContext, + ByteBuf byteBuf) throws Exception { + System.out.println("Received data"); + } + }); + ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080)); + future.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture channelFuture) + throws Exception { + if (channelFuture.isSuccess()) { + System.out.println("Server bound"); + } else { + System.err.println("Bind attempt failed"); + channelFuture.cause().printStackTrace(); + } + } + }); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapSharingEventLoopGroup.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapSharingEventLoopGroup.java" new file mode 100644 index 0000000..650deeb --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapSharingEventLoopGroup.java" @@ -0,0 +1,75 @@ +package nia.chapter8; + +import io.netty.bootstrap.Bootstrap; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; + +import java.net.InetSocketAddress; + +/** + * Listing 8.5 Bootstrapping a server + * + * @author Norman Maurer + * @author Marvin Wolfthal + */ +public class BootstrapSharingEventLoopGroup { + + /** + * Listing 8.5 Bootstrapping a server + * */ + public void bootstrap() { + ServerBootstrap bootstrap = new ServerBootstrap(); + bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup()) + .channel(NioServerSocketChannel.class) + .childHandler( + new SimpleChannelInboundHandler() { + ChannelFuture connectFuture; + @Override + public void channelActive(ChannelHandlerContext ctx) + throws Exception { + Bootstrap bootstrap = new Bootstrap(); + bootstrap.channel(NioSocketChannel.class).handler( + new SimpleChannelInboundHandler() { + @Override + protected void channelRead0( + ChannelHandlerContext ctx, ByteBuf in) + throws Exception { + System.out.println("Received data"); + } + }); + bootstrap.group(ctx.channel().eventLoop()); + connectFuture = bootstrap.connect( + new InetSocketAddress("www.manning.com", 80)); + } + + @Override + protected void channelRead0( + ChannelHandlerContext channelHandlerContext, + ByteBuf byteBuf) throws Exception { + if (connectFuture.isDone()) { + // do something with the data + } + } + }); + ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080)); + future.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture channelFuture) + throws Exception { + if (channelFuture.isSuccess()) { + System.out.println("Server bound"); + } else { + System.err.println("Bind attempt failed"); + channelFuture.cause().printStackTrace(); + } + } + }); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapWithInitializer.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapWithInitializer.java" new file mode 100644 index 0000000..b742f82 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/BootstrapWithInitializer.java" @@ -0,0 +1,40 @@ +package nia.chapter8; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.codec.http.HttpClientCodec; +import io.netty.handler.codec.http.HttpObjectAggregator; + +import java.net.InetSocketAddress; + +/** + * Listing 8.6 Bootstrapping and using ChannelInitializer + * + * @author Norman Maurer + */ +public class BootstrapWithInitializer { + + /** + * Listing 8.6 Bootstrapping and using ChannelInitializer + * */ + public void bootstrap() throws InterruptedException { + ServerBootstrap bootstrap = new ServerBootstrap(); + bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup()) + .channel(NioServerSocketChannel.class) + .childHandler(new ChannelInitializerImpl()); + ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080)); + future.sync(); + } + + final class ChannelInitializerImpl extends ChannelInitializer { + @Override + protected void initChannel(Channel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + pipeline.addLast(new HttpClientCodec()); + pipeline.addLast(new HttpObjectAggregator(Integer.MAX_VALUE)); + + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/GracefulShutdown.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/GracefulShutdown.java" new file mode 100644 index 0000000..aa4d7d6 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/GracefulShutdown.java" @@ -0,0 +1,49 @@ +package nia.chapter8; + +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.util.concurrent.Future; + +import java.net.InetSocketAddress; + +/** + * Listing 8.9 Graceful shutdown + * + * @author Norman Maurer + * @author Marvin Wolfthal + */ +public class GracefulShutdown { + public static void main(String args[]) { + GracefulShutdown client = new GracefulShutdown(); + client.bootstrap(); + } + + /** + * Listing 8.9 Graceful shutdown + */ + public void bootstrap() { + EventLoopGroup group = new NioEventLoopGroup(); + Bootstrap bootstrap = new Bootstrap(); + bootstrap.group(group) + .channel(NioSocketChannel.class) + //... + .handler( + new SimpleChannelInboundHandler() { + @Override + protected void channelRead0( + ChannelHandlerContext channelHandlerContext, + ByteBuf byteBuf) throws Exception { + System.out.println("Received data"); + } + } + ); + bootstrap.connect(new InetSocketAddress("www.manning.com", 80)).syncUninterruptibly(); + //,,, + Future future = group.shutdownGracefully(); + // block until the group has shutdown + future.syncUninterruptibly(); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/InvalidBootstrapClient.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/InvalidBootstrapClient.java" new file mode 100644 index 0000000..e2273a1 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/InvalidBootstrapClient.java" @@ -0,0 +1,45 @@ +package nia.chapter8; + +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.oio.OioSocketChannel; + +import java.net.InetSocketAddress; + +/** + * Listing 8.3 Incompatible Channel and EventLoopGroup + * + * @author Norman Maurer + */ +public class InvalidBootstrapClient { + + public static void main(String args[]) { + InvalidBootstrapClient client = new InvalidBootstrapClient(); + client.bootstrap(); + } + + /** + * Listing 8.3 Incompatible Channel and EventLoopGroup + * */ + public void bootstrap() { + EventLoopGroup group = new NioEventLoopGroup(); + Bootstrap bootstrap = new Bootstrap(); + bootstrap.group(group).channel(OioSocketChannel.class) + .handler(new SimpleChannelInboundHandler() { + @Override + protected void channelRead0( + ChannelHandlerContext channelHandlerContext, + ByteBuf byteBuf) throws Exception { + System.out.println("Received data"); + } + }); + ChannelFuture future = bootstrap.connect( + new InetSocketAddress("www.manning.com", 80)); + future.syncUninterruptibly(); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/package-info.java" new file mode 100644 index 0000000..5268dc3 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter8/src/main/java/nia/chapter8/package-info.java" @@ -0,0 +1,20 @@ +/** + * Created by kerr. + * + * Listing 8.1 Bootstrapping a client {@link nia.chapter8.BootstrapClient#bootstrap()} + * + * Listing 8.3 Incompatible Channel and EventLoopGroup {@link nia.chapter8.InvalidBootstrapClient#bootstrap()} + * + * Listing 8.4 Bootstrapping a server{@link nia.chapter8.BootstrapServer#bootstrap()} + * + * Listing 8.5 Bootstrapping a server {@link nia.chapter8.BootstrapSharingEventLoopGroup#bootstrap()} + * + * Listing 8.6 Bootstrapping and using ChannelInitializer {@link nia.chapter8.BootstrapWithInitializer#bootstrap()} + * + * Listing 8.7 Using attributes {@link nia.chapter8.BootstrapClientWithOptionsAndAttrs#bootstrap()} + * + * Listing 8.8 Using Bootstrap with DatagramChannel {@link nia.chapter8.BootstrapDatagramChannel#bootstrap()} + * + * Listing 8.9 Graceful shutdown {@link nia.chapter8.GracefulShutdown#bootstrap()} + */ +package nia.chapter8; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/pom.xml" new file mode 100644 index 0000000..03f3bf3 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/pom.xml" @@ -0,0 +1,15 @@ + + + 4.0.0 + + nia + nia-samples-parent + 2.0-SNAPSHOT + ../pom.xml + + + chapter9 + Chapter 9. Unit testing + Unit testing + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/AbsIntegerEncoder.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/AbsIntegerEncoder.java" new file mode 100644 index 0000000..e18d8e7 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/AbsIntegerEncoder.java" @@ -0,0 +1,24 @@ +package nia.chapter9; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageEncoder; + +import java.util.List; + +/** + * Listing 9.3 AbsIntegerEncoder + * + * @author Norman Maurer + */ +public class AbsIntegerEncoder extends + MessageToMessageEncoder { + @Override + protected void encode(ChannelHandlerContext channelHandlerContext, + ByteBuf in, List out) throws Exception { + while (in.readableBytes() >= 4) { + int value = Math.abs(in.readInt()); + out.add(value); + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/FixedLengthFrameDecoder.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/FixedLengthFrameDecoder.java" new file mode 100644 index 0000000..3fd54c0 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/FixedLengthFrameDecoder.java" @@ -0,0 +1,33 @@ +package nia.chapter9; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + +import java.util.List; + +/** + * Listing 9.1 FixedLengthFrameDecoder + * + * @author Norman Maurer + */ +public class FixedLengthFrameDecoder extends ByteToMessageDecoder { + private final int frameLength; + + public FixedLengthFrameDecoder(int frameLength) { + if (frameLength <= 0) { + throw new IllegalArgumentException( + "frameLength must be a positive integer: " + frameLength); + } + this.frameLength = frameLength; + } + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, + List out) throws Exception { + while (in.readableBytes() >= frameLength) { + ByteBuf buf = in.readBytes(frameLength); + out.add(buf); + } + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/FrameChunkDecoder.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/FrameChunkDecoder.java" new file mode 100644 index 0000000..caa5a40 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/FrameChunkDecoder.java" @@ -0,0 +1,35 @@ +package nia.chapter9; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; +import io.netty.handler.codec.TooLongFrameException; + +import java.util.List; + +/** + * Listing 9.5 FrameChunkDecoder + * + * @author Norman Maurer + */ +public class FrameChunkDecoder extends ByteToMessageDecoder { + private final int maxFrameSize; + + public FrameChunkDecoder(int maxFrameSize) { + this.maxFrameSize = maxFrameSize; + } + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, + List out) + throws Exception { + int readableBytes = in.readableBytes(); + if (readableBytes > maxFrameSize) { + // discard the bytes + in.clear(); + throw new TooLongFrameException(); + } + ByteBuf buf = in.readBytes(readableBytes); + out.add(buf); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/package-info.java" new file mode 100644 index 0000000..9ee9cd4 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/main/java/nia/chapter9/package-info.java" @@ -0,0 +1,10 @@ +/** + * Created by kerr. + * + * Listing 9.1 FixedLengthFrameDecoder {@link nia.chapter9.FixedLengthFrameDecoder} + * + * Listing 9.3 AbsIntegerEncoder {@link nia.chapter9.AbsIntegerEncoder} + * + * Listing 9.5 FrameChunkDecoder {@link nia.chapter9.FrameChunkDecoder} + */ +package nia.chapter9; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/AbsIntegerEncoderTest.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/AbsIntegerEncoderTest.java" new file mode 100644 index 0000000..f07feb4 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/AbsIntegerEncoderTest.java" @@ -0,0 +1,35 @@ +package nia.test.chapter9; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.embedded.EmbeddedChannel; +import nia.chapter9.AbsIntegerEncoder; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Listing 9.4 Testing the AbsIntegerEncoder + * + * @author Norman Maurer + */ +public class AbsIntegerEncoderTest { + @Test + public void testEncoded() { + ByteBuf buf = Unpooled.buffer(); + for (int i = 1; i < 10; i++) { + buf.writeInt(i * -1); + } + + EmbeddedChannel channel = new EmbeddedChannel( + new AbsIntegerEncoder()); + assertTrue(channel.writeOutbound(buf)); + assertTrue(channel.finish()); + + // read bytes + for (int i = 1; i < 10; i++) { + assertEquals(i, channel.readOutbound()); + } + assertNull(channel.readOutbound()); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/FixedLengthFrameDecoderTest.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/FixedLengthFrameDecoderTest.java" new file mode 100644 index 0000000..fda1349 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/FixedLengthFrameDecoderTest.java" @@ -0,0 +1,76 @@ +package nia.test.chapter9; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.embedded.EmbeddedChannel; +import nia.chapter9.FixedLengthFrameDecoder; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Listing 9.2 Testing the FixedLengthFrameDecoder + * + * @author Norman Maurer + */ +public class FixedLengthFrameDecoderTest { + @Test + public void testFramesDecoded() { + ByteBuf buf = Unpooled.buffer(); + for (int i = 0; i < 9; i++) { + buf.writeByte(i); + } + ByteBuf input = buf.duplicate(); + EmbeddedChannel channel = new EmbeddedChannel( + new FixedLengthFrameDecoder(3)); + // write bytes + assertTrue(channel.writeInbound(input.retain())); + assertTrue(channel.finish()); + + // read messages + ByteBuf read = (ByteBuf) channel.readInbound(); + assertEquals(buf.readSlice(3), read); + read.release(); + + read = (ByteBuf) channel.readInbound(); + assertEquals(buf.readSlice(3), read); + read.release(); + + read = (ByteBuf) channel.readInbound(); + assertEquals(buf.readSlice(3), read); + read.release(); + + assertNull(channel.readInbound()); + buf.release(); + } + + @Test + public void testFramesDecoded2() { + ByteBuf buf = Unpooled.buffer(); + for (int i = 0; i < 9; i++) { + buf.writeByte(i); + } + ByteBuf input = buf.duplicate(); + + EmbeddedChannel channel = new EmbeddedChannel( + new FixedLengthFrameDecoder(3)); + assertFalse(channel.writeInbound(input.readBytes(2))); + assertTrue(channel.writeInbound(input.readBytes(7))); + + assertTrue(channel.finish()); + ByteBuf read = (ByteBuf) channel.readInbound(); + assertEquals(buf.readSlice(3), read); + read.release(); + + read = (ByteBuf) channel.readInbound(); + assertEquals(buf.readSlice(3), read); + read.release(); + + read = (ByteBuf) channel.readInbound(); + assertEquals(buf.readSlice(3), read); + read.release(); + + assertNull(channel.readInbound()); + buf.release(); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/FrameChunkDecoderTest.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/FrameChunkDecoderTest.java" new file mode 100644 index 0000000..afb883b --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/FrameChunkDecoderTest.java" @@ -0,0 +1,51 @@ +package nia.test.chapter9; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.embedded.EmbeddedChannel; +import io.netty.handler.codec.TooLongFrameException; +import nia.chapter9.FrameChunkDecoder; +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Listing 9.6 Testing FrameChunkDecoder + * + * @author Norman Maurer + */ + +public class FrameChunkDecoderTest { + @Test + public void testFramesDecoded() { + ByteBuf buf = Unpooled.buffer(); + for (int i = 0; i < 9; i++) { + buf.writeByte(i); + } + ByteBuf input = buf.duplicate(); + + EmbeddedChannel channel = new EmbeddedChannel( + new FrameChunkDecoder(3)); + + assertTrue(channel.writeInbound(input.readBytes(2))); + try { + channel.writeInbound(input.readBytes(4)); + Assert.fail(); + } catch (TooLongFrameException e) { + // expected exception + } + assertTrue(channel.writeInbound(input.readBytes(3))); + assertTrue(channel.finish()); + + // Read frames + ByteBuf read = (ByteBuf) channel.readInbound(); + assertEquals(buf.readSlice(2), read); + read.release(); + + read = (ByteBuf) channel.readInbound(); + assertEquals(buf.skipBytes(4).readSlice(3), read); + read.release(); + buf.release(); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/package-info.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/package-info.java" new file mode 100644 index 0000000..ccfd2d7 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/chapter9/src/test/java/nia/test/chapter9/package-info.java" @@ -0,0 +1,10 @@ +/** + * Created by kerr. + * + * Listing 9.2 Testing the FixedLengthFrameDecoder {@link nia.test.chapter9.FixedLengthFrameDecoderTest} + * + * Listing 9.4 Testing the AbsIntegerEncoder {@link nia.test.chapter9.AbsIntegerEncoderTest} + * + * Listing 9.6 Testing FrameChunkDecoder {@link nia.test.chapter9.FrameChunkDecoderTest} + */ +package nia.test.chapter9; \ No newline at end of file diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/pom.xml" new file mode 100644 index 0000000..76911a2 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/pom.xml" @@ -0,0 +1,102 @@ + + + 4.0.0 + nia + nia-samples-parent + 2.0-SNAPSHOT + pom + Sample Code for Netty in Action + + utils + chapter1 + chapter2 + chapter4 + chapter5 + chapter6 + chapter7 + chapter8 + chapter9 + chapter10 + chapter11 + chapter12 + chapter13 + + + 4.11 + 4.1.12.Final + UTF-8 + UTF-8 + + + + + io.netty + netty-all + ${netty.version} + + + nia + utils + 2.0-SNAPSHOT + + + junit + junit + ${junit.version} + + + + + + io.netty + netty-all + + + nia + utils + + + junit + junit + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.7 + 1.7 + + + + maven-clean-plugin + 2.6.1 + + + maven-failsafe-plugin + 2.18.1 + + + maven-surefire-plugin + 2.18.1 + + + org.apache.maven.plugins + maven-jar-plugin + 2.6 + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + + + + + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/pom.xml" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/pom.xml" new file mode 100644 index 0000000..1079cd7 --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/pom.xml" @@ -0,0 +1,24 @@ + + + 4.0.0 + + + 2.0-SNAPSHOT + nia + utils + Common Utilities + + + + + maven-compiler-plugin + 3.3 + + 1.7 + 1.8 + + + + + diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/src/main/java/nia/util/BogusKeyStore.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/src/main/java/nia/util/BogusKeyStore.java" new file mode 100644 index 0000000..5edbaad --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/src/main/java/nia/util/BogusKeyStore.java" @@ -0,0 +1,150 @@ +package nia.util; + +/** + * @author Norman Maurer + */ + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +/** + * Fork of Netty + */ +public final class BogusKeyStore { + private static final short[] DATA = + new short[]{0xfe, 0xed, 0xfe, 0xed, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, + 0x00, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x00, 0x00, 0x01, 0x1a, 0x9f, 0x57, 0xa5, 0x27, 0x00, + 0x00, 0x01, 0x9a, 0x30, 0x82, 0x01, 0x96, 0x30, 0x0e, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x2a, 0x02, + 0x11, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x01, 0x82, 0x48, 0x6d, 0xcf, 0x16, 0xb5, 0x50, 0x95, 0x36, 0xbf, + 0x47, 0x27, 0x50, 0x58, 0x0d, 0xa2, 0x52, 0x7e, 0x25, 0xab, 0x14, 0x1a, 0x26, 0x5e, 0x2d, 0x8a, 0x23, 0x90, + 0x60, 0x7f, 0x12, 0x20, 0x56, 0xd1, 0x43, 0xa2, 0x6b, 0x47, 0x5d, 0xed, 0x9d, 0xd4, 0xe5, 0x83, 0x28, 0x89, + 0xc2, 0x16, 0x4c, 0x76, 0x06, 0xad, 0x8e, 0x8c, 0x29, 0x1a, 0x9b, 0x0f, 0xdd, 0x60, 0x4b, 0xb4, 0x62, 0x82, + 0x9e, 0x4a, 0x63, 0x83, 0x2e, 0xd2, 0x43, 0x78, 0xc2, 0x32, 0x1f, 0x60, 0xa9, 0x8a, 0x7f, 0x0f, 0x7c, 0xa6, + 0x1d, 0xe6, 0x92, 0x9e, 0x52, 0xc7, 0x7d, 0xbb, 0x35, 0x3b, 0xaa, 0x89, 0x73, 0x4c, 0xfb, 0x99, 0x54, 0x97, + 0x99, 0x28, 0x6e, 0x66, 0x5b, 0xf7, 0x9b, 0x7e, 0x6d, 0x8a, 0x2f, 0xfa, 0xc3, 0x1e, 0x71, 0xb9, 0xbd, 0x8f, + 0xc5, 0x63, 0x25, 0x31, 0x20, 0x02, 0xff, 0x02, 0xf0, 0xc9, 0x2c, 0xdd, 0x3a, 0x10, 0x30, 0xab, 0xe5, 0xad, + 0x3d, 0x1a, 0x82, 0x77, 0x46, 0xed, 0x03, 0x38, 0xa4, 0x73, 0x6d, 0x36, 0x36, 0x33, 0x70, 0xb2, 0x63, 0x20, + 0xca, 0x03, 0xbf, 0x5a, 0xf4, 0x7c, 0x35, 0xf0, 0x63, 0x1a, 0x12, 0x33, 0x12, 0x58, 0xd9, 0xa2, 0x63, 0x6b, + 0x63, 0x82, 0x41, 0x65, 0x70, 0x37, 0x4b, 0x99, 0x04, 0x9f, 0xdd, 0x5e, 0x07, 0x01, 0x95, 0x9f, 0x36, 0xe8, + 0xc3, 0x66, 0x2a, 0x21, 0x69, 0x68, 0x40, 0xe6, 0xbc, 0xbb, 0x85, 0x81, 0x21, 0x13, 0xe6, 0xa4, 0xcf, 0xd3, + 0x67, 0xe3, 0xfd, 0x75, 0xf0, 0xdf, 0x83, 0xe0, 0xc5, 0x36, 0x09, 0xac, 0x1b, 0xd4, 0xf7, 0x2a, 0x23, 0x57, + 0x1c, 0x5c, 0x0f, 0xf4, 0xcf, 0xa2, 0xcf, 0xf5, 0xbd, 0x9c, 0x69, 0x98, 0x78, 0x3a, 0x25, 0xe4, 0xfd, 0x85, + 0x11, 0xcc, 0x7d, 0xef, 0xeb, 0x74, 0x60, 0xb1, 0xb7, 0xfb, 0x1f, 0x0e, 0x62, 0xff, 0xfe, 0x09, 0x0a, 0xc3, + 0x80, 0x2f, 0x10, 0x49, 0x89, 0x78, 0xd2, 0x08, 0xfa, 0x89, 0x22, 0x45, 0x91, 0x21, 0xbc, 0x90, 0x3e, 0xad, + 0xb3, 0x0a, 0xb4, 0x0e, 0x1c, 0xa1, 0x93, 0x92, 0xd8, 0x72, 0x07, 0x54, 0x60, 0xe7, 0x91, 0xfc, 0xd9, 0x3c, + 0xe1, 0x6f, 0x08, 0xe4, 0x56, 0xf6, 0x0b, 0xb0, 0x3c, 0x39, 0x8a, 0x2d, 0x48, 0x44, 0x28, 0x13, 0xca, 0xe9, + 0xf7, 0xa3, 0xb6, 0x8a, 0x5f, 0x31, 0xa9, 0x72, 0xf2, 0xde, 0x96, 0xf2, 0xb1, 0x53, 0xb1, 0x3e, 0x24, 0x57, + 0xfd, 0x18, 0x45, 0x1f, 0xc5, 0x33, 0x1b, 0xa4, 0xe8, 0x21, 0xfa, 0x0e, 0xb2, 0xb9, 0xcb, 0xc7, 0x07, 0x41, + 0xdd, 0x2f, 0xb6, 0x6a, 0x23, 0x18, 0xed, 0xc1, 0xef, 0xe2, 0x4b, 0xec, 0xc9, 0xba, 0xfb, 0x46, 0x43, 0x90, + 0xd7, 0xb5, 0x68, 0x28, 0x31, 0x2b, 0x8d, 0xa8, 0x51, 0x63, 0xf7, 0x53, 0x99, 0x19, 0x68, 0x85, 0x66, 0x00, + 0x00, 0x00, 0x01, 0x00, 0x05, 0x58, 0x2e, 0x35, 0x30, 0x39, 0x00, 0x00, 0x02, 0x3a, 0x30, 0x82, 0x02, 0x36, + 0x30, 0x82, 0x01, 0xe0, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x04, 0x48, 0x59, 0xf1, 0x92, 0x30, 0x0d, 0x06, + 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x81, 0xa0, 0x31, 0x0b, 0x30, + 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x4b, 0x52, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, + 0x08, 0x13, 0x0a, 0x4b, 0x79, 0x75, 0x6e, 0x67, 0x67, 0x69, 0x2d, 0x64, 0x6f, 0x31, 0x14, 0x30, 0x12, 0x06, + 0x03, 0x55, 0x04, 0x07, 0x13, 0x0b, 0x53, 0x65, 0x6f, 0x6e, 0x67, 0x6e, 0x61, 0x6d, 0x2d, 0x73, 0x69, 0x31, + 0x1a, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x11, 0x54, 0x68, 0x65, 0x20, 0x4e, 0x65, 0x74, 0x74, + 0x79, 0x20, 0x50, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0b, + 0x13, 0x0f, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x73, 0x31, + 0x30, 0x30, 0x2e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x27, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x63, 0x68, + 0x61, 0x74, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6e, 0x65, 0x74, 0x74, 0x79, 0x2e, 0x67, + 0x6c, 0x65, 0x61, 0x6d, 0x79, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x6e, 0x65, 0x74, 0x30, 0x20, 0x17, 0x0d, 0x30, + 0x38, 0x30, 0x36, 0x31, 0x39, 0x30, 0x35, 0x34, 0x31, 0x33, 0x38, 0x5a, 0x18, 0x0f, 0x32, 0x31, 0x38, 0x37, + 0x31, 0x31, 0x32, 0x34, 0x30, 0x35, 0x34, 0x31, 0x33, 0x38, 0x5a, 0x30, 0x81, 0xa0, 0x31, 0x0b, 0x30, 0x09, + 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x4b, 0x52, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, + 0x13, 0x0a, 0x4b, 0x79, 0x75, 0x6e, 0x67, 0x67, 0x69, 0x2d, 0x64, 0x6f, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, + 0x55, 0x04, 0x07, 0x13, 0x0b, 0x53, 0x65, 0x6f, 0x6e, 0x67, 0x6e, 0x61, 0x6d, 0x2d, 0x73, 0x69, 0x31, 0x1a, + 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x11, 0x54, 0x68, 0x65, 0x20, 0x4e, 0x65, 0x74, 0x74, 0x79, + 0x20, 0x50, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, + 0x0f, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x73, 0x31, 0x30, + 0x30, 0x2e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x27, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x63, 0x68, 0x61, + 0x74, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6e, 0x65, 0x74, 0x74, 0x79, 0x2e, 0x67, 0x6c, + 0x65, 0x61, 0x6d, 0x79, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x6e, 0x65, 0x74, 0x30, 0x5c, 0x30, 0x0d, 0x06, 0x09, + 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 0x00, 0x30, 0x48, 0x02, 0x41, + 0x00, 0xc3, 0xe3, 0x5e, 0x41, 0xa7, 0x87, 0x11, 0x00, 0x42, 0x2a, 0xb0, 0x4b, 0xed, 0xb2, 0xe0, 0x23, 0xdb, + 0xb1, 0x3d, 0x58, 0x97, 0x35, 0x60, 0x0b, 0x82, 0x59, 0xd3, 0x00, 0xea, 0xd4, 0x61, 0xb8, 0x79, 0x3f, 0xb6, + 0x3c, 0x12, 0x05, 0x93, 0x2e, 0x9a, 0x59, 0x68, 0x14, 0x77, 0x3a, 0xc8, 0x50, 0x25, 0x57, 0xa4, 0x49, 0x18, + 0x63, 0x41, 0xf0, 0x2d, 0x28, 0xec, 0x06, 0xfb, 0xb4, 0x9f, 0xbf, 0x02, 0x03, 0x01, 0x00, 0x01, 0x30, 0x0d, + 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x41, 0x00, 0x65, 0x6c, + 0x30, 0x01, 0xc2, 0x8e, 0x3e, 0xcb, 0xb3, 0x77, 0x48, 0xe9, 0x66, 0x61, 0x9a, 0x40, 0x86, 0xaf, 0xf6, 0x03, + 0xeb, 0xba, 0x6a, 0xf2, 0xfd, 0xe2, 0xaf, 0x36, 0x5e, 0x7b, 0xaa, 0x22, 0x04, 0xdd, 0x2c, 0x20, 0xc4, 0xfc, + 0xdd, 0xd0, 0x82, 0x20, 0x1c, 0x3d, 0xd7, 0x9e, 0x5e, 0x5c, 0x92, 0x5a, 0x76, 0x71, 0x28, 0xf5, 0x07, 0x7d, + 0xa2, 0x81, 0xba, 0x77, 0x9f, 0x2a, 0xd9, 0x44, 0x00, 0x00, 0x00, 0x01, 0x00, 0x05, 0x6d, 0x79, 0x6b, 0x65, + 0x79, 0x00, 0x00, 0x01, 0x1a, 0x9f, 0x5b, 0x56, 0xa0, 0x00, 0x00, 0x01, 0x99, 0x30, 0x82, 0x01, 0x95, 0x30, + 0x0e, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x2a, 0x02, 0x11, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x01, + 0x81, 0x29, 0xa8, 0xb6, 0x08, 0x0c, 0x85, 0x75, 0x3e, 0xdd, 0xb5, 0xe5, 0x1a, 0x87, 0x68, 0xd1, 0x90, 0x4b, + 0x29, 0x31, 0xee, 0x90, 0xbc, 0x9d, 0x73, 0xa0, 0x3f, 0xe9, 0x0b, 0xa4, 0xef, 0x30, 0x9b, 0x36, 0x9a, 0xb2, + 0x54, 0x77, 0x81, 0x07, 0x4b, 0xaa, 0xa5, 0x77, 0x98, 0xe1, 0xeb, 0xb5, 0x7c, 0x4e, 0x48, 0xd5, 0x08, 0xfc, + 0x2c, 0x36, 0xe2, 0x65, 0x03, 0xac, 0xe5, 0xf3, 0x96, 0xb7, 0xd0, 0xb5, 0x3b, 0x92, 0xe4, 0x14, 0x05, 0x7a, + 0x6a, 0x92, 0x56, 0xfe, 0x4e, 0xab, 0xd3, 0x0e, 0x32, 0x04, 0x22, 0x22, 0x74, 0x47, 0x7d, 0xec, 0x21, 0x99, + 0x30, 0x31, 0x64, 0x46, 0x64, 0x9b, 0xc7, 0x13, 0xbf, 0xbe, 0xd0, 0x31, 0x49, 0xe7, 0x3c, 0xbf, 0xba, 0xb1, + 0x20, 0xf9, 0x42, 0xf4, 0xa9, 0xa9, 0xe5, 0x13, 0x65, 0x32, 0xbf, 0x7c, 0xcc, 0x91, 0xd3, 0xfd, 0x24, 0x47, + 0x0b, 0xe5, 0x53, 0xad, 0x50, 0x30, 0x56, 0xd1, 0xfa, 0x9c, 0x37, 0xa8, 0xc1, 0xce, 0xf6, 0x0b, 0x18, 0xaa, + 0x7c, 0xab, 0xbd, 0x1f, 0xdf, 0xe4, 0x80, 0xb8, 0xa7, 0xe0, 0xad, 0x7d, 0x50, 0x74, 0xf1, 0x98, 0x78, 0xbc, + 0x58, 0xb9, 0xc2, 0x52, 0xbe, 0xd2, 0x5b, 0x81, 0x94, 0x83, 0x8f, 0xb9, 0x4c, 0xee, 0x01, 0x2b, 0x5e, 0xc9, + 0x6e, 0x9b, 0xf5, 0x63, 0x69, 0xe4, 0xd8, 0x0b, 0x47, 0xd8, 0xfd, 0xd8, 0xe0, 0xed, 0xa8, 0x27, 0x03, 0x74, + 0x1e, 0x5d, 0x32, 0xe6, 0x5c, 0x63, 0xc2, 0xfb, 0x3f, 0xee, 0xb4, 0x13, 0xc6, 0x0e, 0x6e, 0x74, 0xe0, 0x22, + 0xac, 0xce, 0x79, 0xf9, 0x43, 0x68, 0xc1, 0x03, 0x74, 0x2b, 0xe1, 0x18, 0xf8, 0x7f, 0x76, 0x9a, 0xea, 0x82, + 0x3f, 0xc2, 0xa6, 0xa7, 0x4c, 0xfe, 0xae, 0x29, 0x3b, 0xc1, 0x10, 0x7c, 0xd5, 0x77, 0x17, 0x79, 0x5f, 0xcb, + 0xad, 0x1f, 0xd8, 0xa1, 0xfd, 0x90, 0xe1, 0x6b, 0xb2, 0xef, 0xb9, 0x41, 0x26, 0xa4, 0x0b, 0x4f, 0xc6, 0x83, + 0x05, 0x6f, 0xf0, 0x64, 0x40, 0xe1, 0x44, 0xc4, 0xf9, 0x40, 0x2b, 0x3b, 0x40, 0xdb, 0xaf, 0x35, 0xa4, 0x9b, + 0x9f, 0xc4, 0x74, 0x07, 0xe5, 0x18, 0x60, 0xc5, 0xfe, 0x15, 0x0e, 0x3a, 0x25, 0x2a, 0x11, 0xee, 0x78, 0x2f, + 0xb8, 0xd1, 0x6e, 0x4e, 0x3c, 0x0a, 0xb5, 0xb9, 0x40, 0x86, 0x27, 0x6d, 0x8f, 0x53, 0xb7, 0x77, 0x36, 0xec, + 0x5d, 0xed, 0x32, 0x40, 0x43, 0x82, 0xc3, 0x52, 0x58, 0xc4, 0x26, 0x39, 0xf3, 0xb3, 0xad, 0x58, 0xab, 0xb7, + 0xf7, 0x8e, 0x0e, 0xba, 0x8e, 0x78, 0x9d, 0xbf, 0x58, 0x34, 0xbd, 0x77, 0x73, 0xa6, 0x50, 0x55, 0x00, 0x60, + 0x26, 0xbf, 0x6d, 0xb4, 0x98, 0x8a, 0x18, 0x83, 0x89, 0xf8, 0xcd, 0x0d, 0x49, 0x06, 0xae, 0x51, 0x6e, 0xaf, + 0xbd, 0xe2, 0x07, 0x13, 0xd8, 0x64, 0xcc, 0xbf, 0x00, 0x00, 0x00, 0x01, 0x00, 0x05, 0x58, 0x2e, 0x35, 0x30, + 0x39, 0x00, 0x00, 0x02, 0x34, 0x30, 0x82, 0x02, 0x30, 0x30, 0x82, 0x01, 0xda, 0xa0, 0x03, 0x02, 0x01, 0x02, + 0x02, 0x04, 0x48, 0x59, 0xf2, 0x84, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, + 0x05, 0x05, 0x00, 0x30, 0x81, 0x9d, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x4b, + 0x52, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x0a, 0x4b, 0x79, 0x75, 0x6e, 0x67, 0x67, + 0x69, 0x2d, 0x64, 0x6f, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x0b, 0x53, 0x65, 0x6f, + 0x6e, 0x67, 0x6e, 0x61, 0x6d, 0x2d, 0x73, 0x69, 0x31, 0x1a, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, + 0x11, 0x54, 0x68, 0x65, 0x20, 0x4e, 0x65, 0x74, 0x74, 0x79, 0x20, 0x50, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, + 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x0c, 0x43, 0x6f, 0x6e, 0x74, 0x72, 0x69, 0x62, + 0x75, 0x74, 0x6f, 0x72, 0x73, 0x31, 0x30, 0x30, 0x2e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x27, 0x73, 0x65, + 0x63, 0x75, 0x72, 0x65, 0x63, 0x68, 0x61, 0x74, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6e, + 0x65, 0x74, 0x74, 0x79, 0x2e, 0x67, 0x6c, 0x65, 0x61, 0x6d, 0x79, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x6e, 0x65, + 0x74, 0x30, 0x20, 0x17, 0x0d, 0x30, 0x38, 0x30, 0x36, 0x31, 0x39, 0x30, 0x35, 0x34, 0x35, 0x34, 0x30, 0x5a, + 0x18, 0x0f, 0x32, 0x31, 0x38, 0x37, 0x31, 0x31, 0x32, 0x33, 0x30, 0x35, 0x34, 0x35, 0x34, 0x30, 0x5a, 0x30, + 0x81, 0x9d, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x4b, 0x52, 0x31, 0x13, 0x30, + 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x0a, 0x4b, 0x79, 0x75, 0x6e, 0x67, 0x67, 0x69, 0x2d, 0x64, 0x6f, + 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x0b, 0x53, 0x65, 0x6f, 0x6e, 0x67, 0x6e, 0x61, + 0x6d, 0x2d, 0x73, 0x69, 0x31, 0x1a, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x11, 0x54, 0x68, 0x65, + 0x20, 0x4e, 0x65, 0x74, 0x74, 0x79, 0x20, 0x50, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x31, 0x15, 0x30, 0x13, + 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x0c, 0x43, 0x6f, 0x6e, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x6f, 0x72, + 0x73, 0x31, 0x30, 0x30, 0x2e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x27, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, + 0x63, 0x68, 0x61, 0x74, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6e, 0x65, 0x74, 0x74, 0x79, + 0x2e, 0x67, 0x6c, 0x65, 0x61, 0x6d, 0x79, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x6e, 0x65, 0x74, 0x30, 0x5c, 0x30, + 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 0x00, 0x30, + 0x48, 0x02, 0x41, 0x00, 0x95, 0xb3, 0x47, 0x17, 0x95, 0x0f, 0x57, 0xcf, 0x66, 0x72, 0x0a, 0x7e, 0x5b, 0x54, + 0xea, 0x8c, 0x6f, 0x79, 0xde, 0x94, 0xac, 0x0b, 0x5a, 0xd4, 0xd6, 0x1b, 0x58, 0x12, 0x1a, 0x16, 0x3d, 0xfe, + 0xdf, 0xa5, 0x2b, 0x86, 0xbc, 0x64, 0xd4, 0x80, 0x1e, 0x3f, 0xf9, 0xe2, 0x04, 0x03, 0x79, 0x9b, 0xc1, 0x5c, + 0xf0, 0xf1, 0xf3, 0xf1, 0xe3, 0xbf, 0x3f, 0xc0, 0x1f, 0xdd, 0xdb, 0xc0, 0x5b, 0x21, 0x02, 0x03, 0x01, 0x00, + 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x41, + 0x00, 0x02, 0xd7, 0xdd, 0xbd, 0x0c, 0x8e, 0x21, 0x20, 0xef, 0x9e, 0x4f, 0x1f, 0xf5, 0x49, 0xf1, 0xae, 0x58, + 0x9b, 0x94, 0x3a, 0x1f, 0x70, 0x33, 0xf0, 0x9b, 0xbb, 0xe9, 0xc0, 0xf3, 0x72, 0xcb, 0xde, 0xb6, 0x56, 0x72, + 0xcc, 0x1c, 0xf0, 0xd6, 0x5a, 0x2a, 0xbc, 0xa1, 0x7e, 0x23, 0x83, 0xe9, 0xe7, 0xcf, 0x9e, 0xa5, 0xf9, 0xcc, + 0xc2, 0x61, 0xf4, 0xdb, 0x40, 0x93, 0x1d, 0x63, 0x8a, 0x50, 0x4c, 0x11, 0x39, 0xb1, 0x91, 0xc1, 0xe6, 0x9d, + 0xd9, 0x1a, 0x62, 0x1b, 0xb8, 0xd3, 0xd6, 0x9a, 0x6d, 0xb9, 0x8e, 0x15, 0x51}; + + private BogusKeyStore() { + // Unused + } + + public static InputStream asInputStream() { + byte[] data = new byte[DATA.length]; + for (int i = 0; i < data.length; i++) { + data[i] = (byte) DATA[i]; + } + return new ByteArrayInputStream(data); + } + + public static char[] getCertificatePassword() { + return "secret".toCharArray(); + } + + public static char[] getKeyStorePassword() { + return "secret".toCharArray(); + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/src/main/java/nia/util/BogusSslContextFactory.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/src/main/java/nia/util/BogusSslContextFactory.java" new file mode 100644 index 0000000..e6d5ffd --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/src/main/java/nia/util/BogusSslContextFactory.java" @@ -0,0 +1,47 @@ +package nia.util; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import java.security.KeyStore; +import java.security.Security; + +/** + * Fork of Netty + */ +public final class BogusSslContextFactory { + private static final String PROTOCOL = "TLS"; + + private static final SSLContext SERVER_CONTEXT; + + static { + String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm"); + if (algorithm == null) { + algorithm = "SunX509"; + } + + SSLContext serverContext = null; + try { + KeyStore ks = KeyStore.getInstance("JKS"); + ks.load(BogusKeyStore.asInputStream(), BogusKeyStore.getKeyStorePassword()); + + // Set up key manager factory to use our key store + KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm); + kmf.init(ks, BogusKeyStore.getCertificatePassword()); + + // Initialize the SSLContext to work with our key managers. + serverContext = SSLContext.getInstance(PROTOCOL); + serverContext.init(kmf.getKeyManagers(), null, null); + } catch (Exception e) { + throw new Error("Failed to initialize the server-side SSLContext", e); + } + SERVER_CONTEXT = serverContext; + } + + private BogusSslContextFactory() { + // Unused + } + + public static SSLContext getServerContext() { + return SERVER_CONTEXT; + } +} diff --git "a/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/src/main/java/nia/util/BogusTrustManagerFactory.java" "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/src/main/java/nia/util/BogusTrustManagerFactory.java" new file mode 100644 index 0000000..46cec5b --- /dev/null +++ "b/\347\233\270\345\205\263\350\265\204\346\226\231/netty-in-action-2.0-SNAPSHOT/utils/src/main/java/nia/util/BogusTrustManagerFactory.java" @@ -0,0 +1,59 @@ +package nia.util; + +import javax.net.ssl.ManagerFactoryParameters; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactorySpi; +import javax.net.ssl.X509TrustManager; +import java.security.InvalidAlgorithmParameterException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + +/** + * Fork of Netty + */ +public class BogusTrustManagerFactory + extends TrustManagerFactorySpi { + private static final TrustManager DUMMY_TRUST_MANAGER = new X509TrustManager() { + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) + throws CertificateException { + // Always trust - it is an example. + // You should do something in the real world. + // You will reach here only if you enabled client certificate auth, + // as described in SecureChatSslContextFactory. + System.err.println("UNKNOWN CLIENT CERTIFICATE: " + chain[0].getSubjectDN()); + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) + throws CertificateException { + // Always trust - it is an example. + // You should do something in the real world. + System.err.println("UNKNOWN SERVER CERTIFICATE: " + chain[0].getSubjectDN()); + } + }; + + @Override + protected TrustManager[] engineGetTrustManagers() { + return new TrustManager[]{DUMMY_TRUST_MANAGER}; + } + + @Override + protected void engineInit(KeyStore keystore) + throws KeyStoreException { + // Unused + } + + @Override + protected void engineInit(ManagerFactoryParameters managerFactoryParameters) + throws InvalidAlgorithmParameterException { + // Unused + } +}