Skip to content

Commit

Permalink
Use struct for client configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
nochso committed Jul 16, 2016
1 parent 8d2a864 commit f6e26b6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
53 changes: 36 additions & 17 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,58 +39,77 @@ type Client struct {
Torrent *torrent.Torrent
Progress int64
Uploaded int64
Port int
Seed bool
Config ClientConfig
}

// ClientConfig specifies the behaviour of a client.
type ClientConfig struct {
TorrentPath string
Port int
TorrentPort int
Seed bool
TCP bool
MaxConnections int
}

// NewClientConfig creates a new default configuration.
func NewClientConfig() ClientConfig {
return ClientConfig{
Port: 8080,
TorrentPort: 50007,
Seed: false,
TCP: true,
MaxConnections: 200,
}
}

// NewClient creates a new torrent client based on a magnet or a torrent file.
// If the torrent file is on http, we try downloading it.
func NewClient(torrentPath string, port int, torrentPort int, seed bool, tcp bool, maxConnections int) (client Client, err error) {
func NewClient(cfg ClientConfig) (client Client, err error) {
var t *torrent.Torrent
var c *torrent.Client

client.Port = port
client.Config = cfg

// Create client.
c, err = torrent.NewClient(&torrent.Config{
DataDir: os.TempDir(),
NoUpload: !seed,
Seed: seed,
DisableTCP: !tcp,
ListenAddr: fmt.Sprintf(":%d", torrentPort),
NoUpload: !cfg.Seed,
Seed: cfg.Seed,
DisableTCP: !cfg.TCP,
ListenAddr: fmt.Sprintf(":%d", cfg.TorrentPort),
})

if err != nil {
return client, ClientError{Type: "creating torrent client", Origin: err}
}

client.Client = c
client.Seed = seed

// Add torrent.

// Add as magnet url.
if strings.HasPrefix(torrentPath, "magnet:") {
if t, err = c.AddMagnet(torrentPath); err != nil {
if strings.HasPrefix(cfg.TorrentPath, "magnet:") {
if t, err = c.AddMagnet(cfg.TorrentPath); err != nil {
return client, ClientError{Type: "adding torrent", Origin: err}
}
} else {
// Otherwise add as a torrent file.

// If it's online, we try downloading the file.
if isHTTP.MatchString(torrentPath) {
if torrentPath, err = downloadFile(torrentPath); err != nil {
if isHTTP.MatchString(cfg.TorrentPath) {
if cfg.TorrentPath, err = downloadFile(cfg.TorrentPath); err != nil {
return client, ClientError{Type: "downloading torrent file", Origin: err}
}
}

if t, err = c.AddTorrentFromFile(torrentPath); err != nil {
if t, err = c.AddTorrentFromFile(cfg.TorrentPath); err != nil {
return client, ClientError{Type: "adding torrent to the client", Origin: err}
}
}

client.Torrent = t
client.Torrent.SetMaxEstablishedConns(maxConnections)
client.Torrent.SetMaxEstablishedConns(cfg.MaxConnections)

go func() {
<-t.GotInfo()
Expand Down Expand Up @@ -184,15 +203,15 @@ func (c *Client) Render() {
fmt.Println(t.Info().Name)
fmt.Println(strings.Repeat("=", len(t.Info().Name)))
if c.ReadyForPlayback() {
fmt.Printf("Stream: \thttp://localhost:%d\n", c.Port)
fmt.Printf("Stream: \thttp://localhost:%d\n", c.Config.Port)
}
if currentProgress > 0 {
fmt.Printf("Progress: \t%s / %s %.2f%%\n", complete, size, c.percentage())
}
if currentProgress < t.Info().TotalLength() {
fmt.Printf("Download speed: %s\n", downloadSpeed)
}
if c.Seed {
if c.Config.Seed {
fmt.Printf("Upload speed: \t%s\n", uploadSpeed)
}
}
Expand Down
25 changes: 11 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,22 @@ const (

func main() {
// Parse flags.
var port, torrentPort int
var seed, tcp *bool
var player *string
var maxConnections int

player = flag.String("player", "", "Open the stream with a video player ("+joinPlayerNames()+")")
flag.IntVar(&port, "port", 8080, "Port to stream the video on")
flag.IntVar(&torrentPort, "torrent-port", 50007, "Port to listen for incoming torrent connections")
seed = flag.Bool("seed", false, "Seed after finished downloading")
flag.IntVar(&maxConnections, "conn", 200, "Maximum number of connections")
tcp = flag.Bool("tcp", true, "Allow connections via TCP")
player := flag.String("player", "", "Open the stream with a video player ("+joinPlayerNames()+")")
cfg := NewClientConfig()
flag.IntVar(&cfg.Port, "port", cfg.Port, "Port to stream the video on")
flag.IntVar(&cfg.TorrentPort, "torrent-port", cfg.TorrentPort, "Port to listen for incoming torrent connections")
flag.BoolVar(&cfg.Seed, "seed", cfg.Seed, "Seed after finished downloading")
flag.IntVar(&cfg.MaxConnections, "conn", cfg.MaxConnections, "Maximum number of connections")
flag.BoolVar(&cfg.TCP, "tcp", cfg.TCP, "Allow connections via TCP")
flag.Parse()
if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(exitNoTorrentProvided)
}
cfg.TorrentPath = flag.Arg(0)

// Start up the torrent client.
client, err := NewClient(flag.Arg(0), port, torrentPort, *seed, *tcp, maxConnections)
client, err := NewClient(cfg)
if err != nil {
log.Fatalf(err.Error())
os.Exit(exitErrorInClient)
Expand All @@ -47,7 +44,7 @@ func main() {
// Http handler.
go func() {
http.HandleFunc("/", client.GetFile)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), nil))
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(cfg.Port), nil))
}()

// Open selected video player
Expand All @@ -56,7 +53,7 @@ func main() {
for !client.ReadyForPlayback() {
time.Sleep(time.Second)
}
openPlayer(*player, port)
openPlayer(*player, cfg.Port)
}()
}

Expand Down

0 comments on commit f6e26b6

Please sign in to comment.