From c2dd264efc30851fc4be4eab7bc63e4827725701 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Sat, 05 May 2018 22:31:47 +0000
Subject: [PATCH] Added sending the subrun

---
 playground/tsclient.py |   48 +++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/playground/tsclient.py b/playground/tsclient.py
index b0d422d..4b0724c 100755
--- a/playground/tsclient.py
+++ b/playground/tsclient.py
@@ -17,8 +17,8 @@
 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}
+def get_client_id(addr, my_ip, my_hostname, subrun):
+	client_auth={'ip':my_ip,'hostname':my_hostname, 'subrun':subrun}
 	response=requests.post(addr+"/api/register/", data=client_auth)
 	if(response.status_code==200):
 		client_data=json.loads(response.text)
@@ -39,7 +39,10 @@
 		return (rid,tape,vtu,status)
 	else:
 		print(response.text)
-		raise ValueError
+		if(response.status_code==400):
+			raise ValueError
+		else:
+			raise NameError
 
 
 def ping_run(addr,cid, rid):
@@ -49,6 +52,16 @@
 		return
 	else:
 		raise ValueError
+
+def client_ping(addr,cid):
+	client_data={'client_id':cid}
+	response=requests.post(addr+"/api/pingclient/", 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}
@@ -97,8 +110,8 @@
 		self.id=subid
 		self.ip=get_ip()
 		self.hostname=get_hostname()
-		self.update_seconds=update_second
-#		self.subthreads=[]
+		self.update_seconds=update_seconds
+
 
 	def stop(self):
 		self._stop_event.set()
@@ -125,18 +138,34 @@
 	def run(self):
 		while(not self.isStopped()):
 			try:
-				cid=get_client_id(self.conn_address, self.ip, self.hostname)
+				cid=get_client_id(self.conn_address, self.ip, self.hostname, self.id)
 			except:
 				print("[{}] Could not get CID.".format(self.id))
 				self.sleep(10)
 				continue
 			#print("Got CID. getting RID.")
+			client_ping_time_elapsed=0
 			while(not self.isStopped()):
 				try:
 					(rid,tape,vtu,status)=get_run(self.conn_address,cid)
-				except:
+				except NameError:
 					print("[{}] Could not get RID.".format(self.id))
-					self.sleep(10)	
+					self.sleep(10)
+					client_ping_time_elapsed+=10
+					if(client_ping_time_elapsed>=250):
+						try:
+							client_ping(self.conn_address,cid)
+						except:
+							break
+						client_ping_time_elapsed=0
+					#if you put break instead of continue, there is no need to ping client. And it is more robust... 
+					continue
+				except ValueError:
+					print("[{}] Wrong CID? Getting new CID.".format(self.id))
+					#self.sleep(10)
+					break
+				except:
+					print("[{}] Cannot connect. Server down? Retrying....".format(self.id))
 					break
 				else:
 					#start separate thread with simulations.
@@ -285,6 +314,7 @@
 
 	t=ClientThread(update_seconds=100)
 	t.start()
-	print("main")
+	#t.join()
+	#print("main")
 	while(True):
 		sleep(1000)

--
Gitblit v1.9.3