init - add late joining (only works when paused)
This commit is contained in:
parent
c958cc6634
commit
1ff5e452bb
5 changed files with 22 additions and 17 deletions
|
@ -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}")
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue