This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
403 lines (374 loc) · 11.1 KB
/
main.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# -*- coding: utf8 -*-
import subprocess
import os
import sys
import time
from time import sleep
import MySQLdb
import random
import shutil
from conn import connMysql
from caldistance import CalDis
from getPathLossExp import read_data
from getPathLossExp import getPathLossExp
from loadc import getConductivity
from trim import trim
from txt2image import txt2image
from txt2html import plaintext2html
import getHeight
from getHeight import getPathInfo
from getHeight import formatPathInfo
from getHeight import getHTT
from getHeight import getHRR
from millinton import cal_milliton
MIN_POINT = 2
TRACE_FILE_NAME = "simple.tr"
def get_job():
conn = connMysql()
cur = conn.cursor()
sql = "SELECT * FROM `main` WHERE `status`=0 LIMIT 0,30"
cur.execute(sql)
dataset = cur.fetchall()
cur.close()
conn.close()
return dataset
def split_job(dataset):
jobs={}
for data in dataset:
jobs.setdefault(data[1],[]).append(data)
for job in jobs:
yield list(jobs.get(job,()))
def split_parameters(dataset):
simulation_paras = []
dataset = dataset.split("END,")
for data in dataset:
parameters_set = data.split(",")
simulation_paras.append(parameters_set)
return simulation_paras
def do_simulation(job):
# for DEBUG
print 'doing simulation'
try:
running_id = job[0][1]
parameters = job[0][2]
except Exception,e:
print "ERROR- Point Num ERROR OR Lack LAT LNG"
mark_as_complete(job[0])
return 1
para_set = split_parameters(parameters)
clean_pre_data(running_id)
for data in para_set:
lat1 = data[0].split("(")[1].strip()
lng1 = data[1].split(")")[0].strip()
lat2 = data[2].split("(")[1].strip()
lng2 = data[3].split(")")[0].strip()
if (data[4] == "PARA"):
# If it has parameters
# then
freq = float(data[5])
pol = data[6]
if (pol == "Vertical"):
pol = 0
else:
pol = 1
height = float(data[7])
height_r = float(data[8])
bandwidth = float(data[9])
# Now its time to run simulation
result_set = run_GRWAVE(running_id,lat1,lng1,lat2,lng2,freq,pol,height,height_r,bandwidth)
run_NS(running_id,result_set)
# Upload the result
upload_FINAL_RESULT(running_id)
print 'succed!'
mark_as_complete(running_id)
return 0
#print freq,pol,height,height_r,bandwidth
def run_GRWAVE(running_id,lat1,lng1,lat2,lng2,freq,pol,height,height_r,bandwidth):
#save_res(show_res("out"),job[0])
# calculate the distance between two points
try:
dis = CalDis(lat1,lng1,lat2,lng2)
except Exception,e:
print "error cal dis"
mark_as_complete(running_id)
try:
con1 = getConductivity(lat1,lng1)
except Exception,e:
print "error cal con1"
mark_as_complete(running_id)
try:
geo_height = getPathInfo(lat1,lng1,lat2,lng2)
#print 'geo_height = ', geo_height
if geo_height == None:
raise Exception,e
elif geo_height < 0:
# I really dont understand why geo_height < 0 !!!
geo_height = 0
except Exception,e:
print e
print "cannot grab geo_info"
mark_as_complete(running_id)
try:
geo_height2 = formatPathInfo(geo_height)
print 'geo_height2 = ', geo_height2
if geo_height2 < 0:
# don't understand either
geo_height2 = 0
except Exception,e:
mark_as_complete(running_id)
print "geo height grab error"
print geo_height
return 1
try:
#hei1_mean = getHeight_mean(lat1,lng1,lat2,lng2)
if getHTT(geo_height2) < 0:
HTT = height
else:
HTT = getHTT(geo_height2) + height
if getHRR(geo_height2) < 0:
HRR = height_r
else:
HRR = getHRR(geo_height2) + height_r
#HTT = getHTT(geo_height2) + height
#HRR = getHRR(geo_height2) + height_r
print "HTT,HRR:",HTT,HRR
except Exception,e:
print "error calculate HTT HRR"
mark_as_complete(running_id)
return 1
try:
res =\
cal_milliton(geo_height2,int(freq),int(pol),dis,HTT,HRR,height,height_r)
if res is not None:
save_Et(res,running_id)
#@ NOW FIX THIS
except Exception,e:
print e
mark_as_complete(running_id)
return 1
try:
fp = open("inp","w")
except Exception,e:
print "error open"
command1 = "HTT {} \n\
HRR {} \n\
IPOLRN {}\n\
FREQ {} \n\
SIGMA {} \n\
EPSLON 30 \n\
dmin 1\n\
dmax {}\n\
dstep 5\n\
GO\
".format(str(HTT),str(HRR),pol,freq,con1,dis)
fp.write(command1)
fp.close()
fp = open("inp2","w")
command = "HTT {} \n\
HRR {} \n\
IPOLRN {}\n\
FREQ {} \n\
SIGMA {} \n\
EPSLON 30 \n\
dmin 1\n\
dmax 1000\n\
dstep 5\n\
GO\
".format(str(HTT),str(HRR),pol,freq,con1)
fp.write(command)
fp.close()
# except Exception,E:
# print command1
# print "Error writing inp inp2"
#return 1
# running grwave the result will save in out
rungrwave=os.getcwd()+"/gr <inp >out"
rungrwave_for_ns2=os.getcwd()+"/gr <inp2 >out2"
runget_result="sh "+os.getcwd()+"/get_res.sh"
run_get_KM = "perl get_KM.pl out2 > KM"
run_get_BPL = "perl get_BasicPathLoss.pl out2 > BPL"
try:
process1 = subprocess.Popen(rungrwave,shell=True)
process1.wait()
except Exception,E:
print "Error process 1"
mark_as_complete(running_id)
return 1
try:
process2 = subprocess.Popen(rungrwave_for_ns2,shell=True)
process2.wait()
except Exception,e:
print "Error process 2"
mark_as_complete(running_id)
return 1
try:
process3 = subprocess.Popen(run_get_KM,shell=True)
process3.wait()
except Exception,E:
print "Error process 3"
mark_as_complete(running_id)
return 1
try:
process4 = subprocess.Popen(run_get_BPL,shell=True)
process4.wait()
except Exception,E:
print "Error process 4"
mark_as_complete(running_id)
return 1
try:
DAT_KM = read_data("KM")
DAT_BPL = read_data("BPL")[3:]
except Exception,E:
print "Error process read data"
mark_as_complete(running_id)
return 1
try:
PathLossExp = getPathLossExp(DAT_KM,DAT_BPL)
except Exception,e:
print "Error get pathlossexp"
mark_as_complete(running_id)
return 1
result_set = []
result_set.append(bandwidth)
result_set.append(freq)
result_set.append(dis)
return result_set
def run_NS(running_id,result_set):
bandwidth = result_set[0]
freq = result_set[1]
dis = result_set[2]
try:
bandwidth_ns = " " + str(bandwidth)+"bps"+ " "
freq_ns = " "+str(freq)+"e6"+" "
dis_ns = " "+str(dis*1000)+" "
run_ns2 = os.getcwd()+"/ns dsr.tcl "
run_ns2 = run_ns2+str(0.1)+bandwidth_ns+freq_ns+dis_ns
print run_ns2
process = subprocess.Popen(run_ns2,shell=True)
process.wait()
except Exception,e:
print "run_ns2 error"
mark_as_complete(running_id)
try:
run_ana_throughput = "perl mea.pl "+TRACE_FILE_NAME
process_out =\
subprocess.Popen(run_ana_throughput,stdout=subprocess.PIPE,shell=True).communicate()
except Exception,e:
print "run_ana_throughput error"
print process_out[0]
print len(process_out[0].split(","))
save_res(process_out[0],"throughput",running_id)
try:
run_ana_del = "perl del.pl "+TRACE_FILE_NAME
process_out =\
subprocess.Popen(run_ana_del,stdout=subprocess.PIPE,shell=True).communicate()
except Exception,e:
print "run_ana_del error"
mark_as_complete(running_id)
print process_out[0]
print len(process_out[0].split(","))
save_res(process_out[0],"delay",running_id)
return 0
def upload_FINAL_RESULT(running_id):
try:
random_num = upload_res()
except Exception,e:
mark_as_complete(running_id)
print "upload res error"
return 1
try:
save_res(random_num,"res",running_id)
except Exception,e:
mark_as_complete(running_id)
print "save res error"
return 1
#K = subprocess.Popen(runget_result,shell=True,stdout=subprocess.PIPE)
#req0 = mark_as_complete(running_id)
#save_res(show_res("out"),job[0])
#print show_res("out")
def show_res(file):
fout = open(file,"r")
return fout.read()
def save_res(data,name,running_id):
conn = connMysql()
cur = conn.cursor()
sql = "UPDATE `gr`.`main` SET `"+name+"` = \""+data+"\" where running_id= " +str(running_id)
print sql
cur.execute(sql)
conn.commit()
cur.close
conn.close()
#
def save_Et(Et,running_id):
conn = connMysql()
cur = conn.cursor()
get_old_Et = "Select `Et` from `gr`.`main` where running_id = "+\
str(running_id)
cur.execute(get_old_Et)
pre_res = cur.fetchone()[0]
if pre_res:
new_data = pre_res+","+str(Et)
else:
new_data = str(Et)
print new_data
sql = "UPDATE `gr`.`main` SET `Et` = \""+new_data+"\" where running_id="+\
str(running_id)
print sql
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
def upload_res():
random_num = str(random.randrange(0,10001,2))
print 'random_num = ',random_num
try:
shutil.copy2(os.getcwd()+"/out","/home/quake0day/www/Torterra/res/"+random_num)
shutil.copy2(os.getcwd()+"/simple.tr","/home/quake0day/www/Torterra/tr/"+random_num)
except Exception,e:
print "upload error"
return random_num
def save_res_plot(res,running_id):
conn = connMysql()
cur = conn.cursor()
sql = "UPDATE `gr`.`main` SET `throughput` =\""+res+"\" where running_id=\
"+str(running_id)
print sql
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
return 0
def mark_as_complete(running_id):
conn = connMysql()
cur = conn.cursor()
sql = "UPDATE `gr`.`main` SET `status` = 1 where running_id= " +str(running_id)
print sql
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
return 0
def clean_pre_data(running_id):
conn = connMysql()
cur = conn.cursor()
sql = "UPDATE `gr`.`main` SET `Et` = null where running_id= " +str(running_id)
# print sql
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
return 0
if __name__ == '__main__':
#main
while 1:
sleep(1)
try:
job = split_job(get_job())
except Exception,e:
pass
try:
task = job.next()
do_simulation(task)
sleep(1)
except Exception,e:
pass