2015-08-23 03:20:58 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"fmt"
|
|
|
|
)
|
2015-08-21 17:27:26 +00:00
|
|
|
|
|
|
|
type Engine struct {
|
|
|
|
Input chan Result
|
2015-08-23 03:20:58 +00:00
|
|
|
handlers []*GenericHandler
|
2015-08-24 05:28:45 +00:00
|
|
|
checks []*GenericCheck
|
2015-08-23 03:20:58 +00:00
|
|
|
killswitch chan bool
|
2015-08-24 05:28:45 +00:00
|
|
|
killresponse chan bool
|
2015-08-21 17:27:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 03:20:58 +00:00
|
|
|
func NewEngine() *Engine {
|
2015-08-24 05:28:45 +00:00
|
|
|
engine := &Engine{
|
2015-08-23 03:20:58 +00:00
|
|
|
make(chan Result),
|
|
|
|
make([]*GenericHandler, 0),
|
2015-08-24 05:28:45 +00:00
|
|
|
make([]*GenericCheck, 0),
|
|
|
|
make(chan bool, 0),
|
|
|
|
make(chan bool, 0),
|
2015-08-21 17:27:26 +00:00
|
|
|
}
|
2015-08-24 05:28:45 +00:00
|
|
|
|
|
|
|
go engine.startProcessor()
|
|
|
|
|
|
|
|
return engine
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Engine) AddCheck(check *GenericCheck) {
|
|
|
|
if check == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.checks = append(this.checks, check)
|
|
|
|
log.Print(fmt.Sprintf("%v", this.checks))
|
2015-08-23 03:20:58 +00:00
|
|
|
}
|
2015-08-21 17:27:26 +00:00
|
|
|
|
2015-08-23 03:20:58 +00:00
|
|
|
func (this *Engine) AddHandler(handler *GenericHandler) {
|
|
|
|
if handler == nil {
|
|
|
|
return
|
|
|
|
}
|
2015-08-21 17:27:26 +00:00
|
|
|
|
2015-08-23 03:20:58 +00:00
|
|
|
this.handlers = append(this.handlers, handler)
|
|
|
|
log.Print(fmt.Sprintf("%v", this.handlers))
|
|
|
|
}
|
2015-08-21 17:27:26 +00:00
|
|
|
|
2015-08-24 05:28:45 +00:00
|
|
|
func (this *Engine) Stop() bool {
|
|
|
|
this.killswitch<- true
|
|
|
|
return <-this.killresponse
|
2015-08-21 17:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Engine) startProcessor() {
|
|
|
|
statuses := make(map[string]Status)
|
|
|
|
for true {
|
2015-08-24 05:28:45 +00:00
|
|
|
select {
|
|
|
|
case result := <-this.Input:
|
|
|
|
//No transition if we don't exist
|
|
|
|
if result.Status == statuses[result.RecordValue] {
|
|
|
|
continue
|
|
|
|
}
|
2015-08-21 17:27:26 +00:00
|
|
|
|
2015-08-24 05:28:45 +00:00
|
|
|
//Create a record with to, from
|
|
|
|
change := Transition{
|
|
|
|
result.Status,
|
|
|
|
statuses[result.RecordValue],
|
|
|
|
result.RecordValue,
|
|
|
|
}
|
2015-08-21 17:27:26 +00:00
|
|
|
|
2015-08-24 05:28:45 +00:00
|
|
|
//Send the record to everyone who cares
|
|
|
|
for _, relay := range this.handlers {
|
|
|
|
relay.Channel<- change
|
|
|
|
}
|
2015-08-21 17:27:26 +00:00
|
|
|
|
2015-08-24 05:28:45 +00:00
|
|
|
//And set our new status
|
|
|
|
statuses[result.RecordValue] = result.Status
|
|
|
|
case <-this.killswitch:
|
|
|
|
this.killresponse<- this.stop()
|
|
|
|
return
|
2015-08-21 17:27:26 +00:00
|
|
|
}
|
2015-08-24 05:28:45 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-21 17:27:26 +00:00
|
|
|
|
2015-08-24 05:28:45 +00:00
|
|
|
func (this *Engine) stop() bool {
|
|
|
|
exitStatus := true
|
|
|
|
for _, handler := range this.handlers {
|
|
|
|
exitStatus = exitStatus && handler.Stop()
|
|
|
|
}
|
|
|
|
for _, check := range this.checks {
|
|
|
|
exitStatus = exitStatus && check.Stop()
|
2015-08-21 17:27:26 +00:00
|
|
|
}
|
2015-08-24 05:28:45 +00:00
|
|
|
return exitStatus
|
2015-08-21 17:27:26 +00:00
|
|
|
}
|
2015-08-24 05:28:45 +00:00
|
|
|
|