-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
141 lines (106 loc) · 3.95 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"context"
"github.com/shimmeringbee/controller/layers"
"github.com/shimmeringbee/controller/state"
"github.com/shimmeringbee/da"
lw "github.com/shimmeringbee/logwrap"
"github.com/shimmeringbee/logwrap/impl/golog"
"github.com/shimmeringbee/persistence"
"github.com/shimmeringbee/persistence/impl/file"
"log"
"os"
"os/signal"
"path/filepath"
)
func main() {
ctx := context.Background()
l := lw.New(golog.Wrap(log.New(os.Stderr, "", log.LstdFlags)))
l.LogInfo(ctx, "Shimmering Bee: Controller - Copyright 2019-2020 Shimmering Bee Contributors - Starting...")
directories := enumerateDirectories(ctx, l)
l.LogInfo(ctx, "Directory enumeration complete.", lw.Datum("directories", directories))
l.LogInfo(ctx, "Persisted data initialising.")
section := file.New(directories.Data)
newLogger, err := configureLogging(filepath.Join(directories.Config, "logging"), directories.Log, l)
if err != nil {
l.LogFatal(ctx, "Failed to load logging configuration.", lw.Err(err))
}
l = newLogger
gatewayCfgs, err := loadGatewayConfigurations(filepath.Join(directories.Config, "gateways"))
if err != nil {
l.LogFatal(ctx, "Failed to load gateway configurations.", lw.Err(err))
}
l.LogInfo(ctx, "Loaded gateway configurations.", lw.Datum("configCount", len(gatewayCfgs)))
interfaceCfgs, err := loadInterfaceConfigurations(filepath.Join(directories.Config, "interfaces"))
if err != nil {
l.LogFatal(ctx, "Failed to load interface configurations.", lw.Err(err))
}
l.LogInfo(ctx, "Loaded interface configurations.", lw.Datum("configCount", len(interfaceCfgs)))
eventbus := state.NewEventBus()
l.LogInfo(ctx, "Initialising device organiser.")
deviceOrganiser := state.NewDeviceOrganiser(section.Section("Organiser"), eventbus)
gwMux := state.NewGatewayMux(eventbus)
l.LogInfo(ctx, "Linking device organiser to mux.")
deviceOrganiserMuxCh := updateDeviceOrganiserFromMux(&deviceOrganiser)
eventbus.Subscribe(deviceOrganiserMuxCh)
outputStack := layers.PassThruStack{}
l.LogInfo(ctx, "Starting interfaces.")
startedInterfaces, err := startInterfaces(interfaceCfgs, gwMux, eventbus, &deviceOrganiser, outputStack, l)
if err != nil {
l.LogFatal(ctx, "Failed to start interfaces.", lw.Err(err))
}
l.LogInfo(ctx, "Starting gateways.")
startedGateways, err := startGateways(gatewayCfgs, gwMux, l, section.Section("Gateway"))
if err != nil {
l.LogFatal(ctx, "Failed to start gateways.", lw.Err(err))
}
for _, gw := range gwMux.Gateways() {
deviceOrganiser.AddDevice(gw.Self().Identifier().String())
}
l.LogInfo(ctx, "Controller ready.")
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, os.Kill)
s := <-signalCh
l.LogInfo(ctx, "Signal received, shutting down.", lw.Datum("signal", s.String()))
for _, intf := range startedInterfaces {
l.LogInfo(ctx, "Shutting down interface.", lw.Datum("interface", intf.Name))
if err := intf.Shutdown(); err != nil {
l.LogError(ctx, "Failed to shutdown gateway.", lw.Err(err), lw.Datum("interface", intf.Name))
}
}
for _, gw := range startedGateways {
l.LogInfo(ctx, "Shutting down gateway.", lw.Datum("gateway", gw.Name))
if err := gw.Gateway.Stop(ctx); err != nil {
l.LogError(ctx, "Failed to shutdown gateway.", lw.Err(err), lw.Datum("gateway", gw.Name))
}
gw.Shutdown()
}
l.LogInfo(ctx, "Shutting down gateway mux.")
gwMux.Stop()
l.LogInfo(ctx, "Shutting device organiser mux link.")
deviceOrganiserMuxCh <- nil
if syncer, ok := section.(persistence.Syncer); ok {
l.LogInfo(ctx, "Syncing persistence.")
syncer.Sync()
}
l.LogInfo(ctx, "Shut down complete.")
}
func updateDeviceOrganiserFromMux(do *state.DeviceOrganiser) chan any {
ch := make(chan any, 100)
go func() {
for {
select {
case e := <-ch:
switch ce := e.(type) {
case da.DeviceAdded:
do.AddDevice(ce.Device.Identifier().String())
case da.DeviceRemoved:
do.RemoveDevice(ce.Device.Identifier().String())
case nil:
return
}
}
}
}()
return ch
}