示例#1
0
func RestoreDownloader(url string, fp string, dp []DownloadProgress) (dl *Downloader, err error) {
	dfs := getDown() + fp
	sf, err := iotools.OpenSafeFile(dfs)
	if err != nil {
		//can't create file on path
		return nil, err
	}
	s, err := sf.Stat()
	if err != nil {
		return nil, err
	}
	wp := new(monitor.WorkerPool)
	for _, r := range dp {
		dow := CreatePartialDownloader(url, sf, r.From, r.Pos, r.To)
		mv := monitor.MonitoredWorker{Itw: dow}

		//add to worker pool
		wp.AppendWork(&mv)

	}
	d := Downloader{
		sf: sf,
		wp: wp,
		Fi: FileInfo{FileName: fp, Size: s.Size(), Url: url},
	}
	return &d, nil
}
示例#2
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
}
示例#3
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)

}
示例#4
0
func TestWorkerPool(t *testing.T) {
	wp := monitor.WorkerPool{}
	for i := 0; i < 20; i++ {
		mw := &monitor.MonitoredWorker{Itw: &TestWorkPool{From: 0, To: 20, id: rand.Int31()}}
		wp.AppendWork(mw)
	}
	wp.StartAll()
	time.Sleep(time.Second)
	log.Println("------------------Work Started------------------")
	log.Println(wp.GetAllProgress())
	log.Println("------------------Get All Progress--------------")
	time.Sleep(time.Second)
	wp.StopAll()
	log.Println("------------------Work Stop-------------------")

	time.Sleep(time.Second)
	wp.StartAll()
	time.Sleep(time.Second * 5)
	wp.StopAll()
	wp.StartAll()
	wp.StopAll()
}