X-Git-Url: https://git.ucc.asn.au/?p=progcomp2013.git;a=blobdiff_plain;f=qchess%2Fsrc%2Fserver.py;h=5479997f55bdbd1629560c423964063673464304;hp=95af126cf6fab59a5cd8aaf7b009aa6c92fcaad3;hb=9b5054e5a06872ae85aca693079c17a0cd40c339;hpb=64978647120812a13948e2146de81281b38a54d5 diff --git a/qchess/src/server.py b/qchess/src/server.py index 95af126..5479997 100644 --- a/qchess/src/server.py +++ b/qchess/src/server.py @@ -1,58 +1,81 @@ def dedicated_server(): - max_games = 4 + global log_files + + max_games = 5 games = [] + gameID = 0 while True: # Get players + gameID += 1 + log("Getting clients...") s = socket.socket() + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(("0.0.0.0", 4562)) s.listen(2) ss = s.accept() - debug("Got white player") + log("Got white player") + + args = ["python", "qchess.py", "--no-graphics", "@network::"+str(4600+2*len(games)), "@network::"+str(4600+2*len(games))] + if len(log_files) != 0: + for l in log_files: + if l.name == "": + args.append("--log") + else: + args.append("--log="+str(l.name)+"_"+str(gameID)) - g = subprocess.Popen(["python", "qchess.py", "@network::"+str(4700+len(games)), "@network::"+str(4700+len(games)), "--log="+"_".join(str(datetime.datetime.now()).split(" ")) + ".log"], stdout=subprocess.PIPE) + g = subprocess.Popen(args, stdout=subprocess.PIPE) games.append(g) - ss[0].send("white " + str(4700 + len(games)-1)) - ss[0].shutdown(socket.SHUT_RDWR) + time.sleep(0.5) + ss[0].send("white " + str(4600 + 2*(len(games)-1))) + ss[0].shutdown(socket.SHUT_RD) ss[0].close() time.sleep(0.5) ss = s.accept() - debug("Got black player") + log("Got black player") - ss[0].send("black " + str(4700 + len(games)-1)) - ss[0].shutdown(socket.SHUT_RDWR) + time.sleep(0.5) + ss[0].send("black " + str(4600 + 2*(len(games)-1))) + ss[0].shutdown(socket.SHUT_RD) ss[0].close() s.shutdown(socket.SHUT_RDWR) s.close() + while len(games) > max_games: - ready = select.select(map(lambda e : e.stdout, games),[], [], None) - for r in ready: + #log("Too many games; waiting for game to finish...") + ready = select.select(map(lambda e : e.stdout, games),[], []) + for r in ready[0]: s = r.readline().strip(" \r\n").split(" ") if s[0] == "white" or s[0] == "black": for g in games[:]: if g.stdout == r: + log("Game " + str(g) + " has finished") games.remove(g) + + return 0 + +def client(addr, player="@human"): + -def client(addr): s = socket.socket() s.connect((addr, 4562)) [colour,port] = s.recv(1024).strip(" \r\n").split(" ") - debug("Colour: " + colour + ", port: " + port) + #debug("Colour: " + colour + ", port: " + port) s.shutdown(socket.SHUT_RDWR) s.close() if colour == "white": - p = subprocess.Popen(["python", "qchess.py", "@human", "@network:"+addr+":"+port]) + p = subprocess.Popen(["python", "qchess.py", player, "@network:"+addr+":"+port]) else: - p = subprocess.Popen(["python", "qchess.py", "@network:"+addr+":"+port, "@human"]) + p = subprocess.Popen(["python", "qchess.py", "@network:"+addr+":"+port, player]) p.wait() - sys.exit(0) \ No newline at end of file + return 0 \ No newline at end of file