diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-12-28 16:21:57 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-12-28 16:21:57 (GMT) |
commit | 60232a24d5501a2558888cbd9e8635edc545b8ef (patch) | |
tree | 60c02137db2f5034bae9c1bfab6e9bf3c722bb61 | |
parent | e40cf7868364567c2f6c05ddf1dfa3eff4804a79 (diff) |
refactoring of session, seperate public from private interface
-rw-r--r-- | src/helsinki.at/rhimport/session.go | 109 |
1 files changed, 56 insertions, 53 deletions
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 |