<commit_before>package main

import (
	"fmt"
	"io"
	"net"
	"os"
	"time"
)

type DataResponse struct {
	data []byte
	bytesRead int
	err error
}

func ReadToChannel(path string, dataChan, returnChan chan *DataResponse) {
	datares := <- returnChan
	file, err := os.Open(path)
	if err != nil {
		datares.err = err
		dataChan <- datares
		return
	}

	for {
		bytesRead, err := file.Read(datares.data)
		datares.bytesRead = bytesRead
		datares.err = err
		dataChan <- datares
		if err != nil {
			if err := file.Close(); err != nil {
				fmt.Println("Failed to close", path, err)
			}
			break
		}
		datares = <- returnChan
	}

}

func SendFile(path string, blocksize int, addr *net.UDPAddr) {
	started := time.Now()

	rrq, err := NewRRQresponse(addr, blocksize)
	if err != nil {
		fmt.Println("Failed to create rrq", err)
		return
	}
	fmt.Println("GET", path, "blocksize", rrq.blocksize)

	if err := rrq.WriteOACK(); err != nil {
		return
	}

	bufsize := 10
	dataChan := make(chan *DataResponse, bufsize)
	returnChan := make(chan *DataResponse, bufsize)

	datapool := make([]byte, blocksize*bufsize)
	for i := 0; i < bufsize; i++ {
		start := blocksize * i
		end := start + blocksize
		returnChan <- &DataResponse{data: datapool[start:end]}
	}

	go ReadToChannel(path, dataChan, returnChan)

	totalBytes := 0

	for {
		datares := <- dataChan
		totalBytes += datares.bytesRead
		if datares.err == io.EOF {
			rrq.Write(datares.data[:datares.bytesRead])
			rrq.End()
			break
		} else if datares.err != nil {
			fmt.Println("Failed to read data from file:", datares.err)
			break
		} else {
			rrq.Write(datares.data[:datares.bytesRead])
		}

		returnChan <- datares
	}

	took := time.Since(started)

	speed := float64(totalBytes) / took.Seconds() / 1024 / 1024

	fmt.Printf("Sent %v bytes in %v %f MB/s\n", totalBytes, took, speed)
	fmt.Println("END-GET", path)
	fmt.Println()
	fmt.Println()

}

func main() {
	fmt.Println("hello2")
	addr, err := net.ResolveUDPAddr("udp", ":1234")
	if err != nil {
		fmt.Println("Failed to resolve address", err)
		return
	}

	conn, err := net.ListenUDP("udp", addr)
	if err != nil {
		fmt.Println("Failed to listen UDP", err)
		return
	}

	data := make([]byte, 50)
<commit_msg>Revert "Small speed optimization by resusing slices"<commit_after>package main

import (
	"fmt"
	"net"
	"os"
	"io"
	"time"
)


func SendFile(path string, blocksize int, addr *net.UDPAddr) {
	started := time.Now()

	rrq, err := NewRRQresponse(addr, blocksize)
	if err != nil {
		fmt.Println("Failed to create rrq", err)
		return
	}
	fmt.Println("GET", path, "blocksize", rrq.blocksize)

	if err := rrq.WriteOACK(); err != nil {
		return
	}


	file, err := os.Open(path)
	if err != nil {
		fmt.Println("Failed to open file", path, err)
		// TODO: write error package
		return
	}

	b := make([]byte, rrq.blocksize)

	totalBytes := 0

	for {
		bytesRead, err := file.Read(b)
		totalBytes += bytesRead

		if err == io.EOF {
			rrq.Write(b[:bytesRead])
			rrq.End()
			break
		} else if err != nil {
			fmt.Println("Error while reading", file, err)
			// TODO: write error package
			return
		}

		rrq.Write(b[:bytesRead])
	}

	file.Close()
	took := time.Since(started)

	speed := float64(totalBytes) / took.Seconds() / 1024 / 1024

	fmt.Printf("Sent %v bytes in %v %f MB/s\n", totalBytes, took, speed)
	fmt.Println("END-GET", path)
	fmt.Println()
	fmt.Println()

}


func main() {
	fmt.Println("hello2")
	addr, err := net.ResolveUDPAddr("udp", ":1234")
	if err != nil {
		fmt.Println("Failed to resolve address", err)
		return
	}

	conn, err := net.ListenUDP("udp", addr)
	if err != nil {
		fmt.Println("Failed to listen UDP" , err)
		return
	}

	data := make([]byte, 50)

{'fraction_non_alphanumeric': 0.15627552409474543, 'fraction_numerical': 0.010618023414102912, 'mean_word_length': 3.4158653846153846, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3538994', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1378, 'n_words': 358}