| 
									
										
										
										
											2022-03-27 16:43:15 +02:00
										 |  |  | from Code.Communication.APIRequests import APIRequests | 
					
						
							| 
									
										
										
										
											2022-03-27 18:11:51 +02:00
										 |  |  | from Code.Communication.Direction import Direction, mirror | 
					
						
							| 
									
										
										
										
											2022-03-27 12:59:14 +02:00
										 |  |  | from Code.Communication.Member import Member | 
					
						
							| 
									
										
										
										
											2022-03-27 16:43:15 +02:00
										 |  |  | from Code.Config import GeneralConfig | 
					
						
							| 
									
										
										
										
											2022-03-27 12:59:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Neighbours: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	def __init__(self, own_process: Member): | 
					
						
							|  |  |  | 		self.neighbours = {} | 
					
						
							|  |  |  | 		self.own_process = own_process | 
					
						
							| 
									
										
										
										
											2022-03-27 16:43:15 +02:00
										 |  |  | 		self.api = APIRequests() | 
					
						
							| 
									
										
										
										
											2022-03-27 12:59:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	def connect(self, direction, ip, port): | 
					
						
							|  |  |  | 		print(f"connecting to {ip}:{port} on  {direction} side") | 
					
						
							| 
									
										
										
										
											2022-03-27 18:11:51 +02:00
										 |  |  | 		new_neighbour = self.api.connectToMember(self.own_process, ip, port, mirror(direction)) | 
					
						
							| 
									
										
										
										
											2022-03-27 16:43:15 +02:00
										 |  |  | 		self.neighbours[direction] = new_neighbour | 
					
						
							| 
									
										
										
										
											2022-03-27 12:59:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-27 16:43:15 +02:00
										 |  |  | 	def accept_connection(self, direction: Direction, ip, port) -> tuple[Member, bool]: | 
					
						
							| 
									
										
										
										
											2022-03-27 12:59:14 +02:00
										 |  |  | 		if direction in self.neighbours: | 
					
						
							| 
									
										
										
										
											2022-03-27 16:43:15 +02:00
										 |  |  | 			return self.neighbours[direction], False | 
					
						
							| 
									
										
										
										
											2022-03-27 12:59:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		member = Member(ip, port) | 
					
						
							|  |  |  | 		print(f"Adding neighbour {member.__repr__()}") | 
					
						
							|  |  |  | 		self.neighbours[direction] = member | 
					
						
							| 
									
										
										
										
											2022-03-27 16:43:15 +02:00
										 |  |  | 		return self.own_process, True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	def get_edge(self, direction: Direction): | 
					
						
							|  |  |  | 		if direction in self.neighbours: | 
					
						
							| 
									
										
										
										
											2022-03-28 16:29:53 +02:00
										 |  |  | 			print(f"Getting ghost edge from {self.neighbours[direction]}") | 
					
						
							| 
									
										
										
										
											2022-03-27 18:11:51 +02:00
										 |  |  | 			return self.api.get_edge(self.neighbours[direction], mirror(direction)) | 
					
						
							| 
									
										
										
										
											2022-03-27 16:43:15 +02:00
										 |  |  | 		elif direction == Direction.RIGHT or direction.LEFT: | 
					
						
							|  |  |  | 			return [False] * GeneralConfig.fields_amount_y | 
					
						
							| 
									
										
										
										
											2022-03-27 18:11:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	def toggle_pause(self,new_state:bool): | 
					
						
							|  |  |  | 		for neighbour in self.neighbours.values(): | 
					
						
							| 
									
										
										
										
											2022-03-28 16:29:53 +02:00
										 |  |  | 			print(f"Telling member {neighbour} toggle pause with {new_state}") | 
					
						
							| 
									
										
										
										
											2022-03-27 18:11:51 +02:00
										 |  |  | 			self.api.toggle_pause(neighbour,new_state) |