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

Java tutorial

Introduction

Here is the source code for in.voidma.lemming.traffic.TrafficLatencyHandler.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 io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import io.netty.util.Timer;
import io.netty.util.TimerTask;

import java.util.concurrent.TimeUnit;

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

    private static final Timer timer = new HashedWheelTimer();

    private boolean inBound;
    private boolean outBound;

    private int TimeLagMS;

    public TrafficLatencyHandler(int timeLagMS) {
        TimeLagMS = timeLagMS;
    }

    @Override
    public void read(ChannelHandlerContext ctx) throws Exception {
        if (inBound) {
            timer.newTimeout(new TimerTask() {
                @Override
                public void run(Timeout timeout) throws Exception {
                    ctx.read();
                }
            }, TimeLagMS, TimeUnit.MILLISECONDS);
        }
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if (outBound) {
            timer.newTimeout(new TimerTask() {
                @Override
                public void run(Timeout timeout) throws Exception {
                    ctx.write(msg, promise);
                }
            }, TimeLagMS, TimeUnit.MILLISECONDS);
        }
    }

    @Override
    public void flush(ChannelHandlerContext ctx) throws Exception {
        if (outBound) {
            timer.newTimeout(new TimerTask() {
                @Override
                public void run(Timeout timeout) throws Exception {
                    ctx.flush();
                }
            }, TimeLagMS, TimeUnit.MILLISECONDS);
        }
    }

    public boolean isInBound() {
        return inBound;
    }

    public void setInBound(boolean inBound) {
        this.inBound = inBound;
    }

    public void toggleInBound() {
        this.inBound = !inBound;
    }

    public boolean isOutBound() {
        return outBound;
    }

    public void setOutBound(boolean outBound) {
        this.outBound = outBound;
    }

    public void toggleOutBound() {
        this.outBound = !outBound;
    }

    public int getTimeLagMS() {
        return TimeLagMS;
    }

    public void setTimeLagMS(int timeLagMS) {
        TimeLagMS = timeLagMS;
    }
}