...

Source file src/github.com/concurrency-8/main.go

Documentation: github.com/concurrency-8

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

View as plain text