2021-04-04 21:34:46 +00:00
|
|
|
#!/usr/bin/env python3
|
2020-10-25 01:23:24 +00:00
|
|
|
import multiprocessing
|
|
|
|
import time
|
2020-11-01 00:38:46 +00:00
|
|
|
import sys
|
2021-04-17 21:51:43 +00:00
|
|
|
from typing import Any
|
2020-11-01 00:38:46 +00:00
|
|
|
import webbrowser
|
2021-04-04 21:34:46 +00:00
|
|
|
from setproctitle import setproctitle
|
2020-10-25 01:23:24 +00:00
|
|
|
|
2021-04-17 21:51:43 +00:00
|
|
|
from helpers.the_terminator import Terminator
|
2020-10-25 01:23:24 +00:00
|
|
|
|
2021-04-17 21:51:43 +00:00
|
|
|
|
|
|
|
def startServer(notifications=False):
|
2021-04-19 14:45:20 +00:00
|
|
|
# Only spend the time importing the Server if we want to start the server. Speeds up web browser opens.
|
|
|
|
from server import BAPSicleServer
|
|
|
|
|
2020-11-01 00:31:58 +00:00
|
|
|
server = multiprocessing.Process(target=BAPSicleServer)
|
|
|
|
server.start()
|
|
|
|
|
2021-04-17 20:28:57 +00:00
|
|
|
sent_start_notif = False
|
2021-04-17 21:51:43 +00:00
|
|
|
|
|
|
|
terminator = Terminator()
|
2021-04-04 21:34:46 +00:00
|
|
|
try:
|
2021-04-17 21:51:43 +00:00
|
|
|
while not terminator.terminate:
|
|
|
|
time.sleep(1)
|
2021-04-04 21:34:46 +00:00
|
|
|
if server and server.is_alive():
|
2021-04-17 20:28:57 +00:00
|
|
|
if notifications and not sent_start_notif:
|
2022-03-12 15:59:36 +00:00
|
|
|
notif("Welcome to BAPSicle!")
|
2021-04-17 20:28:57 +00:00
|
|
|
sent_start_notif = True
|
2021-04-04 21:34:46 +00:00
|
|
|
pass
|
|
|
|
else:
|
2022-03-12 15:59:36 +00:00
|
|
|
printer("Server dead. Exiting.")
|
2021-04-17 20:28:57 +00:00
|
|
|
if notifications:
|
2022-03-12 15:59:36 +00:00
|
|
|
notif("BAPSicle Server Stopped!")
|
2022-03-19 17:22:42 +00:00
|
|
|
sys.exit(0)
|
2021-04-17 21:51:43 +00:00
|
|
|
|
|
|
|
if server and server.is_alive():
|
|
|
|
server.terminate()
|
2022-03-11 23:55:53 +00:00
|
|
|
server.join(timeout=20) # If we somehow get stuck stopping BAPSicle let it die.
|
2021-04-17 21:51:43 +00:00
|
|
|
|
|
|
|
# Catch the handler being killed externally.
|
2021-04-04 21:34:46 +00:00
|
|
|
except Exception as e:
|
2021-09-11 16:18:35 +00:00
|
|
|
printer("Received Exception {} with args: {}".format(
|
|
|
|
type(e).__name__, e.args))
|
2021-04-17 21:51:43 +00:00
|
|
|
if server and server.is_alive():
|
|
|
|
server.terminate()
|
2021-09-24 19:26:48 +00:00
|
|
|
server.join(timeout=20)
|
2021-04-17 21:51:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def printer(msg: Any):
|
|
|
|
print("LAUNCHER:{}".format(msg))
|
2020-11-01 00:31:58 +00:00
|
|
|
|
2022-03-12 17:08:29 +00:00
|
|
|
|
2022-03-12 15:59:36 +00:00
|
|
|
def notif(msg: str):
|
|
|
|
print("NOTIFICATION:{}".format(msg))
|
|
|
|
|
2020-11-01 02:37:40 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
if __name__ == "__main__":
|
2020-11-01 02:37:40 +00:00
|
|
|
# On Windows, calling this function is necessary.
|
|
|
|
# Causes all kinds of loops if not present.
|
|
|
|
# IT HAS TO BE RIGHT HERE, AT THE TOP OF __MAIN__
|
|
|
|
# NOT INSIDE AN IF STATEMENT. RIGHT. HERE.
|
|
|
|
# If it's not here, multiprocessing just doesn't run in the package.
|
|
|
|
# Freeze support refers to being packaged with Pyinstaller.
|
|
|
|
multiprocessing.freeze_support()
|
2022-03-12 15:01:51 +00:00
|
|
|
setproctitle("BAPSicle - Launcher")
|
2020-11-01 00:38:46 +00:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
# We got an argument! It's probably Platypus's UI.
|
|
|
|
try:
|
|
|
|
if (sys.argv[1]) == "Start Server":
|
2022-03-12 15:59:36 +00:00
|
|
|
notif("BAPSicle is starting, please wait...")
|
2020-11-01 00:38:46 +00:00
|
|
|
webbrowser.open("http://localhost:13500/")
|
2021-04-17 20:28:57 +00:00
|
|
|
startServer(notifications=True)
|
|
|
|
if sys.argv[1] == "Server":
|
|
|
|
webbrowser.open("http://localhost:13500/")
|
|
|
|
if sys.argv[1] == "Presenter":
|
|
|
|
webbrowser.open("http://localhost:13500/presenter/")
|
2020-11-01 00:38:46 +00:00
|
|
|
except Exception as e:
|
2022-03-12 15:59:36 +00:00
|
|
|
printer(
|
2021-09-11 15:49:08 +00:00
|
|
|
"ALERT:BAPSicle failed with exception of type {}:{}".format(
|
|
|
|
type(e).__name__, e
|
|
|
|
)
|
|
|
|
)
|
2022-03-19 17:22:42 +00:00
|
|
|
sys.exit(1)
|
2020-11-01 00:38:46 +00:00
|
|
|
|
2022-03-19 17:22:42 +00:00
|
|
|
sys.exit(0)
|
2020-11-01 00:38:46 +00:00
|
|
|
else:
|
|
|
|
startServer()
|
2022-03-19 17:22:42 +00:00
|
|
|
sys.exit(0)
|