Put main bapsicle server into class.
This commit is contained in:
parent
90d5020a87
commit
88f28b6090
1 changed files with 142 additions and 144 deletions
286
server.py
286
server.py
|
@ -43,181 +43,179 @@ setproctitle("server.py")
|
||||||
|
|
||||||
|
|
||||||
class BAPSicleServer:
|
class BAPSicleServer:
|
||||||
|
|
||||||
|
default_state = {
|
||||||
|
"server_version": "",
|
||||||
|
"server_build": "",
|
||||||
|
"server_name": "URY BAPSicle",
|
||||||
|
"host": "localhost",
|
||||||
|
"port": 13500,
|
||||||
|
"ws_port": 13501,
|
||||||
|
"num_channels": 3,
|
||||||
|
"serial_port": None,
|
||||||
|
"ser_connected": False,
|
||||||
|
"myradio_api_key": None,
|
||||||
|
"myradio_base_url": "https://ury.org.uk/myradio",
|
||||||
|
"myradio_api_url": "https://ury.org.uk/api"
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
player: List[multiprocessing.Process] = []
|
||||||
|
websockets_server: multiprocessing.Process
|
||||||
|
controller_handler: multiprocessing.Process
|
||||||
|
player_handler: multiprocessing.Process
|
||||||
|
webserver: multiprocessing.Process
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
||||||
startServer()
|
self.startServer()
|
||||||
|
|
||||||
# def get_flask(self):
|
def startServer(self):
|
||||||
# return app
|
if isMacOS():
|
||||||
|
multiprocessing.set_start_method("spawn", True)
|
||||||
|
|
||||||
|
process_title = "startServer"
|
||||||
|
setproctitle(process_title)
|
||||||
|
# multiprocessing.current_process().name = process_title
|
||||||
|
|
||||||
default_state = {
|
self.logger = LoggingManager("BAPSicleServer")
|
||||||
"server_version": "",
|
|
||||||
"server_build": "",
|
|
||||||
"server_name": "URY BAPSicle",
|
|
||||||
"host": "localhost",
|
|
||||||
"port": 13500,
|
|
||||||
"ws_port": 13501,
|
|
||||||
"num_channels": 3,
|
|
||||||
"serial_port": None,
|
|
||||||
"ser_connected": False,
|
|
||||||
"myradio_api_key": None,
|
|
||||||
"myradio_base_url": "https://ury.org.uk/myradio",
|
|
||||||
"myradio_api_url": "https://ury.org.uk/api"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
self.state = StateManager("BAPSicleServer", self.logger, self.default_state)
|
||||||
|
# TODO: Check these match, if not, trigger any upgrade noticies / welcome
|
||||||
|
self.state.update("server_version", config.VERSION)
|
||||||
|
build_commit = "Dev"
|
||||||
|
if isBundelled():
|
||||||
|
build_commit = build.BUILD
|
||||||
|
self.state.update("server_build", build_commit)
|
||||||
|
|
||||||
channel_to_q: List[Queue] = []
|
for channel in range(self.state.state["num_channels"]):
|
||||||
channel_from_q: List[Queue] = []
|
|
||||||
ui_to_q: List[Queue] = []
|
|
||||||
websocket_to_q: List[Queue] = []
|
|
||||||
controller_to_q: List[Queue] = []
|
|
||||||
|
|
||||||
channel_p: List[multiprocessing.Process] = []
|
self.player_to_q.append(multiprocessing.Queue())
|
||||||
websockets_server: multiprocessing.Process
|
self.player_from_q.append(multiprocessing.Queue())
|
||||||
controller_handler: multiprocessing.Process
|
self.ui_to_q.append(multiprocessing.Queue())
|
||||||
webserver: multiprocessing.Process
|
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?
|
||||||
|
|
||||||
def startServer():
|
self.player.append(
|
||||||
process_title = "startServer"
|
multiprocessing.Process(
|
||||||
setproctitle(process_title)
|
target=player.Player,
|
||||||
# multiprocessing.current_process().name = process_title
|
args=(channel, self.player_to_q[-1], self.player_from_q[-1], self.state)
|
||||||
|
)
|
||||||
global logger
|
|
||||||
global state
|
|
||||||
logger = LoggingManager("BAPSicleServer")
|
|
||||||
|
|
||||||
state = StateManager("BAPSicleServer", logger, default_state)
|
|
||||||
# TODO: Check these match, if not, trigger any upgrade noticies / welcome
|
|
||||||
state.update("server_version", config.VERSION)
|
|
||||||
build_commit = "Dev"
|
|
||||||
if isBundelled():
|
|
||||||
build_commit = build.BUILD
|
|
||||||
state.update("server_build", build_commit)
|
|
||||||
|
|
||||||
if isMacOS():
|
|
||||||
multiprocessing.set_start_method("spawn", True)
|
|
||||||
for channel in range(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())
|
|
||||||
|
|
||||||
# TODO Replace state with individual read-only StateManagers or something nicer?
|
|
||||||
|
|
||||||
channel_p.append(
|
|
||||||
multiprocessing.Process(
|
|
||||||
target=player.Player,
|
|
||||||
args=(channel, channel_to_q[-1], channel_from_q[-1], state)
|
|
||||||
# daemon=True
|
|
||||||
)
|
)
|
||||||
|
self.player[channel].start()
|
||||||
|
|
||||||
|
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)
|
||||||
)
|
)
|
||||||
channel_p[channel].start()
|
self.api_handler.start()
|
||||||
|
|
||||||
global api_from_q, api_to_q, api_handler, player_handler, websockets_server, controller_handler # , webserver
|
self.player_handler = multiprocessing.Process(
|
||||||
api_to_q = multiprocessing.Queue()
|
target=PlayerHandler,
|
||||||
api_from_q = multiprocessing.Queue()
|
args=(self.player_from_q, self.websocket_to_q, self.ui_to_q, self.controller_to_q),
|
||||||
api_handler = multiprocessing.Process(
|
)
|
||||||
target=APIHandler, args=(api_to_q, api_from_q, state)
|
self.player_handler.start()
|
||||||
)
|
|
||||||
api_handler.start()
|
|
||||||
|
|
||||||
player_handler = multiprocessing.Process(
|
# Note, state here will become a copy in the process.
|
||||||
target=PlayerHandler,
|
# It will not update, and callbacks will not work :/
|
||||||
args=(channel_from_q, websocket_to_q, ui_to_q, controller_to_q),
|
websockets_server = multiprocessing.Process(
|
||||||
)
|
target=WebsocketServer, args=(self.player_to_q, self.websocket_to_q, self.state)
|
||||||
player_handler.start()
|
)
|
||||||
|
websockets_server.start()
|
||||||
|
|
||||||
# Note, state here will become a copy in the process.
|
controller_handler = multiprocessing.Process(
|
||||||
# It will not update, and callbacks will not work :/
|
target=MattchBox, args=(self.player_to_q, self.controller_to_q, self.state)
|
||||||
websockets_server = multiprocessing.Process(
|
)
|
||||||
target=WebsocketServer, args=(channel_to_q, websocket_to_q, state)
|
controller_handler.start()
|
||||||
)
|
|
||||||
websockets_server.start()
|
|
||||||
|
|
||||||
controller_handler = multiprocessing.Process(
|
webserver = multiprocessing.Process(
|
||||||
target=MattchBox, args=(channel_to_q, controller_to_q, state)
|
target=WebServer, args=(self.player_to_q, self.ui_to_q, self.api_to_q, self.api_from_q, self.state)
|
||||||
)
|
)
|
||||||
controller_handler.start()
|
webserver.start()
|
||||||
|
|
||||||
webserver = multiprocessing.Process(
|
# TODO Move this to player or installer.
|
||||||
target=WebServer, args=(channel_to_q, ui_to_q, api_to_q, api_from_q, state)
|
if False:
|
||||||
)
|
if not isMacOS():
|
||||||
webserver.start()
|
|
||||||
|
|
||||||
# TODO Move this to player or installer.
|
# Temporary RIP.
|
||||||
if False:
|
|
||||||
if not isMacOS():
|
|
||||||
|
|
||||||
# Temporary RIP.
|
# Welcome Speech
|
||||||
|
|
||||||
# Welcome Speech
|
text_to_speach = pyttsx3.init()
|
||||||
|
text_to_speach.save_to_file(
|
||||||
|
"""Thank-you for installing BAPSicle - the play-out server from the broadcasting and presenting suite.
|
||||||
|
By default, this server is accepting connections on port 13500
|
||||||
|
The version of the server service is {}
|
||||||
|
Please refer to the documentation included with this application for further assistance.""".format(
|
||||||
|
config.VERSION
|
||||||
|
),
|
||||||
|
"dev/welcome.mp3",
|
||||||
|
)
|
||||||
|
text_to_speach.runAndWait()
|
||||||
|
|
||||||
text_to_speach = pyttsx3.init()
|
new_item: Dict[str, Any] = {
|
||||||
text_to_speach.save_to_file(
|
"channel_weight": 0,
|
||||||
"""Thank-you for installing BAPSicle - the play-out server from the broadcasting and presenting suite.
|
"filename": "dev/welcome.mp3",
|
||||||
By default, this server is accepting connections on port 13500
|
"title": "Welcome to BAPSicle",
|
||||||
The version of the server service is {}
|
"artist": "University Radio York",
|
||||||
Please refer to the documentation included with this application for further assistance.""".format(
|
}
|
||||||
config.VERSION
|
|
||||||
),
|
|
||||||
"dev/welcome.mp3",
|
|
||||||
)
|
|
||||||
text_to_speach.runAndWait()
|
|
||||||
|
|
||||||
new_item: Dict[str, Any] = {
|
self.player_to_q[0].put("ADD:" + json.dumps(new_item))
|
||||||
"channel_weight": 0,
|
self.player_to_q[0].put("LOAD:0")
|
||||||
"filename": "dev/welcome.mp3",
|
self.player_to_q[0].put("PLAY")
|
||||||
"title": "Welcome to BAPSicle",
|
|
||||||
"artist": "University Radio York",
|
|
||||||
}
|
|
||||||
|
|
||||||
channel_to_q[0].put("ADD:" + json.dumps(new_item))
|
while True:
|
||||||
channel_to_q[0].put("LOAD:0")
|
time.sleep(10000)
|
||||||
channel_to_q[0].put("PLAY")
|
|
||||||
|
|
||||||
while True:
|
def stopServer(self):
|
||||||
time.sleep(10000)
|
print("Stopping Controllers")
|
||||||
|
if self.controller_handler:
|
||||||
|
self.controller_handler.terminate()
|
||||||
|
self.controller_handler.join()
|
||||||
|
|
||||||
|
print("Stopping Websockets")
|
||||||
|
self.websocket_to_q[0].put("WEBSOCKET:QUIT")
|
||||||
|
if self.websockets_server:
|
||||||
|
self.websockets_server.join()
|
||||||
|
|
||||||
def stopServer():
|
print("Stopping API Handler")
|
||||||
global channel_p, channel_from_q, channel_to_q, websockets_server, controller_handler, webserver
|
if self.api_handler:
|
||||||
print("Stopping Controllers")
|
self.api_handler.terminate()
|
||||||
if controller_handler:
|
self.api_handler.join()
|
||||||
controller_handler.terminate()
|
|
||||||
controller_handler.join()
|
|
||||||
|
|
||||||
print("Stopping Websockets")
|
print("Stopping server.py")
|
||||||
websocket_to_q[0].put("WEBSOCKET:QUIT")
|
for q in self.player_to_q:
|
||||||
if websockets_server:
|
q.put("ALL:QUIT")
|
||||||
websockets_server.join()
|
|
||||||
del websockets_server
|
|
||||||
|
|
||||||
print("Stopping server.py")
|
for player in self.player:
|
||||||
for q in channel_to_q:
|
player.join()
|
||||||
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
|
|
||||||
print("Stopped all players.")
|
|
||||||
|
|
||||||
print("Stopping webserver")
|
del self.player_from_q
|
||||||
|
del self.player_to_q
|
||||||
|
print("Stopped all players.")
|
||||||
|
|
||||||
if webserver:
|
print("Stopping player handler")
|
||||||
webserver.terminate()
|
if self.player_handler:
|
||||||
webserver.join()
|
self.player_handler.terminate()
|
||||||
|
self.player_handler.join()
|
||||||
|
|
||||||
print("Stopped webserver")
|
print("Stopping webserver")
|
||||||
|
|
||||||
|
if self.webserver:
|
||||||
|
self.webserver.terminate()
|
||||||
|
self.webserver.join()
|
||||||
|
|
||||||
|
print("Stopped webserver")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in a new issue