|
| 1 | +#CW |
| 2 | +import concore |
| 3 | +import requests |
| 4 | +import time |
| 5 | +from ast import literal_eval |
| 6 | +import os |
| 7 | + |
| 8 | +#time.sleep(7) |
| 9 | +timeout_max = 20 |
| 10 | +concore.delay = 0.02 |
| 11 | + |
| 12 | +try: |
| 13 | + apikey=open(concore.inpath+'1/concore.apikey',newline=None).readline().rstrip() |
| 14 | +except: |
| 15 | + try: |
| 16 | + #perhaps this should be removed for security |
| 17 | + apikey=open('./concore.apikey',newline=None).readline().rstrip() |
| 18 | + except: |
| 19 | + apikey = '' |
| 20 | + |
| 21 | +try: |
| 22 | + yuyu=open(concore.inpath+'1/concore.yuyu',newline=None).readline().rstrip() |
| 23 | +except: |
| 24 | + try: |
| 25 | + yuyu=open('./concore.yuyu',newline=None).readline().rstrip() |
| 26 | + except: |
| 27 | + yuyu = 'yuyu' |
| 28 | + |
| 29 | +try: |
| 30 | + name1=open(concore.inpath+'1/concore.name1',newline=None).readline().rstrip() |
| 31 | +except: |
| 32 | + try: |
| 33 | + name1=open('./concore.name1',newline=None).readline().rstrip() |
| 34 | + except: |
| 35 | + name1 = 'u' |
| 36 | + |
| 37 | +try: |
| 38 | + name2=open(concore.inpath+'1/concore.name2',newline=None).readline().rstrip() |
| 39 | +except: |
| 40 | + try: |
| 41 | + name2=open('./concore.name2',newline=None).readline().rstrip() |
| 42 | + except: |
| 43 | + name2 = 'ym' |
| 44 | + |
| 45 | +try: |
| 46 | + init_simtime_u = open(concore.inpath+'1/concore.init1',newline=None).readline().rstrip() |
| 47 | +except: |
| 48 | + try: |
| 49 | + init_simtime_u = open('./concore.init1',newline=None).readline().rstrip() |
| 50 | + except: |
| 51 | + init_simtime_u = "[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]" |
| 52 | + |
| 53 | +try: |
| 54 | + init_simtime_ym = open(concore.inpath+'1/concore.init2',newline=None).readline().rstrip() |
| 55 | +except: |
| 56 | + try: |
| 57 | + init_simtime_ym = open('./concore.init2',newline=None).readline().rstrip() |
| 58 | + except: |
| 59 | + init_simtime_ym = "[0.0, 0.0, 0.0]" |
| 60 | + |
| 61 | +print(apikey) |
| 62 | +print(yuyu) |
| 63 | +print(name1+'='+init_simtime_u) |
| 64 | +print(name2+'='+init_simtime_ym) |
| 65 | + |
| 66 | +while not os.path.exists(concore.inpath+'1/'+name1): |
| 67 | + time.sleep(concore.delay) |
| 68 | + |
| 69 | + |
| 70 | +#Nsim = 150 |
| 71 | +concore.default_maxtime(150) |
| 72 | +#init_simtime_u = "[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]" |
| 73 | +#init_simtime_ym = "[0.0, 0.0, 0.0]" |
| 74 | + |
| 75 | +ym = concore.initval(init_simtime_ym) |
| 76 | +oldym = init_simtime_ym |
| 77 | +oldt = 0 |
| 78 | + |
| 79 | + |
| 80 | +while(concore.simtime<concore.maxtime): |
| 81 | + print("CW outer loop") |
| 82 | + while concore.unchanged(): |
| 83 | + u = concore.read(1,name1,init_simtime_u) |
| 84 | + f = {'file1': open(concore.inpath+'1/'+name1, 'rb')} |
| 85 | + print("CW: before post u="+str(u)) |
| 86 | + print('http://www.controlcore.org/pm/'+yuyu+apikey+'&fetch='+name2) |
| 87 | + r = requests.post('http://www.controlcore.org/pm/'+yuyu+apikey+'&fetch='+name2, files=f,timeout=timeout_max) |
| 88 | + if r.status_code!=200: |
| 89 | + print("bad POST request "+str(r.status_code)) |
| 90 | + quit() |
| 91 | + if len(r.text)!=0: |
| 92 | + try: |
| 93 | + t=literal_eval(r.text)[0] |
| 94 | + except: |
| 95 | + print("bad eval "+r.text) |
| 96 | + timeout_count = 0 |
| 97 | + t1 = time.perf_counter() |
| 98 | + print("CW: after post status="+str(r.status_code)+" r.content="+str(r.content)+" t="+str(t)) |
| 99 | + #while r.text==oldym or len(r.content)==0: |
| 100 | + while oldt==t or len(r.content)==0: |
| 101 | + time.sleep(concore.delay) |
| 102 | + print("CW waiting status="+str(r.status_code)+" content="+ r.content.decode('utf-8')+" t="+str(t)) |
| 103 | + f = {'file1': open(concore.inpath+'1/'+name1, 'rb')} |
| 104 | + try: |
| 105 | + r = requests.post('http://www.controlcore.org/pm/'+yuyu+apikey+'&fetch='+name2, files=f,timeout=timeout_max) |
| 106 | + except: |
| 107 | + print("CW: bad request") |
| 108 | + timeout_count += 1 |
| 109 | + if r.status_code!=200 or time.perf_counter()-t1 > 1.1*timeout_max: #timeout_count>100: |
| 110 | + print("timeout or bad POST request "+str(r.status_code)) |
| 111 | + quit() |
| 112 | + if len(r.text)!=0: |
| 113 | + try: |
| 114 | + t=literal_eval(r.text)[0] |
| 115 | + except: |
| 116 | + print("bad eval "+r.text) |
| 117 | + oldt = t |
| 118 | + oldym = r.text |
| 119 | + print("CW: oldym="+oldym+" t="+str(concore.simtime)) |
| 120 | + concore.write(1,name2,oldym) |
| 121 | +#concore.write(1,"ym",init_simtime_ym) |
| 122 | +print("retry="+str(concore.retrycount)) |
| 123 | + |
| 124 | + |
0 commit comments