controller and player are distinct objects

FIX #18
This commit is contained in:
boyska 2016-09-08 20:45:43 +02:00
parent 7cc772163f
commit 2bbbf534eb
No known key found for this signature in database
GPG key ID: 7395DCAE58289CA9
2 changed files with 24 additions and 17 deletions

View file

@ -19,13 +19,13 @@ logging.basicConfig(level=logging.INFO,
import gevent
from gevent.wsgi import WSGIServer
from .mpc import Player, get_mpd_client
from .mpc import Controller, get_mpd_client
from .event import Monitor
from .config import get_conf
from .rpc import create_app
def on_player_crash(*args, **kwargs):
def on_main_crash(*args, **kwargs):
print('A crash occurred in "main" greenlet. Aborting...')
sys.exit(1)
@ -34,17 +34,17 @@ class Larigira(object):
def __init__(self):
self.log = logging.getLogger('larigira')
self.conf = get_conf()
self.player = Player(self.conf)
self.controller = Controller(self.conf)
if 'DB_URI' in self.conf:
self.monitor = Monitor(self.player.q, self.conf)
self.monitor = Monitor(self.controller.q, self.conf)
else:
self.monitor = None
self.player.link_exception(on_player_crash)
self.controller.link_exception(on_main_crash)
self.http_server = WSGIServer(('', 5000),
create_app(self.player.q, self))
create_app(self.controller.q, self))
def start(self):
self.player.start()
self.controller.start()
if self.monitor is not None:
self.monitor.start()
self.http_server.start()
@ -71,7 +71,7 @@ def main():
def sig(*args):
print('invoked sig', args)
larigira.player.q.put('signal', *args)
larigira.controller.q.put('signal', *args)
gevent.signal(signal.SIGHUP, sig, signal.SIGHUP)
gevent.wait()

View file

@ -42,13 +42,11 @@ class MpcWatcher(ParentedLet):
yield ('mpc', status)
class Player(gevent.Greenlet):
class Player:
def __init__(self, conf):
gevent.Greenlet.__init__(self)
self.min_playlist_length = 10
self.log = logging.getLogger(self.__class__.__name__)
self.q = Queue()
self.conf = conf
self.log = logging.getLogger(self.__class__.__name__)
self.min_playlist_length = 10
def _get_mpd(self):
mpd_client = MPDClient(use_unicode=True)
@ -90,6 +88,15 @@ class Player(gevent.Greenlet):
except CommandError:
self.log.exception("Cannot insert song {}".format(uri))
class Controller(gevent.Greenlet):
def __init__(self, conf):
gevent.Greenlet.__init__(self)
self.log = logging.getLogger(self.__class__.__name__)
self.conf = conf
self.q = Queue()
self.player = Player(self.conf)
def _run(self):
mw = MpcWatcher(self.q, self.conf, client=None)
mw.parent_greenlet = self
@ -98,7 +105,7 @@ class Player(gevent.Greenlet):
t.parent_greenlet = self
t.start()
# at the very start, run a check!
gevent.Greenlet.spawn(self.check_playlist)
gevent.Greenlet.spawn(self.player.check_playlist)
while True:
value = self.q.get()
self.log.debug('<- %s' % str(value))
@ -106,15 +113,15 @@ class Player(gevent.Greenlet):
kind = value['kind']
args = value['args']
if kind == 'timer' or (kind == 'mpc' and args[0] in ('player', 'playlist')):
gevent.Greenlet.spawn(self.check_playlist)
gevent.Greenlet.spawn(self.player.check_playlist)
elif kind == 'mpc':
pass
elif kind == 'add':
try:
self.enqueue(args[0])
self.player.enqueue(args[0])
except AssertionError:
raise
except Exception as exc:
except Exception:
self.log.exception("Error while adding to queue; bad audiogen output?")
else:
self.log.warning("Unknown message: %s" % str(value))