-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
72 lines (67 loc) · 1.81 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"os"
"time"
"github.com/abu-lang/abusim-core/abusim-goabu-agent/endpoint"
"github.com/abu-lang/abusim-core/abusim-goabu-agent/memory"
"github.com/abu-lang/abusim-core/schema"
"github.com/abu-lang/goabu"
"github.com/abu-lang/goabu/communication"
goabuconfig "github.com/abu-lang/goabu/config"
"log"
)
func main() {
// I check if a config is present on the Args...
if len(os.Args) < 2 {
log.Fatalln("Config not found, exiting")
}
// ... and I deserialize it to get its fields
configStr := os.Args[1]
agent := schema.AgentConfiguration{}
err := agent.Deserialize(configStr)
if err != nil {
log.Fatalf("Bad config deserialization: %v", err)
}
// I create the memory for the agent...
log.Println("Creating memory")
mem, err := memory.New(agent.MemoryController, agent.Memory)
if err != nil {
log.Fatalln(err)
}
// ... I create the executer...
log.Println("Creating executer")
logConfig := goabuconfig.LogConfig{
Encoding: "console",
Level: goabuconfig.LogError,
}
exec, err := goabu.NewExecuter(mem, agent.Rules, communication.NewMemberlistAgent(agent.Name, 5000, logConfig, agent.Endpoints...), logConfig)
if err != nil {
log.Fatal(err)
}
// ... and I create the paused variable
paused := false
// I connect to the coordinator...
log.Println("Connecting to coordinator")
end, err := endpoint.New()
if err != nil {
log.Fatalln(err)
}
defer end.Close()
// ... I send to it the initialization message...
err = end.SendInit(agent.Name)
if err != nil {
log.Fatalln(err)
}
// ... and I start the main message loop
go end.HandleMessages(exec, agent, &paused)
// Finally, I start the executer loop
log.Println("Starting main loop")
for {
// I execute a command if not paused...
if !paused {
exec.Exec()
}
// ... and I sleep for a while
time.Sleep(agent.Tick)
}
}