コード例 #1
0
ファイル: reliable_sock.py プロジェクト: VictorJuliani/Redes
    def receivePacket(self, data):
        packet = Packet(data)
        packet.unwrap()

        self.attempts = 0

        if not packet.validChecksum() or random.randint(
                1, 100
        ) < self.pcorr:  # do not receive broken packets or simulate a broken packet
            print "Bad checksum received on seg: " + str(
                packet.seg) + " ack: " + str(packet.ack)
            return None

        if packet.con > 0:
            self.seg = packet.con  # set seg with nextSeg received in con field
            self.ack = packet.con
            self.init = True
            if not self.requesting:  # this socket received the connection request, then ack it
                print "Connection request from " + str(self.addr)
                self.buff.put(Packet('', end=packet.end, con=self.nextSeg)
                              )  # ack with nextSeg expected on con field
                return packet
            else:
                print "Connection established!"
        elif (packet.ack > 0):  # ack packet
            self.lock.acquire(True)

            if packet.ack == self.ack:  # expected ack!
                self.ack += 1
                self.playTimer()  # ack received, start timer again
                print "Received expected ack " + str(
                    packet.ack) + " on connection " + str(self.addr)

                if packet.end:  # ack for end packet received
                    self.endCon()
            self.lock.release()
        elif packet.seg < self.nextSeg:
            self.ackPacket(
                packet.seg, packet.end
            )  # old packet received again, ACK might be lost.. send it again
            print "Duplicated packet " + str(
                packet.seg) + ". Sending ack again and ignoring"
        elif self.nextSeg == packet.seg:  # expected seg!
            self.nextSeg += 1
            self.ackPacket(packet.seg, packet.end)
            print "Acking packet " + str(packet.seg)
            return packet
        #else:
        #	print "Bad packet received! Seg: " + str(packet.seg) + " expected: " + str(self.nextSeg) + " ack: " + str(packet.ack)

        return None
コード例 #2
0
ファイル: reliable_sock.py プロジェクト: VictorJuliani/Redes
	def receivePacket(self, data):
		packet = Packet(data)
		packet.unwrap()

		self.attempts = 0

		if not packet.validChecksum() or random.randint(1, 100) < self.pcorr: # do not receive broken packets or simulate a broken packet
			print "Bad checksum received on seg: " + str(packet.seg) + " ack: " + str(packet.ack)
			return None

		if packet.con > 0:		
			self.seg = packet.con # set seg with nextSeg received in con field
			self.ack = packet.con
			self.init = True
			if not self.requesting: # this socket received the connection request, then ack it
				print "Connection request from " + str(self.addr)
				self.buff.put(Packet('', end = packet.end, con = self.nextSeg)) # ack with nextSeg expected on con field
				return packet
			else:
				print "Connection established!"
		elif (packet.ack > 0): # ack packet
			self.lock.acquire(True)

			if packet.ack == self.ack: # expected ack!
				self.ack += 1
				self.playTimer() # ack received, start timer again
		 		print "Received expected ack " + str(packet.ack) + " on connection " + str(self.addr)

				if packet.end: # ack for end packet received
					self.endCon()
			self.lock.release()
		elif packet.seg < self.nextSeg:
			self.ackPacket(packet.seg, packet.end) # old packet received again, ACK might be lost.. send it again
			print "Duplicated packet " + str(packet.seg) + ". Sending ack again and ignoring"
		elif self.nextSeg == packet.seg: # expected seg!
			self.nextSeg += 1
			self.ackPacket(packet.seg, packet.end)
			print "Acking packet " + str(packet.seg)
			return packet
		#else:
		#	print "Bad packet received! Seg: " + str(packet.seg) + " expected: " + str(self.nextSeg) + " ack: " + str(packet.ack)
		
		return None