Example #1
0
func (t *WriteTransport) sendAckPacket() {

	fmt.Printf("sending ack packet (blocknum = %d) back to client with addr %v\n", t.blocknum, t.clientAddr)
	ackPacket := packet.NewAckPacket(t.blocknum)
	_, err := t.conn.WriteToUDP(ackPacket.ToBytes(), t.clientAddr)
	CheckError(err)

	t.blocknum = t.blocknum + uint16(1)
	fmt.Println("just increased t.blocknum to be", t.blocknum)

}
Example #2
0
func TestBytesToDataPacket(t *testing.T) {
	for i, test := range testDataPacket {
		ret := packet.ToPacket(test.bitVersion)
		if !reflect.DeepEqual(ret, test.packetVersion) {
			t.Errorf("Failed Test %d: DataPacket: toPacket(%v)=%v DESIRED: %v", i, test.bitVersion, ret, test.packetVersion)
		}
	}
}

//test AckPacket conversion to bytes
var testAckPacket = []struct {
	packetVersion packet.AckPacket
	bitVersion    []byte
	err           bool
}{
	{packet.NewAckPacket(uint16(0)), stringToBytes("\x00" + string(packet.ACK) + "\x00\x00"), false},      //0
	{packet.NewAckPacket(uint16(1)), stringToBytes("\x00" + string(packet.ACK) + "\x00\x01"), false},      //1
	{packet.NewAckPacket(uint16(12289)), stringToBytes("\x00" + string(packet.ACK) + "\x30\x01"), false},  //order of blocknum bytes is big endian
	{packet.NewAckPacket(uint16(0xffff)), stringToBytes("\x00" + string(packet.ACK) + "\xff\xff"), false}, //max blocknum
}

func TestAckPacketToBytes(t *testing.T) {
	for i, test := range testAckPacket {
		ret := test.packetVersion.ToBytes()

		same := true
		same = len(test.bitVersion) == len(ret)
		for j, b := range test.bitVersion {
			if ret[j] != b {
				same = false
			}