diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-12-20 01:29:13 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-12-20 01:29:13 (GMT) |
commit | 8fc2a9c93d566feae576c1421b3c64dabc4b4976 (patch) | |
tree | 2fcef9fdc3fa620d44989985b1878b807dbe456f /src | |
parent | 9f860e6f74791e103aed25a006dee55d7b3822b5 (diff) |
improved type safety
Diffstat (limited to 'src')
-rw-r--r-- | src/helsinki.at/rhimport/fetcher.go | 11 | ||||
-rw-r--r-- | src/helsinki.at/rhimport/importer.go | 55 | ||||
-rw-r--r-- | src/helsinki.at/rhimport/rddb.go | 28 | ||||
-rw-r--r-- | src/helsinki.at/rhimport/session.go | 43 | ||||
-rw-r--r-- | src/helsinki.at/rhimportd/ctrlTelnet.go | 8 | ||||
-rw-r--r-- | src/helsinki.at/rhimportd/ctrlWeb.go | 8 | ||||
-rw-r--r-- | src/helsinki.at/rhimportd/ctrlWebSimple.go | 4 | ||||
-rw-r--r-- | src/helsinki.at/rhimportd/main.go | 4 |
8 files changed, 95 insertions, 66 deletions
diff --git a/src/helsinki.at/rhimport/fetcher.go b/src/helsinki.at/rhimport/fetcher.go index 300abef..a8ac22c 100644 --- a/src/helsinki.at/rhimport/fetcher.go +++ b/src/helsinki.at/rhimport/fetcher.go @@ -101,7 +101,7 @@ func FetchFileCurl(ctx *ImportContext, res *FetchResult, uri *url.URL) (err erro cbdata := &FetcherCurlCBData{remotename: path.Base(uri.Path)} defer cbdata.Cleanup() - if cbdata.basepath, err = ioutil.TempDir(ctx.Config.TempDir, "rhimportd-"); err != nil { + if cbdata.basepath, err = ioutil.TempDir(ctx.conf.TempDir, "rhimportd-"); err != nil { return } @@ -142,7 +142,7 @@ func FetchFileLocal(ctx *ImportContext, res *FetchResult, uri *url.URL) (err err ctx.ProgressCallBack(1, "fetching", 1.0, ctx.ProgressCallBackData) } - ctx.SourceFile = filepath.Join(ctx.Config.LocalFetchDir, path.Clean("/"+uri.Path)) + ctx.SourceFile = filepath.Join(ctx.conf.LocalFetchDir, path.Clean("/"+uri.Path)) var src *os.File if src, err = os.Open(ctx.SourceFile); err != nil { res.ResponseCode = http.StatusBadRequest @@ -194,13 +194,14 @@ func checkPassword(ctx *ImportContext, result *FetchResult) (err error) { cached := true for { + res_ch := make(chan getPasswordResult) req := getPasswordRequest{} req.username = ctx.UserName req.cached = cached - req.response = make(chan getPasswordResult) - ctx.RdDb.getPasswordChan <- req + req.response = res_ch + ctx.rddb.getPasswordChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { return res.err } diff --git a/src/helsinki.at/rhimport/importer.go b/src/helsinki.at/rhimport/importer.go index 1404259..f65515b 100644 --- a/src/helsinki.at/rhimport/importer.go +++ b/src/helsinki.at/rhimport/importer.go @@ -40,8 +40,8 @@ var ( ) type ImportContext struct { - *Config - *RdDb + conf *Config + rddb *RdDbChan UserName string Password string Trusted bool @@ -63,10 +63,10 @@ type ImportContext struct { ProgressCallBackData interface{} } -func NewImportContext(conf *Config, rddb *RdDb, user string) *ImportContext { +func NewImportContext(conf *Config, rddb *RdDbChan, user string) *ImportContext { ctx := new(ImportContext) - ctx.Config = conf - ctx.RdDb = rddb + ctx.conf = conf + ctx.rddb = rddb ctx.UserName = user ctx.Password = "" ctx.Trusted = false @@ -121,13 +121,14 @@ func (ctx *ImportContext) SanityCheck() error { } func (ctx *ImportContext) getPassword(cached bool) (err error) { + res_ch := make(chan getPasswordResult) req := getPasswordRequest{} req.username = ctx.UserName req.cached = cached - req.response = make(chan getPasswordResult) - ctx.RdDb.getPasswordChan <- req + req.response = res_ch + ctx.rddb.getPasswordChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { return res.err } @@ -136,12 +137,13 @@ func (ctx *ImportContext) getPassword(cached bool) (err error) { } func (ctx *ImportContext) getGroupOfCart() error { + res_ch := make(chan getGroupOfCartResult) req := getGroupOfCartRequest{} req.cart = ctx.Cart - req.response = make(chan getGroupOfCartResult) - ctx.RdDb.getGroupOfCartChan <- req + req.response = res_ch + ctx.rddb.getGroupOfCartChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { return res.err } @@ -150,12 +152,13 @@ func (ctx *ImportContext) getGroupOfCart() error { } func (ctx *ImportContext) getShowInfo() (carts []uint, err error) { + res_ch := make(chan getShowInfoResult) req := getShowInfoRequest{} req.showid = ctx.ShowId - req.response = make(chan getShowInfoResult) - ctx.RdDb.getShowInfoChan <- req + req.response = res_ch + ctx.rddb.getShowInfoChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { err = res.err return @@ -170,12 +173,13 @@ func (ctx *ImportContext) getShowInfo() (carts []uint, err error) { } func (ctx *ImportContext) checkMusicGroup() (bool, error) { + res_ch := make(chan checkMusicGroupResult) req := checkMusicGroupRequest{} req.group = ctx.GroupName - req.response = make(chan checkMusicGroupResult) - ctx.RdDb.checkMusicGroupChan <- req + req.response = res_ch + ctx.rddb.checkMusicGroupChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { return false, res.err } @@ -183,12 +187,13 @@ func (ctx *ImportContext) checkMusicGroup() (bool, error) { } func (ctx *ImportContext) getMusicInfo() (err error) { + res_ch := make(chan getMusicInfoResult) req := getMusicInfoRequest{} req.group = ctx.GroupName - req.response = make(chan getMusicInfoResult) - ctx.RdDb.getMusicInfoChan <- req + req.response = res_ch + ctx.rddb.getMusicInfoChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { return res.err } @@ -251,7 +256,7 @@ func add_cart(ctx *ImportContext, res *ImportResult) (err error) { w.Close() var resp *http.Response - if resp, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { + if resp, err = send_post_request(ctx.conf.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { return } defer resp.Body.Close() @@ -296,7 +301,7 @@ func add_cut(ctx *ImportContext, res *ImportResult) (err error) { w.Close() var resp *http.Response - if resp, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { + if resp, err = send_post_request(ctx.conf.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { return } defer resp.Body.Close() @@ -343,7 +348,7 @@ func remove_cart(ctx *ImportContext, res *ImportResult) (err error) { w.Close() var resp *http.Response - if resp, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { + if resp, err = send_post_request(ctx.conf.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { return } defer resp.Body.Close() @@ -380,7 +385,7 @@ func remove_cut(ctx *ImportContext, res *ImportResult) (err error) { w.Close() var resp *http.Response - if resp, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { + if resp, err = send_post_request(ctx.conf.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { return } defer resp.Body.Close() @@ -455,7 +460,7 @@ func import_audio(ctx *ImportContext, res *ImportResult) (err error) { if easy != nil { defer easy.Cleanup() - easy.Setopt(curl.OPT_URL, ctx.Config.RDXportEndpoint) + easy.Setopt(curl.OPT_URL, ctx.conf.RDXportEndpoint) easy.Setopt(curl.OPT_POST, true) var form *curl.Form diff --git a/src/helsinki.at/rhimport/rddb.go b/src/helsinki.at/rhimport/rddb.go index 25ef876..f596171 100644 --- a/src/helsinki.at/rhimport/rddb.go +++ b/src/helsinki.at/rhimport/rddb.go @@ -49,7 +49,7 @@ type getPasswordResult struct { type getPasswordRequest struct { username string cached bool - response chan getPasswordResult + response chan<- getPasswordResult } type getGroupOfCartResult struct { @@ -59,7 +59,7 @@ type getGroupOfCartResult struct { type getGroupOfCartRequest struct { cart uint - response chan getGroupOfCartResult + response chan<- getGroupOfCartResult } type getShowInfoResult struct { @@ -73,7 +73,7 @@ type getShowInfoResult struct { type getShowInfoRequest struct { showid uint - response chan getShowInfoResult + response chan<- getShowInfoResult } type checkMusicGroupResult struct { @@ -83,7 +83,7 @@ type checkMusicGroupResult struct { type checkMusicGroupRequest struct { group string - response chan checkMusicGroupResult + response chan<- checkMusicGroupResult } type getMusicInfoResult struct { @@ -94,7 +94,15 @@ type getMusicInfoResult struct { type getMusicInfoRequest struct { group string - response chan getMusicInfoResult + response chan<- getMusicInfoResult +} + +type RdDbChan struct { + getPasswordChan chan<- getPasswordRequest + getGroupOfCartChan chan<- getGroupOfCartRequest + getShowInfoChan chan<- getShowInfoRequest + checkMusicGroupChan chan<- checkMusicGroupRequest + getMusicInfoChan chan<- getMusicInfoRequest } type RdDb struct { @@ -289,6 +297,16 @@ func (self *RdDb) dispatchRequests() { } } +func (self *RdDb) GetChannels() *RdDbChan { + ch := &RdDbChan{} + ch.getPasswordChan = self.getPasswordChan + ch.getGroupOfCartChan = self.getGroupOfCartChan + ch.getShowInfoChan = self.getShowInfoChan + ch.checkMusicGroupChan = self.checkMusicGroupChan + ch.getMusicInfoChan = self.getMusicInfoChan + return ch +} + func (self *RdDb) Cleanup() { self.quit <- true <-self.done diff --git a/src/helsinki.at/rhimport/session.go b/src/helsinki.at/rhimport/session.go index 219c233..718d13e 100644 --- a/src/helsinki.at/rhimport/session.go +++ b/src/helsinki.at/rhimport/session.go @@ -61,7 +61,7 @@ type sessionRunResponse struct { type sessionRunRequest struct { user string id string - response chan sessionRunResponse + response chan<- sessionRunResponse } type sessionAddProgressHandlerResponse struct { @@ -72,8 +72,8 @@ type sessionAddProgressHandlerRequest struct { user string id string userdata interface{} - handler chan ProgressData - response chan sessionAddProgressHandlerResponse + handler chan<- ProgressData + response chan<- sessionAddProgressHandlerResponse } type sessionAddDoneHandlerResponse struct { @@ -84,8 +84,8 @@ type sessionAddDoneHandlerRequest struct { user string id string userdata interface{} - handler chan ImportResult - response chan sessionAddDoneHandlerResponse + handler chan<- ImportResult + response chan<- sessionAddDoneHandlerResponse } type sessionRemoveResponse struct { @@ -125,12 +125,13 @@ func (self *SessionStore) newSession(ctx ImportContext) (resp newSessionResponse } func (self *SessionStore) NewSession(ctx ImportContext) (string, error) { + res_ch := make(chan newSessionResponse) req := newSessionRequest{} req.ctx = ctx - req.response = make(chan newSessionResponse) + req.response = res_ch self.newChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { return "", res.err } @@ -147,17 +148,18 @@ func (self *SessionStore) run(user, id string) (resp sessionRunResponse) { } func (self *SessionStore) Run(user, id string) error { + res_ch := make(chan sessionRunResponse) req := sessionRunRequest{} req.user = user req.id = id - req.response = make(chan sessionRunResponse) + req.response = res_ch self.runChan <- req - res := <-req.response + res := <-res_ch return res.err } -func (self *SessionStore) addProgressHandler(user, id string, userdata interface{}, handler chan ProgressData) (resp sessionAddProgressHandlerResponse) { +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 { @@ -166,20 +168,21 @@ func (self *SessionStore) addProgressHandler(user, id string, userdata interface return } -func (self *SessionStore) AddProgressHandler(user, id string, userdata interface{}, handler chan ProgressData) error { +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 = make(chan sessionAddProgressHandlerResponse) + req.response = res_ch self.addProgressChan <- req - res := <-req.response + res := <-res_ch return res.err } -func (self *SessionStore) addDoneHandler(user, id string, userdata interface{}, handler chan ImportResult) (resp sessionAddDoneHandlerResponse) { +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 { @@ -188,16 +191,17 @@ func (self *SessionStore) addDoneHandler(user, id string, userdata interface{}, return } -func (self *SessionStore) AddDoneHandler(user, id string, userdata interface{}, handler chan ImportResult) error { +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 = make(chan sessionAddDoneHandlerResponse) + req.response = res_ch self.addDoneChan <- req - res := <-req.response + res := <-res_ch return res.err } @@ -218,13 +222,14 @@ func (self *SessionStore) remove(user, id string) (resp sessionRemoveResponse) { } func (self *SessionStore) Remove(user, id string) error { + res_ch := make(chan sessionRemoveResponse) req := sessionRemoveRequest{} req.user = user req.id = id - req.response = make(chan sessionRemoveResponse) + req.response = res_ch self.removeChan <- req - res := <-req.response + res := <-res_ch return res.err } diff --git a/src/helsinki.at/rhimportd/ctrlTelnet.go b/src/helsinki.at/rhimportd/ctrlTelnet.go index 49971e1..04d90da 100644 --- a/src/helsinki.at/rhimportd/ctrlTelnet.go +++ b/src/helsinki.at/rhimportd/ctrlTelnet.go @@ -43,7 +43,7 @@ type TelnetClient struct { scanner *bufio.Scanner writer *bufio.Writer conf *rhimport.Config - rddb *rhimport.RdDb + rddb *rhimport.RdDbChan ctx *rhimport.ImportContext } @@ -148,7 +148,7 @@ func (c *TelnetClient) handle_cmd_set(args []string) { } if c.ctx == nil { c.ctx = rhimport.NewImportContext(c.conf, c.rddb, "") - c.ctx.Trusted = false + c.ctx.Trusted = true } switch strings.ToLower(args[0]) { case "username": @@ -365,7 +365,7 @@ func (c *TelnetClient) handle() { } } -func newTelnetClient(conn net.Conn, conf *rhimport.Config, rddb *rhimport.RdDb) (c *TelnetClient) { +func newTelnetClient(conn net.Conn, conf *rhimport.Config, rddb *rhimport.RdDbChan) (c *TelnetClient) { rhl.Println("telnet-ctrl: new client from:", conn.RemoteAddr()) c = &TelnetClient{} c.conn = conn @@ -377,7 +377,7 @@ func newTelnetClient(conn net.Conn, conf *rhimport.Config, rddb *rhimport.RdDb) return c } -func StartControlTelnet(addr_s string, conf *rhimport.Config, rddb *rhimport.RdDb) { +func StartControlTelnet(addr_s string, conf *rhimport.Config, rddb *rhimport.RdDbChan) { rhl.Println("telnet-ctrl: listening on", addr_s) server, err := net.Listen("tcp", addr_s) diff --git a/src/helsinki.at/rhimportd/ctrlWeb.go b/src/helsinki.at/rhimportd/ctrlWeb.go index 8aff492..6b6568d 100644 --- a/src/helsinki.at/rhimportd/ctrlWeb.go +++ b/src/helsinki.at/rhimportd/ctrlWeb.go @@ -32,16 +32,16 @@ import ( type webHandler struct { *rhimport.Config - *rhimport.RdDb + *rhimport.RdDbChan trusted bool - H func(*rhimport.Config, *rhimport.RdDb, bool, http.ResponseWriter, *http.Request) + H func(*rhimport.Config, *rhimport.RdDbChan, bool, http.ResponseWriter, *http.Request) } func (self webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - self.H(self.Config, self.RdDb, self.trusted, w, r) + self.H(self.Config, self.RdDbChan, self.trusted, w, r) } -func StartControlWeb(addr_s string, conf *rhimport.Config, rddb *rhimport.RdDb) { +func StartControlWeb(addr_s string, conf *rhimport.Config, rddb *rhimport.RdDbChan) { http.Handle("/public/simple", webHandler{conf, rddb, false, webSimpleHandler}) http.Handle("/trusted/simple", webHandler{conf, rddb, true, webSimpleHandler}) diff --git a/src/helsinki.at/rhimportd/ctrlWebSimple.go b/src/helsinki.at/rhimportd/ctrlWebSimple.go index da240a8..8ea5ac3 100644 --- a/src/helsinki.at/rhimportd/ctrlWebSimple.go +++ b/src/helsinki.at/rhimportd/ctrlWebSimple.go @@ -91,7 +91,7 @@ func webSimpleResponse(w http.ResponseWriter, result *rhimport.ImportResult) { encoder.Encode(respdata) } -func webSimpleParseRequest(conf *rhimport.Config, rddb *rhimport.RdDb, trusted bool, r *http.Request) (ctx *rhimport.ImportContext, err error) { +func webSimpleParseRequest(conf *rhimport.Config, rddb *rhimport.RdDbChan, trusted bool, r *http.Request) (ctx *rhimport.ImportContext, err error) { decoder := json.NewDecoder(r.Body) reqdata := newWebSimpleRequestData(conf) @@ -121,7 +121,7 @@ func webSimpleParseRequest(conf *rhimport.Config, rddb *rhimport.RdDb, trusted b return } -func webSimpleHandler(conf *rhimport.Config, rddb *rhimport.RdDb, trusted bool, w http.ResponseWriter, r *http.Request) { +func webSimpleHandler(conf *rhimport.Config, rddb *rhimport.RdDbChan, trusted bool, w http.ResponseWriter, r *http.Request) { rhdl.Printf("SimpleHandler: request for '%s'", html.EscapeString(r.URL.Path)) var ctx *rhimport.ImportContext diff --git a/src/helsinki.at/rhimportd/main.go b/src/helsinki.at/rhimportd/main.go index b6ac68b..d599cd5 100644 --- a/src/helsinki.at/rhimportd/main.go +++ b/src/helsinki.at/rhimportd/main.go @@ -82,7 +82,7 @@ func main() { go func() { defer wg.Done() rhl.Println("starting web-ctrl") - StartControlWeb(*web_addr_s, conf, rddb) + StartControlWeb(*web_addr_s, conf, rddb.GetChannels()) rhl.Println("web-ctrl finished") }() @@ -90,7 +90,7 @@ func main() { go func() { defer wg.Done() rhl.Println("starting telnet-ctrl") - StartControlTelnet(*telnet_addr_s, conf, rddb) + StartControlTelnet(*telnet_addr_s, conf, rddb.GetChannels()) rhl.Println("telnet-ctrl finished") }() |