Skip to content

Documentation for Stdcandidateselector Module

STDandidateSelector

Bases: CandidateSelector

Source code in nebula/core/situationalawareness/discovery/candidateselection/stdcandidateselector.py
 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
class STDandidateSelector(CandidateSelector):
    def __init__(self):
        self.candidates = []
        self.candidates_lock = Locker(name="candidates_lock")

    async def set_config(self, config):
        pass

    async def add_candidate(self, candidate):
        self.candidates_lock.acquire()
        self.candidates.append(candidate)
        self.candidates_lock.release()

    async def select_candidates(self):
        """
        Select mean number of neighbors
        """
        self.candidates_lock.acquire()
        mean_neighbors = round(sum(n for _, n, _ in self.candidates) / len(self.candidates) if self.candidates else 0)
        logging.info(f"mean number of neighbors: {mean_neighbors}")
        cdts = self.candidates[:mean_neighbors]
        not_selected = set(self.candidates) - set(cdts)
        self.candidates_lock.release()
        return (cdts, not_selected)

    async def remove_candidates(self):
        self.candidates_lock.acquire()
        self.candidates = []
        self.candidates_lock.release()

    async def any_candidate(self):
        self.candidates_lock.acquire()
        any = True if len(self.candidates) > 0 else False
        self.candidates_lock.release()
        return any

select_candidates() async

Select mean number of neighbors

Source code in nebula/core/situationalawareness/discovery/candidateselection/stdcandidateselector.py
20
21
22
23
24
25
26
27
28
29
30
async def select_candidates(self):
    """
    Select mean number of neighbors
    """
    self.candidates_lock.acquire()
    mean_neighbors = round(sum(n for _, n, _ in self.candidates) / len(self.candidates) if self.candidates else 0)
    logging.info(f"mean number of neighbors: {mean_neighbors}")
    cdts = self.candidates[:mean_neighbors]
    not_selected = set(self.candidates) - set(cdts)
    self.candidates_lock.release()
    return (cdts, not_selected)