forked from scakemyer/quasar
-
Notifications
You must be signed in to change notification settings - Fork 43
/
jsonrpc.go
212 lines (175 loc) · 4.35 KB
/
jsonrpc.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package xbmc
import (
"errors"
"fmt"
"net"
"time"
"github.com/elgatito/elementum/jsonrpc"
"github.com/gin-gonic/gin"
"github.com/anacrolix/sync"
)
// Args ...
type Args []interface{}
// Object ...
type Object map[string]interface{}
// Results ...
var Results map[string]chan interface{}
var (
XBMCLocalHost *XBMCHost = nil
XBMCHosts = []*XBMCHost{}
// XBMCJSONRPCPort is a port for XBMCJSONRPC (RCP of Kodi)
XBMCJSONRPCPort = "9090"
// XBMCExJSONRPCPort is a port for XBMCExJSONRPC (RCP of python part of the plugin)
XBMCExJSONRPCPort = "65221"
mu sync.RWMutex
)
func Init() {
mu.Lock()
defer mu.Unlock()
XBMCHosts = []*XBMCHost{}
for _, host := range []string{
"::1",
"127.0.0.1",
} {
if conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, XBMCJSONRPCPort), time.Second*3); err == nil {
XBMCLocalHost = &XBMCHost{host}
XBMCHosts = append(XBMCHosts, XBMCLocalHost)
conn.Close()
log.Debugf("Adding local host %s", host)
} else {
log.Debugf("Could not connect to the host %s: %s", host, err)
}
}
}
func ContainsXBMCHost(host string) bool {
mu.RLock()
defer mu.RUnlock()
for _, h := range XBMCHosts {
if h.Host == host {
return true
}
}
return false
}
func AddLocalXBMCHost(host string) (*XBMCHost, error) {
h, err := AddXBMCHost(host)
XBMCLocalHost = h
return h, err
}
func AddXBMCHost(host string) (*XBMCHost, error) {
mu.Lock()
defer mu.Unlock()
for _, h := range XBMCHosts {
if h != nil && h.Host == host {
return h, nil
}
}
h := &XBMCHost{host}
XBMCHosts = append(XBMCHosts, h)
return h, nil
}
func RemoveXBMCHost(host string) error {
mu.Lock()
defer mu.Unlock()
index := -1
for i, h := range XBMCHosts {
if h.Host == host {
index = i
break
}
}
if index > -1 {
XBMCHosts = append(XBMCHosts[:index], XBMCHosts[index+1:]...)
return nil
} else {
return fmt.Errorf("Could not find host '%s'", host)
}
}
func GetLocalXBMCHost() (*XBMCHost, error) {
mu.RLock()
defer mu.RUnlock()
if XBMCLocalHost != nil {
return XBMCLocalHost, nil
}
return nil, errors.New("No local XBMCHost found")
}
func GetXBMCHostWithContext(ctx *gin.Context) (*XBMCHost, error) {
// If request is not coming from addon's python part - then it can be a browser request or Kodi,
// so we communicate with any existing Kodi connection.
if ctx.GetHeader("User-Agent") != "plugin.video.elementum" {
return GetLocalXBMCHost()
}
return GetXBMCHost(ctx.ClientIP())
}
func GetXBMCHost(host string) (*XBMCHost, error) {
mu.RLock()
if host == "" {
mu.RUnlock()
return GetLocalXBMCHost()
}
for _, h := range XBMCHosts {
if h != nil && h.Host == host {
mu.RUnlock()
return h, nil
}
}
mu.RUnlock()
return AddXBMCHost(host)
}
func (h XBMCHost) getJSONConnection() (net.Conn, error) {
return net.DialTimeout("tcp", net.JoinHostPort(h.Host, XBMCJSONRPCPort), time.Second*5)
}
func (h XBMCHost) getExJSONConnection() (net.Conn, error) {
return net.DialTimeout("tcp", net.JoinHostPort(h.Host, XBMCExJSONRPCPort), time.Second*5)
}
func (h XBMCHost) executeJSONRPC(method string, retVal interface{}, args Args) error {
if args == nil {
args = Args{}
}
conn, err := h.getJSONConnection()
if err != nil {
log.Error(err)
log.Critical("No available JSON-RPC connection to Kodi")
return err
}
if conn != nil {
defer conn.Close()
client := jsonrpc.NewClient(conn)
return client.Call(method, args, retVal)
}
return errors.New("No available JSON-RPC connection to Kodi")
}
func (h XBMCHost) executeJSONRPCO(method string, retVal interface{}, args Object) error {
if args == nil {
args = Object{}
}
conn, err := h.getJSONConnection()
if err != nil {
log.Error(err)
log.Critical("No available JSON-RPC connection to Kodi")
return err
}
if conn != nil {
defer conn.Close()
client := jsonrpc.NewClient(conn)
return client.Call(method, args, retVal)
}
return errors.New("No available JSON-RPC connection to Kodi")
}
func (h XBMCHost) executeJSONRPCEx(method string, retVal interface{}, args Args) error {
if args == nil {
args = Args{}
}
conn, err := h.getExJSONConnection()
if err != nil {
log.Error(err)
log.Critical("No available JSON-RPC connection to the add-on")
return err
}
if conn != nil {
defer conn.Close()
client := jsonrpc.NewClient(conn)
return client.Call(method, args, retVal)
}
return errors.New("No available JSON-RPC connection to the add-on")
}