Verteiltesystheme/Code/Communication/RequestHandler.py

78 lines
2.4 KiB
Python

import json
import re
from dataclasses import asdict
from http.server import BaseHTTPRequestHandler
from Code.Communication.Direction import Direction
from Code.Communication.Neighbours import Neighbours
from Code.UI.PlayingField import GameState
class RequestHandler(BaseHTTPRequestHandler):
neighbours: Neighbours = None
game_state: GameState = None
def do_GET(self):
print("got Get request")
if self.path == "/":
self.send_response(200, "running")
elif self.path.startswith("/border/"):
direction = re.findall("/border/(left|right)", self.path, flags=re.IGNORECASE)[0]
cells = self.game_state.field.get_edge(Direction[direction.upper()])
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(cells).encode('utf8'))
else:
self.send_response(404)
self.end_headers()
"""
/connect/right
/connect/left
with body : {ip:"string",port:number}
"""
def do_POST(self):
if self.path.startswith("/connect/"):
direction = re.findall("/connect/(left|right)", self.path, flags=re.IGNORECASE)[0]
data_string = self.rfile.read(int(self.headers['Content-Length']))
print(f"Got connection request with value {data_string}")
data = json.loads(data_string)
neighbour, accepted = self.neighbours.accept_connection(Direction[direction.upper()],
data["ip"],
data["port"])
print(f"Sending neighbour: {neighbour}")
if accepted:
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(asdict(neighbour)).encode('utf8'))
else:
self.send_response(307)
self.send_header('Location', f"http://{neighbour.ip}:{neighbour.port}{self.path}")
self.end_headers()
elif self.path.startswith("/pause/"):
new_state = re.findall("/pause/(stop|start)", self.path, flags=re.IGNORECASE)[0] == "start"
print(f"pause endpoint {new_state} old state {self.game_state.pause_for_input}")
if new_state == self.game_state.pause_for_input:
print("got pause signal but already in the correct state")
self.send_response(200)
self.end_headers()
else:
self.send_response(200)
self.game_state.pause_for_input = new_state
self.neighbours.toggle_pause(new_state)
self.end_headers()
else:
self.send_response(404)
self.end_headers()