-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.py
92 lines (76 loc) · 2.33 KB
/
net.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
from flask import Flask
from flask import request
import os
import time
import requests
import random
import uuid
app = Flask(__name__)
queue = []
activeThreads = []
currentThread = -1
responses = {}
def _getChunk(hash):
print(f"added hash {hash} to queue, waiting for response...")
queue.insert(0,hash)
while not hash in responses.keys():
time.sleep(0.01)
return responses.pop(hash)
def _getChunkGroup(bulkId, chunks):
print(f"added chunk group to queue, waiting for response...")
print(type(bulkId))
chunkData = {"bulkId": str(bulkId), "chunks": chunks}
queue.insert(0,chunkData)
while not str(bulkId) in responses.keys():
time.sleep(0.01)
print("found bulk id in responses!")
return responses.pop(str(bulkId))
# For minetest mod to long poll
@app.route('/local/recieverequest')
def pollRequest():
global currentThread
id = request.args.get("id")
print(f"new thread with id {id}")
maxThreads = int(request.args.get("maxThreads"))
activeThreads.insert(0, id)
if currentThread == -1:
currentThread = activeThreads[0]
while (queue == []) or (currentThread != id):
time.sleep(0)
activeThreads.remove(id)
currentThread = currentThread[len(currentThread)-1]
print("Found item in queue, it is being served!")
print("new thread: ")
print(currentThread)
return queue.pop(0)
# For minetest mod to send request back
@app.route('/local/sendrequest', methods=['POST'])
def sendRequest():
print("Got post request...")
data = request.json
print("PACKET TYPE: ")
print(data["type"])
if data["type"] == "chunk":
responses[data["hash"]] = data["blocks"]
else:
print("Is bulk id, so placing into bulk id dict")
print(type(data["bulkId"]))
responses[data["bulkId"]] = data
print("responses: ")
print(responses.keys())
return "OK"
@app.route('/chunk')
def getChunk():
x = request.args.get('x')
y = request.args.get('y')
if x == None or y == None:
return "Invalid URL params"
return _getChunk(x+","+y)
@app.route('/chunkgroup')
def getChunkGroup():
chunksIn = request.args.getlist("chunk")
key = uuid.uuid4()
print(chunksIn)
return _getChunkGroup(key, chunksIn)
if __name__ == '__main__':
app.run(port=8080,host="0.0.0.0")