2015-08-21 17:27:26 +00:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
type Engine struct {
|
|
|
|
Input chan Result
|
2015-08-23 00:35:33 +00:00
|
|
|
output []*GenericHandler
|
2015-08-21 17:27:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 00:35:33 +00:00
|
|
|
func CreateEngine(handlers []*GenericHandler) *Engine {
|
2015-08-21 17:27:26 +00:00
|
|
|
input := make(chan Result)
|
|
|
|
|
|
|
|
engine := Engine{
|
|
|
|
input,
|
|
|
|
handlers,
|
|
|
|
}
|
|
|
|
|
|
|
|
e := &engine
|
|
|
|
|
|
|
|
go e.startProcessor()
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Engine) startProcessor() {
|
|
|
|
statuses := make(map[string]Status)
|
|
|
|
for true {
|
|
|
|
result := <-this.Input
|
|
|
|
|
|
|
|
//No transition if we don't exist
|
|
|
|
if result.Status == statuses[result.RecordValue] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create a record with to, from
|
|
|
|
change := Transition{
|
|
|
|
result.Status,
|
|
|
|
statuses[result.RecordValue],
|
|
|
|
result.RecordValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
//Send the record to everyone who cares
|
|
|
|
for _, relay := range this.output {
|
|
|
|
relay.channel<- change
|
|
|
|
}
|
|
|
|
|
|
|
|
//And set our new status
|
|
|
|
statuses[result.RecordValue] = result.Status
|
|
|
|
}
|
|
|
|
}
|