Пример #1
0
    def add_new_task(self, task):
        if task.header.task_id in self.tasks:
            raise RuntimeError("Task has been already added")
        if not self.key_id:
            raise ValueError("'key_id' is not set")
        if not SocketAddress.is_proper_address(self.listen_address,
                                               self.listen_port):
            raise IOError("Incorrect socket address")

        prev_pub_addr, prev_pub_port, prev_nat_type = self.node.pub_addr, self.node.pub_port, self.node.nat_type
        self.node.pub_addr, self.node.pub_port, self.node.nat_type = yield self.get_external_address(
        )

        if prev_pub_addr != self.node.pub_addr or \
           prev_pub_port != self.node.pub_port or \
           prev_nat_type != self.node.nat_type:
            self.update_task_signatures()

        task.header.task_owner_address = self.listen_address
        task.header.task_owner_port = self.listen_port
        task.header.task_owner_key_id = self.key_id
        task.header.task_owner = self.node
        task.header.signature = self.sign_task_header(task.header)

        self.dir_manager.clear_temporary(task.header.task_id,
                                         undeletable=task.undeletable)
        self.dir_manager.get_task_temporary_dir(task.header.task_id,
                                                create=True)

        task.register_listener(self)
        task.task_status = TaskStatus.waiting

        self.tasks[task.header.task_id] = task

        ts = TaskState()
        ts.status = TaskStatus.waiting
        ts.outputs = task.get_output_names()
        ts.total_subtasks = task.get_total_tasks()
        ts.time_started = time.time()

        self.tasks_states[task.header.task_id] = ts

        if self.task_persistence:
            self.dump_task(task.header.task_id)
            logger.info("Task {} added".format(task.header.task_id))
            self.notice_task_updated(task.header.task_id)
Пример #2
0
 def test_is_proper_address(self):
     assert SocketAddress.is_proper_address("127.0.0.1", 1020)
     assert not SocketAddress.is_proper_address("127.0.0.1", 0)
     assert not SocketAddress.is_proper_address("127.0.0.1", "ABC")
     assert not SocketAddress.is_proper_address("AB?*@()F*)A", 1020)
Пример #3
0
 def test_is_proper_address(self):
     import socket
     from struct import pack
     from random import randint
     from ipaddress import IPv6Address
     for i in range(5000):
         assert SocketAddress.is_proper_address(
             socket.inet_ntoa(pack('>I', randint(0, 16777215))),
             randint(1, 10000))
     for i in range(5000):
         assert SocketAddress.is_proper_address(
             str(IPv6Address(randint(0, 2**128 - 1))), randint(1, 10000))
     assert not SocketAddress.is_proper_address('1.2.3.4', '')
     assert not SocketAddress.is_proper_address('1.2.3.4', None)
     assert not SocketAddress.is_proper_address('1.2.3.4', '0xdead')
     assert not SocketAddress.is_proper_address('1.2.3.4', '-1')
     assert not SocketAddress.is_proper_address('1.2.3.4', '0')
     assert not SocketAddress.is_proper_address('1.2.3.4', '65536')
     assert not SocketAddress.is_proper_address('1.2.3.4',
                                                '65536655366536655366553')
     assert not SocketAddress.is_proper_address('1.2.3:40102', '')
     assert not SocketAddress.is_proper_address('1.2.3.4.', '40102')
     assert not SocketAddress.is_proper_address('.1.2.3.4', '40102')
     assert not SocketAddress.is_proper_address('1..2.3.4', '40102')
     assert not SocketAddress.is_proper_address('1..3.4:40102', '2')
     assert not SocketAddress.is_proper_address('1.2.3.256', '40102')
     assert not SocketAddress.is_proper_address('[0:1:2:3:4:5:6]', '1')
     assert not SocketAddress.is_proper_address('[0:1:2:3:4:5:6:7:8]', '1')
     assert not SocketAddress.is_proper_address('[0:1:2:33333:4:5:6:7]',
                                                '1')
     assert not SocketAddress.is_proper_address('[0:1:2:-3:4:5:6:7]', '1')