-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
executable file
*146 lines (132 loc) * 4.08 KB
/
app.py
File metadata and controls
executable file
*146 lines (132 loc) * 4.08 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import subprocess
import multiprocessing
import ast
import grpc
import p4_runtimes
import p4_read_topo
import p4_counter
import p4_read_table
import p4_write_table
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app, supports_credentials=True) # Quan Fen Chi Kua Yu
# Tian Jia Lu You He Shi Tu Han Shu
@app.route("/")
def index():
return render_template("index.html")
# Du Tuo Bu Jie Gou
@app.route("/read_topo")
def read_topo():
try:
data = p4_read_topo.read_topo()
return jsonify(data)
except Exception as e:
return str(e)
# Liu Liang Jian Kong
@app.route("/send_probe")
def send_probe():
command = "mx h1 python3 probe.py "
e_port = "2,2,2,2,3,3,3,3,1" # Tan Zhen Lu Jing
try:
process = subprocess.Popen(command + e_port, shell=True, text=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process.wait(5)
output = process.communicate()[0]
# return output # Ce Shi Yong
start = output.index("{")
end = output.rindex("}") + 1
result = output[start:end]
return jsonify(ast.literal_eval(result))
except subprocess.TimeoutExpired as e:
return "Qing Qiu Chao Shi "
except Exception as e:
return str(e)
# Du Qu Ji Shu Qi
@app.route("/read_counter")
def read_counter():
try:
args = p4_runtimes.check_parser()
p4info_helper, sws = p4_runtimes.sw_connection(args.p4info)
data = p4_counter.read_counters(p4info_helper, sws)
flag = True
except grpc.RpcError as e:
p4_runtimes.printGrpcError(e)
flag = False
except Exception as e:
print(e)
flag = False
p4_runtimes.shut_down()
if flag:
return jsonify(data)
else:
return "Cha Xun Ji Shu Qi Shi Bai "
# Du Qu Liu Biao
@app.route("/read_table")
def read_tables():
try:
args = p4_runtimes.check_parser()
p4info_helper, sws = p4_runtimes.sw_connection(args.p4info)
data = p4_read_table.read_tables(p4info_helper, sws)
flag = True
except grpc.RpcError as e:
p4_runtimes.printGrpcError(e)
flag = False
except Exception as e:
print(e)
flag = False
p4_runtimes.shut_down()
if flag:
return jsonify(data)
else:
return "Cha Xun Liu Biao Shi Bai "
# Xia Fa Liu Biao
@app.route("/write_table", methods=["POST"])
def write_tables():
form_args = request.get_json()
try:
args = p4_runtimes.check_parser()
p4info_helper, sws = p4_runtimes.sw_connection(args.p4info)
p4_write_table.sw_set_P4(p4info_helper, args.bmv2_json, sws)
p4_write_table.write_tables(p4info_helper, sws, form_args)
flag = True
except grpc.RpcError as e:
p4_runtimes.printGrpcError(e)
flag = False
except Exception as e:
print(e)
flag = False
p4_runtimes.shut_down()
if flag:
return "Xia Fa Liu Biao Cheng Gong "
else:
return "Xia Fa Liu Biao Shi Bai "
# Chuang Jian Zi Jin Cheng
def new_process(command):
process = subprocess.Popen(command, shell=True, text=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
result = process.communicate()[0]
while process.poll() is None: # Jin Cheng Wei Zhong Zhi
line = process.stdout.readline()
result += line
return result
if __name__ == "__main__":
try:
# new_process("make clean")
# run_p4_proc = multiprocessing.Process(target=new_process("make run"))
# run_p4_proc.start()
# print("mininetQi Dong Wan Cheng ")
# Xiu Gai Wen Jian Quan Xian
new_process("sudo chmod -R 777 .")
app.run(host="127.0.0.1", port=5555, debug=True, threaded=False)
except KeyboardInterrupt:
pass
except Exception as e:
print(e)
# run_p4_proc.terminate()
# print("mininetYi Ting Zhi ")
# new_process("make stop")
# new_process("make clean")
# print("Huan Cun Wen Jian Qing Li Wan Cheng ")