All Downloads are FREE. Search and download functionalities are using the official Maven repository.

test.http.HttpServerTest Maven / Gradle / Ivy

There is a newer version: 1.5.4
Show newest version
/*
 *    Copyright (c) [2021] [Peking University]
 *    [BDWare DOIP SDK] is licensed under Mulan PSL v2.
 *    You can use this software according to the terms and conditions of the Mulan PSL v2.
 *    You may obtain a copy of Mulan PSL v2 at:
 *             http://license.coscl.org.cn/MulanPSL2
 *    THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *    See the Mulan PSL v2 for more details.
 */

package test.http;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
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 io.netty.handler.codec.http.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicInteger;

import static io.netty.handler.codec.http.HttpResponseStatus.OK;

public class HttpServerTest {
    static Logger LOGGER = LogManager.getLogger(HttpServerTest.class);

    public static void main(String[] args) {
        new HttpServerTest().run();
    }

    public void run() {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b1 = new ServerBootstrap();
            b1.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
            HttpHandler h = new HttpHandler();
            b1.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(21044)
                    .childHandler(
                            new ChannelInitializer() {
                                @Override
                                protected void initChannel(SocketChannel arg0) {
                                    arg0.pipeline()
                                            .addLast(new HttpServerCodec())
                                            .addLast(new HttpObjectAggregator(10 * 1024 * 1024))
                                            .addLast(h);
                                }
                            });
            final Channel ch = b1.bind(21044).sync().channel();
            LOGGER.debug("[CMHttpServer] listen master port at:" + 21044);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (; ; ) {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            LOGGER.info("Total:" + counter.get());
        }
    }
    static String longStr = getLongStr();

    public static String getLongStr() {
        StringBuilder sb = new StringBuilder("cold");
        for (int i = 0; i < 1024 * 1024; i++)
            sb.append("a");
        return sb.toString();
    }

    static AtomicInteger counter = new AtomicInteger(0);

    @ChannelHandler.Sharable
    static class HttpHandler extends SimpleChannelInboundHandler {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
            counter.incrementAndGet();
            try {
                FullHttpMessage hm = (FullHttpMessage) msg;
                String doid = ((FullHttpMessage) msg).headers().get("doid");
                DefaultFullHttpResponse response;
                if (doid != null && doid.contains("small"))
                    response =
                            new DefaultFullHttpResponse(
                                    HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer("cold".getBytes(StandardCharsets.UTF_8)));
                else
                    response = new DefaultFullHttpResponse(
                            HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(longStr
                            .getBytes(StandardCharsets.UTF_8)));

                ctx.writeAndFlush(response);
                ctx.close();
            } catch (Throwable t) {
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy