Computer Network Lab
Computer Network Lab
Code:-
#include <iostream>
#include <thread>
#include <chrono>
#include <queue>
#include <ctime>
#include <cstdlib>
using namespace std;
class LeakyBucket {
private:
int capacity; // Bucket capacity
int rate; // Output rate (packets per second)
queue<int> bucket;
public:
LeakyBucket(int capacity, int rate) : capacity(capacity), rate(rate) {}
void addPacket(int packetSize) {
if (bucket.size() + packetSize <= capacity) {
for (int i = 0; i < packetSize; i++) {
bucket.push(1);
}
} else {
cout << "Packet dropped due to overflow." << endl;
}
}
void leakPacket() {
if (!bucket.empty()) {
bucket.pop();
cout << "Packet sent." << endl;
} else {
cout << "Bucket is empty. No packet to send." << endl;
}
}
int getBucketSize() {
return bucket.size();
}
void run(int totalPackets) {
for (int i = 1; i <= totalPackets; i++) {
cout << "Packet " << i << " arriving with a size of 1." << endl;
addPacket(1);
leakPacket();