Skip to content

Instantly share code, notes, and snippets.

@sidsbrmnn
Created December 9, 2019 17:56
Show Gist options
  • Save sidsbrmnn/d0a7862aff9e851337e7c411f0a994cb to your computer and use it in GitHub Desktop.
Save sidsbrmnn/d0a7862aff9e851337e7c411f0a994cb to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class LeakyBucket {
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
System.out.print("Enter bucket size: ");
final int size = sc.nextInt();
System.out.print("Enter output rate: ");
final int outRate = sc.nextInt();
System.out.print("Enter input size: ");
final int n = sc.nextInt();
System.out.print("Enter input rate: ");
final int inRate = sc.nextInt();
int buffer = 0;
while (n != 0) {
System.out.println("\nIncoming rate: " + inRate);
if (inRate <= size - buffer) {
buffer += inRate;
} else {
System.out.println("Packet lost: " + inRate - (size - buffer));
buffer = size;
}
System.out.println("Bucket buffer has " + buffer + " out of " + size);
buffer -= outRate;
System.out.println("After outgoing, buffer has " + buffer + " out of " + size);
n--;
Thread.sleep(1000);
}
sc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment