-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
45 lines (42 loc) · 997 Bytes
/
manager.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
package main
import "log"
type Manager struct {
register chan *User
relation chan *Relation
debug chan int
}
func NewManager() *Manager {
return &Manager{
make(chan *User),
make(chan *Relation),
make(chan int),
}
}
func (m Manager) manage(users map[string]*User, g *Graph) {
for {
select {
case newUser := <-m.register:
if correct := g.AddAloneNode(newUser.Id); correct {
users[newUser.Id] = newUser
} else {
log.Printf("Error user %s is already on the system", newUser.Id)
}
case newRel := <-m.relation:
if correct := g.AddEdge(newRel.Scanned.Id, newRel.Scans.Id); !correct {
log.Printf("Error relation %s -> %s already on the system", newRel.Scanned.Id, newRel.Scans.Id)
}
case op := <-m.debug:
if op == 0 {
return
} else if op == 1 {
g.Log()
} else if op == 2 {
g.ToJson("static/graph.json")
} else if op == 3 {
g.FromJson("static/graph.json")
} else {
log.Printf("Unknown operation %d", op)
}
}
}
}