예제 #1
0
파일: chord.py 프로젝트: XiaoFJU/p2p_chord
    def creat_finger_table(self):
        print("creat_finger_table...")
        bar = ProgressBar(total=self.total)

        # finger list is like [1, 2, 4, 8...]
        finger = list()
        i = 1
        while i < self.SIZE/2 + 1:
            finger.append(i)
            i *= 2

        for _, now in self.chord.items():
            for i in finger:
                target = (i + now.address) % self.SIZE

                finded_flag = False
                first_address = None
                for _, node in self.chord.items():
                    if first_address is None:
                        first_address = node.address
                    if node.address >= target and node.address is not now.address:
                        now.finger_table[i] = node.address
                        finded_flag = True
                        break
                if not finded_flag:
                    now.finger_table[i] = first_address
            bar.next()
예제 #2
0
파일: chord.py 프로젝트: XiaoFJU/p2p_chord
    def __init__(self, int_nodeList, ID_SIZE):
        print("init chord...")
        bar = ProgressBar(total=len(int_nodeList))

        self.node_list = sorted(int_nodeList)

        # create Node object
        self.chord = OrderedDict()
        for num in self.node_list:
            self.chord[num] = Node(num, self)
            bar.next()

        print("done.")

        self.total = len(self.chord)

        # size of chord
        self.SIZE = ID_SIZE

        self.set_neighbor()
        self.creat_finger_table()
예제 #3
0
파일: chord.py 프로젝트: XiaoFJU/p2p_chord
    def set_neighbor(self):
        print("set neighbor...")
        bar = ProgressBar(total=self.total)

        first = None
        previous = None
        for _, now in self.chord.items():
            # save first node address
            if first == None:
                first = now
            elif previous != None:
                # set the previous of now Node
                now.predecessor = previous
                # set the next of previous Node
                previous.successor = now
            previous = now
            bar.next()

        # next of last node is first node
        previous.successor = first
        # pre of first node is last node
        first.predecessor = previous
        print("done.")
예제 #4
0
def main():

    # if file not exist, create it.
    nodes = myRandom.random_file(RANDOM_FILE, ID_SIZE, NODE_NUMBER)

    # assume we already have all nodes at first time
    chord = _chord.Chord(nodes, ID_SIZE)

    #################################
    # Q1: 每一個節點所需負責之key值個數  #
    #################################
    print("\n===========Q1=============")

    count = dict()

    # no matter what node start, get same answer
    node_index = nodes[randint(0, NODE_NUMBER - 1)]
    node = chord.chord[node_index]
    print("start node:", node)
    for i in range(1, 11):
        file_count = i * 10 * NODE_NUMBER
        print("numK:", file_count)
        bar = ProgressBar(total=file_count)

        # clear count
        for j in nodes:
            count[j] = 0

        for _ in range(file_count):
            file_key = randint(0, ID_SIZE - 1)
            addr, record = node.search(file_key, list())

            logs.debug(node, "find", file_key, ">> ")
            logs.debug("\tnode:", addr, " record:", record)

            # use random list `nodes` mapping
            count[addr] += 1

            bar.next()

        # cacalculate ans
        ans = sorted(count.items(), key=operator.itemgetter(1))

        max = ans[0][1]
        mid = ans[NODE_NUMBER // 2][1]
        min = ans[NODE_NUMBER - 1][1]

        print(max, mid, min)

    #######################
    # Q2: 搜尋時所需之hop數  #
    #######################
    print("\n===========Q2=============")

    hops = [0] * int(log(ID_SIZE, 2))

    bar = ProgressBar(total=100 * NODE_NUMBER)
    for i in nodes:
        node = chord.chord[i]
        for j in range(100):
            k = randint(0, ID_SIZE - 1)
            addr, record = node.search(k, [])

            hops_num = len(record) - 1
            hops[hops_num] += 1

            bar.next()

    print("total hops:\n", hops)
    print("proportion of total hops:\n",
          [x / (100 * NODE_NUMBER) for x in hops])