-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
90 lines (73 loc) · 2.33 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import threading, signal, logging, shlex, traceback
from singstarmic.discoveryserver import DiscoveryServer
from singstarmic.appserver import AppServer
from singstarmic.catalogueserver import CatalogueServer
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
class MicServer:
is_running = False
servers = {}
def addServer(self, server):
thread = threading.Thread(target=server.run)
thread.name = server.__class__.__name__
self.servers[thread.name.lower()] = (server, thread)
return self
def stop(self, signal = None, frame = None):
self.is_running = False
while self.servers:
(_, (server, thread)) = self.servers.popitem()
log.info('Stopping ' + thread.name)
server.stop()
thread.join()
log.info('All servers stopped...')
def command(self, text):
lex = shlex.shlex(text.strip())
lex.quotes = '"'
lex.whitespace_split = True
lex.commenters = ''
arguments = list(lex)
if len(arguments) > 0:
# Check if we are targeting this or some other module
target = self
if arguments[0].lower() in self.servers:
target = self.servers[arguments.pop(0).lower()][0]
# Check if we have this command
command = arguments.pop(0)
if not hasattr(target, command):
log.warning('Command `{0:s}` is not supported by module {1:s}'.format(command, target.__class__.__name__))
return
# Run command
try:
# Dangerous but mkai, PoC!
arguments = [eval(argument) for argument in arguments]
getattr(target, command)(*arguments)
except Exception as e:
log.error(e)
def run(self):
self.is_running = True
# Run all servers
for _, (_, thread) in self.servers.items():
thread.start()
# Run command loop
log.info('All servers started')
while self.is_running:
self.command(input(''))
log.info('Done')
def exit(self):
self.stop()
micServer = MicServer()
signal.signal(signal.SIGINT, micServer.stop)
try:
discoveryServer = DiscoveryServer('Karaoke Party!')
catalogueServer = CatalogueServer()
appServer = AppServer()
# Do some linking
catalogueServer.onPlaylistChanged(appServer.refreshPlaylist)
# Add all different server parts
micServer.addServer(discoveryServer).addServer(catalogueServer).addServer(appServer)
# Run the whole thing
micServer.run()
except Exception as e:
micServer.stop(None, None)
if not isinstance(e, EOFError):
log.exception("")