Init - Randaustausch almost working in 1D
This commit is contained in:
parent
8cf2d4d7f1
commit
1bca8c3c89
10 changed files with 170 additions and 57 deletions
19
Code/Communication/APIRequests.py
Normal file
19
Code/Communication/APIRequests.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
from dataclasses import asdict
|
||||
|
||||
import requests as requests
|
||||
|
||||
from Code.Communication.Direction import Direction
|
||||
from Code.Communication.Member import Member
|
||||
|
||||
|
||||
class APIRequests:
|
||||
|
||||
def connectToMember(self, own_process: Member, ip, port, direction: Direction) -> Member:
|
||||
body = asdict(own_process)
|
||||
response = requests.post(f"http://{ip}:{port}/connect/{direction.name}", json=body)
|
||||
jsonValue = response.json()
|
||||
return Member(jsonValue["ip"], jsonValue["port"])
|
||||
|
||||
def get_edge(self, target: Member, direction: Direction):
|
||||
response = requests.get(f"http://{target.ip}:{target.port}/border/{direction.name}")
|
||||
return response.json()
|
|
@ -4,3 +4,5 @@ from enum import Enum
|
|||
class Direction(Enum):
|
||||
LEFT = 1
|
||||
RIGHT = 2
|
||||
TOP = 3
|
||||
BOTTOM = 4
|
||||
|
|
|
@ -5,3 +5,4 @@ from dataclasses import dataclass
|
|||
class Member:
|
||||
ip: str
|
||||
port: int
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
from Code.Communication.APIRequests import APIRequests
|
||||
from Code.Communication.Direction import Direction
|
||||
from Code.Communication.Member import Member
|
||||
from Code.Config import GeneralConfig
|
||||
|
||||
|
||||
class Neighbours:
|
||||
|
@ -7,16 +9,24 @@ class Neighbours:
|
|||
def __init__(self, own_process: Member):
|
||||
self.neighbours = {}
|
||||
self.own_process = own_process
|
||||
self.api = APIRequests()
|
||||
|
||||
def connect(self, direction, ip, port):
|
||||
print(f"connecting to {ip}:{port} on {direction} side")
|
||||
pass
|
||||
new_neighbour = self.api.connectToMember(self.own_process, ip, port, direction)
|
||||
self.neighbours[direction] = new_neighbour
|
||||
|
||||
def acceptConnection(self, direction: Direction, ip, port) -> tuple[Member, bool]:
|
||||
def accept_connection(self, direction: Direction, ip, port) -> tuple[Member, bool]:
|
||||
if direction in self.neighbours:
|
||||
return (self.neighbours[direction],False)
|
||||
return self.neighbours[direction], False
|
||||
|
||||
member = Member(ip, port)
|
||||
print(f"Adding neighbour {member.__repr__()}")
|
||||
self.neighbours[direction] = member
|
||||
return (self.own_process,True)
|
||||
return self.own_process, True
|
||||
|
||||
def get_edge(self, direction: Direction):
|
||||
if direction in self.neighbours:
|
||||
return self.api.get_edge(self.neighbours[direction], direction)
|
||||
elif direction == Direction.RIGHT or direction.LEFT:
|
||||
return [False] * GeneralConfig.fields_amount_y
|
||||
|
|
|
@ -6,16 +6,26 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
|
|||
|
||||
from Code.Communication.Direction import Direction
|
||||
from Code.Communication.Neighbours import Neighbours
|
||||
from Code.UI.Field import Field
|
||||
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")
|
||||
self.end_headers()
|
||||
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'))
|
||||
|
||||
def handle(self) -> None:
|
||||
super().handle()
|
||||
|
@ -29,12 +39,12 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|||
"""
|
||||
|
||||
def do_POST(self):
|
||||
print("Got post request")
|
||||
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 conenction request with value {data_string}")
|
||||
data = json.loads(data_string)
|
||||
neighbour, accepted = self.neighbours.acceptConnection(Direction[direction.upper()], data["ip"], data["port"])
|
||||
neighbour, accepted = self.neighbours.accept_connection(Direction[direction.upper()], data["ip"], data["port"])
|
||||
|
||||
print(f"Sending neighbour: {neighbour}")
|
||||
if accepted:
|
||||
|
|
|
@ -2,11 +2,14 @@ from http.server import HTTPServer
|
|||
|
||||
from Code.Communication.Neighbours import Neighbours
|
||||
from Code.Communication.RequestHandler import RequestHandler
|
||||
from Code.UI.PlayingField import GameState
|
||||
|
||||
|
||||
class Server:
|
||||
def __init__(self, neighbours: Neighbours):
|
||||
def __init__(self, neighbours: Neighbours,game_state:GameState):
|
||||
self.server = None
|
||||
self.neighbours = neighbours
|
||||
self.game_state = game_state
|
||||
self.port = neighbours.own_process.port
|
||||
self.ip = neighbours.own_process.ip
|
||||
|
||||
|
@ -16,6 +19,7 @@ class Server:
|
|||
|
||||
def start(self):
|
||||
RequestHandler.neighbours = self.neighbours
|
||||
RequestHandler.game_state = self.game_state
|
||||
print(f"HTTP Server Running on {self.ip}: {self.port}")
|
||||
self.server = HTTPServer((self.ip, self.port), RequestHandler)
|
||||
self.server.serve_forever()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue