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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
//
// rhimportd
//
// The Radio Helsinki Rivendell Import Daemon
//
//
// Copyright (C) 2015 Christian Pointner <equinox@helsinki.at>
//
// This file is part of rhimportd.
//
// rhimportd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// rhimportd is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with rhimportd. If not, see <http://www.gnu.org/licenses/>.
//
package rhimport
import (
"encoding/base32"
"fmt"
"github.com/satori/go.uuid"
"strings"
)
type Session struct {
ImportContext
ImportResult
// TODO: add creation time for timeout
}
type ProgressData struct {
step int
step_name string
progress float64
userdata interface{}
}
type newSessionResponse struct {
id string
err error
}
type newSessionRequest struct {
ctx ImportContext
response chan newSessionResponse
}
type sessionRunResponse struct {
err error
}
type sessionRunRequest struct {
user string
id string
response chan<- sessionRunResponse
}
type sessionAddProgressHandlerResponse struct {
err error
}
type sessionAddProgressHandlerRequest struct {
user string
id string
userdata interface{}
handler chan<- ProgressData
response chan<- sessionAddProgressHandlerResponse
}
type sessionAddDoneHandlerResponse struct {
err error
}
type sessionAddDoneHandlerRequest struct {
user string
id string
userdata interface{}
handler chan<- ImportResult
response chan<- sessionAddDoneHandlerResponse
}
type sessionRemoveResponse struct {
err error
}
type sessionRemoveRequest struct {
user string
id string
response chan sessionRemoveResponse
}
type SessionStore struct {
store map[string]map[string]Session
quit chan bool
done chan bool
newChan chan newSessionRequest
runChan chan sessionRunRequest
addProgressChan chan sessionAddProgressHandlerRequest
addDoneChan chan sessionAddDoneHandlerRequest
removeChan chan sessionRemoveRequest
}
func (self *SessionStore) Init() (err error) {
return
}
func (self *SessionStore) newSession(ctx ImportContext) (resp newSessionResponse) {
b := uuid.NewV4().Bytes()
resp.id = strings.ToLower(strings.TrimRight(base32.StdEncoding.EncodeToString(b), "="))
if _, exists := self.store[ctx.UserName]; !exists {
self.store[ctx.UserName] = make(map[string]Session)
}
self.store[ctx.UserName][resp.id] = Session{ImportContext: ctx}
rhdl.Printf("SessionStore: created session for '%s' -> %s", ctx.UserName, resp.id)
return
}
func (self *SessionStore) NewSession(ctx ImportContext) (string, error) {
res_ch := make(chan newSessionResponse)
req := newSessionRequest{}
req.ctx = ctx
req.response = res_ch
self.newChan <- req
res := <-res_ch
if res.err != nil {
return "", res.err
}
return res.id, nil
}
func (self *SessionStore) run(user, id string) (resp sessionRunResponse) {
if _, exists := self.store[user][id]; exists {
rhdl.Printf("SessionStore: running session '%s/%s'", user, id)
} else {
resp.err = fmt.Errorf("SessionStore: session '%s/%s' not found", user, id)
}
return
}
func (self *SessionStore) Run(user, id string) error {
res_ch := make(chan sessionRunResponse)
req := sessionRunRequest{}
req.user = user
req.id = id
req.response = res_ch
self.runChan <- req
res := <-res_ch
return res.err
}
func (self *SessionStore) addProgressHandler(user, id string, userdata interface{}, handler chan<- ProgressData) (resp sessionAddProgressHandlerResponse) {
if _, exists := self.store[user][id]; exists {
rhdl.Printf("SessionStore: adding progress handler to '%s/%s'", user, id)
} else {
resp.err = fmt.Errorf("SessionStore: session '%s/%s' not found", user, id)
}
return
}
func (self *SessionStore) AddProgressHandler(user, id string, userdata interface{}, handler chan<- ProgressData) error {
res_ch := make(chan sessionAddProgressHandlerResponse)
req := sessionAddProgressHandlerRequest{}
req.user = user
req.id = id
req.userdata = userdata
req.handler = handler
req.response = res_ch
self.addProgressChan <- req
res := <-res_ch
return res.err
}
func (self *SessionStore) addDoneHandler(user, id string, userdata interface{}, handler chan<- ImportResult) (resp sessionAddDoneHandlerResponse) {
if _, exists := self.store[user][id]; exists {
rhdl.Printf("SessionStore: adding done handler to '%s/%s'", user, id)
} else {
resp.err = fmt.Errorf("SessionStore: session '%s/%s' not found", user, id)
}
return
}
func (self *SessionStore) AddDoneHandler(user, id string, userdata interface{}, handler chan<- ImportResult) error {
res_ch := make(chan sessionAddDoneHandlerResponse)
req := sessionAddDoneHandlerRequest{}
req.user = user
req.id = id
req.userdata = userdata
req.handler = handler
req.response = res_ch
self.addDoneChan <- req
res := <-res_ch
return res.err
}
func (self *SessionStore) remove(user, id string) (resp sessionRemoveResponse) {
if _, exists := self.store[user][id]; exists {
delete(self.store[user], id)
rhdl.Printf("SessionStore: removed session '%s/%s'", user, id)
if _, exists := self.store[user]; exists {
if len(self.store[user]) == 0 {
delete(self.store, user)
rhdl.Printf("SessionStore: removed user '%s'", user)
}
}
} else {
resp.err = fmt.Errorf("SessionStore: session '%s/%s' not found", user, id)
}
return
}
func (self *SessionStore) Remove(user, id string) error {
res_ch := make(chan sessionRemoveResponse)
req := sessionRemoveRequest{}
req.user = user
req.id = id
req.response = res_ch
self.removeChan <- req
res := <-res_ch
return res.err
}
func (self *SessionStore) dispatchRequests() {
defer func() { self.done <- true }()
for {
select {
case <-self.quit:
return
case req := <-self.newChan:
req.response <- self.newSession(req.ctx)
case req := <-self.runChan:
req.response <- self.run(req.user, req.id)
case req := <-self.addProgressChan:
req.response <- self.addProgressHandler(req.user, req.id, req.userdata, req.handler)
case req := <-self.addDoneChan:
req.response <- self.addDoneHandler(req.user, req.id, req.userdata, req.handler)
case req := <-self.removeChan:
req.response <- self.remove(req.user, req.id)
}
}
}
func (self *SessionStore) Cleanup() {
self.quit <- true
<-self.done
close(self.quit)
close(self.done)
close(self.newChan)
close(self.runChan)
close(self.addProgressChan)
close(self.addDoneChan)
close(self.removeChan)
}
func NewSessionStore(conf *Config) (store *SessionStore, err error) {
store = new(SessionStore)
store.quit = make(chan bool)
store.done = make(chan bool)
store.store = make(map[string]map[string]Session)
store.newChan = make(chan newSessionRequest)
store.runChan = make(chan sessionRunRequest)
store.addProgressChan = make(chan sessionAddProgressHandlerRequest)
store.addDoneChan = make(chan sessionAddDoneHandlerRequest)
store.removeChan = make(chan sessionRemoveRequest)
go store.dispatchRequests()
return
}
|