-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
89 lines (81 loc) · 2.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"os"
"sync"
"github.com/concurrency-8/args"
"github.com/concurrency-8/torrent"
"github.com/sethgrid/multibar"
)
func main() {
var wait sync.WaitGroup
downloadpath := ""
var errormsg = `Usage of ./main:
--help
Print this help message and exit.
--download -d
Specify the download path for downloading the files.
--rescap -rc
True if pause and resume feature is needed. False otherwise.
--resume -r
True to resume partially downloaded files.
--files [path] [path] ...
List of Torrent Files
Sample input:
./concurrency-8 --files File1 File2 File3 -v -d ../../`
l := len(os.Args)
if l == 1 || os.Args[1] == "--help" {
fmt.Println(errormsg)
return
}
files := make([]string, 0)
filesflag := false
resumeflag := false
rcflag := false
verboseflag := false
for i := 1; i < l; i++ {
arg := os.Args[i]
if filesflag == true && arg[0] != '-' {
files = append(files, arg)
} else {
filesflag = false
if arg == "--resume" || arg == "-r" {
resumeflag = true
} else if arg == "--verbose" || arg == "-v" {
verboseflag = true
} else if arg == "--rescap" || arg == "-rc" {
rcflag = true
} else if arg == "--download" || arg == "-d" {
downloadpath = os.Args[i+1]
}
}
if arg == "--files" {
filesflag = true
}
}
//Parsing CLI arguments complete.
args.ARGS.FilePath = files
args.ARGS.Resume = resumeflag
args.ARGS.Verbose = verboseflag
args.ARGS.DownloadPath = downloadpath
args.ARGS.ResumeCapability = rcflag
wait.Add(len(files))
ports := make([]int, len(files))
//start peer ports from 20000. There's actually no restriction on the port numbers.
//According to the specification, initially tracker UDP has ports from 6881-6889
//Note that some of these ports may already be in use by the OS, in that case, Download fails.
progressbars, _ := multibar.New()
ports[0] = 20000
for i, file := range files {
if ports[i] == 0 {
ports[i] = ports[i-1] + 1
}
bar := progressbars.MakeBar(100, file)
go func(file string, port int) {
torrent.DownloadFromFile(file, port, &bar)
defer wait.Done()
}(file, ports[i])
}
go progressbars.Listen()
wait.Wait()
}