init - add sync and spawning of gliders
This commit is contained in:
parent
331e463592
commit
bcb8de9a9c
12 changed files with 135 additions and 30 deletions
|
@ -14,8 +14,10 @@ class APIRequests:
|
|||
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}")
|
||||
def get_edge(self, target: Member, direction: Direction,counter:int):
|
||||
print(f"Getting {direction} edge from {target} with url http://{target.ip}:{target.port}/border/{direction.name}")
|
||||
response = requests.get(f"http://{target.ip}:{target.port}/border/{direction.name}?counter={counter}")
|
||||
print(f"Got {direction} edge from {target}")
|
||||
return response.json()
|
||||
|
||||
def toggle_pause(self, neighbour: Member, new_state: bool):
|
||||
|
|
28
Code/Communication/EdgeSync.py
Normal file
28
Code/Communication/EdgeSync.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
from Code.Communication.Direction import Direction
|
||||
|
||||
|
||||
class EdgeSync:
|
||||
|
||||
def __init__(self, current_edges: dict, counter: int):
|
||||
self.old_edges = {}
|
||||
self.current_edges = current_edges
|
||||
self.counter = counter
|
||||
self.counter_old = counter - 1
|
||||
|
||||
def get_edge(self, edg_pos: Direction, counter: int) -> list:
|
||||
while counter == self.counter + 1:
|
||||
pass
|
||||
|
||||
if counter == self.counter:
|
||||
return self.current_edges[edg_pos]
|
||||
elif counter == self.counter_old:
|
||||
return self.old_edges[edg_pos]
|
||||
else:
|
||||
raise ValueError(f"Requested edge for counter {counter}, but having counter {self.counter} ")
|
||||
|
||||
|
||||
def update_edges(self, new_edges, counter: int):
|
||||
self.counter_old = self.counter
|
||||
self.counter = counter
|
||||
self.old_edges = self.current_edges
|
||||
self.current_edges = new_edges
|
|
@ -25,10 +25,9 @@ class Neighbours:
|
|||
self.neighbours[direction] = member
|
||||
return self.own_process, True
|
||||
|
||||
def get_edge(self, direction: Direction):
|
||||
def get_edge(self, direction: Direction,counter:int):
|
||||
if direction in self.neighbours:
|
||||
print(f"Getting ghost edge from {self.neighbours[direction]}")
|
||||
return self.api.get_edge(self.neighbours[direction], mirror(direction))
|
||||
return self.api.get_edge(self.neighbours[direction], mirror(direction),counter)
|
||||
elif direction == Direction.RIGHT or direction.LEFT:
|
||||
return [False] * GeneralConfig.fields_amount_y
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import json
|
||||
import re
|
||||
import urllib.parse
|
||||
from dataclasses import asdict
|
||||
from http.server import BaseHTTPRequestHandler
|
||||
|
||||
|
@ -16,19 +17,25 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|||
print("got Get request")
|
||||
if self.path == "/":
|
||||
self.send_response(200, "running")
|
||||
self.end_headers()
|
||||
return
|
||||
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()])
|
||||
|
||||
direction = re.findall("/border/(left|right).*", self.path, flags=re.IGNORECASE)[0]
|
||||
params = urllib.parse.parse_qs(self.path)
|
||||
counter = int(params[list(params.keys())[0]][0])
|
||||
print(f"Getting {direction} edge for counter {counter}")
|
||||
cells = self.game_state.edge_sync.get_edge(Direction[direction.upper()], counter)
|
||||
print(f"Serving {direction} edge")
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type', 'application/json')
|
||||
self.end_headers()
|
||||
self.wfile.write(json.dumps(cells).encode('utf8'))
|
||||
return
|
||||
|
||||
else:
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
|
||||
return
|
||||
|
||||
"""
|
||||
/connect/right
|
||||
|
@ -53,25 +60,30 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|||
self.send_header('Content-Type', 'application/json')
|
||||
self.end_headers()
|
||||
self.wfile.write(json.dumps(asdict(neighbour)).encode('utf8'))
|
||||
return
|
||||
else:
|
||||
self.send_response(307)
|
||||
self.send_header('Location', f"http://{neighbour.ip}:{neighbour.port}{self.path}")
|
||||
self.end_headers()
|
||||
return
|
||||
|
||||
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}")
|
||||
print(f"pause endpoint {new_state} old state {self.game_state.is_evolving}")
|
||||
|
||||
if new_state == self.game_state.pause_for_input:
|
||||
if new_state == self.game_state.is_evolving:
|
||||
print("got pause signal but already in the correct state")
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
return
|
||||
else:
|
||||
self.send_response(200)
|
||||
self.game_state.pause_for_input = new_state
|
||||
self.game_state.is_evolving = new_state
|
||||
self.neighbours.toggle_pause(new_state)
|
||||
self.end_headers()
|
||||
return
|
||||
|
||||
else:
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
return
|
||||
|
|
|
@ -1,10 +1,16 @@
|
|||
from http.server import HTTPServer
|
||||
from socketserver import ThreadingMixIn
|
||||
|
||||
from Code.Communication.Neighbours import Neighbours
|
||||
from Code.Communication.RequestHandler import RequestHandler
|
||||
from Code.UI.PlayingField import GameState
|
||||
|
||||
|
||||
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
|
||||
""" This class allows to handle requests in separated threads.
|
||||
No further content needed, don't touch this. """
|
||||
|
||||
|
||||
class Server:
|
||||
def __init__(self, neighbours: Neighbours,game_state:GameState):
|
||||
self.server = None
|
||||
|
@ -21,6 +27,6 @@ class Server:
|
|||
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 = ThreadedHTTPServer((self.ip, self.port), RequestHandler)
|
||||
self.server.serve_forever()
|
||||
print("Stopped server")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue