瀏覽代碼

check for key in ptable on launch, move logging to 'log()'

David 10 年之前
父節點
當前提交
ab92ce93d2
共有 1 個文件被更改,包括 25 次插入13 次删除
  1. 25 13
      wsread.py

+ 25 - 13
wsread.py

@@ -16,6 +16,12 @@ ptable["video"] = [ "/home/nginx/scripts-snowmix/working-udp-video.sh", "1", "0"
 ptable["rtmp"] =  [ "/home/nginx/scripts-snowmix/stream.sh"]
 
 log_list = {}
+DEBUG = False
+
+def log(s):
+	if DEBUG == False:
+		return
+	print(s)
 
 def conn(target):
 	global ws
@@ -34,25 +40,25 @@ def close():
 	ws.close()
 
 def sighandler(signal,frame):
-	print("\rExiting!")
+	log("\rExiting!")
 	close()
-	print("Bye!")
+	log("Bye!")
 	sys.exit(0)
 
 def stop(key):
 	if key not in p_list:
-		print("key %s doesn't exist! not killing!" % (key))
+		log("key %s doesn't exist! not killing!" % (key))
 		return
 	try:
 		os.killpg(os.getpgid(p_list[key].pid), signal.SIGTERM)
 	except Exception as e:
-		print("This process (%d) can't be killed! Why?" % p_list[key].pid)
-		print(e)
+		log("This process (%d) can't be killed! Why?" % p_list[key].pid)
+		log(e)
 
 	try:
 		log_list[key].close()
 	except Exception as e:
-		print("ex file: %s" % e)
+		log("ex file: %s" % e)
 
 	if key in log_list:
 		del log_list[key]
@@ -61,10 +67,14 @@ def stop(key):
 		
 def launch(key,options=None):
 	if key in p_list:
-		print("key %s exist! not running!" % (key))
+		log("key %s exist! not running!" % (key))
 		return
 	
-	tolaunch = ptable[j["key"]]
+	if key not in ptable:
+		log("This process (%s) is not on my table!" % key)
+		return
+
+	tolaunch = ptable[key]
 	if options is not None:
 		tolaunch = tolaunch + options
 
@@ -84,9 +94,9 @@ def sendmsg(msg):
 		ret=s.recv(4096)
 		s.close()
 	except Exception as e:
-		print(e)
+		log(e)
 		return None
-	print("[nc output] %s " % ret)
+	log("[nc output] %s " % ret)
 	return ret
 
 ip="ws://192.168.1.208:9001"
@@ -98,21 +108,23 @@ while True:
 	try:
 		data = ws.recv()
 	except Exception as e:
-		print(e)
+		log(e)
 		conn(ip)
 		continue
 
 	try:
 		j = json.loads(data)
 	except Exception as e:
-		print(e)
-		print("Invalid json?")
+		log(e)
+		log("Invalid json?")
 		continue
 	
 	if ("type" not in j or "command" not in j or
 		"key" not in j or j["type"] != "snowmix"):
+		log("Dropping: %s" % j)
 		continue
 
+	log(j)
 	if j["command"] == "launch":
 		launch(j["key"])