Ejemplo n.º 1
0
 def _sender_packet_loop(self):
     if self.mode == SCUMode.RecvMode:
         raise Exception
     while True:
         try:
             packet = SCUPacket()
             packet.from_raw(self.socket.recv(2048))
             # # TODO: remove this
             # psuedo packet loss
             if random.random() >= 0.5:
                 continue
             if packet.header.id not in self.connection_manager:
                 continue
             if packet.header.typ == SCUPacketType.Fin.value:
                 self.connection_manager[packet.header.id].put(
                     (True, packet.header.seq))
             elif packet.header.typ == SCUPacketType.Rtr.value:
                 self.connection_manager[packet.header.id].put(
                     (False, packet.header.seq))
         except Exception as e:  # recvが失敗した時とputが失敗した時は(適当)
             if e == KeyboardInterrupt:
                 raise KeyboardInterrupt
             else:
                 import traceback
                 traceback.print_exc()
Ejemplo n.º 2
0
    def _receiver_packet_loop(self):
        if self.mode == SCUMode.SendMode:
            raise Exception
        received_files_flag = {}
        received_files_length = {}
        while True:
            try:
                data, from_addr = self.socket.recvfrom(2048)
                packet = SCUPacket()
                packet.from_raw(data)
                # TODO: remove this
                # psuedo packet loss
                if random.random() >= 0.5:
                    continue
                key = utils.endpoint2str(from_addr, packet.header.id)
                if key not in self.received_files_data:
                    self.received_files_data[key] = [b""] * 100
                    received_files_flag[key] = False

                if received_files_flag[key]:
                    self.response(SCUPacketType.Fin.value, from_addr,
                                  packet.header.id, 0)
                    continue

                if packet.header.typ == SCUPacketType.DataEnd.value or packet.header.typ == SCUPacketType.Data.value:
                    if packet.header.typ == SCUPacketType.DataEnd.value:
                        received_files_length[key] = packet.header.seq + 1

                    self.received_files_data[key][
                        packet.header.seq] = packet.payload
                    rtr = self.calculate_rtr(key, packet.header.seq)
                    if rtr is not None:  # 再送要求する必要あり
                        self.response(SCUPacketType.Rtr.value, from_addr,
                                      packet.header.id, rtr)
                    elif key in received_files_length and self.is_all_received(
                            key, received_files_length[key]):  #  ファイル受信完了
                        received_files_flag[key] = True
                        self.response(SCUPacketType.Fin.value, from_addr,
                                      packet.header.id, 0)
                        self.file_received.put(
                            (key, received_files_length[key]))

            except Exception as e:  # recvが失敗した時とputが失敗した時は(適当)
                if e == KeyboardInterrupt:
                    raise KeyboardInterrupt
                else:
                    import traceback
                    traceback.print_exc()
Ejemplo n.º 3
0
    def _receiver_packet_loop(self):
        if self.mode == SCUMode.SendMode:
            raise Exception
        received_files_flag = {}
        received_files_length = {}
        while True:
            try:
                data, from_addr = self.socket.recvfrom(2048)
                packet = SCUPacket()
                packet.from_raw(data)

                key = utils.endpoint2str(from_addr, packet.header.id)
                if key not in self.received_files_data:
                    self.received_files_data[key] = [b""] * 100
                    received_files_flag[key] = False

                if received_files_flag[key]:
                    self.response(SCUPacketType.Fin.value, from_addr,
                                  packet.header.id, 0)
                    continue

                if packet.header.typ == SCUPacketType.DataEnd.value or packet.header.typ == SCUPacketType.Data.value:
                    if packet.header.typ == SCUPacketType.DataEnd.value:
                        received_files_length[key] = packet.header.seq + 1

                    self.received_files_data[key][
                        packet.header.seq] = packet.payload
                    rtr = self.calculate_rtr(key, packet.header.seq)
                    if rtr is not None:  # Need to send retransmission request
                        self.response(SCUPacketType.Rtr.value, from_addr,
                                      packet.header.id, rtr)
                    elif key in received_files_length and self.is_all_received(
                            key, received_files_length[key]):  # Received file:
                        received_files_flag[key] = True
                        self.response(SCUPacketType.Fin.value, from_addr,
                                      packet.header.id, 0)
                        self.file_received.put(
                            (key, received_files_length[key]))

            except Exception as e:  # When recv or put failed:
                if e == KeyboardInterrupt:
                    raise KeyboardInterrupt
                else:
                    import traceback
                    traceback.print_exc()
Ejemplo n.º 4
0
 def _sender_packet_loop(self):
     if self.mode == SCUMode.RecvMode:
         raise Exception
     while True:
         try:
             packet = SCUPacket()
             packet.from_raw(self.socket.recv(2048))
             if packet.header.id not in self.connection_manager:
                 continue
             if packet.header.typ == SCUPacketType.Fin.value:
                 self.connection_manager[packet.header.id].put((True, packet.header.seq))
             elif packet.header.typ == SCUPacketType.Rtr.value:
                 self.connection_manager[packet.header.id].put((False, packet.header.seq))
         except Exception as e: # When recv fails and when put fails (appropriate)
             if e == KeyboardInterrupt:
                 raise KeyboardInterrupt
             else:
                 import traceback
                 traceback.print_exc()
Ejemplo n.º 5
0
 def _sender_packet_loop(self):
     prev_packet = SCUPacket()
     while True:
         try:
             packet = SCUPacket()
             packet.from_raw(self.socket.recv(2048))
             # # psuedo packet loss TODO: remove
             # if random.random() >= 0.5:
             #     continue
             if packet.header.id not in self.connection_manager:
                 continue
             if prev_packet.header.__dict__ == packet.__dict__:
                 continue
             prev_packet = packet
             self.connection_manager[packet.header.id].put(packet)
         except Exception as e:  # recvが失敗した時とputが失敗した時は(適当)
             if e == KeyboardInterrupt:
                 raise KeyboardInterrupt
             else:
                 import traceback
                 traceback.print_exc()
Ejemplo n.º 6
0
 def _receiver_packet_loop(self):
     prev_packet = SCUPacket()
     while True:
         try:
             data, from_addr = self.socket.recvfrom(2048)
             # # # TODO: remove this
             # # psuedo packet loss
             # if random.random() >= 0.5:
             #     continue
             packet = SCUPacket()
             packet.from_raw(data)
             if prev_packet.__dict__ == packet.__dict__:
                 continue
             prev_packet = packet
             self.task_manager.put((packet, from_addr))
         except Exception as e:
             if e == KeyboardInterrupt:
                 raise KeyboardInterrupt
             else:
                 import traceback
                 traceback.print_exc()