def print_solution_set(sol_set): """ Print the value of each solution in an solution set. :param sol_set: solution set :return: no return """ for sol in sol_set: ToolFunction.log('value: %f' % (sol.get_value())) return
def start(port): """ Start the control server. :param port: The port occupied by the control server :return: no return """ local_ip = socket.gethostbyname(socket.gethostname()) ToolFunction.log("Control server ip_port: %s:%s" %(local_ip, port)) cs = ControlServer(local_ip, port) cs.start()
def start(self): """ Start this control server. :return: no return """ lock = threading.RLock() # start threads worker = [] worker.append(threading.Thread(target=self.communication)) for t in worker: t.setDaemon(True) t.start() # start main thread while True: cmd = int(self.input("[zoosrv] Please input command sequence number, 1: print evaluation servers, 2: " "shut down evaluation servers, 3: exit\n")) with lock: if cmd == 1: ToolFunction.log("The number of evaluation servers: %s" % len(self.evaluation_server)) ToolFunction.log(str(self.evaluation_server)) elif cmd == 2: self.shut_down_control() elif cmd == 3: return
def shut_down(self, ip_port): """ Shut down one evaluation server, which bounds to ip_port. :param ip_port: ip:port of the evaluation server :return: no return """ es = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # if ip_port not in self.evaluation_server: ToolFunction.log("no such ip:port") return ip, port = ip_port.split(":") port = int(port) addr = (ip, port) es.connect(addr) es.sendall("control server: shutdown#".encode()) result = receive(1024, es) if result == "success": ToolFunction.log("%s: manage to shut down" % ip_port) else: ToolFunction.log("fail to shut down") es.close()
def start_server(self, control_server, shared_fold): """ Start this evaluation server. :param control_server: control server address :param shared_fold: current working directory :return: target function name """ # send evaluation server address to control server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((self.explain_address(control_server))) s.sendall("evaluation server#".encode()) receive(self.__data_length, s) s.sendall( (self.__server_ip + ':' + str(self.__server_port) + "#").encode()) s.close() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((self.__server_ip, self.__server_port)) s.listen(5) all_connect = 0 restart = False ToolFunction.log( "Initialize evaluation process successfully: ip=%s, port=%s" % (self.__server_ip, self.__server_port)) while True: # get x from client es, address = s.accept() # print all_connect + 1, ' get connected...' all_connect += 1 cmd = receive(self.__data_length, es) if cmd == "control server: shutdown": es.sendall("success#".encode()) break elif cmd == "client: calculate": es.sendall("calculate\n".encode()) try: msg = receive(self.__data_length, es) es.sendall("receive\n".encode()) if msg == "pposs": msg = receive(self.__data_length, es) addr, func, constraint = msg.split(":") es.sendall("receive\n".encode()) load = Loader() module = load.load(shared_fold + addr) calculate_fx = module[func] calculate_constraint = module[constraint] data = receive(self.__data_length, es) x = [] data_str = data.split(' ') for istr in data_str: x.append(float(istr)) fx = calculate_fx(Solution(x=x)) c = calculate_constraint(Solution(x=x)) fx_x = str(fx) + ' ' + str(c) + "\n" es.sendall(fx_x.encode()) msg = "Server:%s:%s f(x):%s constraint:%s connect num:%s client address:%s" % ( self.__server_ip, self.__server_port, fx, c, all_connect, address) ToolFunction.log(msg) elif msg == "asracos": msg = receive(self.__data_length, es) addr, func = msg.split(":") es.sendall("receive\n".encode()) load = Loader() module = load.load(shared_fold + addr) calculate = module[str(func)] data = receive(self.__data_length, es) x = [] data_str = data.split(' ') for istr in data_str: x.append(float(istr)) fx = calculate(Solution(x=x)) fx_x = str(fx) + "\n" es.sendall(fx_x.encode()) msg = "Server:%s:%s f(x):%s connect num:%s client address:%s" % ( self.__server_ip, self.__server_port, fx, all_connect, address) ToolFunction.log(msg) else: ToolFunction.log( "Exception: %s method is not implemented" % msg) except BaseException as excepmsg: ToolFunction.log("Exception") ToolFunction.log(str(excepmsg)) es.sendall(("Exception: " + str(excepmsg)).encode()) restart = True break # print ("send result finished, result: " + str(fx_x)) elif cmd == "control server: restart": restart = True break else: ToolFunction.log("Command error: no such cmd") es.close() ToolFunction.log("Server close!") s.close() if restart is True: ToolFunction.log("Server restart") self.start_server(control_server, shared_fold)
def print_solution(self): ToolFunction.log('x: ' + repr(self.__x)) ToolFunction.log('value: ' + repr(self.__value))
def communication(self): """ main process :return: """ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # s.bind((self.ip, self.port)) s.listen(20) while True: client, address = s.accept() cmd = receive(1024, client) client.sendall("success#\n".encode()) # receive from evaluation servers if cmd == "evaluation server": ip_port = receive(1024, client) if ip_port not in self.evaluation_server: self.evaluation_server.append(ip_port) ToolFunction.log("Receive ip_port from evaluation server: " + ip_port) # send to client elif cmd == "client: require servers": require_num = int(receive(1024, client)) require_num = min(require_num, len(self.evaluation_server)) msg = "" for i in range(require_num): if i == 0: msg += self.evaluation_server[i] else: msg = msg + ' ' + self.evaluation_server[i] if require_num == 0: msg += "No available evaluation process" msg += '\n' self.evaluation_server = self.evaluation_server[require_num:] if require_num <= 1: client.sendall(('Get %d evaluation process\n' % require_num).encode()) else: client.sendall(('Get %d evaluation processes\n' % require_num).encode()) client.sendall(msg.encode()) ToolFunction.log("provide %d evaluation servers for task: %s" % (require_num, address)) # receive from client elif cmd == "client: return servers": data_str = receive(1024, client) ToolFunction.log("Receive ip_port from client: " + data_str) ip_ports = data_str.split() for ip_port in ip_ports: if ip_port not in self.evaluation_server: self.evaluation_server.append(ip_port) # exception happens at client. restart evaluation servers elif cmd == "client: restart": data_str = receive(1024, client) ToolFunction.log("Client exception, receive ip_port from client: " + data_str) ip_ports = data_str.split() for ip_port in ip_ports: if ip_port not in self.evaluation_server: ip, port = ip_port.split(":") port = int(port) addr = (ip, port) s.connect(addr) s.sendall("control server: restart\n".encode()) self.evaluation_server.append(ip_port) else: ToolFunction.log("Command error")
def shut_down_control(self): """ Shut down evaluation servers. :return: no return """ try: ToolFunction.log("you can input three different kinds of commands") ToolFunction.log("1.all ==> shut down all servers. e.g. all") ToolFunction.log("2.ip:ip1 ==> shut down all servers having ip1 as ip. e.g. ip:127.0.0.1") ToolFunction.log("3.ip_port: ip1:port1 ip2:port2 ip3:port3 ... ==> shut down specific servers. e.g. ip_port: 127.0.0.1:20000 127.0.0.1:20001") msg = self.input() new_cal_server = copy.deepcopy(self.evaluation_server) if msg == "all": for ip_port in self.evaluation_server: self.shut_down(ip_port) new_cal_server.remove(ip_port) elif msg[:7] == "ip_port": ip_ports = msg[9:].split(' ') for ip_port in ip_ports: self.shut_down(ip_port) new_cal_server.remove(ip_port) elif msg[:2] == "ip": ip = msg[3:] for ip_port in self.evaluation_server: sip, port = ip_port.split(":") if sip == ip: self.shut_down(ip_port) new_cal_server.remove(ip_port) else: ToolFunction.log("No such command") self.evaluation_server = new_cal_server except Exception as e: ToolFunction.log("input error")