예제 #1
0
        def convert_peer_info_item_to_peer_item(item):
            """(peer_id, PeerInfo) -> (peer_id, PeerObject)

            :param item: (peer_id, PeerInfo)
            :return: peer_id, PeerObject)
            """
            return item[0], PeerObject(self.__channel_name, item[1])
예제 #2
0
 def get_leader_object(self):
     mock_info = PeerInfo(peer_id="peer_id",
                          group_id='a',
                          target="192.0.0.1:1234",
                          status=PeerStatus.unknown,
                          cert=self.__peer_auth.peer_cert,
                          order=0)
     mock_peer_object = PeerObject(list(conf.CHANNEL_OPTION)[0], mock_info)
     return mock_peer_object
예제 #3
0
    def add_peer(self, peer_info):
        """add_peer to peer_manager

        :param peer_info: PeerInfo
        :return: create_peer_order
        """
        logging.debug(f"add peer id: {peer_info.peer_id}")

        # If exist same peer_target in peer_list, delete exist one.
        # this is temporary rule that will be argued.
        # if peer_info.peer_id not in self.peer_list[conf.ALL_GROUP_ID].keys():
        #     exist_peer = self.__get_peer_by_target(peer_info.target)
        #     if exist_peer is not None:
        #         self.remove_peer(exist_peer.peer_id, exist_peer.group_id)

        self.__init_peer_group(peer_info.group_id)

        util.logger.spam(f"peer_manager::add_peer try make PeerObject")
        peer = PeerObject(peer_info)

        # add_peer logic must be atomic
        self.__add_peer_lock.acquire()

        if peer_info.order <= 0:
            if peer_info.peer_id in self.peer_list[peer_info.group_id]:
                peer_info.order = self.peer_list[peer_info.group_id][
                    peer_info.peer_id].order
            else:
                peer_info.order = self.__make_peer_order(peer_info)

        logging.debug(
            f"new peer order {peer_info.peer_id} : {peer_info.order}")

        # set to leader peer
        if (self.peer_leader[peer_info.group_id] == 0) or (len(
                self.peer_list[peer_info.group_id]) == 0):
            logging.debug("Set Group Leader Peer: " + str(peer_info.order))
            self.peer_leader[peer_info.group_id] = peer_info.order

        if (self.peer_leader[conf.ALL_GROUP_ID] == 0) or (len(
                self.peer_list[conf.ALL_GROUP_ID]) == 0):
            logging.debug("Set ALL Leader Peer: " + str(peer_info.order))
            self.peer_leader[conf.ALL_GROUP_ID] = peer_info.order

        self.peer_list[peer_info.group_id][peer_info.peer_id] = peer_info
        self.peer_list[conf.ALL_GROUP_ID][peer_info.peer_id] = peer_info
        self.peer_order_list[peer_info.group_id][
            peer_info.order] = peer_info.peer_id
        self.peer_order_list[conf.ALL_GROUP_ID][
            peer_info.order] = peer_info.peer_id
        self.__peer_object_list[peer_info.group_id][peer_info.peer_id] = peer
        self.__peer_object_list[conf.ALL_GROUP_ID][peer_info.peer_id] = peer

        self.__add_peer_lock.release()

        return peer_info.order
예제 #4
0
    def setUp(self):
        test_util.print_testname(self._testMethodName)
        self.__peer_auth = test_util.create_peer_auth()

        peer_service_mock = Mock()
        peer_service_mock.peer_manager = Mock()
        mock_info = PeerInfo(peer_id=self.__peer_id,
                             group_id='a',
                             target="192.0.0.1:1234",
                             status=PeerStatus.unknown,
                             cert=self.__peer_auth.get_public_der(),
                             order=0)
        mock_peer_object = PeerObject(mock_info)

        def get_leader_object():
            return mock_peer_object

        peer_service_mock.peer_manager.get_leader_object = get_leader_object
        ObjectManager().peer_service = peer_service_mock