-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadedResults.py
159 lines (137 loc) · 6.05 KB
/
UploadedResults.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
#import cgi
import datetime
import wsgiref.handlers
try:
import json
except:
import simplejson as json
import string
import cPickle as pickle
#import pickle
from google.appengine.runtime import apiproxy_errors
from google.appengine.ext import db
#from google.appengine.api import users
from google.appengine.api import taskqueue
from google.appengine.ext import webapp
from google.appengine.api import memcache
#from operator import attrgetter
import random
import time
import zlib
import threading
import structures
import logging
from structures import global_dict
import numpy
import marshal
import Queue
#from Queue import EMPTY
global_sync = {}
#sync_lock = threading.Lock()
#write_lock = threading.Lock()
last_write = {}
locks = {}
class UploadedResults(webapp.RequestHandler):
def post(self):
global global_dict
global global_sync
global locks
global last_write
#global sync_lock
starttime = time.time()
post_body = self.request.body
sections = post_body.split('&')
results = {}
for pair in sections:
ab = pair.split('=')
if ab is not None and len(ab) == 2:
results[ab[0]] = ab[1]
client = results.get("client","ERROR")
version = results.get("version","ERROR")
rumble = results.get("game",None)
bota = results.get("fname",None)
botb = results.get("sname",None)
uploads_allowed = global_dict.get("uploads allowed",None)
uploads_allowed_expired = global_dict.get("uploads allowed check time",None)
if uploads_allowed is None or uploads_allowed_expired is None or datetime.datetime.now() > uploads_allowed_expired :
tq = taskqueue.Queue()
tqs_r = tq.fetch_statistics_async()
tqs = tqs_r.get_result()
last_min = tqs.executed_last_minute
if last_min is None or last_min <= 120:
last_min = 120
tasks = tqs.tasks
if tasks is None:
tasks is 0
backlog = float(tasks)/last_min
uploads_allowed = backlog < 5
global_dict["uploads allowed"]=uploads_allowed
global_dict["uploads allowed check time"] = datetime.datetime.now() + datetime.timedelta(1./(24*60))
if not uploads_allowed:
bota_name = bota.split(" ")[0].split(".")[-1]
botb_name = botb.split(" ")[0].split(".")[-1]
self.response.out.write("OK. Queue full," + bota_name + " vs " + botb_name + " discarded.")
logging.info("Queue full, discarding " + bota_name + " vs " + botb_name)
return
if (version in structures.allowed_versions
and client in structures.allowed_clients
and rumble is not None
and bota is not None
and botb is not None):
#prio_q = taskqueue.Queue("priority-battles")
#prio_task_list_rpc = prio_q.lease_tasks_by_tag_async(30,1,rumble)
try:
taskqueue.add(url='/HandleQueuedResults', payload=json.dumps(results))
bota_name = bota.split(" ")[0].split(".")[-1]
botb_name = botb.split(" ")[0].split(".")[-1]
logging.info("adding " + bota_name + " vs " + botb_name )
except apiproxy_errors.OverQuotaError:
bota_name = bota.split(" ")[0].split(".")[-1]
botb_name = botb.split(" ")[0].split(".")[-1]
self.response.out.write("OK. Queue full," + bota_name + " vs " + botb_name + " discarded.")
logging.info("discarding " + bota_name + " vs " + botb_name + " via error")
#time.sleep(0.5)
return
except taskqueue.Error:
bota_name = bota.split(" ")[0].split(".")[-1]
botb_name = botb.split(" ")[0].split(".")[-1]
self.response.out.write("OK. Task queue error," + bota_name + " vs " + botb_name + " discarded.")
# prio_task_list = prio_task_list_rpc.get_result()
# if prio_task_list:
# prio_string = prio_task_list[0].payload
# self.response.out.write(prio_string)
# logging.info("sending back priority battle: " + prio_string + ", " + rumble)
# prio_q.delete_tasks(prio_task_list)
rq_name = rumble + "|queue"
try:
rumble_queue = global_dict[rq_name]
try:
prio_string = rumble_queue.get_nowait()
logging.info("sending back priority battle: " + prio_string + ", " + rumble)
self.response.out.write(prio_string)
#logging.info("Sent back priority battles: " + prio_string)
except Queue.Empty:
#logging.info("No available priority battles")
prio_string = None
except KeyError:
logging.info("No queue for rumble " + rumble + ", adding one!")
global_dict[rq_name] = Queue.Queue(maxsize=300)
bota_name = bota.split(" ")[0].split(".")[-1]
botb_name = botb.split(" ")[0].split(".")[-1]
self.response.out.write("OK. " + bota_name + " vs " + botb_name + " added to queue")
elapsed = time.time() - starttime
self.response.out.write(" in " + str(int(round(elapsed*1000))) + "ms")
if results["user"] == "Put_Your_Name_Here":
self.response.out.write("\nPlease set your username in /robocode/roborumble/{rumblename}.txt!")
else:
logging.info("version: " + client)
self.response.out.write("OK. CLIENT NOT SUPPORTED. Use one of: " + str(structures.allowed_clients) + ", not " + client)
#time.sleep(0.0)
application = webapp.WSGIApplication([
('/UploadedResults', UploadedResults)
], debug=True)
def main():
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()