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
|
//
// 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 (
"net/http"
)
const (
SESSION_NEW = iota
SESSION_RUNNING
SESSION_CANCELED
SESSION_DONE
)
type SessionChan struct {
runChan chan<- bool
cancelChan chan<- bool
addProgressChan chan<- sessionAddProgressHandlerRequest
addDoneChan chan<- sessionAddDoneHandlerRequest
}
type Session struct {
ctx ImportContext
state int
done chan bool
cancelIntChan chan bool
progressIntChan chan ProgressData
doneIntChan chan ImportResult
runChan chan bool
cancelChan chan bool
addProgressChan chan sessionAddProgressHandlerRequest
addDoneChan chan sessionAddDoneHandlerRequest
// TODO: add pub/sub for progress and done
}
type ProgressData struct {
step int
step_name string
progress float64
}
type sessionAddProgressHandlerResponse struct {
err error
}
type sessionAddProgressHandlerRequest struct {
userdata interface{}
callback ImportProgressCB
response chan<- sessionAddProgressHandlerResponse
}
type sessionAddDoneHandlerResponse struct {
err error
}
type sessionAddDoneHandlerRequest struct {
userdata interface{}
callback func(*ImportResult, interface{})
response chan<- sessionAddDoneHandlerResponse
}
func session_progress_callback(step int, step_name string, progress float64, userdata interface{}) {
out := userdata.(chan<- ProgressData)
out <- ProgressData{step, step_name, progress}
}
func session_import_run(ctx ImportContext, done chan<- ImportResult) {
if err := ctx.SanityCheck(); err != nil {
done <- ImportResult{http.StatusBadRequest, err.Error(), 0, 0}
return
}
if res, err := FetchFile(&ctx); err != nil {
done <- ImportResult{http.StatusInternalServerError, err.Error(), 0, 0}
return
} else if res.ResponseCode != http.StatusOK {
done <- ImportResult{res.ResponseCode, res.ErrorString, 0, 0}
return
}
if res, err := ImportFile(&ctx); err != nil {
done <- ImportResult{http.StatusInternalServerError, err.Error(), 0, 0}
return
} else {
done <- *res
return
}
}
func (self *Session) run() {
self.ctx.ProgressCallBack = session_progress_callback
self.ctx.ProgressCallBackData = (chan<- ProgressData)(self.progressIntChan)
self.ctx.Cancel = self.cancelIntChan
go session_import_run(self.ctx, self.doneIntChan)
self.state = SESSION_RUNNING
return
}
func (self *SessionChan) Run() {
self.runChan <- true
}
func (self *SessionChan) Cancel() {
self.cancelChan <- true
}
func (self *Session) addProgressHandler(userdata interface{}, cb ImportProgressCB) (resp sessionAddProgressHandlerResponse) {
rhdl.Printf("Session: addProgressHandler called with: %+v", userdata)
return
}
func (self *SessionChan) AddProgressHandler(userdata interface{}, cb ImportProgressCB) error {
res_ch := make(chan sessionAddProgressHandlerResponse)
req := sessionAddProgressHandlerRequest{}
req.userdata = userdata
req.callback = cb
req.response = res_ch
self.addProgressChan <- req
res := <-res_ch
return res.err
}
func (self *Session) addDoneHandler(userdata interface{}, cb func(*ImportResult, interface{})) (resp sessionAddDoneHandlerResponse) {
rhdl.Printf("Session: addDoneHandler called with: %+v", userdata)
return
}
func (self *SessionChan) AddDoneHandler(userdata interface{}, cb func(*ImportResult, interface{})) error {
res_ch := make(chan sessionAddDoneHandlerResponse)
req := sessionAddDoneHandlerRequest{}
req.userdata = userdata
req.callback = cb
req.response = res_ch
self.addDoneChan <- req
res := <-res_ch
return res.err
}
func (self *Session) dispatchRequests() {
defer func() { self.done <- true }()
for {
select {
case <-self.runChan:
if self.state == SESSION_NEW {
self.run()
}
case <-self.cancelChan:
if self.state == SESSION_RUNNING {
rhdl.Println("Session: canceling running import")
select {
case self.cancelIntChan <- true:
default: // session got canceled already
}
} else {
return
}
case req := <-self.addProgressChan:
req.response <- self.addProgressHandler(req.userdata, req.callback)
case req := <-self.addDoneChan:
req.response <- self.addDoneHandler(req.userdata, req.callback)
case progress := <-self.progressIntChan:
rhdl.Printf("Session: got progress: %+v", progress)
// TODO: call all subscribed progress handler
case result := <-self.doneIntChan:
self.state = SESSION_DONE
rhdl.Printf("Session: import is done: %+v", result)
// TODO: call all subscribed done handler
}
}
}
func (self *Session) getInterface() *SessionChan {
ch := &SessionChan{}
ch.runChan = self.runChan
ch.cancelChan = self.cancelChan
ch.addProgressChan = self.addProgressChan
ch.addDoneChan = self.addDoneChan
return ch
}
func (self *Session) Cleanup() {
self.cancelChan <- true
rhdl.Printf("waiting for session to close")
<-self.done
close(self.done)
close(self.progressIntChan)
close(self.doneIntChan)
close(self.runChan)
close(self.cancelChan)
close(self.addProgressChan)
close(self.addDoneChan)
rhdl.Printf("session is now cleaned up")
}
func NewSession(ctx *ImportContext) (session *Session) {
session = new(Session)
session.state = SESSION_NEW
session.ctx = *ctx
session.done = make(chan bool)
session.cancelIntChan = make(chan bool, 1)
session.progressIntChan = make(chan ProgressData)
session.doneIntChan = make(chan ImportResult)
session.runChan = make(chan bool)
session.cancelChan = make(chan bool)
session.addProgressChan = make(chan sessionAddProgressHandlerRequest)
session.addDoneChan = make(chan sessionAddDoneHandlerRequest)
go session.dispatchRequests()
return
}
|