in.voidma.lemming.traffic.TrafficDropHandler.java Source code

Java tutorial

Introduction

Here is the source code for in.voidma.lemming.traffic.TrafficDropHandler.java

Source

/**
 * Lemming - A Spigot plugin for network stress testing.
 * Copyright (C) 2017 Miles W. Silberling-Cook
 * <p>
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 * <p>
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * <p>
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 */
package in.voidma.lemming.traffic;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;

import java.util.Random;

/**
 * TODO: Fill out javaDoc coment for in.voidma.lemming.internal.client.traffic:TrafficDropHandler
 *
 * @author NthTensor
 * @version 1.0
 * @since 2017-04-26
 */
public class TrafficDropHandler extends ChannelDuplexHandler {

    int channelDropChance;
    Random random = new Random();

    public TrafficDropHandler(int channelDropChance) {
        channelDropChance = channelDropChance;
    }

    @Override
    public void read(ChannelHandlerContext ctx) throws Exception {
        if (random.nextInt(100) < channelDropChance) {
            return;
        } else {
            super.read(ctx);
        }
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if (random.nextInt(100) < channelDropChance) {
            return;
        } else {
            super.write(ctx, msg, promise);
        }
    }
}