-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorrent_client.go
95 lines (83 loc) · 2.35 KB
/
torrent_client.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
90
91
92
93
94
95
package main
import (
"time"
"github.com/anacrolix/torrent"
)
//AutoTorrent -
type AutoTorrent interface {
//guid byte array
StopTorrent()
StartTorrent()
}
//autotorrent implements the AutoTorrent interface
type atorrent struct {
GUID string
UpdateChan chan TMessage
Torrent *torrent.Torrent
StartTime int64
close bool
}
//TMessage -
type TMessage struct {
Name string `json:"name"`
GUID string `json:"guid"`
BytesTotal int64 `json:"bytetot"`
BytesComplete int64 `json:"bytecom"`
DownloadSpeed float32 `json:"down"`
Percent float32 `json:"pct"`
ConnectedPeers int `json:"peers"`
StartTime int64 `json:"st"`
LastUpdate int64 `json:"lastupdate"`
}
//NewAutoTorrent -
func NewAutoTorrent(guid string, torrent *torrent.Torrent, updateCh chan TMessage) AutoTorrent {
at := &atorrent{
GUID: guid,
UpdateChan: updateCh,
Torrent: torrent,
StartTime: time.Now().Unix(),
}
return at
}
func (at *atorrent) StopTorrent() {
at.Torrent.Drop()
at.close = true
}
func (at *atorrent) StartTorrent() {
<-at.Torrent.GotInfo()
at.Torrent.DownloadAll()
var (
lastDOwnload int64 = 0
totalSize int64 = at.Torrent.Info().TotalLength()
)
for at.Torrent.BytesMissing() != 0 && !at.close {
//inform update chan that new info is ready
at.UpdateChan <- TMessage{
Name: at.Torrent.Name(),
GUID: at.GUID,
BytesTotal: totalSize,
BytesComplete: at.Torrent.BytesCompleted(),
DownloadSpeed: float32(at.Torrent.BytesCompleted()-lastDOwnload) / 2000.0,
ConnectedPeers: at.Torrent.Stats().TotalPeers,
StartTime: at.StartTime,
LastUpdate: time.Now().Unix(),
Percent: 100.0 * float32(at.Torrent.BytesCompleted()) / float32(totalSize),
}
time.Sleep(2 * time.Second)
lastDOwnload = at.Torrent.BytesCompleted()
}
//should be 100%
at.UpdateChan <- TMessage{
Name: at.Torrent.Name(),
GUID: at.GUID,
BytesTotal: totalSize,
BytesComplete: at.Torrent.BytesCompleted(),
DownloadSpeed: float32(at.Torrent.BytesCompleted()-lastDOwnload) / 2000.0,
ConnectedPeers: at.Torrent.Stats().TotalPeers,
StartTime: at.StartTime,
LastUpdate: time.Now().Unix(),
Percent: 100.0 * float32(at.Torrent.BytesCompleted()) / float32(totalSize),
}
//do we stop or let it run?
//at.StopTorrent()
}