org.nmrfx.server.ServerHandler.java Source code

Java tutorial

Introduction

Here is the source code for org.nmrfx.server.ServerHandler.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.nmrfx.server;

/**
 *
 * @author brucejohnson
 */
import io.netty.buffer.ByteBuf;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import java.util.function.Consumer;

/**
 * Handles a server-side channel.
 */
public class ServerHandler extends ChannelInboundHandlerAdapter { // (1)

    final Consumer<String> consumer;
    StringBuilder sBuilder = new StringBuilder();

    public ServerHandler(Consumer consumer) {
        this.consumer = consumer;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ByteBuf in = (ByteBuf) msg;
        try {
            while (in.isReadable()) { // (1)
                char ch = (char) in.readByte();
                System.out.print(ch);
                System.out.flush();
                if (ch == '\n') {
                    consumer.accept(sBuilder.toString());
                    sBuilder.setLength(0);
                } else {
                    sBuilder.append(ch);
                }
            }
        } finally {
            ReferenceCountUtil.release(msg); // (2)
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        ctx.close();
    }
}