Example #1
0
    def apply_state_change(self, data, callback=None):
        # TODO: This code is intertwined untestable rubbish. Need to pull out
        #   steps into seperate methods to allow testing...
        '''Will take the provided data and propegate out the necessary changes
        to the sessions Remote Player.

        On return, the remote player, if present, has been asked to sync up.
        The callback is called once the remote player has confirmed it is in sync.
        Callback is always called after this function has returned.'''

        logger.debug('Got state change request on session {}, new state {}'.format(self.id, data))

        # if we're changing player
        new_url = data.get('player_url')
        if isinstance(new_url, basestring) and (self.player == None or new_url != self.model.player_url):

            # aquire the new one, do first in case of failure
            if new_url == '':
                new_player = None
            else:
                new_player = self.player_set.acquire_player(self.name, new_url)

            # push out a request to clear the old one, retry apply with new player :)
            if self.player:
                self.clear_player()
                self.player_set.release_player(self.name, self.model.player_url)

            # save this stuff
            self.player = new_player
            self.model.player_url = new_url
            self.model.save()

            # it's a new player, we want to resend everything, not just a delta
            full_data = extract_attrs(self.model, SessionProxy.DEFAULTS.iterkeys())
            full_data.update(data)
            data = full_data

        if self.player is None:
            # persist changes
            update_attributes(self.model, data, SessionProxy.DEFAULTS.keys(), False)
            self.model.save()

            if callback is not None:
                # done this way to make sure the function is called after we've returned
                IOLoop.instance().add_callback(partial(callback, 204, None))
            return

        def player_callback(status, body):
            if 200 <= status < 300:
                # make sure to save the new state
                update_attributes(self.model, data, SessionProxy.DEFAULTS.keys(), False)
                self.model.save()
            else:
                logger.error('Non 2xx response, {}, from Remote Player: {}'.format(status, body))

            if callback:
                callback(status, body)

        # send of the request to the player
        self.player.send_request('PATCH', '/playback', data, callback=player_callback)
    def test_create_response(self):
        kwargs = {'status': 200, 'id': 42, 'body': {1: 2}}
        msg = kpm.Response(**kwargs)
        dicteq(kwargs, u.extract_attrs(msg, kwargs.keys()))
        kwargs['type'] = 'RESPONSE'
        dicteq(kwargs, msg.to_json_dict())

        msg2 = kpm.Response.from_json_dict(kwargs)
        dicteq(msg2.to_json_dict(), kwargs)
    def test_create_request(self):
        kwargs = {'method': 'POST', 'id': 42, 'path': '/dood', 'body': {1: 2}}
        msg = kpm.Request(**kwargs)
        dicteq(kwargs, u.extract_attrs(msg, kwargs.keys()))
        kwargs['type'] = 'REQUEST'
        dicteq(kwargs, msg.to_json_dict())

        msg2 = kpm.Request.from_json_dict(kwargs)
        dicteq(msg2.to_json_dict(), kwargs)
Example #4
0
 def to_json(self):
     data = {
         'url': self.url,
         'name': self.name,
         'duration': self.duration,
     }
     data.update(extract_attrs(self.model, ['id', 'player_url', 'playlist',
         'paused', 'position', 'volume']))
     return data
    def test_create_event(self):
        kwargs = {
            'name': 'A momentous eventous!',
            'path': '/dood',
            'body': {1: 2}
        }
        msg = kpm.Event(**kwargs)
        dicteq(kwargs, u.extract_attrs(msg, kwargs.keys()))
        kwargs['type'] = 'EVENT'
        dicteq(kwargs, msg.to_json_dict())

        msg2 = kpm.Event.from_json_dict(kwargs)
        dicteq(msg2.to_json_dict(), kwargs)
    def test_create_response(self):
        kwargs = {
            'status': 200,
            'id': 42,
            'body': {1: 2}
        }
        msg = kpm.Response(**kwargs)
        dicteq(kwargs, u.extract_attrs(msg, kwargs.keys()))
        kwargs['type'] = 'RESPONSE'
        dicteq(kwargs, msg.to_json_dict())

        msg2 = kpm.Response.from_json_dict(kwargs)
        dicteq(msg2.to_json_dict(), kwargs)
    def test_create_request(self):
        kwargs = {
            'method': 'POST',
            'id': 42,
            'path': '/dood',
            'body': {1: 2}
        }
        msg = kpm.Request(**kwargs)
        dicteq(kwargs, u.extract_attrs(msg, kwargs.keys()))
        kwargs['type'] = 'REQUEST'
        dicteq(kwargs, msg.to_json_dict())

        msg2 = kpm.Request.from_json_dict(kwargs)
        dicteq(msg2.to_json_dict(), kwargs)
    def test_create_event(self):
        kwargs = {
            'name': 'A momentous eventous!',
            'path': '/dood',
            'body': {
                1: 2
            }
        }
        msg = kpm.Event(**kwargs)
        dicteq(kwargs, u.extract_attrs(msg, kwargs.keys()))
        kwargs['type'] = 'EVENT'
        dicteq(kwargs, msg.to_json_dict())

        msg2 = kpm.Event.from_json_dict(kwargs)
        dicteq(msg2.to_json_dict(), kwargs)
Example #9
0
    def get_current_state(self):
        state = extract_attrs(self.model, SessionProxy.DEFAULTS.iterkeys())
        state['duration'] = self.duration

        return state