0% found this document useful (0 votes)
22 views5 pages

21BCE2670

The document implements the Leaky Bucket Algorithm for congestion control. It defines a LeakyBucket class with methods to update token levels and accept packets. An example shows initializing a bucket and simulating packet arrivals, with outputs indicating if packets are accepted or dropped.

Uploaded by

I'm KING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

21BCE2670

The document implements the Leaky Bucket Algorithm for congestion control. It defines a LeakyBucket class with methods to update token levels and accept packets. An example shows initializing a bucket and simulating packet arrivals, with outputs indicating if packets are accepted or dropped.

Uploaded by

I'm KING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB ASSESSMET-1A

BCSE308P: Computer Networks Lab

Slot: L25+L26

Date: 20-07-2023

Faculty in Charge: Prakash G

Name: KAIBADI.DILEEP

RegNo: 21BCE2670

Question:

Implement the Congestion Control using Leaky


Bucket Algorithm.
Code:

import time

class LeakyBucket:
def __init__(self, bucket_size, output_rate):
self.bucket_size = bucket_size
self.output_rate = output_rate
self.bucket_content = 0
self.last_time = time.time()

def get_tokens(self):
now = time.time()
elapsed_time = now - self.last_time
tokens_to_add = elapsed_time *
self.output_rate
self.bucket_content = max(0,
self.bucket_content - tokens_to_add)
self.last_time = now

def accept_packet(self, packet_size):


self.get_tokens()
if packet_size <= self.bucket_content +
self.output_rate:
self.bucket_content += packet_size
return True
return False

# Example usage for congestion control


if __name__ == "__main__":
bucket = LeakyBucket(bucket_size=100,
output_rate=10) # Bucket size of 100 tokens, output
rate of 10 tokens per second

# Simulate packet arrivals with different sizes


packet_sizes = [30, 20, 15, 10, 25, 50, 40, 5]

for packet_size in packet_sizes:


if bucket.accept_packet(packet_size):
print(f"Packet of size {packet_size} accepted
and sent.")
else:
print(f"Packet of size {packet_size} dropped
due to congestion.")

Output:

Algorithm for the Leaky Bucket Algorithm:

1. Class Definition:
 Create a class called LeakyBucket for the
Leaky Bucket Algorithm implementation.
 Initialize the class with two parameters:

bucket_size (maximum number of tokens the


bucket can hold) and output_rate (tokens
removed per second).
 Set the initial bucket content to 0.

 Set the last update time to the current time.

2. get_tokens Method:
 Get the current time.

 Calculate the elapsed time since the last

update.
 Calculate the number of tokens to remove

from the bucket based on the output rate


multiplied by the elapsed time.
 Update the number of tokens in the bucket

after removing the tokens, ensuring it does not


go below 0.
 Update the last update time to the current

time.
3. accept_packet Method:
 Call the get_tokens method to update the

number of tokens in the bucket.


 If the packet size is less than or equal to the

available tokens in the bucket plus the output


rate (effective capacity), then:
 Add the packet size to the bucket content.

 Return True to indicate that the packet is

accepted and sent.


 If the packet size exceeds the effective
capacity (available tokens + output rate),
then:
 The packet cannot be accommodated, so

return False to indicate that the packet is


dropped due to congestion.
4. Example Usage:
 Create an instance of the LeakyBucket class

with a bucket size of 100 tokens and an output


rate of 10 tokens per second.
 Simulate packet arrivals with different sizes.

 For each packet size, use the accept_packet

method to check if the packet can be accepted


or dropped based on the Leaky Bucket
Algorithm's constraints.

You might also like