2015-08-23 03:20:58 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
)
|
|
|
|
|
|
|
|
type Handler interface{
|
|
|
|
Handle(transition Transition)
|
2015-08-24 05:28:45 +00:00
|
|
|
Stop() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGenericHandler(input chan Transition, proxy Handler) *GenericHandler {
|
|
|
|
handler := &GenericHandler{
|
|
|
|
input,
|
|
|
|
make(chan bool, 0),
|
|
|
|
make(chan bool, 0),
|
|
|
|
proxy,
|
|
|
|
}
|
|
|
|
go handler.run()
|
|
|
|
return handler
|
2015-08-23 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GenericHandler struct {
|
|
|
|
Channel chan Transition
|
|
|
|
killswitch chan bool
|
2015-08-24 05:28:45 +00:00
|
|
|
killresponse chan bool
|
|
|
|
proxy Handler
|
2015-08-23 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 05:28:45 +00:00
|
|
|
func (this *GenericHandler) Stop() bool {
|
|
|
|
this.killswitch<- true
|
|
|
|
return <-this.killresponse
|
2015-08-23 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 05:28:45 +00:00
|
|
|
func (this *GenericHandler) run() {
|
2015-08-23 03:20:58 +00:00
|
|
|
for true {
|
|
|
|
var transition Transition
|
|
|
|
select {
|
|
|
|
case <-this.killswitch:
|
2015-08-24 05:28:45 +00:00
|
|
|
this.killresponse<- this.proxy.Stop()
|
2015-08-23 03:20:58 +00:00
|
|
|
return
|
|
|
|
case transition = <-this.Channel:
|
2015-08-24 05:28:45 +00:00
|
|
|
this.proxy.Handle(transition)
|
2015-08-23 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|