Home / Class/ StringEchoHandler Class — netty Architecture

StringEchoHandler Class — netty Architecture

Architecture documentation for the StringEchoHandler class in SocketStringEchoTest.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  909b0e90_0599_acbc_32f0_551a36a80b00["StringEchoHandler"]
  df86325f_4f17_aff7_6451_5bba846cc2b4["SocketStringEchoTest.java"]
  909b0e90_0599_acbc_32f0_551a36a80b00 -->|defined in| df86325f_4f17_aff7_6451_5bba846cc2b4
  d2065bd2_8a3f_b9e5_58b0_ec86692ab050["StringEchoHandler()"]
  909b0e90_0599_acbc_32f0_551a36a80b00 -->|method| d2065bd2_8a3f_b9e5_58b0_ec86692ab050
  0365dabd_ad23_9ecf_235b_d3788512fe56["channelActive()"]
  909b0e90_0599_acbc_32f0_551a36a80b00 -->|method| 0365dabd_ad23_9ecf_235b_d3788512fe56
  0c8fe149_f08d_8564_0083_8ba17d525735["channelRead0()"]
  909b0e90_0599_acbc_32f0_551a36a80b00 -->|method| 0c8fe149_f08d_8564_0083_8ba17d525735
  83ecf1fc_3546_03ab_4f26_a8484653cc2f["channelReadComplete()"]
  909b0e90_0599_acbc_32f0_551a36a80b00 -->|method| 83ecf1fc_3546_03ab_4f26_a8484653cc2f
  996dbdad_00fa_5b2e_8020_52b967ecbb1f["exceptionCaught()"]
  909b0e90_0599_acbc_32f0_551a36a80b00 -->|method| 996dbdad_00fa_5b2e_8020_52b967ecbb1f
  933dd887_64a5_5916_dc12_8d7d695fe1a2["channelInactive()"]
  909b0e90_0599_acbc_32f0_551a36a80b00 -->|method| 933dd887_64a5_5916_dc12_8d7d695fe1a2

Relationship Graph

Source Code

testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketStringEchoTest.java lines 148–209

    static class StringEchoHandler extends SimpleChannelInboundHandler<String> {
        private final boolean autoRead;
        private final Promise<Void> donePromise;
        private int dataIndex;
        volatile Channel channel;
        final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();

        StringEchoHandler(boolean autoRead, Promise<Void> donePromise) {
            this.autoRead = autoRead;
            this.donePromise = donePromise;
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            channel = ctx.channel();
            if (!autoRead) {
                ctx.read();
            }
        }

        @Override
        public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
            if (!data[dataIndex].equals(msg)) {
                donePromise.tryFailure(new IllegalStateException("index: " + dataIndex + " didn't match!"));
                ctx.close();
                return;
            }

            if (channel.parent() != null) {
                String delimiter = random.nextBoolean() ? "\r\n" : "\n";
                channel.write(msg + delimiter);
            }

            if (++dataIndex >= data.length) {
                donePromise.setSuccess(null);
            }
        }

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            try {
                ctx.flush();
            } finally {
                if (!autoRead) {
                    ctx.read();
                }
            }
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            if (exception.compareAndSet(null, cause)) {
                donePromise.tryFailure(new IllegalStateException("exceptionCaught: " + ctx.channel(), cause));
                ctx.close();
            }
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            donePromise.tryFailure(new IllegalStateException("channelInactive: " + ctx.channel()));
        }
    }

Frequently Asked Questions

What is the StringEchoHandler class?
StringEchoHandler is a class in the netty codebase, defined in testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketStringEchoTest.java.
Where is StringEchoHandler defined?
StringEchoHandler is defined in testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketStringEchoTest.java at line 148.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free