2017-05-11 09:39:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-05-15 04:57:38 +00:00
|
|
|
//crm "loafle.com/overflow/crawler_manager_go"
|
2017-05-15 10:32:25 +00:00
|
|
|
cm "loafle.com/overflow/agent_api/config_manager"
|
|
|
|
msg "loafle.com/overflow/agent_api/messages"
|
|
|
|
col "loafle.com/overflow/collector_go"
|
|
|
|
dat "loafle.com/overflow/data_sender_go"
|
|
|
|
evt "loafle.com/overflow/event_sender_go"
|
2017-05-15 04:57:38 +00:00
|
|
|
pol "loafle.com/overflow/long_poller_go"
|
2017-05-11 10:29:32 +00:00
|
|
|
"log"
|
2017-05-11 09:39:01 +00:00
|
|
|
)
|
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
func main() {
|
2017-05-11 10:29:32 +00:00
|
|
|
log.SetPrefix("Agent : ")
|
2017-05-15 05:46:12 +00:00
|
|
|
StartAgent()
|
|
|
|
}
|
|
|
|
|
2017-05-15 04:57:38 +00:00
|
|
|
|
|
|
|
type Agent struct {
|
2017-05-15 10:32:25 +00:00
|
|
|
cm cm.ConfigManager
|
2017-05-15 05:46:12 +00:00
|
|
|
taskCh chan interface{}
|
2017-05-15 04:57:38 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 05:46:12 +00:00
|
|
|
func StartAgent() {
|
|
|
|
agt := Agent{}
|
2017-05-15 04:57:38 +00:00
|
|
|
agt.startAgent()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (agt *Agent) startAgent() {
|
2017-05-15 10:32:25 +00:00
|
|
|
//1. cfg start()
|
|
|
|
//2. evt start()
|
2017-05-16 03:34:56 +00:00
|
|
|
evtStartChn := make(chan bool, 0)
|
|
|
|
err := evt.Start(evtStartChn)
|
2017-05-16 03:35:47 +00:00
|
|
|
//if err != nil {
|
|
|
|
// processError(err)
|
|
|
|
//}
|
2017-05-16 03:34:56 +00:00
|
|
|
<- evtStartChn
|
2017-05-15 10:32:25 +00:00
|
|
|
//3. dat start()
|
|
|
|
datStarted := make(chan bool)
|
|
|
|
dat.Start(datStarted, agt.cm.GetGlobalConfig())
|
|
|
|
<-datStarted
|
|
|
|
//4. pol start() -> getting an Auth key from API server and store that key.
|
|
|
|
authKey := make(chan string)
|
|
|
|
ch, err := pol.Start(authKey, agt.cm.GetGlobalConfig())
|
2017-05-15 05:46:12 +00:00
|
|
|
if err != nil {
|
2017-05-15 10:32:25 +00:00
|
|
|
evt.AddEventData(msg.TaskResult{msg.AGT_STOP, false, err })
|
|
|
|
return
|
2017-05-15 05:46:12 +00:00
|
|
|
}
|
2017-05-15 10:32:25 +00:00
|
|
|
<-authKey
|
2017-05-15 05:46:12 +00:00
|
|
|
agt.taskCh = ch
|
2017-05-15 10:32:25 +00:00
|
|
|
//5. scm start(secretKey)
|
|
|
|
//6. crm start()
|
|
|
|
//7. col start() -> evt result
|
|
|
|
dataCh := make(chan interface{})
|
|
|
|
colStarted := make(chan bool)
|
|
|
|
col.Start(colStarted, dataCh, agt.cm)
|
|
|
|
<-colStarted
|
|
|
|
|
|
|
|
res := msg.TaskResult{msg.AGT_START, true, nil}
|
|
|
|
evt.AddEventData(res)
|
2017-05-15 05:46:12 +00:00
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
go agt.processCollectingData(dataCh)
|
2017-05-15 04:57:38 +00:00
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
//8. processing tasks
|
2017-05-15 05:46:12 +00:00
|
|
|
go agt.waitAgentTask()
|
2017-05-11 09:39:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
func (agt *Agent) waitAgentTask() {
|
2017-05-15 04:57:38 +00:00
|
|
|
for {
|
2017-05-15 10:32:25 +00:00
|
|
|
d := <-agt.taskCh
|
|
|
|
task := d.(msg.AgentTask)
|
|
|
|
switch task.Command {
|
|
|
|
case msg.TASK_POLLER_INTERVAL_UPDATE:
|
|
|
|
agt.processUpdatePolInterval(task)
|
|
|
|
break
|
|
|
|
case msg.TASK_SENSOR_START:
|
|
|
|
agt.processStartSensor(task)
|
|
|
|
break
|
|
|
|
case msg.TASK_SENSOR_STOP:
|
|
|
|
agt.processStopSensor(task)
|
|
|
|
break
|
|
|
|
case msg.TASK_SENSOR_ADD:
|
|
|
|
agt.processAddSensor(task)
|
|
|
|
break
|
|
|
|
case msg.TASK_SENSOR_REMOVE:
|
|
|
|
agt.processRemoveSensor(task)
|
|
|
|
break
|
|
|
|
case msg.TASK_SENSOR_UPDATE:
|
|
|
|
agt.processUpdateSensor(task)
|
|
|
|
break
|
|
|
|
case msg.TASK_AGENT_UPDATE:
|
|
|
|
agt.processUpdateAgent(task)
|
|
|
|
break
|
|
|
|
case msg.TASK_CRAWLER_UPDATE:
|
|
|
|
agt.processUpdateCRM(task)
|
|
|
|
break
|
|
|
|
case msg.TASK_LOG_SEND:
|
|
|
|
agt.processSendLog(task)
|
|
|
|
break
|
2017-05-15 04:57:38 +00:00
|
|
|
default:
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-11 09:39:01 +00:00
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
func (agt *Agent) processCollectingData(ch chan interface{}) {
|
|
|
|
for {
|
|
|
|
data := <-ch
|
|
|
|
dat.AddData(data)
|
|
|
|
}
|
|
|
|
}
|
2017-05-11 10:29:32 +00:00
|
|
|
|
2017-05-15 04:57:38 +00:00
|
|
|
func (agt *Agent) stopAgent() {
|
2017-05-15 10:32:25 +00:00
|
|
|
//col stop()
|
|
|
|
colStopped := make(chan bool)
|
|
|
|
col.Stop(colStopped)
|
|
|
|
<-colStopped
|
|
|
|
//crm stop()
|
|
|
|
//scm stop()
|
|
|
|
//dat stop()
|
|
|
|
datStopped := make(chan bool)
|
|
|
|
dat.Stop(datStopped)
|
|
|
|
<-datStopped
|
|
|
|
//pol stop()
|
|
|
|
polStopped := make(chan bool)
|
|
|
|
err := pol.Stop(polStopped)
|
|
|
|
if err != nil {
|
|
|
|
evt.AddEventData(msg.TaskResult{msg.AGT_STOP, false, err })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
<-polStopped
|
|
|
|
|
|
|
|
//evt result
|
|
|
|
evt.AddEventData(msg.TaskResult{msg.AGT_STOP, true, nil })
|
|
|
|
//evt stop()
|
2017-05-16 04:07:28 +00:00
|
|
|
evt.Stop()
|
|
|
|
|
2017-05-15 04:57:38 +00:00
|
|
|
}
|
2017-05-11 10:29:32 +00:00
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
func (agt *Agent) processStartSensor(task msg.AgentTask) {
|
2017-05-15 04:57:38 +00:00
|
|
|
//col startSensor() -> evt result
|
2017-05-15 10:32:25 +00:00
|
|
|
id := task.Params["sensorId"]
|
|
|
|
col.StartSensor(id)
|
|
|
|
evt.AddEventData(msg.TaskResult{msg.TASK_SENSOR_START, true, nil })
|
2017-05-15 04:57:38 +00:00
|
|
|
}
|
2017-05-11 10:29:32 +00:00
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
func (agt *Agent) processStopSensor(task msg.AgentTask) {
|
2017-05-15 04:57:38 +00:00
|
|
|
//col stopSensor() -> evt result
|
2017-05-15 10:32:25 +00:00
|
|
|
id := task.Params["sensorId"]
|
|
|
|
col.StopSensor(id)
|
|
|
|
evt.AddEventData(msg.TaskResult{msg.TASK_SENSOR_STOP, true, nil })
|
2017-05-15 04:57:38 +00:00
|
|
|
}
|
2017-05-11 10:29:32 +00:00
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
func (agt *Agent) processAddSensor(task msg.AgentTask) {
|
|
|
|
id := task.Params["sensorId"]
|
|
|
|
//scm addSensor()
|
|
|
|
//crm addSensor()
|
|
|
|
//col addSensor()
|
|
|
|
err := col.AddSensor(id)
|
|
|
|
if err != nil {
|
|
|
|
evt.AddEventData(msg.TaskResult{msg.TASK_SENSOR_ADD, false, err })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//evt.AddEventData(msg.TaskResult{msg.TASK_SENSOR_ADD, true, nil })
|
2017-05-15 04:57:38 +00:00
|
|
|
}
|
2017-05-11 10:29:32 +00:00
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
func (agt *Agent) processRemoveSensor(task msg.AgentTask) {
|
|
|
|
id := task.Params["sensorId"]
|
|
|
|
//col removeSensor()
|
|
|
|
err := col.RemSensor(id)
|
|
|
|
if err != nil {
|
|
|
|
evt.AddEventData(msg.TaskResult{msg.TASK_SENSOR_REMOVE, false, err })
|
|
|
|
}
|
|
|
|
//crm removeSensor()
|
|
|
|
//scm removeSensor()
|
|
|
|
//evt.AddEventData(msg.TaskResult{msg.TASK_SENSOR_REMOVE, true, nil })
|
2017-05-15 04:57:38 +00:00
|
|
|
}
|
2017-05-11 10:29:32 +00:00
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
func (agt *Agent) processUpdateSensor(task msg.AgentTask) {
|
|
|
|
//col stopSensor()
|
|
|
|
//scm updateSensor()
|
|
|
|
//crm updateSensor()
|
|
|
|
//col updateSensor()
|
|
|
|
//evt.AddEventData(msg.TaskResult{msg.TASK_SENSOR_UPDATE, true, nil })
|
2017-05-15 04:57:38 +00:00
|
|
|
}
|
2017-05-11 10:29:32 +00:00
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
func (agt *Agent) processUpdateCRM(task msg.AgentTask) {
|
|
|
|
//(loop)col stopSensor()
|
2017-05-15 04:57:38 +00:00
|
|
|
//crm updateCRM()
|
2017-05-15 10:32:25 +00:00
|
|
|
//(loop)col startSensor()
|
|
|
|
//evt.AddEventData(msg.TaskResult{msg.TASK_CRAWLER_UPDATE, true, nil })
|
2017-05-15 04:57:38 +00:00
|
|
|
}
|
2017-05-11 10:29:32 +00:00
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
func (agt *Agent) processUpdateAgent(task msg.AgentTask) {
|
|
|
|
//mola
|
|
|
|
//evt.AddEventData(msg.TaskResult{msg.TASK_AGENT_UPDATE, true, nil })
|
2017-05-15 04:57:38 +00:00
|
|
|
}
|
2017-05-11 09:39:01 +00:00
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
func (agt *Agent) processSendLog(task msg.AgentTask) {
|
|
|
|
//mola
|
|
|
|
//evt.AddEventData(msg.TaskResult{msg.TASK_LOG_SEND, true, nil })
|
2017-05-15 05:46:12 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 10:32:25 +00:00
|
|
|
func (agt *Agent) processUpdatePolInterval(task msg.AgentTask) {
|
|
|
|
interval := task.Params["interval"]
|
|
|
|
err := pol.UpdateInterval(interval)
|
|
|
|
if err != nil {
|
|
|
|
evt.AddEventData(msg.TaskResult{msg.TASK_POLLER_INTERVAL_UPDATE, false, err })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
evt.AddEventData(msg.TaskResult{msg.TASK_POLLER_INTERVAL_UPDATE, true, nil })
|
2017-05-15 05:46:12 +00:00
|
|
|
}
|
|
|
|
|