forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeaky Bucket
105 lines (94 loc) · 2.25 KB
/
Leaky Bucket
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#define size 20
int front = -1, rear = -1;
int queue[size];
int bucketSize, outRate, nPacket, packetSize, storedSize=0, outgoing;
void enqueue(int packSize)
{
if (front == -1)
{
front++;
}
rear++;
if (rear == 20)
{
rear = 0;
}
queue[rear] = packSize;
}
int dequeue()
{
int temp = queue[front];
if (front == rear)
{
front = rear = -1;
}
else
{
front++;
}
if (front == 20)
{
front = 0;
}
return temp;
}
void emptyBucket()
{
printf("\nEmpying bucket!");
while (storedSize)
{
int outgoing = 0;
while (outgoing < outRate && storedSize > 0)
{
packetSize = dequeue();
outgoing += packetSize;
printf("\n-->Outgoing packet size = %d\n", packetSize);
storedSize -= packetSize;
printf("(%d out of %d left in bucket.", bucketSize - storedSize, bucketSize);
}
sleep(1);
}
}
int main()
{
printf("Enter the bucket size: ");
scanf("%d", &bucketSize);
printf("\nEnter outgoing rate: ");
scanf("%d", &outRate);
while (1)
{
int outgoing = 0;
printf("\nEnter the number of incoming packets: ");
scanf("%d", &nPacket);
if (nPacket == 0)
{
break;
}
for (int i = 0; i < nPacket; i++)
{
printf("\nEnter the size of packet[%d]: ", i + 1);
scanf("%d", &packetSize);
if (packetSize > bucketSize - storedSize)
{
printf("Packet Dropped");
emptyBucket();
i--;
continue;
}
storedSize += packetSize;
enqueue(packetSize);
printf("(%d out of %d in bucket).\n", bucketSize - storedSize, bucketSize);
}
while (outgoing < outRate && storedSize > 0)
{
packetSize = dequeue();
outgoing += packetSize;
printf("\n-->Outgoing packet size = %d\n", packetSize);
storedSize -= packetSize;
printf("\n(%d out of %d left in bucket).", bucketSize - storedSize, bucketSize);
}
sleep(1);
}
emptyBucket();
}