Write a C++ program to simulate the behavior of the token bucket algorithm.
Token Bucket Algorithm:
Figure 2 shows the token bucket algorithm. Assume that the arriving packets have a variable size and the queue has a finite buffer. At each clock tick, the program randomly selects the number of arriving packets and the size of each packet, and using the token bucket algorithm, the program tries to send the packets out of the queue.
Tokens are added at the rate of 'r' per tick. Tokens are discarded if the bucket is full. The bucket capacity is 'c' tokens. One token is removed and discarded per byte transmitted.
Arrival
Process
Departure
Yes
Discard
Figure 2: Token bucket algorithm
The following is the token bucket algorithm for variable-length packets:
1. Add tokens to the bucket at the rate of 'r' tokens per tick (token rate).
2. For a packet of 'a' bytes:
a. If at least 'n' tokens are available, send the packet and remove 'n' tokens from the bucket.
b. If fewer than 'n' tokens are available, hold the packet until sufficient tokens are available.
3. Go to step 1.
The program inputs are the number of clock ticks for simulation, the maximum packet arrival rate (maximum number of arriving packets at each tick), the maximum packet size (in bytes), the size of the queue (in bytes), the token rate (number of tokens added at each tick), and the bucket capacity (maximum number of tokens in the bucket). Assume the number of ticks for simulation is fixed to 15.
Use the following format when you ask the user to enter the inputs of the token bucket algorithm:
Maximum packet arrival rate:
Maximum packet size:
Size of the queue:
Token rate:
Capacity:
The program outputs, at each tick, are the number of arrived packets, number of departed packets, number of dropped packets, and number of packets in the queue.
Use the following format to display the outputs of the token bucket algorithm:
Tick# Arrived packets Departed packets Dropped packets Packets in queue