VideoServerApp.java 8.44 KB
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.genersoft.iot.vmp.jt1078.app;

import com.genersoft.iot.vmp.jt1078.http.GeneralResponseWriter;
import com.genersoft.iot.vmp.jt1078.http.NettyHttpServerHandler;
import com.genersoft.iot.vmp.jt1078.publisher.PublishManager;
import com.genersoft.iot.vmp.jt1078.server.Jtt1078Handler;
import com.genersoft.iot.vmp.jt1078.server.Jtt1078MessageDecoder;
import com.genersoft.iot.vmp.jt1078.util.Configs;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
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.InetAddress;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.Signal;
import sun.misc.SignalHandler;

public class VideoServerApp {
    private static Logger logger = LoggerFactory.getLogger(VideoServerApp.class);

    public VideoServerApp() {
    }

    public void createListenter() throws Exception {
        Configs.init("/app.properties");
        PublishManager.init();
        String tagMapping = null;
        final VideoServer videoServer = new VideoServer();
        final HttpServer httpServer = new HttpServer();
        Signal.handle(new Signal("TERM"), new SignalHandler() {
            public void handle(Signal signal) {
                videoServer.shutdown();
                HttpServer var10000 = httpServer;
                VideoServerApp.HttpServer.shutdown();
            }
        });
        VideoServerApp.VideoServer.getInstance().start(11078, (String) tagMapping, 3333);
        VideoServerApp.HttpServer.start((String) tagMapping, 3333);
    }

    public void newVideoServer(final String key, int port, final int httpPort) throws Exception {
        VideoServer videoServer = new VideoServer();
        HttpServer httpServer = null;
        boolean videoFlag = false;
        boolean httpFlag = false;
        long nw = (new Date()).getTime();
        VideoServer sourceVideoServer = (VideoServer) CacheMapUtil.getCacheMapUtil().videoServerMap.get(port);
        if (Objects.isNull(sourceVideoServer)) {
            videoServer = new VideoServer();
            videoFlag = true;
            CacheMapUtil.getCacheMapUtil().videoServerMap.put(port, videoServer);
        }
        if (videoFlag) {
            videoServer.start(port, key, httpPort);
        }
    }

    public static void stopServer(int port, Integer httpPort) {
        stopVideoServer(port);
//        stopHttpServer(httpPort);
//        channelMappingMap.remove(httpPort);
    }

    public  void stopServer2(int port, int httpPort) {
        stopVideoServer(port);
//        stopHttpServer(httpPort);
//        channelMappingMap.remove(httpPort);
    }


    private static void stopVideoServer(Integer port) {
        VideoServer videoServer = (VideoServer)  CacheMapUtil.getCacheMapUtil().videoServerMap.get(port);
        if (Objects.nonNull(videoServer)) {
            videoServer.shutdown();
            CacheMapUtil.getCacheMapUtil().videoServerMap.remove(port);
        }

    }

    private static void stopHttpServer(Integer port) {


    }

    static class HttpServer {
        private static ServerBootstrap serverBootstrap;
        private static EventLoopGroup bossGroup;
        private static EventLoopGroup workerGroup;
        private static Integer httpPort;

        HttpServer() {
        }

        private static void start(final String tagMapping, final int port) throws Exception {
            bossGroup = new NioEventLoopGroup();
            workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors());
            httpPort = port;
            ServerBootstrap bootstrap = new ServerBootstrap();
            ((ServerBootstrap) ((ServerBootstrap) bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)).childHandler(new ChannelInitializer<SocketChannel>() {
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new ChannelHandler[]{new GeneralResponseWriter(), new HttpResponseEncoder(), new HttpRequestDecoder(), new HttpObjectAggregator(65536), new NettyHttpServerHandler(tagMapping, port)});
                }
            }).option(ChannelOption.SO_BACKLOG, 1024)).childOption(ChannelOption.SO_KEEPALIVE, true);

            try {
                InterruptedException e;
                try {
                    ChannelFuture f = bootstrap.bind(InetAddress.getByName("0.0.0.0"), port).sync();
                    VideoServerApp.logger.info("HTTP Server started at: {}", port);

                    f.channel().closeFuture().sync();
                    e = null;
                } catch (InterruptedException var7) {
                    e = var7;
                    VideoServerApp.logger.error("http server error", e);
                }
            } finally {
                shutdown();
            }

        }

        private static void shutdown() {

            try {
                bossGroup.shutdownGracefully();
            } catch (Exception e) {
                e.printStackTrace();
            }

            bossGroup = null;

            try {
                workerGroup.shutdownGracefully();
            } catch (Exception e) {
                e.printStackTrace();
            }

            workerGroup = null;
        }
    }

    static class VideoServer {
        private  ServerBootstrap serverBootstrap;
        private  EventLoopGroup bossGroup;
        private  EventLoopGroup workerGroup;
        private static Integer port;

        public void start(int port, final String tagMapping, final Integer httpPort) throws Exception {
            VideoServerApp.VideoServer.port = port;
            serverBootstrap = new ServerBootstrap();
            serverBootstrap.option(ChannelOption.SO_BACKLOG, Configs.getInt("server.backlog", 102400));
            bossGroup = new NioEventLoopGroup(Configs.getInt("server.worker-count", Runtime.getRuntime().availableProcessors()));
            workerGroup = new NioEventLoopGroup();
            serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class);
            serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
                protected void initChannel(SocketChannel channel) throws Exception {
                    ChannelPipeline p = channel.pipeline();
                    p.addLast(new ChannelHandler[]{new Jtt1078MessageDecoder()});
                    p.addLast(new ChannelHandler[]{new Jtt1078Handler(tagMapping, httpPort)});
                }
            });
            Channel ch = serverBootstrap.bind(InetAddress.getByName("0.0.0.0"), port).sync().channel();
            CacheMapUtil.getCacheMapUtil().portChannel.put(port, ch);
            VideoServerApp.logger.info("Video Server started at: {}", port);
            ch.closeFuture();
            ch = null;
            CacheMapUtil.getCacheMapUtil().portChannel.remove(port);
        }

        public void shutdown() {
            Channel channel = (Channel)  CacheMapUtil.getCacheMapUtil().portChannel.get(port);
            Exception e;
            if (Objects.nonNull(channel)) {
                try {
                    channel.closeFuture();
                } catch (Exception var5) {
                    e = var5;
                    e.printStackTrace();
                }

                channel = null;
                CacheMapUtil.getCacheMapUtil().portChannel.remove(port);
            }

            try {
                bossGroup.shutdownGracefully();
            } catch (Exception var4) {
                e = var4;
                e.printStackTrace();
            }

            try {
                workerGroup.shutdownGracefully();
            } catch (Exception var3) {
                e = var3;
                e.printStackTrace();
            }

        }

        public static VideoServer getInstance() {
            return new VideoServer();
        }
    }
}