forked from broderickhyman/albiondata-client
/
router.go
62 lines (56 loc) · 1.32 KB
/
router.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
package client
import (
"encoding/gob"
"os"
"github.com/ao-data/albiondata-client/log"
photon "github.com/ao-data/photon_spectator"
)
//Router struct definitions
type Router struct {
albionstate *albionState
newOperation chan operation
recordPhotonCommand chan photon.PhotonCommand
quit chan bool
}
func newRouter() *Router {
return &Router{
albionstate: &albionState{LocationId: -1},
newOperation: make(chan operation, 1000),
recordPhotonCommand: make(chan photon.PhotonCommand, 1000),
quit: make(chan bool, 1),
}
}
func (r *Router) run() {
var encoder *gob.Encoder
var file *os.File
if ConfigGlobal.RecordPath != "" {
file, err := os.Create(ConfigGlobal.RecordPath)
if err != nil {
log.Error("Could not open commands output file ", err)
} else {
encoder = gob.NewEncoder(file)
}
}
for {
select {
case <-r.quit:
log.Debug("Closing router...")
if file != nil {
err := file.Close()
if err != nil {
log.Error("Could not close commands output file ", err)
}
}
return
case op := <-r.newOperation:
go op.Process(r.albionstate)
case command := <-r.recordPhotonCommand:
if encoder != nil {
err := encoder.Encode(command)
if err != nil {
log.Error("Could not encode command ", err)
}
}
}
}
}