Put main bapsicle server into class.

This commit is contained in:
Matthew Stratford 2021-04-17 21:47:46 +01:00
parent 90d5020a87
commit 88f28b6090

136
server.py
View file

@ -43,13 +43,6 @@ setproctitle("server.py")
class BAPSicleServer:
def __init__(self):
startServer()
# def get_flask(self):
# return app
default_state = {
"server_version": "",
@ -66,85 +59,87 @@ default_state = {
"myradio_api_url": "https://ury.org.uk/api"
}
channel_to_q: List[Queue] = []
channel_from_q: List[Queue] = []
player_to_q: List[Queue] = []
player_from_q: List[Queue] = []
ui_to_q: List[Queue] = []
websocket_to_q: List[Queue] = []
controller_to_q: List[Queue] = []
api_from_q: Queue
api_to_q: Queue
channel_p: List[multiprocessing.Process] = []
player: List[multiprocessing.Process] = []
websockets_server: multiprocessing.Process
controller_handler: multiprocessing.Process
player_handler: multiprocessing.Process
webserver: multiprocessing.Process
def __init__(self):
self.startServer()
def startServer(self):
if isMacOS():
multiprocessing.set_start_method("spawn", True)
def startServer():
process_title = "startServer"
setproctitle(process_title)
# multiprocessing.current_process().name = process_title
global logger
global state
logger = LoggingManager("BAPSicleServer")
self.logger = LoggingManager("BAPSicleServer")
state = StateManager("BAPSicleServer", logger, default_state)
self.state = StateManager("BAPSicleServer", self.logger, self.default_state)
# TODO: Check these match, if not, trigger any upgrade noticies / welcome
state.update("server_version", config.VERSION)
self.state.update("server_version", config.VERSION)
build_commit = "Dev"
if isBundelled():
build_commit = build.BUILD
state.update("server_build", build_commit)
self.state.update("server_build", build_commit)
if isMacOS():
multiprocessing.set_start_method("spawn", True)
for channel in range(state.state["num_channels"]):
for channel in range(self.state.state["num_channels"]):
channel_to_q.append(multiprocessing.Queue())
channel_from_q.append(multiprocessing.Queue())
ui_to_q.append(multiprocessing.Queue())
websocket_to_q.append(multiprocessing.Queue())
controller_to_q.append(multiprocessing.Queue())
self.player_to_q.append(multiprocessing.Queue())
self.player_from_q.append(multiprocessing.Queue())
self.ui_to_q.append(multiprocessing.Queue())
self.websocket_to_q.append(multiprocessing.Queue())
self.controller_to_q.append(multiprocessing.Queue())
# TODO Replace state with individual read-only StateManagers or something nicer?
channel_p.append(
self.player.append(
multiprocessing.Process(
target=player.Player,
args=(channel, channel_to_q[-1], channel_from_q[-1], state)
# daemon=True
args=(channel, self.player_to_q[-1], self.player_from_q[-1], self.state)
)
)
channel_p[channel].start()
self.player[channel].start()
global api_from_q, api_to_q, api_handler, player_handler, websockets_server, controller_handler # , webserver
api_to_q = multiprocessing.Queue()
api_from_q = multiprocessing.Queue()
api_handler = multiprocessing.Process(
target=APIHandler, args=(api_to_q, api_from_q, state)
self.api_to_q = multiprocessing.Queue()
self.api_from_q = multiprocessing.Queue()
self.api_handler = multiprocessing.Process(
target=APIHandler, args=(self.api_to_q, self.api_from_q, self.state)
)
api_handler.start()
self.api_handler.start()
player_handler = multiprocessing.Process(
self.player_handler = multiprocessing.Process(
target=PlayerHandler,
args=(channel_from_q, websocket_to_q, ui_to_q, controller_to_q),
args=(self.player_from_q, self.websocket_to_q, self.ui_to_q, self.controller_to_q),
)
player_handler.start()
self.player_handler.start()
# Note, state here will become a copy in the process.
# It will not update, and callbacks will not work :/
websockets_server = multiprocessing.Process(
target=WebsocketServer, args=(channel_to_q, websocket_to_q, state)
target=WebsocketServer, args=(self.player_to_q, self.websocket_to_q, self.state)
)
websockets_server.start()
controller_handler = multiprocessing.Process(
target=MattchBox, args=(channel_to_q, controller_to_q, state)
target=MattchBox, args=(self.player_to_q, self.controller_to_q, self.state)
)
controller_handler.start()
webserver = multiprocessing.Process(
target=WebServer, args=(channel_to_q, ui_to_q, api_to_q, api_from_q, state)
target=WebServer, args=(self.player_to_q, self.ui_to_q, self.api_to_q, self.api_from_q, self.state)
)
webserver.start()
@ -175,47 +170,50 @@ def startServer():
"artist": "University Radio York",
}
channel_to_q[0].put("ADD:" + json.dumps(new_item))
channel_to_q[0].put("LOAD:0")
channel_to_q[0].put("PLAY")
self.player_to_q[0].put("ADD:" + json.dumps(new_item))
self.player_to_q[0].put("LOAD:0")
self.player_to_q[0].put("PLAY")
while True:
time.sleep(10000)
def stopServer():
global channel_p, channel_from_q, channel_to_q, websockets_server, controller_handler, webserver
def stopServer(self):
print("Stopping Controllers")
if controller_handler:
controller_handler.terminate()
controller_handler.join()
if self.controller_handler:
self.controller_handler.terminate()
self.controller_handler.join()
print("Stopping Websockets")
websocket_to_q[0].put("WEBSOCKET:QUIT")
if websockets_server:
websockets_server.join()
del websockets_server
self.websocket_to_q[0].put("WEBSOCKET:QUIT")
if self.websockets_server:
self.websockets_server.join()
print("Stopping API Handler")
if self.api_handler:
self.api_handler.terminate()
self.api_handler.join()
print("Stopping server.py")
for q in channel_to_q:
for q in self.player_to_q:
q.put("ALL:QUIT")
for channel in channel_p:
try:
channel.join()
except Exception as e:
print("*** Ignoring exception:", e)
pass
finally:
del channel
del channel_from_q
del channel_to_q
for player in self.player:
player.join()
del self.player_from_q
del self.player_to_q
print("Stopped all players.")
print("Stopping player handler")
if self.player_handler:
self.player_handler.terminate()
self.player_handler.join()
print("Stopping webserver")
if webserver:
webserver.terminate()
webserver.join()
if self.webserver:
self.webserver.terminate()
self.webserver.join()
print("Stopped webserver")