forked from hekmon/transmissionrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
110 lines (97 loc) · 2.52 KB
/
controller.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package transmissionrpc
import (
"errors"
"math/rand"
"net/http"
"net/url"
"sync"
"time"
cleanhttp "github.com/hashicorp/go-cleanhttp"
)
const (
// RPCVersion indicates the exact transmission RPC version this library is build against
RPCVersion = 17
defaultUserAgent = "github.com/hekmon/transmissionrpc"
)
// Config is the input data needed to make a connection to Transmission RPC.
type Config struct {
// UserAgent is set to this package's url if not provided here.
UserAgent string
// Client is set to a clean and isolated client if not provided
CustomClient *http.Client
}
// New returns an initialized and ready to use Controller
func New(transmissionRPCendpoint *url.URL, extra *Config) (c *Client, err error) {
// handle user input
if transmissionRPCendpoint == nil {
err = errors.New("please provide an Transmission RPC endpoint URL")
return
}
if extra != nil {
if extra.UserAgent == "" {
extra.UserAgent = defaultUserAgent
}
if extra.CustomClient == nil {
extra.CustomClient = cleanhttp.DefaultPooledClient()
}
} else {
extra = &Config{
UserAgent: defaultUserAgent,
CustomClient: cleanhttp.DefaultPooledClient(),
}
}
// Initialize & return ready to use client
c = &Client{
endpoint: *transmissionRPCendpoint,
http: extra.CustomClient,
userAgent: extra.UserAgent,
tagGenerator: rand.New(newLockedRandomSource(time.Now().Unix())),
}
return
}
// Client is the base object to interract with a remote transmission rpc endpoint.
// It must be created with New().
type Client struct {
// HTTP Client
endpoint url.URL
http *http.Client
userAgent string
// Transmission RPC protections
tagGenerator *rand.Rand
sessionID string
sessionIDAccess sync.RWMutex
}
func (c *Client) getRandomTag() int {
return c.tagGenerator.Int()
}
func (c *Client) getSessionID() string {
defer c.sessionIDAccess.RUnlock()
c.sessionIDAccess.RLock()
return c.sessionID
}
func (c *Client) updateSessionID(newID string) {
defer c.sessionIDAccess.Unlock()
c.sessionIDAccess.Lock()
c.sessionID = newID
}
// rand.NewSource is not thread-safe, so access should be serialized
type lockedRandomSource struct {
mut sync.Mutex
src rand.Source
}
func newLockedRandomSource(seed int64) rand.Source {
return &lockedRandomSource{
src: rand.NewSource(seed),
}
}
func (lrs *lockedRandomSource) Int63() (rnd int64) {
lrs.mut.Lock()
rnd = lrs.src.Int63()
lrs.mut.Unlock()
return
}
func (lrs *lockedRandomSource) Seed(seed int64) {
lrs.mut.Lock()
lrs.src.Seed(seed)
lrs.mut.Unlock()
}