Exemplo n.º 1
0
    def run(self):
        while True:
            try:
                client_socket, address = self.server_socket.accept()
                print(f"Connection from {address} has been established!")
                tcp_connection = TcpConnection(client_socket)
                status, peer_id = self.handle_request(tcp_connection)

                if status is True:
                    tcp_connection.send((11).to_bytes(CODE_SIZE,
                                                      byteorder='big'))
                    self.main_controller.tell({
                        'header': 8,
                        'body': (peer_id, tcp_connection)
                    })
                else:
                    tcp_connection.send((12).to_bytes(CODE_SIZE,
                                                      byteorder='big'))
                    tcp_connection.close()
            except Exception as e:
                print(e)
                tcp_connection.close()
Exemplo n.º 2
0
    def run(self):
        data = {}
        data['info_hash'] = self.info_hash
        data['peer_id'] = self.my_id
        data['ip'] = self.ip
        data['port'] = self.port
        data['event'] = 'start'

        tcp_connection = TcpConnection()
        try:
            tcp_connection.connect(self.tracker_ip, self.tracker_port)
            tcp_connection.send(json.dumps(data).encode('utf-8'))
            response_message = json.loads(
                tcp_connection.receive().decode('utf-8'))
            tcp_connection.close()
        except Exception as e:
            print(e)
            tcp_connection.close()
            self.main_controller.tell({
                'header': 13,
                'body': 'Connection error with tracker'
            })
            return

        if response_message['status_code'] == '0':
            self.main_controller.tell({
                'header':
                13,
                'body':
                'Received status code 0 from tracker'
            })
            return

        self.main_controller.tell({
            'header': 9,
            'body': response_message['peers']
        })

        interval = int(float(response_message['interval']) / 1000) - 1
        data['event'] = 'update'

        while True:
            time.sleep(interval)
            try:
                tcp_connection = TcpConnection()
                tcp_connection.connect(self.tracker_ip, self.tracker_port)
                tcp_connection.send(json.dumps(data).encode('utf-8'))
                response_message = json.loads(
                    tcp_connection.receive().decode('utf-8'))
                tcp_connection.close()
            except Exception as e:
                print(e)
                tcp_connection.close()
                self.main_controller.tell({
                    'header':
                    13,
                    'body':
                    'Connection error with tracker'
                })
                return

            if response_message['status_code'] == '0':
                self.main_controller.tell({
                    'header':
                    13,
                    'body':
                    'Received status code 0 from tracker'
                })
                return

            self.main_controller.tell({
                'header': 9,
                'body': response_message['peers']
            })