Ejemplo n.º 1
0
func (t *ReadTransport) sendDataPacket() bool {
	done := false
	//error := false

	//determine which file this packet is for via tid
	//this is done via header. not done with header code. thus assume for inProcess[tid=uint16(0)] for now.
	t.blocknum = t.blocknum + uint16(1)

	//get the appropriate block for the file based on new blocknum
	var data []byte
	start := (int(t.blocknum) - 1) * MAXDATASIZE
	end := start + MAXDATASIZE
	if end < len(t.file.Data) {
		data = t.file.Data[start:end]
	} else {

		data = t.file.Data[start:]
		done = true
	}

	//create and send datapacket
	dataPacket := packet.NewDataPacket(t.blocknum, data)
	_, err := t.conn.WriteToUDP(dataPacket.ToBytes(), t.clientAddr)
	CheckError(err)

	return done
}
Ejemplo n.º 2
0
func SendDataPacketsToServer(pathToFile string, Conn *net.UDPConn) {
	//open file for reading
	f, err := os.Open(pathToFile)
	CheckError(err)
	defer f.Close()

	//create buffer for storing MAXDATASIZE bytes of file at a time
	buffer := make([]byte, MAXDATASIZE)
	//reader for reading file into buffer
	reader := bufio.NewReader(f)

	tid := uint16(0)
	//keep track of block num
	blockNum := uint16(1)
	for {
		//read
		actualBytesRead, err := reader.Read(buffer)
		CheckError(err)

		//store into data packet
		dataPacket := packet.NewDataPacket(blockNum, buffer, tid, uint16(69))

		//send data packet
		_, err = Conn.Write(dataPacket.ToBytes())
		CheckError(err)

		p := make([]byte, 2048)
		_, err = bufio.NewReader(Conn).Read(p)
		retPacket := packet.ToPacket(p)
		//make sure response is good with correctly returned ack packet
		switch cur := retPacket.(type) {
		case packet.AckPacket:
			fmt.Println("all good for blocknum:", cur.BlockNum)
		case packet.ErrorPacket:
			fmt.Println("error!:", cur.ErrMsg)
		default:
			fmt.Println("ummm wrong type of packet recieved to confirm data packet sent")
		}

		//if done reading file AND sending it, then  quit loop
		if actualBytesRead < MAXDATASIZE {
			break
		}
		//increase blockNum for next portion of packet to be sent
		blockNum = blockNum + uint16(1)
	}
}
Ejemplo n.º 3
0
func TestBytesToWritePacket(t *testing.T) {
	for i, test := range testWritePacket {
		ret := packet.ToPacket(test.bitVersion)
		if !reflect.DeepEqual(ret, test.packetVersion) {
			t.Errorf("Failed Test %d: WritePacket: ToPacket(%v)=%v DESIRED: %v", i, test.bitVersion, ret, test.packetVersion)
		}
	}
}

//test DataPacket conversion to bytes
var testDataPacket = []struct {
	packetVersion packet.DataPacket
	bitVersion    []byte
	err           bool
}{
	{packet.NewDataPacket(uint16(0), []byte("a")), stringToBytes("\x00" + string(packet.DATA) + "\x00\x00" + "a"), false},                //basic read
	{packet.NewDataPacket(uint16(1), []byte("")), stringToBytes("\x00" + string(packet.DATA) + "\x00\x01" + ""), false},                  //empty read
	{packet.NewDataPacket(uint16(12289), ALLBYTES), stringToBytes("\x00" + string(packet.DATA) + "\x30\x01" + string(ALLBYTES)), false},  //all bytes
	{packet.NewDataPacket(uint16(0xffff), ALLBYTES), stringToBytes("\x00" + string(packet.DATA) + "\xff\xff" + string(ALLBYTES)), false}, //max blocknum
}

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

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