Пример #1
0
func TestPartDownload(t *testing.T) {
	c, e := httpclient.GetSize("http://ports.ubuntu.com/dists/precise/main/installer-powerpc/current/images/powerpc/netboot/mini.iso")
	if e != nil {
		t.Error("failed: Get size must be without error")
	}
	f, _ := iotools.CreateSafeFile("part_download.data")
	defer f.Close()
	f.Truncate(c)
	dow := httpclient.CreatePartialDownloader("http://ports.ubuntu.com/dists/precise/main/installer-powerpc/current/images/powerpc/netboot/mini.iso", f, 0, 0, c)
	dow.BeforeRun()
	for {
		sta, _ := dow.DoWork()
		if sta {
			return
		}
	}

}
Пример #2
0
func TestPartDownloadWorker(t *testing.T) {
	return
	url := "http://releases.ubuntu.com/14.04.2/ubuntu-14.04.2-server-amd64.list"
	c, _ := httpclient.GetSize(url)
	c = c / 2
	f, _ := iotools.CreateSafeFile("g_ubuntu-14.04.2-server-amd64.list")
	defer f.Close()
	log.Println(f.Truncate(c))
	dow := httpclient.CreatePartialDownloader(url, f, 0, 0, c)
	mv := monitor.MonitoredWorker{Itw: dow}
	log.Println(mv.Start())
	log.Println(mv.Start())
	time.Sleep(time.Second * 1)
	log.Println(mv.Stop())
	time.Sleep(time.Second * 5)
	log.Println(mv.Start())
	log.Println(mv.Start())
	time.Sleep(time.Second * 5)
	log.Println(mv.Stop())
}
Пример #3
0
func CreateDownloader(url string, fp string, seg int64) (dl *Downloader, err error) {
	c, err := GetSize(url)
	if err != nil {
		//can't get file size
		return nil, err
	}

	dfs := getDown() + fp
	sf, err := iotools.CreateSafeFile(dfs)
	if err != nil {
		//can't create file on path
		return nil, err
	}

	if err := sf.Truncate(c); err != nil {
		//can't truncate file
		return nil, err
	}
	//create part-downloader foreach segment
	ps := c / seg
	wp := new(monitor.WorkerPool)
	for i := int64(0); i < seg-int64(1); i++ {
		d := CreatePartialDownloader(url, sf, ps*i, ps*i, ps*i+ps)
		mv := monitor.MonitoredWorker{Itw: d}
		wp.AppendWork(&mv)
	}
	lastseg := int64(ps * (seg - 1))
	dow := CreatePartialDownloader(url, sf, lastseg, lastseg, c)
	mv := monitor.MonitoredWorker{Itw: dow}

	//add to worker pool
	wp.AppendWork(&mv)
	d := Downloader{
		sf: sf,
		wp: wp,
		Fi: FileInfo{FileName: fp, Size: c, Url: url},
	}
	return &d, nil
}
Пример #4
0
func TestMultiPartDownloadWorker(t *testing.T) {
	partcount := 10
	pc := int64(partcount)
	url := "http://releases.ubuntu.com/14.04.2/ubuntu-14.04.2-server-amd64.list"
	c, _ := httpclient.GetSize(url)
	f, _ := iotools.CreateSafeFile("gm_ubuntu-14.04.2-server-amd64.list")
	defer f.Close()
	f.Truncate(c)
	ps := c / pc
	for i := int64(0); i < pc-1; i++ {
		//log.Println(ps*i, ps*i+ps)
		d := httpclient.CreatePartialDownloader(url, f, ps*i, ps*i, ps*i+ps)
		mv := monitor.MonitoredWorker{Itw: d}
		mv.Start()
	}
	lastseg := c - (ps * (pc - 1))
	dow := httpclient.CreatePartialDownloader(url, f, lastseg, lastseg, c)
	mv := monitor.MonitoredWorker{Itw: dow}
	mv.Start()

	time.Sleep(time.Second * 15)

}
Пример #5
0
func TestMultiPartDownloadPool(t *testing.T) {
	partcount := 10
	pc := int64(partcount)
	url := "http://mirror.yandex.ru/ubuntu-releases/15.04/ubuntu-15.04-snappy-amd64%2bgeneric.img.xz"
	c, _ := httpclient.GetSize(url)
	f, _ := iotools.CreateSafeFile("ubuntu-15.04-snappy-amd64+generic.img.xz ")
	defer f.Close()
	f.Truncate(c)
	ps := c / pc
	wp := monitor.WorkerPool{}
	for i := int64(0); i < pc-1; i++ {
		//log.Println(ps*i, ps*i+ps)
		d := httpclient.CreatePartialDownloader(url, f, ps*i, ps*i, ps*i+ps)
		mv := monitor.MonitoredWorker{Itw: d}
		wp.AppendWork(&mv)
	}
	lastseg := c - (ps * (pc - 1))
	dow := httpclient.CreatePartialDownloader(url, f, lastseg, lastseg, c)
	mv := monitor.MonitoredWorker{Itw: dow}
	wp.AppendWork(&mv)
	wp.StartAll()
	time.Sleep(time.Second * 10000)

}
Пример #6
0
func (pd *PartialDownloader) BeforeDownload() error {
	//create new req
	r, err := http.NewRequest("GET", pd.url, nil)
	if err != nil {
		return err
	}

	r.Header.Add("Range", "bytes="+strconv.FormatInt(pd.dp.Pos, 10)+"-"+strconv.FormatInt(pd.dp.To, 10))
	f, _ := iotools.CreateSafeFile("test")
	r.Write(f)
	f.Close()
	resp, err := pd.client.Do(r)
	if err != nil {
		log.Printf("error: error download part file%v \n", err)
		return err
	}
	//check response
	if resp.StatusCode != 206 {
		log.Printf("error: file not found or moved status:", resp.StatusCode)
		return errors.New("error: file not found or moved")
	}
	pd.req = *resp
	return nil
}