예제 #1
0
    def server_handshake(self):
        is_handshake_successful = False
        while not is_handshake_successful:
            self.get_header()
            self.get_payload_eop()
            self.quantity_packages_to_receive = self.package_header[3]
            print(f'header recebido: {self.r_header}')

            if self.package_header[2] == SERVER_ID:
                print(f'Recebido HS do client - ID correto\n')
                if self.package_header[0] == 1:

                    payload = []
                    header_list = [0 for i in range(10)]
                    header_list[0] = 2  # mensagem to tipo 2 - server ocioso
                    header_list[1] = CLIENT_ID
                    header_list[2] = SERVER_ID

                    datagram_obj = Datagram(payload, header_list)
                    self.package = datagram_obj.get_datagram()
                    self.send_package()
                    is_handshake_successful = True
                    print(f'enviado resposta para o client')

            else:
                print(f'ID do servidor não confere, ignorando mensagem...')
예제 #2
0
    def create_shut_down_signal(self):
        payload = []
        self.header_list = [1 for i in range(10)]
        self.header_list[0] = 5  # mensagem to tipo 1 - handshake
        self.header_list[1] = CLIENT_ID
        self.header_list[2] = SERVER_ID

        datagram_obj = Datagram(payload, self.header_list)
        self.package = datagram_obj.get_datagram()
예제 #3
0
    def receive_full_packages(self):

        self.n_last_package_received = 0

        while self.n_current_package < self.quantity_packages_to_receive:
            self.timer1 = time.time()
            self.timer2 = self.timer1

            while self.com2.rx.getBufferLen() == 0:
                current_time = time.time()
                elapsed_timer1 = current_time - self.timer1
                elapsed_timer2 = current_time - self.timer2

                if elapsed_timer1 >= 20:
                    print(
                        f'Tempo máximo excedido, avisando client desligamento...'
                    )
                    payload = []

                    header_list = [1 for i in range(10)]
                    header_list[0] = 5
                    header_list[1] = CLIENT_ID
                    header_list[2] = SERVER_ID

                    datagram_obj = Datagram(payload, header_list)
                    self.package = datagram_obj.get_datagram()
                    self.send_package()
                    self.shutdown()

                if elapsed_timer2 >= 5:
                    print(
                        f'5 segundos sem receber o proximo pacote, enviando resposta novamente...'
                    )
                    self.send_package()
                    self.timer2 = time.time()

            self.get_header()
            self.get_payload_eop()
            # verify eop and current == last + 1
            # verificar se o pacote atual é igual ao anterior + 1
            self.is_next_package = self.n_last_package_received + 1 == self.n_current_package
            self.is_eop_right = self.r_eop == b'\xff\xaa\xff\xaa'

            print(f'Recebeu o proximo? [{self.is_next_package}]', end=' | ')
            print(f'Is eop ok?  [{self.is_eop_right}]', end=" | ")
            print(
                f'Received package [{self.n_current_package} / {self.quantity_packages_to_receive}]'
            )

            self.build_response()
            self.send_package()

        print(f'Received all packages')
        self.juntar_imagem()
예제 #4
0
 def send_handshake(self):
     packet = Datagram()
     packet.set_head(message_type=2,
                     message_id=1,
                     num_payloads=0,
                     payload_index=0,
                     payload_size=0,
                     error_type=0,
                     restart_index=0)
     packet.set_EOP()
     self.transmitter.send(packet.get_datagram())
예제 #5
0
 def send_packet(self, payload, num_payloads, payload_index):
     packet = Datagram()
     packet.set_head(message_type=1,
                     message_id=1,
                     num_payloads=num_payloads,
                     payload_index=payload_index,
                     payload_size=len(payload),
                     error_type=0,
                     restart_index=0)
     packet.set_payload(payload=payload)
     packet.set_EOP(0)
     self.transmitter.send(packet.get_datagram())
예제 #6
0
    def client_handshake(self):
        handshake_successful = False
        payload = []
        self.header_list = [0 for i in range(10)]

        self.header_list[0] = 1  # mensagem to tipo 1 - handshake
        self.header_list[1] = CLIENT_ID
        self.header_list[2] = SERVER_ID
        self.header_list[3] = len(self.l_packages)
        self.header_list[5] = FILE_ID

        datagram_obj = Datagram(payload, self.header_list)
        datagram = datagram_obj.get_datagram()

        self.package = datagram

        print(f'Enviando handshake para o Servidor..')
        self.send_package()

        self.start_time1 = time.time()
        self.start_time2 = time.time()
        while not handshake_successful:
            while self.com1.rx.getBufferLen() == 0:
                self.time_delta1 = time.time() - self.start_time1
                self.time_delta2 = time.time() - self.start_time2
                if self.time_delta2 > 20:
                    print(
                        f'Tempo máximo excedido, avisando server desligamento...'
                    )
                    self.create_shut_down_signal()
                    self.send_package()
                    time.sleep(1)
                    self.shutdown()

                elif self.time_delta1 >= 5:
                    should_send_again = input(
                        'Didn\'t receive response. Send again? (s/n) ')
                    if should_send_again == 's':
                        print(f'Enviando novamente...\n')
                        self.send_package()
                        self.start_time1 = time.time()
                    else:
                        self.shutdown()

            self.get_header()
            self.get_payload_eop()
            if self.package_header[0] == 2:
                handshake_successful = True
                print(f'Server está ocioso... começando o envio\n')
            else:
                print(f'Não teve sucesso no handshake...')
예제 #7
0
    def build_packages(self):
        self.header_list = [1 for i in range(10)]
        self.header_list[0] = 3  # mensagem to tipo 1 - handshake
        self.header_list[1] = CLIENT_ID
        self.header_list[2] = SERVER_ID

        for index_package in range(len(self.l_bytes_img)):
            payload = self.l_bytes_img[index_package]
            self.header_list[3] = len(self.l_bytes_img)
            self.header_list[4] = index_package + 1
            self.header_list[5] = len(payload)

            datagram = Datagram(payload, self.header_list)
            self.l_packages.append(datagram.get_datagram())

        self.len_packages = len(self.l_packages)
예제 #8
0
    def build_response(self):
        is_package_ok = self.is_next_package and self.is_eop_right
        payload = []
        header_list = [0 for i in range(10)]

        if is_package_ok:  # create type4  msg representing that the package was received successfully
            self.l_received_payloads.append(self.r_payload)
            print(f'package [{self.n_current_package}] received correctly')
            self.n_last_package_received = self.n_current_package
            header_list[0] = 4  # mensagem to tipo 1 - handshake
            header_list[1] = CLIENT_ID
            header_list[2] = SERVER_ID
            header_list[7] = self.n_last_package_received
        else:
            print(
                f'package [{self.n_current_package}] had some error, requesting again..'
            )
            header_list[0] = 6  # mensagem to tipo 6 - solicitando reenvio
            header_list[1] = CLIENT_ID
            header_list[2] = SERVER_ID
            header_list[6] = self.n_last_package_received + 1

        datagram_obj = Datagram(payload, header_list)
        self.package = datagram_obj.get_datagram()