1
0
Fork 0
Verteiltesystheme-game-of-life/Code/Communication/Neighbours.py

33 lines
1.1 KiB
Python

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:
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")
new_neighbour = self.api.connectToMember(self.own_process, ip, port, direction)
self.neighbours[direction] = new_neighbour
def accept_connection(self, direction: Direction, ip, port) -> tuple[Member, bool]:
if direction in self.neighbours:
return self.neighbours[direction], False
member = Member(ip, port)
print(f"Adding neighbour {member.__repr__()}")
self.neighbours[direction] = member
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