-
Notifications
You must be signed in to change notification settings - Fork 3
/
pause.go
95 lines (81 loc) · 1.65 KB
/
pause.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 libtorrent
import (
"reflect"
"time"
"github.com/anacrolix/torrent"
)
func Pause() {
mu.Lock()
defer mu.Unlock()
if pause == nil {
pause = make(map[*torrent.Torrent]int32)
}
for _, t := range torrents {
if _, ok := pause[t]; ok {
continue
}
s := torrentStatus(t)
switch s {
case StatusPaused:
// ignore
case StatusChecking:
// ignore
default:
delete(queue, t)
stopTorrent(t)
pause[t] = s
}
}
mappingStop()
}
func Resume() {
mu.Lock()
defer mu.Unlock()
// every time application call resume() means network configuration changed.
// we need to check if network interfaces / local mapping port were updated. and restart port mapping if so.
ips := portList()
if !reflect.DeepEqual(mappingAddr, ips) {
mappingAddr = ips
lpdForce()
go func() {
mappingPort(1 * time.Second)
mappingStart()
}()
}
if pause == nil {
return
}
now := time.Now().UnixNano()
// at first resume active
for t, s := range pause {
switch s {
case StatusQueued:
default:
if !queueStart(t) { // problem starting? unable to report error. queue it manually.
queue[t] = now
}
}
}
// second run resume queued
for t, s := range pause {
switch s {
case StatusQueued:
// user can remove active torrents from queue while paused.
// so we may still have slots available after 'resume active' step. start until we full.
if len(active) < ActiveCount {
if !startTorrent(t) { // problem starting? unable to report error. queue it manually.
queue[t] = now
}
} else {
queue[t] = now
}
default:
}
}
pause = nil
}
func Paused() bool {
mu.Lock()
defer mu.Unlock()
return pause != nil
}