Example #1
0
func (t WriteTransport) Start() {
	buf := make([]byte, 516)
	defer t.conn.Close()
	for {
		//let client know that we previous write/data packet was all good

		t.sendAckPacket()

		//get data pack

		n, _, err := t.conn.ReadFromUDP(buf)

		CheckError(err)
		p := packet.ToPacket(buf[0:n])
		switch cur := p.(type) {
		case packet.DataPacket:
			t.file.Data = append(t.file.Data, cur.Data...)

		default:
			fmt.Println("ERROR. should have gotten data packet from user. got something else.")
		}

		fmt.Println("n is", n)
		if n < MAXDATASIZE {
			fmt.Println("right before calling sendackpacket again, t.blocknum is", t.blocknum)
			t.sendAckPacket()
			break
		}

	}

	t.storeFile()

}
Example #2
0
func serve() {

	/* prepare a address at port 10001*/
	port := fmt.Sprintf(":%d", PORT)
	fmt.Println("server up and listening at port", port)
	ServerAddr, err := net.ResolveUDPAddr("udp", port)
	fmt.Println(ServerAddr.String())
	CheckError(err)

	/* Now listen at selected port */
	ServerConn, err := net.ListenUDP("udp", ServerAddr)
	CheckError(err)
	defer ServerConn.Close() /* at end of function, ServerConn is closed. */

	buf := make([]byte, 516)

	for {
		n, addr, err := ServerConn.ReadFromUDP(buf)
		fmt.Println(buf[:n])
		fmt.Println(err)
		fmt.Println(addr)
		fmt.Println(n)
		p := packet.ToPacket(buf[0:n])
		fmt.Println("Received ", p, " from ", addr)
		go routePacket(p, addr)
		CheckError(err)
	}
}
Example #3
0
func TestBytesToReadPacket(t *testing.T) {
	for i, test := range testReadPacket {
		ret := packet.ToPacket(test.bitVersion)
		if !reflect.DeepEqual(ret, test.packetVersion) {
			t.Errorf("Failed Test %d: ReadPacket: ToPacket(%v)=%v DESIRED: %v", i, test.bitVersion, ret, test.packetVersion)
		}
	}
}
Example #4
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)
	}
}
Example #5
0
func SendToServer(pathToFile string) {
	ServerAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:10001")
	CheckError(err)

	LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
	CheckError(err)

	Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
	CheckError(err)
	defer Conn.Close()

	if err != nil {
		fmt.Println(pathToFile)
		fmt.Println(err)
	} else {
		//send write request
		tid := uint16(0)
		filename := filepath.Base(pathToFile)
		writePacket := packet.NewWritePacket(filename, tid, uint16(69))
		_, err = Conn.Write(writePacket.ToBytes())
		CheckError(err)

		p := make([]byte, 2048)
		_, err = bufio.NewReader(Conn).Read(p)
		CheckError(err)
		retPacket := packet.ToPacket(p)

		switch resp := retPacket.(type) {
		case packet.AckPacket:
			fmt.Println("acknowledge packet for some blocknum:", resp.BlockNum)
			//all is good. start sending data packets like normal
			SendDataPacketsToServer(pathToFile, Conn)
		case packet.ErrorPacket:
			fmt.Println("error packet with ErrMsg:", string(resp.ErrMsg))
		default:
			fmt.Println("returned packet for write request is erronous")
		}

	}
}
Example #6
0
func (t ReadTransport) Start() {
	buf := make([]byte, 516)
	defer t.conn.Close()
	for {
		//send data packet
		done := t.sendDataPacket()
		//if err {
		//fmt.Println("send data packet failed. do something.... ALSO, send proper error response. NOT BOOL.")
		//break
		//}

		//verify
		n, _, err := t.conn.ReadFromUDP(buf)
		CheckError(err)
		p := packet.ToPacket(buf[0:n])
		fmt.Printf("recieved packet from client for readtransport: %v\n", p)

		if done {
			break
		}
	}

}