Ejemplo n.º 1
0
    def run(self):
        address = '127.0.0.1:%s'%self.port
        operator = DHTOperator(address, self.home_dir, is_init_node=self.init_node, node_name=self.port)
        self.operator = operator

        operator.register_operation('ManageNeighbour', ManageNeighbour)
        operator.register_operation('DiscoveryOperation', DiscoveryOperation)
        operator.register_operation('TopologyCognition', TopologyCognition)

        operator.register_operation('GetRangeDataRequest', GetRangeDataRequestOperation)
        operator.register_operation('GetRangesTable', GetRangesTableOperation)
        operator.register_operation('PutDataBlock', PutDataBlockOperation)
        operator.register_operation('SplitRangeCancel', SplitRangeCancelOperation)
        operator.register_operation('SplitRangeRequest', SplitRangeRequestOperation)
        operator.register_operation('UpdateHashRangeTable', UpdateHashRangeTableOperation)
        operator.register_operation('CheckHashRangeTable', CheckHashRangeTableOperation)
        server = FriServer('0.0.0.0', self.port, operator, server_name='node_%s'%self.port)
        ret = server.start()
        if not ret:
            print 'ERROR: server does not started!'
            return

        self.stopped = False

        while not self.stopped:
            time.sleep(0.1)

        server.stop()
Ejemplo n.º 2
0
    def start(self, neighbour):
        address = '%s:%s' % (self.hostname, self.port)
        if not neighbour:
            is_init_node = True
        else:
            is_init_node = False

        operator_class = OPERATORS_MAP.get(self.node_type, None)
        if operator_class is None:
            logger.error('Node type "%s" does not found!'%self.node_type)
            return False

        op_proc = OperatorProcess(operator_class, address, self.home_dir, self.keystore, \
                                    is_init_node, self.node_name, config=self.config)
        op_proc.start_carefully()

        try:
            oper_manager = OperationsManager(operator_class.OPERATIONS_LIST, self.node_name, self.keystore)
            workers_mgr = WorkersManager(OperationsProcessor, server_name=self.node_name, \
                                            init_params=(oper_manager, self.keystore))
            fri_server = FriServer(self.bind_host, self.port, workers_mgr, self.node_name)
            started = fri_server.start()
            if not started:
                raise Exception('FriServer does not started!')
        except Exception, err:
            time.sleep(1)
            op_proc.stop()
            op_proc.join()
            raise err
Ejemplo n.º 3
0
    def start(self):
        workers_mgr = WorkersManager(MgmtCommandsProcessor, server_name=self.node_name,
                                            init_params=(self.keystore,))
        fri_server = FriServer(self.hostname, self.port, workers_mgr, self.node_name)
        started = fri_server.start()
        if not started:
            raise Exception('FriServer does not started!')

        self.server = fri_server
Ejemplo n.º 4
0
    def test02_ssl_bad_cert(self):
        keystorage = FileBasedKeyStorage(VALID_STORAGE, PASSWD)
        inv_keystorage = FileBasedKeyStorage(INVALID_STORAGE, PASSWD)
        server1 = server2 = None
        try:
            operator = Operator('127.0.0.1:1986', key_storage=keystorage)
            operator.neighbours = ['127.0.0.1:1987']
            operator.register_operation('ECHO', EchoOperation)
            server1 = FriServer('0.0.0.0', 1986, operator, 10, 'node_1', keystorage)
            ret = server1.start()
            self.assertEqual(ret, True)

            operator = Operator('127.0.0.1:1987', key_storage=inv_keystorage)
            operator.neighbours = ['127.0.0.1:1986']
            operator.register_operation('ECHO', EchoOperation)
            server2 = FriServer('0.0.0.0', 1987, operator, 10, 'node_2', inv_keystorage)
            ret = server2.start()
            self.assertEqual(ret, True)

            packet = { 'message_id': 323232,
                        'method': 'ECHO',
                        'sync': False,
                        'sender': '127.0.0.1:1987',
                        'parameters': {'message': 'test message'}}
            packet_obj = FabnetPacketRequest(**packet)
            rcode, rmsg = operator.call_node('127.0.0.1:1986', packet_obj)
            self.assertEqual(rcode, 1, rmsg)

            time.sleep(.1)
        finally:
            if server1:
                server1.stop()
            if server2:
                server2.stop()
    def __start_server(self, processor_class, routine, ks=None, add_args=()):
        server_name = 'test-node'
        cur_thread = threading.current_thread()
        cur_thread.setName('%s-main'%server_name)

        workers_mgr = WorkersManager(processor_class, min_count=1, max_count=8, \
                                server_name=server_name, init_params=(ks,))
        fri_server = FriServer('127.0.0.1', 6666, workers_mgr, server_name)

        fri_server.start()

        try:
            routine()
        finally:
            fri_server.stop()
Ejemplo n.º 6
0
    def start(self, neighbour):
        address = '%s:%s' % (self.hostname, self.port)
        if not neighbour:
            is_init_node = True
        else:
            is_init_node = False

        operator = OPERATOR(address, self.home_dir, self.keystore, is_init_node, self.node_name)

        for (op_name, op_class) in OPERATIONS_MAP.items():
            operator.register_operation(op_name, op_class)

        self.server = FriServer(self.hostname, self.port, operator, \
                                    server_name=self.node_name, \
                                    keystorage=self.keystore)

        started = self.server.start()
        if not started:
            return started

        if is_init_node:
            return True

        packet = FabnetPacketRequest(method='DiscoveryOperation', sender=address)

        rcode, rmsg = self.server.operator.call_node(neighbour, packet)
        if rcode:
            logger.warning('Neighbour %s does not respond!'%neighbour)

        return True
Ejemplo n.º 7
0
class Node:
    def __init__(self, hostname, port, home_dir, node_name='anonymous_node', ks_path=None, ks_passwd=None):
        self.hostname = hostname
        self.port = port
        self.home_dir = home_dir
        self.node_name = node_name
        if ks_path:
            self.keystore = init_keystore(ks_path, ks_passwd)
        else:
            self.keystore = None

        self.server = None

    def start(self, neighbour):
        address = '%s:%s' % (self.hostname, self.port)
        if not neighbour:
            is_init_node = True
        else:
            is_init_node = False

        operator = OPERATOR(address, self.home_dir, self.keystore, is_init_node, self.node_name)

        for (op_name, op_class) in OPERATIONS_MAP.items():
            operator.register_operation(op_name, op_class)

        self.server = FriServer(self.hostname, self.port, operator, \
                                    server_name=self.node_name, \
                                    keystorage=self.keystore)

        started = self.server.start()
        if not started:
            return started

        if is_init_node:
            return True

        packet = FabnetPacketRequest(method='DiscoveryOperation', sender=address)

        rcode, rmsg = self.server.operator.call_node(neighbour, packet)
        if rcode:
            logger.warning('Neighbour %s does not respond!'%neighbour)

        return True

    def stop(self):
        return self.server.stop()
Ejemplo n.º 8
0
    def run(self):
        address = '127.0.0.1:%s'%self.port
        operator = Operator(address)
        self.operator = operator

        operator.register_operation('ManageNeighbour', ManageNeighbour)
        operator.register_operation('DiscoveryOperation', DiscoveryOperation)
        operator.register_operation('TopologyCognition', TopologyCognition)
        server = FriServer('0.0.0.0', self.port, operator, server_name='node_%s'%self.port)
        ret = server.start()
        if not ret:
            return

        self.stopped = False

        while not self.stopped:
            time.sleep(0.1)

        server.stop()
Ejemplo n.º 9
0
    def test_discovery_operation(self):
        server1 = server2 = None
        try:
            operator = Operator("127.0.0.1:1986")
            operator.register_operation("ManageNeighbour", ManageNeighbour)
            operator.register_operation("DiscoveryOperation", DiscoveryOperation)
            operator.register_operation("TopologyCognition", TopologyCognition)
            server1 = FriServer("0.0.0.0", 1986, operator, server_name="node_1")
            ret = server1.start()
            self.assertEqual(ret, True)

            operator1 = Operator("127.0.0.1:1987")
            operator1.register_operation("ManageNeighbour", ManageNeighbour)
            operator1.register_operation("DiscoveryOperation", DiscoveryOperation)
            operator1.register_operation("TopologyCognition", TopologyCognition)
            server2 = FriServer("0.0.0.0", 1987, operator1, server_name="node_2")
            ret = server2.start()
            self.assertEqual(ret, True)

            packet = {
                "message_id": 323232,
                "method": "DiscoveryOperation",
                "sender": "127.0.0.1:1987",
                "parameters": {},
            }
            packet_obj = FabnetPacketRequest(**packet)
            rcode, rmsg = operator1.call_node("127.0.0.1:1986", packet_obj)
            self.assertEqual(rcode, 0, rmsg)

            operator1.wait_response(323232, 1)
            server1.stop()
            time.sleep(3)
            server1 = None

            self.assertEqual(operator.upper_neighbours, ["127.0.0.1:1987"])
            self.assertEqual(operator.superior_neighbours, ["127.0.0.1:1987"])
            self.assertEqual(operator1.upper_neighbours, [])
            self.assertEqual(operator1.superior_neighbours, [])
        finally:
            if server1:
                server1.stop()
            if server2:
                server2.stop()
Ejemplo n.º 10
0
    def __start_node(self, node_name, ks=None):
        server_name = node_name
        cur_thread = threading.current_thread()
        cur_thread.setName('%s-main'%server_name)

        proc = OperatorProcess(Operator, '127.0.0.1:6666', '/tmp', ks, True, server_name)

        proc.start_carefully()

        try:
            oper_manager = OperationsManager([EchoOperation, EchoWithCallOperation, UnauthOper], server_name, ks)
            workers_mgr = WorkersManager(OperationsProcessor, min_count=2, max_count=8, \
                                    server_name=server_name, init_params=(oper_manager, ks))
            fri_server = FriServer('127.0.0.1', 6666, workers_mgr, server_name)
            fri_server.start()
        except Exception, err:
            time.sleep(1)
            proc.stop()
            proc.join()
            raise err
Ejemplo n.º 11
0
    def __test_server(self, keystorage=None):
        server1 = server2 = None
        try:
            operator = Operator('127.0.0.1:1986', key_storage=keystorage)
            operator.neighbours = ['127.0.0.1:1987']
            operator.register_operation('ECHO', EchoOperation)
            server1 = FriServer('0.0.0.0', 1986, operator, 10, 'node_1', keystorage)
            ret = server1.start()
            self.assertEqual(ret, True)

            operator = Operator('127.0.0.1:1987', key_storage=keystorage)
            operator.neighbours = ['127.0.0.1:1986']
            operator.register_operation('ECHO', EchoOperation)
            server2 = FriServer('0.0.0.0', 1987, operator, 10, 'node_2', keystorage)
            ret = server2.start()
            self.assertEqual(ret, True)

            packet = { 'message_id': 323232,
                        'method': 'ECHO',
                        'sync': False,
                        'sender': '127.0.0.1:1987',
                        'parameters': {'message': 'test message'}}

            if keystorage:
                packet['session_id'] = keystorage.get_node_cert_key()

            packet_obj = FabnetPacketRequest(**packet)
            rcode, rmsg = operator.call_node('127.0.0.1:1986', packet_obj)
            self.assertEqual(rcode, 0, rmsg)

            operator.wait_response(323232, 1)

            self.assertEqual(os.path.exists('/tmp/server1.out'), True)
            self.assertEqual(os.path.exists('/tmp/server2.out'), True)

            request = json.loads(open('/tmp/server1.out').read())
            response = json.loads(open('/tmp/server2.out').read())
            self.assertEqual(request, packet)
            good_resp = {'from_node': '127.0.0.1:1986','message_id': 323232,
                'ret_code': 0,
                'ret_message': 'ok',
                'ret_parameters': {'message': 'test message'}}
            self.assertEqual(response, good_resp)
        finally:
            if server1:
                server1.stop()
            if server2:
                server2.stop()
Ejemplo n.º 12
0
    def test_node(self):
        try:
            server = None
            address = '127.0.0.1:1987'
            operator = Operator(address)

            operator.register_operation('NodeStatistic', NodeStatisticOperation)
            server = FriServer('0.0.0.0', 1987, operator, server_name='node_test')
            ret = server.start()
            self.assertEqual(ret, True)
            time.sleep(.5)

            packet = { 'message_id': 323232,
                        'method': 'NodeStatistic',
                        'sender': '',
                        'parameters': {},
                        'sync': True}
            packet_obj = FabnetPacketRequest(**packet)

            client = FriClient()
            ret_packet = client.call_sync('127.0.0.1:1987', packet_obj)

            self.assertEqual(isinstance(ret_packet, FabnetPacketResponse), True)
            self.assertEqual(ret_packet.ret_code, 0, ret_packet.ret_message)
            self.assertTrue(int(ret_packet.ret_parameters['workers_count']) > 2)
            self.assertEqual(int(ret_packet.ret_parameters['uppers_balance']), -1)
            self.assertEqual(int(ret_packet.ret_parameters['superiors_balance']), -1)
            self.assertTrue(int(ret_packet.ret_parameters['threads']) > 6)
            self.assertTrue(int(ret_packet.ret_parameters['memory']) > 1000)
            self.assertEqual(len(ret_packet.ret_parameters['methods_stat']), 1)
            self.assertEqual(ret_packet.ret_parameters['methods_stat']['NodeStatistic'], 1)
            print ret_packet.ret_parameters

            time.sleep(.2)
        except Exception, err:
            print 'ERROR: %s'%err
            raise err
Ejemplo n.º 13
0
    def test_big_data_test(self):
        keystorage = FileBasedKeyStorage(VALID_STORAGE, PASSWD)

        try:
            operator = Operator("127.0.0.1:1986", key_storage=keystorage)
            operator.neighbours = ["127.0.0.1:1987"]
            operator.register_operation("ECHO", EchoOperation)
            server1 = FriServer("0.0.0.0", 1986, operator, 10, "node_1", keystorage)
            ret = server1.start()
            self.assertEqual(ret, True)

            operator = Operator("127.0.0.1:1987", key_storage=keystorage)
            operator.neighbours = ["127.0.0.1:1986"]
            operator.register_operation("ECHO", EchoOperation)
            server2 = FriServer("0.0.0.0", 1987, operator, 10, "node_2", keystorage)
            ret = server2.start()
            self.assertEqual(ret, True)

            print "Init big data..."
            data = "0123456789" * 900000
            print "Done"
            data += ""
            packet = {
                "message_id": 323232,
                "method": "ECHO",
                "sync": False,
                "sender": "127.0.0.1:1987",
                "binary_data": data,
            }
            t0 = datetime.now()
            packet_obj = FabnetPacketRequest(**packet)
            rcode, rmsg = operator.call_node("127.0.0.1:1986", packet_obj)
            self.assertEqual(rcode, 0, rmsg)

            operator.wait_response(323232, 20)
            print "Echo big data time: %s" % (datetime.now() - t0)
            time.sleep(1)
            rcv_data = open("/tmp/big_message.out").read()
            self.assertEqual(len(rcv_data), len(data))
            os.remove("/tmp/big_message.out")
        finally:
            if server1:
                server1.stop()
            if server2:
                server2.stop()
Ejemplo n.º 14
0
    def test_discovery_operation(self):
        server1 = server2 = None
        try:
            operator = Operator('127.0.0.1:1986')
            operator.register_operation('ManageNeighbour', ManageNeighbour)
            operator.register_operation('DiscoveryOperation', DiscoveryOperation)
            operator.register_operation('TopologyCognition', TopologyCognition)
            server1 = FriServer('0.0.0.0', 1986, operator, server_name='node_1')
            ret = server1.start()
            self.assertEqual(ret, True)

            operator1 = Operator('127.0.0.1:1987')
            operator1.register_operation('ManageNeighbour', ManageNeighbour)
            operator1.register_operation('DiscoveryOperation', DiscoveryOperation)
            operator1.register_operation('TopologyCognition', TopologyCognition)
            server2 = FriServer('0.0.0.0', 1987, operator1, server_name='node_2')
            ret = server2.start()
            self.assertEqual(ret, True)

            packet = { 'message_id': 323232,
                        'method': 'DiscoveryOperation',
                        'sender': '127.0.0.1:1987',
                        'parameters': {}}
            packet_obj = FabnetPacketRequest(**packet)
            rcode, rmsg = operator1.call_node('127.0.0.1:1986', packet_obj)
            self.assertEqual(rcode, 0, rmsg)

            time.sleep(1)

            self.assertEqual(operator.upper_neighbours, ['127.0.0.1:1987'])
            self.assertEqual(operator.superior_neighbours, ['127.0.0.1:1987'])
            self.assertEqual(operator1.upper_neighbours, ['127.0.0.1:1986'])
            self.assertEqual(operator1.superior_neighbours, ['127.0.0.1:1986'])
        finally:
            if server1:
                server1.stop()
            if server2:
                server2.stop()