diff --git a/Client/Client.py b/Client/Client.py index 5b3afd9..58f4ab8 100644 --- a/Client/Client.py +++ b/Client/Client.py @@ -267,13 +267,13 @@ def getCutechessCommand(arguments, data, nps): # Options for the Dev Engine devflags = '-engine dir=Engines/ cmd=./{0} proto={1} tc={2}{3} name={4}'.format( devCommand, data['test']['dev']['protocol'], timecontrol, devoptions, - '{0}-{1}'.format(data['test']['engine'], data['test']['dev']['sha'][:8]) + '{0}-{1}'.format(data['test']['engine'], data['test']['dev']['name']) ) # Options for the Base Engine baseflags = '-engine dir=Engines/ cmd=./{0} proto={1} tc={2}{3} name={4}'.format( baseCommand, data['test']['base']['protocol'], timecontrol, baseoptions, - '{0}-{1}'.format(data['test']['engine'], data['test']['base']['sha'][:8]) + '{0}-{1}'.format(data['test']['engine'], data['test']['base']['name']) ) # Options for opening selection @@ -446,47 +446,60 @@ def verifyEngine(arguments, data, engine): # Check for an invalid bench. Signal to the Client and the Server if bench != int(engine['bench']): - reportWrongBenchmark(arguments, data, engine) - print ('[ERROR] Invalid Bench. Got {0} Expected {1}'.format(bench, engine['bench'])) + reportWrongBenchmark(arguments, data, engine, bench) + raise Exception('Invalid Bench. Got {0} Expected {1}'.format(bench, engine['bench'])) # Return a valid bench or otherwise return None return nps if bench == int(engine['bench']) else None -def reportWrongBenchmark(arguments, data, engine): +def reportWrongBenchmark(arguments, data, engine, bench): + - # Server wants user verification, and then information about - # the test that threw the error, and which engine did it data = { - 'username' : arguments.username, - 'password' : arguments.password, - 'testid' : data['test']['id'], - 'engineid' : engine['id'], + 'username' : arguments.username, 'testid' : data['test']['id'], + 'password' : arguments.password, 'machineid' : data['machine']['id'], + 'correct' : engine['bench'] , 'wrong' : bench, + 'engine' : engine['name'], } - # Hit the server with information about wrong benchmark - url = pathjoin(arguments.server, 'wrongBench') - return requests.post(url, data=data, timeout=HTTP_TIMEOUT).text + url = pathjoin(arguments.server, 'clientWrongBench') + data = requests.post(url, data=data, timeout=HTTP_TIMEOUT).text + if data == 'Bad Machine': raise Exception('Bad Machine') def reportNodesPerSecond(arguments, data, nps): - # Server wants user verification, and then information about - # nodes per second searched for a specific machine id value + data = { - 'username' : arguments.username, - 'password' : arguments.password, - 'machineid' : data['machine']['id'], - 'nps' : nps, + 'username' : arguments.username, 'machineid' : data['machine']['id'], + 'password' : arguments.password, 'nps' : nps, } - # Hit the server with information about nps - url = pathjoin(arguments.server, 'submitNPS') - return requests.post(url, data=data, timeout=HTTP_TIMEOUT).text + url = pathjoin(arguments.server, 'clientSubmitNPS') + data = requests.post(url, data=data, timeout=HTTP_TIMEOUT).text + if data == 'Bad Machine': raise Exception('Bad Machine') + +def reportEngineError(arguments, data, line): + + pairing = line.split('(')[1].split(')')[0] + white, black = pairing.split(' vs ') + + error = line.split('{')[1].rstrip().rstrip('}') + error = error.replace('White', '-'.join(white.split('-')[1:]).rstrip()) + error = error.replace('Black', '-'.join(black.split('-')[1:]).rstrip()) + + data = { + 'username' : arguments.username, 'testid' : data['test']['id'], + 'password' : arguments.password, 'machineid' : data['machine']['id'], + 'error' : error, + } + + url = pathjoin(arguments.server, 'clientSubmitError') + data = requests.post(url, data=data, timeout=HTTP_TIMEOUT).text + if data == 'Bad Machine': raise Exception('Bad Machine') def reportResults(arguments, data, wins, losses, draws, crashes, timelosses): - # Server wants user verification, and then information about the machine, - # the result object, the test, and then the actual updated game results data = { 'username' : arguments.username, 'wins' : wins, 'password' : arguments.password, 'losses' : losses, @@ -495,7 +508,6 @@ def reportResults(arguments, data, wins, losses, draws, crashes, timelosses): 'testid' : data['test']['id'], 'timeloss' : timelosses, } - # Hit the server with the updated test results url = pathjoin(arguments.server, 'submitResults') try: return requests.post(url, data=data, timeout=HTTP_TIMEOUT).text except KeyboardInterrupt: sys.exit() @@ -507,6 +519,7 @@ def processCutechess(arguments, data, cutechess, concurrency): # Tracking for game results crashes = timelosses = 0 score = [0, 0, 0]; sent = [0, 0, 0] + errors = ['on time', 'disconnects', 'connection stalls', 'illegal'] while True: @@ -515,13 +528,15 @@ def processCutechess(arguments, data, cutechess, concurrency): if line != '': print(line) else: cutechess.wait(); return - # Parse engine crashes - if 'disconnects' in line or 'connection stalls' in line: - crashes += 1 + # Updated timeloss/crash counters + timelosses += 'on time' in line + crashes += 'disconnects' in line + crashes += 'connection stalls' in line - # Parse losses on time - if 'on time' in line: - timelosses += 1 + # Report any engine errors to the server + for error in errors: + if error in line: + reportEngineError(arguments, data, line) # Parse updates to scores if line.startswith('Score of'): diff --git a/OpenBench/urls.py b/OpenBench/urls.py index 3089a5c..0c32e74 100644 --- a/OpenBench/urls.py +++ b/OpenBench/urls.py @@ -53,10 +53,11 @@ urlpatterns = [ django.urls.path(r'clientGetFiles/', OpenBench.views.clientGetFiles), django.urls.path(r'clientGetBuildInfo/', OpenBench.views.clientGetBuildInfo), django.urls.path(r'clientGetWorkload/', OpenBench.views.clientGetWorkload), + django.urls.path(r'clientWrongBench/', OpenBench.views.clientWrongBench), + django.urls.path(r'clientSubmitNPS/', OpenBench.views.clientSubmitNPS), + django.urls.path(r'clientSubmitError/', OpenBench.views.clientSubmitError), # Links for the Client to work with the Server (DEAD) - django.urls.path(r'wrongBench/', OpenBench.views.wrongBench), - django.urls.path(r'submitNPS/', OpenBench.views.submitNPS), django.urls.path(r'submitResults/', OpenBench.views.submitResults), # Redirect anything else to the Index diff --git a/OpenBench/views.py b/OpenBench/views.py index 7b8134b..d9010da 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -452,6 +452,16 @@ def clientGetBuildInfo(request): @not_minified_response def clientGetWorkload(request): + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + # # + # POST : Return a Dictionary of data in order to complete a workload. If # + # there are no tests for the User, 'None' will be returned. If we # + # cannot authenticate the User, 'Bad Credentials' is returned. If # + # the posted Machine does not belong the the User, 'Bad Machine' # + # is returned. # + # # + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + # Verify the User's credentials user = django.contrib.auth.authenticate( username=request.POST['username'], @@ -461,56 +471,83 @@ def clientGetWorkload(request): # getWorkload() will verify the integrity of the request return HttpResponse(OpenBench.utils.getWorkload(user, request)) - @csrf_exempt @not_minified_response -def wrongBench(request): +def clientWrongBench(request): - # Verify that we got a valid login + # Verify the User's credentials user = django.contrib.auth.authenticate( username=request.POST['username'], password=request.POST['password']) if user == None: return HttpResponse('Bad Credentials') - try: - # Find the engine with the bad bench - engineid = int(request.POST['engineid']) - engine = Engine.objects.get(id=engineid) + # Verify the Machine belongs to the User + machine = Machine.objects.get(id=int(request.POST['machineid'])) + if machine.owner != user.username: return HttpResponse('Bad Machine') - # Find and stop the test with the bad bench - testid = int(request.POST['testid']) - test = Test.objects.get(id=testid) - test.finished = True - test.save() + # Find and stop the test with the bad bench + test = Test.objects.get(id=int(request.POST['testid'])) + test.finished = True; test.save() - # Log the bad bench so we know why the test was stopped - LogEvent.objects.create( - data='Invalid Bench', - author=request.POST['username'], - test=test) - except: pass + # Collect information on the Error + wrong = request.POST['wrong'] + correct = request.POST['correct'] + name = request.POST['engine'] + + # Format a nice Error message + message = 'Got {0} Expected {1} for {2}' + message = message.format(wrong, correct, name) + + # Log the error into the Events table + LogEvent.objects.create( + test = test, + data = message, + author = user.username) return HttpResponse('None') @csrf_exempt @not_minified_response -def submitNPS(request): +def clientSubmitNPS(request): - # Verify that we got a valid login + # Verify the User's credentials user = django.contrib.auth.authenticate( username=request.POST['username'], password=request.POST['password']) if user == None: return HttpResponse('Bad Credentials') - # Try to update the NPS for the machine - try: - machine = Machine.objects.get(id=int(request.POST['machineid'])) - machine.mnps = float(request.POST['nps']) / 1e6 - machine.save() - except: return HttpResponse('Bad Machine ID') + # Verify the Machine belongs to the User + machine = Machine.objects.get(id=int(request.POST['machineid'])) + if machine.owner != user.username: return HttpResponse('Bad Machine') + + # Update the NPS and return 'None' to signal no errors + machine.mnps = float(request.POST['nps']) / 1e6; machine.save() + return HttpResponse('None') + +@csrf_exempt +@not_minified_response +def clientSubmitError(request): + + # Verify the User's credentials + user = django.contrib.auth.authenticate( + username=request.POST['username'], + password=request.POST['password']) + if user == None: return HttpResponse('Bad Credentials') + + # Verify the Machine belongs to the User + machine = Machine.objects.get(id=int(request.POST['machineid'])) + if machine.owner != user.username: return HttpResponse('Bad Machine') + + # Log the Error into the Events table + LogEvent.objects.create( + author = user.username, + data = request.POST['error'], + test = Test.objects.get(id=int(request.POST['testid']))) return HttpResponse('None') + + @csrf_exempt @not_minified_response def submitResults(request):