java program for congestion control using leaky bucket algorithm

The main concept of the leaky bucket algorithm is that the output data flow remains constant despite the variant input traffic, such as the water flow in a bucket with a small hole at the bottom. In case the bucket contains water (or packets) then the output flow follows a constant rate, while if the bucket is full any additional load will be lost because of spillover. In a similar way if the bucket is empty the output will be zero. From network perspective, leaky bucket consists of a finite queue (bucket) where all the incoming packets are stored in case there is space in the queue, otherwise the packets are discarded. In order to regulate the output flow, leaky bucket transmits one packet from the queue in a fixed time (e.g. at every clock tick).

				
					import java.util.Scanner;
public class Leaky 
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int bucket=0,op_rate,i,n,bsize;
System.out.println("enter number of packets");
 n=sc.nextInt();
int pkt[]=new int[n];
System.out.println("enter he output rate of the bucket");
op_rate=sc.nextInt();
System.out.println("enter the bucket size");
bsize=sc.nextInt();
System.out.println("enter the arriving packets(size)");
for(i=0;i
pkt[i]=sc.nextInt();
System.out.println("\nsec\tpsize\tbucketsize\taccept/reject\tpkt_send");
System.out.println("-------------------------------------------------------");
for(i=0;i
 {
System.out.print(i+1+"\t"+pkt[i]+"\t");
if(bucket+pkt[i]<=bsize)
{
 bucket+=pkt[i];
 System.out.println(bucket+"\t\taccept\t\t"+min(bucket,op_rate)+"\n");
 bucket=sub(bucket,op_rate);
}
else
{
System.out.println(bucket+"\t\treject\t"+(bucket+pkt[i]-bsize) +"\t"+min(bsize,op_rate)+"\n");
 bucket=bsize;
bucket=sub(bucket,op_rate);
}
}
while(bucket!=0)
 {
System.out.print(++i +"\t 0 \t"+bucket+"\t\taccept\t\t"+min(bucket,op_rate)+"\n");
bucket=sub(bucket,op_rate);
}
sc.close();
}
static int min(int a,int b)
{
return(a
}
static int sub(int a,int b)
{
return(a-b)>0?(a-b):0;
}
}

				
			
OUTPUT

1)

enter number of packets

4
enter he output rate of the bucket

7
enter the bucket size

8

enter the arriving packets(size)

5

7

8

3

sec       psize    bucketsize        accept/reject     pkt_send

-------------------------------------------------------

1          5          5                      accept              5

2          7          7                      accept              7

3          8          8                      accept              7

4          3          4                      accept              4

Leave a Reply