Esempio n. 1
0
class Radius:

    def __init__(self):
        self._fcc = FeedCtrlConnection()
        self._hostID = self._fcc.get_host_master_id()

    def calculate_radius(self):
        radius = self._fcc.get_radius()
        self.__check_trusted(self._hostID, radius, 'MASTER')

    def __check_trusted(self, master_id, radius, prev_app_name, step=1, ):
        if radius < 1 or step > radius:
            return
        trusted = self._fcc.get_trusted(master_id)
        for trusted_id in trusted:
            application_name = self._fcc.get_application_name(trusted_id)
            master = None
            if application_name == 'MASTER':
                master = trusted_id
                self._fcc.set_feed_ids_radius(master, step)
            elif application_name == prev_app_name or prev_app_name == 'MASTER':
                master = self._fcc.get_master_id_from_feed(trusted_id)
                self._fcc.set_feed_ids_radius(master, step)
            else:
                return
            self.__check_trusted(master, step + 1, radius, application_name)
def test_get_trusted():
    with session_scope():
        ecf = EventFactory()
        new_event = ecf.next_event('MASTER/MASTER', {})
        fcc = FeedCtrlConnection()
        fcc.add_event(new_event)
        trust_id1 = generate_random_feed_id()
        new_event = ecf.next_event('MASTER/Trust', {'feed_id': trust_id1})
        fcc.add_event(new_event)
        trust_id2 = generate_random_feed_id()
        new_event = ecf.next_event('MASTER/Trust', {'feed_id': trust_id2})
        fcc.add_event(new_event)
        trust_id3 = generate_random_feed_id()
        new_event = ecf.next_event('MASTER/Trust', {'feed_id': trust_id3})
        fcc.add_event(new_event)
        trust_id4 = generate_random_feed_id()
        new_event = ecf.next_event('MASTER/Name', {'name': trust_id4})
        fcc.add_event(new_event)
        trust_id4 = generate_random_feed_id()
        new_event = ecf.next_event('MASTER/NewFeed', {
            'feed_id': trust_id4,
            'app_name': 'TestApp'
        })
        fcc.add_event(new_event)
        new_event = ecf.next_event('MASTER/Radius', {'radius': 5})
        fcc.add_event(new_event)
        result = fcc.get_trusted(ecf.get_feed_id())
        assert result[0] == trust_id1
        assert result[1] == trust_id2
        assert result[2] == trust_id3
        assert len(result) == 3
class UiFunctionHandler:

    def __init__(self):
        self._fcc = FeedCtrlConnection()
        # try catch or if None??
        lastEvent = self._fcc.get_my_last_event()
        if lastEvent is not None:
            self._ecf = EventFactory(lastEvent)
            self._eventCreationWrapper = EventCreationWrapper(self._ecf)
        else:
            self._ecf = EventFactory()
            self._eventCreationWrapper = EventCreationWrapper(self._ecf)
            _firstEvent = self._eventCreationWrapper.create_MASTER()
            _secondEvent = self._eventCreationWrapper.create_radius(1)
            _thirdEvent = self._eventCreationWrapper.create_name('Anon')
            self._fcc.add_event(_firstEvent)
            self._fcc.add_event(_secondEvent)
            self._fcc.add_event(_thirdEvent)
        self._masterID = self._fcc.get_host_master_id()

    def get_host_master_id(self):
        # returns the host masterID
        return self._masterID

    def get_master_ids(self):
        # return list of masterIDs from FeedCtrlConnection
        return self._fcc.get_all_master_ids()

    def get_all_master_ids_feed_ids(self, masterID):
        # return a list of feed_ids which belong to the given masterID
        return self._fcc.get_all_master_ids_feed_ids(masterID)

    def get_radius_list(self):
        # return a list of feed_ids which are inside the radius
        return self._fcc.get_feed_ids_in_radius()

    def get_trusted(self):
        # return a list of trusted feed_ids
        return self._fcc.get_trusted(self._masterID)

    def set_trusted(self, feed_id, state):
        # sets a feed to trusted or untrusted (event)
        if state:
            new_event = self._eventCreationWrapper.create_trust(feed_id)
        else:
            new_event = self._eventCreationWrapper.create_block(feed_id)

        self._fcc.add_event(new_event)

    def get_blocked(self):
        # return a list of blocked feed_ids
        return self._fcc.get_blocked(self._masterID)

    def get_radius(self):
        # return the current radius
        return self._fcc.get_radius()

    def set_radius(self, radius):
        # sets the new radius
        # calls calcRadius() to recalculate the new Elements, which are in the radius
        self._fcc.set_feed_ids_radius(self._masterID, radius)

    def get_username(self, masterID):
        # return username from given masterID
        return self._fcc.get_username(masterID)

    def set_username(self, name):

        new_event = self._eventCreationWrapper.create_name(name)
        self._fcc.add_event(new_event)

    def get_application(self, feed_id):
        # return application name from given feed_id
        return self._fcc.get_application_name(feed_id)