<commit_before>/**
 * Copyright 2016 AWACS Project.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.awacs.agent;

import io.awacs.core.transport.Client;
import io.awacs.core.transport.ResponseHandler;
import io.awacs.core.util.LoggerPlus;
import io.awacs.core.util.LoggerPlusFactory;
import io.awacs.protocol.binary.BinaryMessage;
import io.awacs.protocol.binary.BinaryMessageDecoder;
import io.awacs.protocol.binary.BinaryMessageEncoder;
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 java.net.InetSocketAddress;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * Created by antong on 16/9/8.
 */
class NettyClient implements Client<BinaryMessage> {

    private static final LoggerPlus logger = LoggerPlusFactory.getLogger(NettyClient.class);

    private List<InetSocketAddress> addresses;

    private Bootstrap bootstrap;

    private EventLoopGroup group;

    private NettyChannelPool pool;

    private NettyClient(List<InetSocketAddress> addresses) {
        this.addresses = addresses;
        start();
    }

    public Bootstrap getBootstrap() {
        return bootstrap;
    }

    //TODO retrieve response from pipeline
    @Override
    public void request(BinaryMessage msg, ResponseHandler<BinaryMessage> handler) {
        ChannelFuture channelFuture = null;
        try {
            channelFuture = pool.borrowChannel();
            Channel channel = channelFuture.channel();
            channel.writeAndFlush(msg);
            logger.debug("Message send successful, remote address: {}", channel.remoteAddress());
        } catch (Exception e) {
            handler.onFailure(e);
        } finally {
            pool.returnObject(channelFuture);
        }
    }

    @Deprecated
    @Override
    public void fwrite(BinaryMessage msg) {
        ChannelFuture channelFuture = null;
        try {
            channelFuture = pool.borrowChannel();
            Channel channel = channelFuture.channel();
            channel.write(msg);
            logger.debug("Message write to buffer, remote address: {}", channel.remoteAddress());
        } finally {
            pool.returnObject(channelFuture);
        }
    }

    @Override
    public void start() {
        try {
            this.bootstrap = new Bootstrap();
            this.group = new NioEventLoopGroup();
            bootstrap.group(group);
            bootstrap.channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(new BinaryMessageDecoder());
                            pipeline.addLast(new BinaryMessageEncoder());
                        }
                    });

            pool = new NettyChannelPool(bootstrap, addresses);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void activate() {

    }

    public void stop() {
        this.group.shutdownGracefully();
        this.pool.clear();
        logger.debug("Netty client stopped.");
    }
<commit_msg>nagel<commit_after>/**
 * Copyright 2016 AWACS Project.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.awacs.agent;

import io.awacs.core.transport.Client;
import io.awacs.core.transport.ResponseHandler;
import io.awacs.core.util.LoggerPlus;
import io.awacs.core.util.LoggerPlusFactory;
import io.awacs.protocol.binary.BinaryMessage;
import io.awacs.protocol.binary.BinaryMessageDecoder;
import io.awacs.protocol.binary.BinaryMessageEncoder;
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 java.net.InetSocketAddress;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * Created by antong on 16/9/8.
 */
class NettyClient implements Client<BinaryMessage> {

    private static final LoggerPlus logger = LoggerPlusFactory.getLogger(NettyClient.class);

    private List<InetSocketAddress> addresses;

    private Bootstrap bootstrap;

    private EventLoopGroup group;

    private NettyChannelPool pool;

    private NettyClient(List<InetSocketAddress> addresses) {
        this.addresses = addresses;
        start();
    }

    public Bootstrap getBootstrap() {
        return bootstrap;
    }

    //TODO retrieve response from pipeline
    @Override
    public void request(BinaryMessage msg, ResponseHandler<BinaryMessage> handler) {
        ChannelFuture channelFuture = null;
        try {
            channelFuture = pool.borrowChannel();
            Channel channel = channelFuture.channel();
            channel.writeAndFlush(msg);
            logger.debug("Message send successful, remote address: {}", channel.remoteAddress());
        } catch (Exception e) {
            handler.onFailure(e);
        } finally {
            pool.returnObject(channelFuture);
        }
    }

    @Deprecated
    @Override
    public void fwrite(BinaryMessage msg) {
        ChannelFuture channelFuture = null;
        try {
            channelFuture = pool.borrowChannel();
            Channel channel = channelFuture.channel();
            channel.write(msg);
            logger.debug("Message write to buffer, remote address: {}", channel.remoteAddress());
        } finally {
            pool.returnObject(channelFuture);
        }
    }

    @Override
    public void start() {
        try {
            this.bootstrap = new Bootstrap();
            this.group = new NioEventLoopGroup();
            bootstrap.group(group);
            bootstrap.channel(NioSocketChannel.class)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(new BinaryMessageDecoder());
                            pipeline.addLast(new BinaryMessageEncoder());
                        }
                    });

            pool = new NettyChannelPool(bootstrap, addresses);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void activate() {

    }

    public void stop() {
        this.group.shutdownGracefully();
        this.pool.clear();
        logger.debug("Netty client stopped.");
    }

{'fraction_non_alphanumeric': 0.09255305789275399, 'fraction_numerical': 0.0030139394700489765, 'mean_word_length': 2.4671310404875926, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '977522', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1991, 'n_words': 532}