Verteiltesystheme/Code/Communication/Server.py

33 lines
1.0 KiB
Python

from http.server import HTTPServer
from socketserver import ThreadingMixIn
from Code.Communication.Neighbours import Neighbours
from Code.Communication.RequestHandler import RequestHandler
from Code.UI.PlayingField import GameState
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
""" This class allows to handle requests in separated threads.
No further content needed, don't touch this. """
class Server:
def __init__(self, neighbours: Neighbours,game_state:GameState):
self.server = None
self.neighbours = neighbours
self.game_state = game_state
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
RequestHandler.game_state = self.game_state
print(f"HTTP Server Running on {self.ip}: {self.port}")
self.server = ThreadedHTTPServer((self.ip, self.port), RequestHandler)
self.server.serve_forever()
print("Stopped server")