Samo Penic
2018-05-05 eccf3a5f7218be8d6a10dbdc5a8202c26341c75f
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
#!/usr/bin/python3
import requests
import json
from time import sleep
import uuid
import subprocess
import os
import shutil
import signal
import sys
import socket
from threading import Thread, Event
 
def get_hostname():
    return socket.gethostname()
 
def get_ip():
    return ((([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")] or [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) + ["no IP found"])[0])
 
def get_client_id(addr, my_ip, my_hostname):
    client_auth={'ip':my_ip,'hostname':my_hostname}
    response=requests.post(addr+"/api/register/", data=client_auth)
    if(response.status_code==200):
        client_data=json.loads(response.text)
        client_id=client_data['id']
        return client_id
    else:
        raise ValueError
 
 
def get_run(addr,cid):
    response=requests.get(addr+"/api/getrun/"+str(cid)+"/")
    if(response.status_code==200):
        client_data=json.loads(response.text)
        rid=client_data['id']
        tape=client_data['tape']
        vtu=client_data['lastVTU']
        status=client_data['status']
        return (rid,tape,vtu,status)
    else:
        print(response.text)
        raise ValueError
 
 
def ping_run(addr,cid, rid):
    client_data={'client_id':cid, 'run_id':rid}
    response=requests.post(addr+"/api/ping/", data=client_data)
    if(response.status_code==200):
        return
    else:
        raise ValueError
 
def send_error_report(addr,cid, rid,errcode):
    client_data={'client_id':cid, 'run_id':rid, 'error_code':errcode}
    response=requests.post(addr+"/api/reporterr/", data=client_data)
    if(response.status_code==200):
        return
    else:
        raise ValueError
 
def upload(addr,cid, rid, vtu, status):
    client_data={'client_id': cid, 'run_id': rid, 'lastVTU': vtu, 'status': status}
    response=requests.post(addr+"/api/upload/", data=client_data)
    if(response.status_code==200):
        return
    else:
        raise ValueError
 
def getNewVTU(directory):
    fset=set()
    for file in os.listdir(directory):
        if file.endswith(".vtu") and file.startswith("timestep_"):
            fset.add(file)
    return fset
 
 
def removeDir(directory):
    os.chdir('/')
    try:
        shutil.rmtree(directory)
    except:
        print("Cannot remove directory "+directory+ "\n")
    return
 
                    
 
 
class ClientThread(Thread):
 
    def __init__(self,conn_address='http://localhost:8000',subid=0, update_seconds=100):
        super(ClientThread,self).__init__()
        self._stop_event = Event()
        self._stop_event.clear()
        self.p=None
        self.workingdir=None
        self.conn_address=conn_address
        self.id=subid
        self.ip=get_ip()
        self.hostname=get_hostname()
        self.update_seconds=update_second
#        self.subthreads=[]
 
    def stop(self):
        self._stop_event.set()
 
    def isStopped(self):
        return self._stop_event.is_set()
 
    def join(self):
        print('joining threads')
        super(ClientThread, self).join()
        if self.p is not None:
            self.p.terminate()
        if self.workingdir is not None:
            removeDir(self.workingdir.fullpath())
 
 
    def sleep(self,s):
        for i in range(0, s):
            if(self.isStopped()):
                return False
            sleep(1)
        return True
    
    def run(self):
        while(not self.isStopped()):
            try:
                cid=get_client_id(self.conn_address, self.ip, self.hostname)
            except:
                print("[{}] Could not get CID.".format(self.id))
                self.sleep(10)
                continue
            #print("Got CID. getting RID.")
            while(not self.isStopped()):
                try:
                    (rid,tape,vtu,status)=get_run(self.conn_address,cid)
                except:
                    print("[{}] Could not get RID.".format(self.id))
                    self.sleep(10)    
                    break
                else:
                    #start separate thread with simulations.
                    self.workingdir=Directory('/tmp/ts_'+str(uuid.uuid4()))
                    self.workingdir.makeifnotexist()
                    self.workingdir.goto()
                    with open(self.workingdir.fullpath()+"/tape", 'w') as f:
                        f.write(tape)
                    if(int(status)==-1):
                        cmd=['trisurf', '--force-from-tape']
                        print("[{}] Run id={} :: Starting from tape.".format(self.id, rid))
                    else:
                        with open(self.workingdir.fullpath()+"/.status",'w') as f:
                            f.write(status)
                        with open(self.workingdir.fullpath()+"/initial.vtu",'w') as f:
                            f.write(vtu)
                        cmd=['trisurf', '--restore-from-vtk', 'initial.vtu']
                        print("[{}] Run id={} :: Restoring from vtk, last timestep {}".format(self.id,rid,status))
                    self.p=subprocess.Popen(cmd, stdout=subprocess.DEVNULL)
                    s=int(status)
                    while(not self.isStopped()):
                        #monitor for new file. If file is present, upload it!
                        newVTU=getNewVTU(self.workingdir.fullpath())
                        if newVTU: #upload
                            try:
                                for nv in sorted(newVTU):
                                    with open(nv,'r') as f:
                                        fc=f.read()
                                    s=s+1
                                    print('[{}] Uploading {}.'.format(self.id,nv))
                                    upload(self.conn_address, cid, rid, fc, s)
                                    os.unlink(nv)
                            except:
                                print("[{}] Could not upload".format(self.id))
                                self.p.terminate()
                                removeDir(self.workingdir.fullpath())
                                self.p=None
                                self.workingdir=None
                                break
                            else:
                                print("[{}] VTU uploaded.".format(self.id))
                        else: #ping
                            try:
                                ping_run(self.conn_address, cid, rid)
                            except:
                                print("[{}] Could not ping.".format(self.id))
                                self.p.terminate()
                                self.p=None
                                removeDir(self.workingdir.fullpath())
                                self.workingdir=None
                                #stop simulations
                                break
                        #check if trisurf is still running. If not break the innermost loop.
                        sleep(1)
                        if(self.p.poll() is not None): # trisurf exited!
                            print("[{}] Trisurf was stopped with return code {}".format(self.id, self.p.returncode))
                            if(self.p.returncode>0):
                                try:
                                    send_error_report(self.conn_address, cid, rid, self.p.returncode)
                                except:
                                    print("[{}] Server didn't accept error report".format(self.id))
                            removeDir(self.workingdir.fullpath())
                            self.workingdir=None
                            self.p=None
                            break
                        self.sleep(self.update_seconds-1)
 
 
 
#Stolen from trisurf library... Therefore, this client is not dependent on the installation of the library.
class Directory:
    '''
    Class deals with the paths where the simulation is run and data is stored.
    '''
    def __init__(self, maindir=".", simdir=""):
        '''Initialization Directory() takes two optional parameters, namely maindir and simdir. Defaults to current directory. It sets local variables maindir and simdir accordingly.'''
        self.maindir=maindir
        self.simdir=simdir
        return
 
    def fullpath(self):
        '''
        Method returns string of path where the data is stored. It combines values of maindir and simdir as maindir/simdir on Unix.
        '''
        return os.path.join(self.maindir,self.simdir)
 
    def exists(self):
        ''' Method checks whether the directory  specified by fullpath() exists. It return True/False on completion.'''
        path=self.fullpath()
        if(os.path.exists(path)):
            return True
        else:
            return False
 
    def make(self):
        ''' Method make() creates directory. If it fails it exits the program with error message.'''
        try:
            os.makedirs(self.fullpath())
        except:
            print("Cannot make directory "+self.fullpath()+"\n")
            exit(1)
        return
 
    def makeifnotexist(self):
        '''Method makeifnotexist() creates directory if it does not exist.'''
        if(self.exists()==0):
            self.make()
            return True
        else:
            return False
 
    def remove(self):
        '''Method remove() removes directory recursively. WARNING! No questions asked.'''
        if(self.exists()):
            try:
                os.rmdir(self.fullpath())
            except:
                print("Cannot remove directory "+self.fullpath()+ "\n")
                exit(1)
        return
 
    def goto(self):
        '''
        Method goto() moves current directory to the one specified by fullpath(). WARNING: when using the relative paths, do not call this function multiple times.
        '''
        try:
            os.chdir(self.fullpath())
        except:
            print("Cannot go to directory "+self.fullpath()+"\n")
        return
 
 
 
#--- SIGINT and SIGTERM HANDLING ---
def signal_handler(signal,frame):
    t.stop()
    t.join()
    print("Process ended with signal " +str(signal))
    sys.exit(signal)
#--- END SIGINT and SIGTERM----
 
if __name__ == '__main__':
 
    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)
 
    t=ClientThread(update_seconds=100)
    t.start()
    print("main")
    while(True):
        sleep(1000)