-
Notifications
You must be signed in to change notification settings - Fork 1
/
fleaTrainer.go
76 lines (60 loc) · 1.46 KB
/
fleaTrainer.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
73
74
75
76
// Samples used in a small go tutorial
//
// Copyright (C) 2017 framp at linux-tips-and-tricks dot de
//
// Samples for go channels, functions and mute
//
// See github.com/framps/golang_tutorial for latest code
// @@@@@@@@@@@@@@@@@@@@@@@@@@
// @@@ under construction @@@
// @@@@@@@@@@@@@@@@@@@@@@@@@@
package main
import (
"fmt"
"math/rand"
"os"
"os/signal"
"time"
"github.com/framps/golang_tutorial/channels/objects"
)
var (
fleaNames = [...]string{"Hugo", "Karl", "Emil"}
numberOfFleas = len(fleaNames)
numberOfLocations = 9
)
func startFleas(fleas []*objects.Flea) {
for i := 0; i < numberOfFleas; i++ {
fleas[i].YoureFree()
}
}
func startStatusReporter(area *objects.Area) {
timer := time.NewTimer(time.Second)
go func(t *time.Timer) {
for {
<-t.C
fmt.Println(area)
t = time.NewTimer(time.Second)
}
}(timer)
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
area := objects.NewArea(numberOfLocations, numberOfFleas)
fleas := make([]*objects.Flea, numberOfFleas)
pubSubCtrl := objects.PubSubController{}
// Add some fleas
for i := 0; i < numberOfFleas; i++ {
fleas[i] = objects.NewFlea(fleaNames[i], &area.Locations[0])
area.Locations[0].AddFlea(fleas[i])
pubSubCtrl.Register("Listen", fleas[i].CommandChannel)
fmt.Printf("Adding flea %s to location %d\n", fleas[i].Name, i)
}
startStatusReporter(area)
startFleas(fleas)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
for {
<-c
os.Exit(0)
}
}