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

org.bdware.doip.audit.client.AuditIrpClientChannel Maven / Gradle / Ivy

The newest version!
package org.bdware.doip.audit.client;

import io.netty.bootstrap.Bootstrap;
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.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.writer.AuditConfig;
import org.bdware.doip.encrypt.SM2Signer;
import org.bdware.irp.irpclient.NettyIrpClientChannel;
import org.bdware.irp.irpclient.NettyIrpTCPClientChannel;
import org.bdware.irp.irplib.crypto.NettyTCPCodeC;
import org.bdware.irp.irplib.util.IrpCommon;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class AuditIrpClientChannel extends NettyIrpClientChannel {
    static Logger logger = LogManager.getLogger(NettyIrpTCPClientChannel.class);
    final Bootstrap b = new Bootstrap();
    static EventLoopGroup group = new NioEventLoopGroup(new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        }
    });


    public AuditIrpClientChannel(DoIdWrapper clientDoId, DoIdWrapper serverDoId, SM2Signer signer, AuditConfig auditConfig) {
        b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);
        b.group(group);
        handler = new AuditIrpClientHandler(clientDoId, serverDoId, signer, auditConfig);
        b.channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, true)
                .handler(
                        new ChannelInitializer() {
                            @Override
                            protected void initChannel(SocketChannel ch) {
                                ChannelPipeline p = ch.pipeline();
                                p.addLast(new LengthFieldBasedFrameDecoder(IrpCommon.MAX_MESSAGE_PACKET_LENGTH, 16, 4, 0, 0))
                                        .addLast(new NettyTCPCodeC())
                                        .addLast(handler);
                            }
                        });
    }


    @Override
    public void close() {
        logger.debug("some one called close!");
        if (handler != null) handler.close();
        isConnected = false;
    }

    static AtomicInteger channelCount = new AtomicInteger(0);

    @Override
    public synchronized void connect(String targetUrl) throws URISyntaxException {
        URI uri = new URI(targetUrl);
        logger.debug("[URI Parse]scheme:" + uri.getScheme() + "  host: " + uri.getHost() + "  port: " + uri.getPort());
        int active = -1;
        try {
            if (channel != null) {
                active = channel.isActive() ? 1 : 0;
                channel.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            String localAddr = null;
            if (channel != null)
                localAddr = channel.localAddress().toString();
            logger.debug("[AuditIrpClientChannel] " + active + " create a channel:"
                    + localAddr + "  tid:" + Thread.currentThread().getId() + " channel count:" + channelCount.get());
            channelCount.incrementAndGet();
            channel = b.connect(uri.getHost(), uri.getPort()).sync().channel();
//            Exception e = new IllegalStateException("----peek stacktrace:channel count:" + channelCount.get());
//            ByteArrayOutputStream bo = new ByteArrayOutputStream();
//            e.printStackTrace(new PrintStream(bo));
//            logger.info(bo.toString());
            handler.setChannel(channel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        isConnected = true;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy