-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBucket.java
70 lines (55 loc) · 1.84 KB
/
Bucket.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//Write a program for congestion control using leaky bucket algorithm.
import java.security.SecureRandom;
import java.util.*;
import java.io.*;
class Bucket
{
public static void main(String args[])
{
Random r=new Random();
Scanner s = new Scanner(System.in);
System.out.println("Enter the input rate: ");
int n=s.nextInt();
System.out.println("Enter the Bucket Size: ");
int bucket_size=s.nextInt();
System.out.println("Enter the Output Rate: ");
int out_rate=s.nextInt();
System.out.println("Enter packet data: ");
int[] input_arr=new int[n];
for(int i=0;i<n;i++)
{
input_arr[i]=r.nextInt(127);//s.nextInt(); here we'll be auto populating the array instead of inputting n numbers
}
int bc=0;//bucket_content=0
//loop through for each packet in input_arr[]
for(int i=0;i<n;i++)
{
//check if the new packet fits into the bucket at the current instance
if(bc+input_arr[i]<=bucket_size)
{ //if new packet can be fit then insert it into the bucket
bc=bc+input_arr[i];
System.out.print(i+"\tPacket: "+input_arr[i]+"\tBucket Content: "+bc+"\tStatus: Accepted\tRemaining: ");
//after processing(displaying) the status display remove bucket content equal to out_rate
bc=bc-out_rate;
//note - removal of element must not cause negative bucket capacity.
if(bc<=0)//bc=bc<=0?0:bc;
{
bc=0;
}
System.out.println(bc);//Display remaining capacity
}
//If the packet does not fit into current bucket then simply drop the packet and continue to out_rate
else
{
System.out.print(i+"\tPacket: "+input_arr[i]+"\tBucket Content: "+bc+"\tStatus: Rejected\tRemaining: ");
//remove outrate from bc as normal cycle
bc=bc-out_rate;
if(bc<=0)//dont let the bucket capacity assume negative values
{
bc=0;
}
System.out.println(bc);
}
}
}
}