12.Write a 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). In the following figure we can notice
the main rationale of leaky bucket algorithm, for both the two approaches (e.g.
leaky bucket with water (a) and with packets (b)).
While leaky
bucket eliminates completely bursty traffic by regulating the incoming data
flow its main drawback is that it drops packets if the bucket is full. Also, it
doesn’t take into account the idle process of the sender which means that if
the host doesn’t transmit data for some time the bucket becomes empty without
permitting the transmission of any packet.
package
bucket;
import
java.util.*;
public class
Bucket {
static void solution(int pktsize, int
output) {
int buketsize = 512;
if (pktsize > buketsize) {
System.out.println("Bucket
overflow");
} else {
while (pktsize > output) {
System.out.println(output +
"bytes outputed");
pktsize = pktsize - output;
}
if (pktsize > 0) {
System.out.println(pktsize +
"bytes outputed");
}
}
}
public static void main(String[] args) {
int output, pktsize, n;
Scanner read = new Scanner(System.in);
Random rand = new Random();
System.out.println("Enter output
rate");
output = read.nextInt();
System.out.println("Enter the
number of packets");
n = read.nextInt();
for (int i = 1; i <= n; i++) {
pktsize = rand.nextInt(1000);
System.out.println("packetno: "
+ i + " packetsize=" + pktsize);
solution(pktsize, output);
}
}
}
Sample output:
Run1:
Enter output
rate
100
Enter the
number of packets
5
packetno: 1 packetsize=564
Bucket
overflow
packetno: 2 packetsize=735
Bucket
overflow
packetno: 3 packetsize=615
Bucket
overflow
packetno: 4 packetsize=129
100bytes
outputed
29bytes
outputed
packetno: 5 packetsize=773
Bucket
overflow
No comments:
Post a Comment