forked from jtakkala/tulva
/
main.go
61 lines (52 loc) · 1.31 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
// Copyright 2013 Jari Takkala. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"log"
"math/rand"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"time"
)
// Unique client ID, encoded as '-' + 'TV' + <version number> + random digits
var PeerID = [20]byte{'-', 'T', 'V', '0', '0', '0', '1'}
// init initializes a random PeerID for this client
func init() {
// Initialize PeerID
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 7; i < 20; i++ {
PeerID[i] = byte(r.Intn(256))
}
}
func main() {
if len(os.Args) != 2 {
log.Fatalf("Usage: %s: <torrent file>\n", os.Args[0])
}
quit := make(chan struct{})
t, err := NewTorrent(os.Args[1], quit)
if err != nil {
log.Fatal(err)
}
log.Println("main : main : Started")
defer log.Println("main : main : Exiting")
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
// Signal handler to catch Ctrl-C and SIGTERM from 'kill' command
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
// Block waiting for a signal
<-c
// Unregister the signal handler
signal.Stop(c)
log.Println("Received Interrupt. Shutting down...")
close(t.quit)
}()
// Launch the torrent
t.Run()
}