-
Notifications
You must be signed in to change notification settings - Fork 119
/
main.go
81 lines (73 loc) · 1.74 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
package main
import (
"flag"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)
// Exit statuses.
const (
_ = iota
exitNoTorrentProvided
exitErrorInClient
)
func main() {
// Parse flags.
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(cfg)
if err != nil {
log.Fatalf(err.Error())
os.Exit(exitErrorInClient)
}
// Http handler.
go func() {
http.HandleFunc("/", client.GetFile)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(cfg.Port), nil))
}()
// Open selected video player
if *player != "" {
go func() {
for !client.ReadyForPlayback() {
time.Sleep(time.Second)
}
openPlayer(*player, cfg.Port)
}()
}
// Handle exit signals.
interruptChannel := make(chan os.Signal, 1)
signal.Notify(interruptChannel,
os.Interrupt,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func(interruptChannel chan os.Signal) {
for range interruptChannel {
log.Println("Exiting...")
client.Close()
os.Exit(0)
}
}(interruptChannel)
// Cli render loop.
for {
client.Render()
time.Sleep(time.Second)
}
}