python语言实现WFQ算法,将这个项目编程三个例程,“发送者”、“接收者”和“路由器”。使用FlowID=1、权重=1和数据包大小1024启动发送端。几秒钟后,使用FlowID=2、权重=1和数据包大小512启动另一个发送器。再过几秒钟,使用FlowID=3、权重=2和数据包大小1024启动第三个发送方。接收者负责数据统计的部分,每次完成一个数据包时记录时间、流ID和数据包的大小。最后画出每个流上接收到的总字节与时间的图表。默认本地主机之间通信
时间: 2024-03-16 09:43:10 浏览: 19
以下是Python语言实现WFQ算法的代码实现,包含发送者、接收者和路由器三个例程:
发送者例程:
```python
import socket
import time
def send(flow_id, weight, packet_size):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
addr = ('localhost', 10000)
while True:
data = str(flow_id) + ',' + str(weight) + ',' + '0' * (packet_size - len(str(flow_id)))
sock.sendto(data.encode(), addr)
time.sleep(1)
```
接收者例程:
```python
import socket
import time
def receive():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('localhost', 10001))
records = {}
while True:
data, addr = sock.recvfrom(1024)
record = data.decode().split(',')
flow_id = int(record[0])
packet_size = int(record[2])
timestamp = time.time()
if flow_id not in records:
records[flow_id] = []
records[flow_id].append((timestamp, packet_size))
```
路由器例程:
```python
import socket
import time
def route():
sock_in = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock_out = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock_in.bind(('localhost', 10000))
addrs = [('localhost', 10001), ('localhost', 10002), ('localhost', 10003)]
queue = []
while True:
data, addr = sock_in.recvfrom(1024)
record = data.decode().split(',')
flow_id = int(record[0])
weight = int(record[1])
packet_size = int(record[2])
queue.append((flow_id, weight, packet_size, time.time()))
queue.sort(key=lambda x: x[3])
total_weights = sum([x[1] for x in queue])
if total_weights == 0:
continue
current_time = time.time()
for i in range(len(queue)):
flow_id, weight, packet_size, timestamp = queue[i]
if current_time - timestamp >= (weight / total_weights):
sock_out.sendto(str(flow_id).encode() + b',' + str(packet_size).encode(), addrs[flow_id-1])
queue.pop(i)
break
```
以下是绘制流量统计图表的代码实现:
```python
import matplotlib.pyplot as plt
def plot(records):
plt.figure(figsize=(10, 5))
colors = ['r', 'g', 'b']
for i, (flow_id, data) in enumerate(records.items()):
x = [t - data[0][0] for t, _ in data]
y = [sum([x[1] for x in data[:j+1]]) for j in range(len(data))]
plt.plot(x, y, color=colors[i], label='Flow ' + str(flow_id))
plt.xlabel('Time (s)')
plt.ylabel('Data (bytes)')
plt.legend()
plt.show()
```
最后,我们可以按照如下方式运行这些例程:
```python
import threading
threading.Thread(target=send, args=(1, 1, 1024)).start()
threading.Thread(target=send, args=(2, 1, 512)).start()
threading.Thread(target=send, args=(3, 2, 1024)).start()
threading.Thread(target=receive).start()
threading.Thread(target=route).start()
```
运行后,我们可以得到每个流上接收到的总字节与时间的图表。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)