From 60232a24d5501a2558888cbd9e8635edc545b8ef Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 28 Dec 2015 17:21:57 +0100 Subject: refactoring of session, seperate public from private interface diff --git a/src/helsinki.at/rhimport/session.go b/src/helsinki.at/rhimport/session.go index 88648f7..71251f1 100644 --- a/src/helsinki.at/rhimport/session.go +++ b/src/helsinki.at/rhimport/session.go @@ -38,13 +38,6 @@ const ( SESSION_TIMEOUT ) -type SessionChan struct { - runChan chan<- time.Duration - cancelChan chan<- bool - addProgressChan chan<- sessionAddProgressHandlerRequest - addDoneChan chan<- sessionAddDoneHandlerRequest -} - type Session struct { ctx ImportContext state int @@ -138,13 +131,6 @@ func (self *Session) run(timeout time.Duration) { return } -func (self *SessionChan) Run(timeout time.Duration) { - select { - case self.runChan <- timeout: - default: // command is already pending or session is about to be closed/removed - } -} - func (self *Session) cancel() { rhdl.Println("Session: canceling running import") select { @@ -154,13 +140,6 @@ func (self *Session) cancel() { self.state = SESSION_CANCELED } -func (self *SessionChan) Cancel() { - select { - case self.cancelChan <- true: - default: // cancel is already pending or session is about to be closed/removed - } -} - func (self *Session) addProgressHandler(userdata interface{}, cb ImportProgressCB) (resp sessionAddProgressHandlerResponse) { if self.state != SESSION_NEW && self.state != SESSION_RUNNING { resp.err = fmt.Errorf("session is already done/canceled") @@ -169,22 +148,6 @@ func (self *Session) addProgressHandler(userdata interface{}, cb ImportProgressC 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 - select { - case self.addProgressChan <- req: - default: - return fmt.Errorf("session is about to be closed/removed") - } - - res := <-res_ch - return res.err -} - func (self *Session) addDoneHandler(userdata interface{}, cb func(ImportResult, interface{}) bool) (resp sessionAddDoneHandlerResponse) { if self.state != SESSION_NEW && self.state != SESSION_RUNNING { resp.err = fmt.Errorf("session is already done/canceled") @@ -203,22 +166,6 @@ func (self *Session) callProgressHandler(p *ProgressData) { } } -func (self *SessionChan) AddDoneHandler(userdata interface{}, cb func(ImportResult, interface{}) bool) error { - res_ch := make(chan sessionAddDoneHandlerResponse) - req := sessionAddDoneHandlerRequest{} - req.userdata = userdata - req.callback = cb - req.response = res_ch - select { - case self.addDoneChan <- req: - default: - return fmt.Errorf("session is about to be closed/removed") - } - - res := <-res_ch - return res.err -} - func (self *Session) callDoneHandler(r *ImportResult) { for _, cb := range self.doneCBs { if cb.cb != nil { @@ -275,6 +222,62 @@ func (self *Session) dispatchRequests() { } } +// ********************************************************* +// Public Interface + +type SessionChan struct { + runChan chan<- time.Duration + cancelChan chan<- bool + addProgressChan chan<- sessionAddProgressHandlerRequest + addDoneChan chan<- sessionAddDoneHandlerRequest +} + +func (self *SessionChan) Run(timeout time.Duration) { + select { + case self.runChan <- timeout: + default: // command is already pending or session is about to be closed/removed + } +} + +func (self *SessionChan) Cancel() { + select { + case self.cancelChan <- true: + default: // cancel is already pending or session is about to be closed/removed + } +} + +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 + select { + case self.addProgressChan <- req: + default: + return fmt.Errorf("session is about to be closed/removed") + } + + res := <-res_ch + return res.err +} + +func (self *SessionChan) AddDoneHandler(userdata interface{}, cb func(ImportResult, interface{}) bool) error { + res_ch := make(chan sessionAddDoneHandlerResponse) + req := sessionAddDoneHandlerRequest{} + req.userdata = userdata + req.callback = cb + req.response = res_ch + select { + case self.addDoneChan <- req: + default: + return fmt.Errorf("session is about to be closed/removed") + } + + res := <-res_ch + return res.err +} + func (self *Session) getInterface() *SessionChan { ch := &SessionChan{} ch.runChan = self.runChan -- cgit v0.10.2