-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathcmdinfer.py
executable file
·57 lines (44 loc) · 1.28 KB
/
cmdinfer.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import json
import glob
RequestBandwidthCommand = "RequestBandwidth"
def fetch_stats(line: str)->dict:
line = line.strip()
try:
stats = json.loads(line)
return stats
except json.decoder.JSONDecodeError:
return None
def request_estimated_bandwidth(line: str)->bool:
line = line.strip()
if RequestBandwidthCommand == line:
return True
return False
def find_estimator_class():
import BandwidthEstimator
return BandwidthEstimator.Estimator
def main(ifd = sys.stdin, ofd = sys.stdout):
estimator_class = find_estimator_class()
estimator = estimator_class()
while True:
line = ifd.readline()
if not line:
break
if isinstance(line, bytes):
line = line.decode("utf-8")
stats = fetch_stats(line)
if stats:
estimator.report_states(stats)
continue
request = request_estimated_bandwidth(line)
if request:
bandwidth = estimator.get_estimated_bandwidth()
ofd.write("{}\n".format(int(bandwidth)).encode("utf-8"))
ofd.flush()
continue
sys.stdout.write(line)
sys.stdout.flush()
if __name__ == '__main__':
main()