From ec673dd8b594a808cb9d9a14937b3dcd4a365dc5 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Sat, 05 May 2018 20:08:23 +0000
Subject: [PATCH] Added pinging client, reconnection works.

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

diff --git a/playground/tsclient.py b/playground/tsclient.py
index b0d422d..83e3938 100755
--- a/playground/tsclient.py
+++ b/playground/tsclient.py
@@ -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()
@@ -131,12 +144,28 @@
 				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.

--
Gitblit v1.9.3