Home / Class/ QuoteOfTheMomentServerHandler Class — netty Architecture

QuoteOfTheMomentServerHandler Class — netty Architecture

Architecture documentation for the QuoteOfTheMomentServerHandler class in QuoteOfTheMomentServerHandler.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  5c162ba2_22d0_f0b3_81f4_3c1de8fec397["QuoteOfTheMomentServerHandler"]
  77e2c8ca_16c0_6fea_b00f_194553a93f06["QuoteOfTheMomentServerHandler.java"]
  5c162ba2_22d0_f0b3_81f4_3c1de8fec397 -->|defined in| 77e2c8ca_16c0_6fea_b00f_194553a93f06
  0ebea5fe_c339_735b_bc00_8745d9949e2f["String()"]
  5c162ba2_22d0_f0b3_81f4_3c1de8fec397 -->|method| 0ebea5fe_c339_735b_bc00_8745d9949e2f
  efe006c7_afef_70b9_ff37_984c0e9e3844["channelRead0()"]
  5c162ba2_22d0_f0b3_81f4_3c1de8fec397 -->|method| efe006c7_afef_70b9_ff37_984c0e9e3844
  8d0f2e9f_9489_38cd_72f0_b6d4fa793ab7["channelReadComplete()"]
  5c162ba2_22d0_f0b3_81f4_3c1de8fec397 -->|method| 8d0f2e9f_9489_38cd_72f0_b6d4fa793ab7
  87836f5b_9ffd_e694_d37d_a801970d1943["exceptionCaught()"]
  5c162ba2_22d0_f0b3_81f4_3c1de8fec397 -->|method| 87836f5b_9ffd_e694_d37d_a801970d1943

Relationship Graph

Source Code

example/src/main/java/io/netty/example/qotm/QuoteOfTheMomentServerHandler.java lines 26–65

public class QuoteOfTheMomentServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {

    private static final Random random = new Random();

    // Quotes from Mohandas K. Gandhi:
    private static final String[] quotes = {
        "Where there is love there is life.",
        "First they ignore you, then they laugh at you, then they fight you, then you win.",
        "Be the change you want to see in the world.",
        "The weak can never forgive. Forgiveness is the attribute of the strong.",
    };

    private static String nextQuote() {
        int quoteId;
        synchronized (random) {
            quoteId = random.nextInt(quotes.length);
        }
        return quotes[quoteId];
    }

    @Override
    public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
        System.err.println(packet);
        if ("QOTM?".equals(packet.content().toString(CharsetUtil.UTF_8))) {
            ctx.write(new DatagramPacket(
                    Unpooled.copiedBuffer("QOTM: " + nextQuote(), CharsetUtil.UTF_8), packet.sender()));
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        // We don't close the channel because we can keep serving requests.
    }
}

Frequently Asked Questions

What is the QuoteOfTheMomentServerHandler class?
QuoteOfTheMomentServerHandler is a class in the netty codebase, defined in example/src/main/java/io/netty/example/qotm/QuoteOfTheMomentServerHandler.java.
Where is QuoteOfTheMomentServerHandler defined?
QuoteOfTheMomentServerHandler is defined in example/src/main/java/io/netty/example/qotm/QuoteOfTheMomentServerHandler.java at line 26.

Analyze Your Own Codebase

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

Try Supermodel Free