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

org.bdware.dogp.client.NettyDOGPUDPClientChannel Maven / Gradle / Ivy

The 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.dogp.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.WriteBufferWaterMark;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.util.concurrent.Future;
import org.bdware.dogp.codec.DOGPMessageToEnvelop;
import org.bdware.dogp.codec.DatagramPacketToDOGPMessageCodec;
import org.bdware.doip.codec.MessageEnvelopeAggregator;
import org.bdware.doip.codec.MessageEnvelopePrinter;
import org.bdware.doip.codec.NaiveEnvelopeToDoMessage;
import org.bdware.doip.codec.doipMessage.MessageEnvelope;
import org.bdware.doip.endpoint.client.NettyDoipClientChannel;
import org.bdware.doip.endpoint.client.NettyDoipClientHandler;
import org.bdware.irp3.client.ClientBootstrapInitializerFactory;

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

public class NettyDOGPUDPClientChannel extends NettyDoipClientChannel {

    int clientPort;
    EventLoopGroup group;
    final Bootstrap b = new Bootstrap();

    public NettyDOGPUDPClientChannel() {
        this(0);
    }

    public NettyDOGPUDPClientChannel(int cp) {
        clientPort = cp;
    }

    @Override
    public void close() {
        remoteAddress = null;
        isConnected = false;
        if (handler != null) handler.close();
        if (group != null) {
            Future future = group.shutdownGracefully();
            future.syncUninterruptibly();
        }
    }

    @Override
    public void connect(String targetUrl) throws URISyntaxException, InterruptedException {
        URI uri = new URI(targetUrl);
        if (group == null) {
            b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);
            b.option(ChannelOption.WRITE_BUFFER_WATER_MARK,
                    new WriteBufferWaterMark(10, 100));
            b.group(ClientBootstrapInitializerFactory.workerGroup);
            super.handler = new NettyDoipClientHandler();
            b.channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, true)
                    .handler(
                            new ChannelInitializer() {
                                @Override
                                protected void initChannel(DatagramChannel ch) {
                                    ch.pipeline().addLast(new DatagramPacketToDOGPMessageCodec(false))
                                            //   .addLast(new DOGPMessagePrinter())
                                            .addLast(new DOGPSenderInjector(uri.getHost(), uri.getPort()))
                                            .addLast(new DOGPMessageToEnvelop(true))
                                            //.addLast(new MessageEnvelopePrinter())
                                            .addLast(new NaiveEnvelopeToDoMessage())
                                            .addLast(handler);
                                }
                            });
        }
        remoteAddress = new InetSocketAddress(uri.getHost(), uri.getPort());
        super.channel = b.bind(clientPort).sync().channel();
        handler.setChannel(channel);
        isConnected = true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy