init - add late joining (only works when paused)

This commit is contained in:
qvalentin 2022-04-12 17:59:01 +02:00
parent c958cc6634
commit 1ff5e452bb
Signed by: qvalentin
GPG Key ID: C979FA1EAFCABF1C
5 changed files with 22 additions and 17 deletions

View File

@ -8,11 +8,11 @@ from Code.Communication.Member import Member
class APIRequests:
def connectToMember(self, own_process: Member, ip, port, direction: Direction) -> Member:
def connectToMember(self, own_process: Member, ip, port, direction: Direction) -> (Member,int):
body = asdict(own_process)
response = requests.post(f"http://{ip}:{port}/connect/{direction.name}", json=body,allow_redirects=True)
jsonValue = response.json()
return Member(jsonValue["ip"], jsonValue["port"])
return Member(jsonValue["ip"], jsonValue["port"]),int(jsonValue["counter"])
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}")

View File

@ -11,10 +11,11 @@ class Neighbours:
self.own_process = own_process
self.api = APIRequests()
def connect(self, direction, ip, port):
def connect(self, direction, ip, port) -> int:
print(f"connecting to {ip}:{port} on {direction} side")
new_neighbour = self.api.connectToMember(self.own_process, ip, port, mirror(direction))
new_neighbour,counter = self.api.connectToMember(self.own_process, ip, port, mirror(direction))
self.neighbours[direction] = new_neighbour
return counter
def accept_connection(self, direction: Direction, ip, port) -> tuple[Member, bool]:
if direction in self.neighbours:

View File

@ -37,14 +37,14 @@ class RequestHandler(BaseHTTPRequestHandler):
self.end_headers()
return
"""
/connect/right
/connect/left
with body : {ip:"string",port:number}
"""
def do_POST(self):
"""
/connect/right
/connect/left
with body : {ip:"string",port:number}
"""
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']))
@ -59,7 +59,9 @@ class RequestHandler(BaseHTTPRequestHandler):
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(asdict(neighbour)).encode('utf8'))
body = asdict(neighbour)
body["counter"]= self.game_state.counter
self.wfile.write(json.dumps(body).encode('utf8'))
return
else:
self.send_response(307)

View File

@ -33,16 +33,18 @@ if __name__ == "__main__":
own_port = 8080
neighbours = Neighbours(own_process=Member(own_ip, own_port))
game_state = GameState(neighbours)
server = Server(neighbours, game_state)
counter = 0
n_direction = Direction.LEFT
if len(args) > 5:
n_direction = args[5]
if len(args) >= 5:
n_ip = args[3]
n_port = int(args[4])
neighbours.connect(Direction[n_direction], n_ip, n_port)
counter= neighbours.connect(Direction[n_direction], n_ip, n_port)
game_state = GameState(neighbours,counter)
server = Server(neighbours, game_state)
serverThread = threading.Thread(target=server.start)
serverThread.start()

View File

@ -11,10 +11,10 @@ from Code.UI.Shape import Shape
class GameState:
def __init__(self, neighbours: Neighbours):
def __init__(self, neighbours: Neighbours,counter):
self.neighbours = neighbours
self.counter = 0
self.counter = counter
self.run = True
self.is_evolving = False
self.field = Field()
@ -75,7 +75,7 @@ class GameState:
self.counter_old = self.counter
def run_game(game_state: GameState):
def run_game(game_state: GameState,counter = 0):
pygame.init()
pygame.display.set_caption(GeneralConfig.window_caption)
window = pygame.display.set_mode((GeneralConfig.width, GeneralConfig.height))