init - add basic server for finding a neighbour
This commit is contained in:
parent
e5e441622b
commit
8cf2d4d7f1
8 changed files with 356 additions and 58 deletions
6
Code/Communication/Direction.py
Normal file
6
Code/Communication/Direction.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from enum import Enum
|
||||
|
||||
|
||||
class Direction(Enum):
|
||||
LEFT = 1
|
||||
RIGHT = 2
|
7
Code/Communication/Member.py
Normal file
7
Code/Communication/Member.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Member:
|
||||
ip: str
|
||||
port: int
|
22
Code/Communication/Neighbours.py
Normal file
22
Code/Communication/Neighbours.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
from Code.Communication.Direction import Direction
|
||||
from Code.Communication.Member import Member
|
||||
|
||||
|
||||
class Neighbours:
|
||||
|
||||
def __init__(self, own_process: Member):
|
||||
self.neighbours = {}
|
||||
self.own_process = own_process
|
||||
|
||||
def connect(self, direction, ip, port):
|
||||
print(f"connecting to {ip}:{port} on {direction} side")
|
||||
pass
|
||||
|
||||
def acceptConnection(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)
|
51
Code/Communication/RequestHandler.py
Normal file
51
Code/Communication/RequestHandler.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
import json
|
||||
import re
|
||||
import socketserver
|
||||
from dataclasses import asdict
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
|
||||
from Code.Communication.Direction import Direction
|
||||
from Code.Communication.Neighbours import Neighbours
|
||||
|
||||
|
||||
class RequestHandler(BaseHTTPRequestHandler):
|
||||
neighbours: Neighbours = None
|
||||
|
||||
def do_GET(self):
|
||||
print("got Get request")
|
||||
if self.path == "/":
|
||||
self.send_response(200, "running")
|
||||
self.end_headers()
|
||||
|
||||
def handle(self) -> None:
|
||||
super().handle()
|
||||
|
||||
|
||||
"""
|
||||
/connect/right
|
||||
/connect/left
|
||||
|
||||
with body : {ip:"string",port:number}
|
||||
"""
|
||||
|
||||
def do_POST(self):
|
||||
print("Got post request")
|
||||
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']))
|
||||
data = json.loads(data_string)
|
||||
neighbour, accepted = self.neighbours.acceptConnection(Direction[direction.upper()], data["ip"], data["port"])
|
||||
|
||||
print(f"Sending neighbour: {neighbour}")
|
||||
if accepted:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type', 'application/json')
|
||||
self.end_headers()
|
||||
self.wfile.write(json.dumps(asdict(neighbour)).encode('utf8'))
|
||||
else:
|
||||
self.send_response(303)
|
||||
self.send_header('Location', f"http://{neighbour.ip}:{neighbour.port}")
|
||||
self.end_headers()
|
||||
|
||||
self.end_headers()
|
||||
|
22
Code/Communication/Server.py
Normal file
22
Code/Communication/Server.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
from http.server import HTTPServer
|
||||
|
||||
from Code.Communication.Neighbours import Neighbours
|
||||
from Code.Communication.RequestHandler import RequestHandler
|
||||
|
||||
|
||||
class Server:
|
||||
def __init__(self, neighbours: Neighbours):
|
||||
self.neighbours = neighbours
|
||||
self.port = neighbours.own_process.port
|
||||
self.ip = neighbours.own_process.ip
|
||||
|
||||
def stop_server(self):
|
||||
print("Trying to stop server")
|
||||
self.server.shutdown()
|
||||
|
||||
def start(self):
|
||||
RequestHandler.neighbours = self.neighbours
|
||||
print(f"HTTP Server Running on {self.ip}: {self.port}")
|
||||
self.server = HTTPServer((self.ip, self.port), RequestHandler)
|
||||
self.server.serve_forever()
|
||||
print("Stopped server")
|
Loading…
Add table
Add a link
Reference in a new issue