def send_heart_beat_package(): infoDB = serviceInfo() trafficDB = serviceTraffic() HB = redisHeartBeatPacket() idfs = infoDB.checkExpiredService() exceed = trafficDB.getExceedTrafficService() # update traffic of all services update_traffic() if idfs["status"] == "success": idfs_info = idfs["info"] for item in idfs_info: revoke(item) HB.updateExpireIdfs(idfs_info) if exceed["status"] == 'success': exceed_info = exceed["info"] for item in exceed_info: revoke(item) HB.updateTrafficExceedIdfs(exceed_info) # send heart_beat package pack_json = HB.generatePakcetContent() if config["CONTROL_SERVER_IP"] != "": #and config["SEND_HEARTBEAT_PACK"] == True: send_socket_request( config["CONTROL_SERVER_IP"], config["CONTROL_SERVER_UDP_PORT"], json.dumps(pack_json), type="UDP" )
def handle(self): data = str(self.request.recv(1024).strip(), 'utf-8') try: json_data = json.loads(data) if json_data["from"] == "host": if self.checkHostIP() == False: return self.sendSocket("error",403) # "ping" # just test if the server is OK. returns "pong" if json_data["command"] == "ping": return self.sendSocket("success","pong") # "new" # create new instance when elif json_data["command"] == "new": if json_data["from"] == "host": max_traffic = json_data["info"]["max_traffic"] max_devices = json_data["info"]["max_devices"] expire_timestamp = json_data["info"]["expire_timestamp"] traffic_strategy = json_data["info"]["traffic_strategy"] service_type = json_data["info"]["type"] dispatch_result = new_service(max_traffic,max_devices,service_type,expire_timestamp,strategy=traffic_strategy) if dispatch_result == None: return self.sendSocket("error",500) elif dispatch_result["status"] == "error": return self.sendSocket("error", dispatch_result["code"]) else: # return service_idf and expire_date rtn_model = { "expire_timestamp": int(expire_timestamp), "service_idf" : dispatch_result["info"]["service_idf"], "config" : dispatch_result["info"]["config"] } return self.sendSocket("success",rtn_model) else: return self.sendSocket("error",402) # connect to the remote server first.the server return the configuration. elif json_data["command"] == "connect": if json_data["from"] == "client": mac_addr = json_data["mac_addr"] service_idf = json_data["service_idf"] #OP result = connect(service_idf,mac_addr) if result["status"] == "error": return self.sendSocket("error",int(result["code"])) else: _data = json.dumps(result["info"]) return self.sendSocket("success",_data) pass else: return self.sendSocket("error",402) # if the instance expired , just kill it # if not necessary, plz don't call IT elif json_data["command"] == "revoke": if json_data["from"] == "host": service_idf = json_data["service_idf"] #OP res = revoke(service_idf) if res["status"] == "error": return self.sendSocket("error",res["code"]) else: return self.sendSocket("success",200) else: return self.sendSocket("error",402) # postpone elif json_data["command"] == "postpone": if json_data["from"] == "host": service_idf = json_data["service_idf"] expire_timestamp = int(json_data["postpone_timestamp"]) res = postpone(service_idf,expire_timestamp) if res["status"] == "error": return self.sendSocket("error",res["code"]) else: return self.sendSocket("success",200) else: return self.sendSocket("error",402) elif json_data["command"] == "increase_traffic": if json_data["from"] == "host": service_idf = json_data["service_idf"] traffic = json_data["traffic"] res = increase_traffic(service_idf,traffic) if res["status"] == "error": return self.sendSocket("error",res["code"]) else: return self.sendSocket("success",200) else: return self.sendSocket("error",402) elif json_data["command"] == "decrease_traffic": if json_data["from"] == "host": service_idf = json_data["service_idf"] traffic = json_data["traffic"] res = decrease_traffic(service_idf,traffic) if res["status"] == "error": return self.sendSocket("error",res["code"]) else: return self.sendSocket("success",200) else: return self.sendSocket("error",402) elif json_data["command"] == "adjust_quota": if json_data["from"] == "host": new_quota = json_data["new_quota"] #注意:这个调整只是临时的,服务只要一重启就会恢复原状 config["SERVICE_QUOTA"] = int(new_quota) return self.sendSocket("success",200) else: return self.sendSocket("error",402) # get real time traffic (debug) elif json_data["command"] == "__get_traffic": if json_data["from"] == "host": service_idf = json_data["service_idf"] trafficDB = serviceTraffic() traffic = trafficDB.getTraffic(service_idf) return self.sendSocket("success",traffic["info"]) else: return self.sendSocket("error",402) else: return self.sendSocket("error",405) except ValueError: return self.sendSocket("error",400) pass