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

org.bdware.doip.audit.client.AuditDoipTCPClientChannel 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 org.bdware.doip.audit.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
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.codec.MessageEnvelopeAggregator;
import org.bdware.doip.codec.MessageEnvelopeCodec;
import org.bdware.doip.codec.doipMessage.MessageEnvelope;
import org.bdware.doip.endpoint.client.NettyDoipClientChannel;

import java.net.URI;
import java.net.URISyntaxException;

public class AuditDoipTCPClientChannel extends NettyDoipClientChannel {
    static Logger LOGGER = LogManager.getLogger(AuditDoipTCPClientChannel.class);
    final Bootstrap b = new Bootstrap();
    static EventLoopGroup group;

    boolean splitEnvelop;
    int maxFrameLength;

    public AuditDoipTCPClientChannel(boolean splitEnvelop, int maxFrameLength, AuditConfig config, DoIdWrapper clientDoId, DoIdWrapper serverDoId) {
        super();
        this.splitEnvelop = splitEnvelop;
        this.maxFrameLength = maxFrameLength;
        if (group == null) {
            group = new NioEventLoopGroup();
        }
        b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);
        b.option(ChannelOption.WRITE_BUFFER_WATER_MARK,
                new WriteBufferWaterMark(2 * maxFrameLength, 10 * maxFrameLength));
        b.group(group);
        handler = new AuditDoipClientHandler(clientDoId, serverDoId, config);
        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(maxFrameLength,
                                                20, 4, 0, 0))
                                        .addLast(new MessageEnvelopeCodec());
//                                if (splitEnvelop) {
//                                    p.addLast(new NaiveEnvelopToDoMessage());
//                                } else
                                {
                                    p.addLast(new MessageEnvelopeAggregator(maxFrameLength - MessageEnvelope.ENVELOPE_LENGTH));
                                }
                                p.addLast(handler);
                            }
                        });
    }

    public AuditDoipTCPClientChannel(AuditConfig config, DoIdWrapper clientDoid, DoIdWrapper serverDoid) {
        this(false, 5 * 1024 * 1024, config, clientDoid, serverDoid);
    }

    @Override
    public void close() {
        if (handler != null) handler.close();
        isConnected = false;
    }

    @Override
    public void connect(String targetUrl) throws URISyntaxException {
        URI uri = new URI(targetUrl);
        // LOGGER.debug("[URI Parse]scheme:" + uri.getScheme() + "  host: " + uri.getHost() + "  port: " + uri.getPort());
        // LOGGER.debug("[DoipClient] Create TCP Client!");
        try {
            // LOGGER.debug("[URI Parse]host: " + uri.getHost() + "  port: " + uri.getPort());
            channel = b.connect(uri.getHost(), uri.getPort()).sync().channel();
            handler.setChannel(channel);
            isConnected = true;
        } catch (Exception e) {
            e.printStackTrace();
            isConnected = false;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy