-
Notifications
You must be signed in to change notification settings - Fork 8
/
controller.go
115 lines (99 loc) · 2.71 KB
/
controller.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
package bifrost_api_controller
import (
"context"
"net"
bifrost_api "github.com/aperturerobotics/bifrost/daemon/api"
"github.com/aperturerobotics/controllerbus/bus"
cbapi "github.com/aperturerobotics/controllerbus/bus/api"
"github.com/aperturerobotics/controllerbus/controller"
"github.com/aperturerobotics/controllerbus/directive"
"github.com/aperturerobotics/starpc/srpc"
"github.com/blang/semver"
"github.com/sirupsen/logrus"
)
// Version is the API version.
var Version = semver.MustParse("0.0.1")
// Controller implements the API controller. The controller looks up the Node,
// acquires its identity, listens and responds to incoming API calls.
type Controller struct {
// le is the logger
le *logrus.Entry
// bus is the controller bus
bus bus.Bus
// listenAddr is the listen address
listenAddr string
// conf is the config
conf *Config
}
// NewController constructs a new API controller.
func NewController(
le *logrus.Entry,
bus bus.Bus,
listenAddr string,
conf *Config,
) *Controller {
return &Controller{
le: le,
bus: bus,
listenAddr: listenAddr,
conf: conf,
}
}
// GetControllerInfo returns information about the controller.
func (c *Controller) GetControllerInfo() *controller.Info {
return controller.NewInfo(
ControllerID,
Version,
"api controller",
)
}
// Execute executes the API controller and the listener.
// Returning nil ends execution.
// Returning an error triggers a retry with backoff.
func (c *Controller) Execute(ctx context.Context) error {
// Construct the API
api, err := bifrost_api.NewAPI(c.bus, c.conf.GetApiConfig())
if err != nil {
return err
}
mux := srpc.NewMux()
api.RegisterAsSRPCServer(mux)
// controllerbus api
if !c.conf.GetDisableBusApi() {
bapi := cbapi.NewAPI(c.bus, c.conf.GetBusApiConfig())
_ = bapi.RegisterAsSRPCServer(mux)
}
c.le.Debug("starting listener")
lis, err := net.Listen("tcp", c.listenAddr)
if err != nil {
return err
}
c.le.Debugf("api listening: %s", lis.Addr().String())
srv := srpc.NewServer(mux)
errCh := make(chan error, 1)
go func() {
errCh <- srpc.AcceptMuxedListener(ctx, lis, srv, nil)
_ = lis.Close()
}()
select {
case <-ctx.Done():
_ = lis.Close()
return nil
case err := <-errCh:
return err
}
}
// HandleDirective asks if the handler can resolve the directive.
func (c *Controller) HandleDirective(ctx context.Context, di directive.Instance) ([]directive.Resolver, error) {
return nil, nil
}
// Close releases any resources used by the controller.
// Error indicates any issue encountered releasing.
func (c *Controller) Close() error {
// nil references to help GC along
c.le = nil
c.bus = nil
return nil
}
// _ is a type assertion
var _ controller.Controller = ((*Controller)(nil))