-
Notifications
You must be signed in to change notification settings - Fork 8
/
accept.go
208 lines (181 loc) · 4.87 KB
/
accept.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package stream_api_accept
import (
"context"
"github.com/aperturerobotics/bifrost/link"
"github.com/aperturerobotics/bifrost/peer"
"github.com/aperturerobotics/bifrost/protocol"
stream_api_rpc "github.com/aperturerobotics/bifrost/stream/api/rpc"
"github.com/aperturerobotics/controllerbus/bus"
"github.com/aperturerobotics/controllerbus/controller"
"github.com/aperturerobotics/controllerbus/directive"
"github.com/sirupsen/logrus"
)
// Controller accepts HandleMountedStream via waiting RPC calls and streams data
// over the request and response streams.
type Controller struct {
// le is the logger
le *logrus.Entry
// conf is the config
conf *Config
// bus is the controller bus
bus bus.Bus
// localPeerID is the local peer id
localPeerID peer.ID
// remotePeerIDs are the acceptable remote peer id
remotePeerIDs []peer.ID
// protocolID is the protocol ID to use
protocolID protocol.ID
// rpcCh is the rpc channel
rpcCh chan *queuedRPC
}
// NewController constructs a new accept controller.
func NewController(
le *logrus.Entry,
conf *Config,
bus bus.Bus,
) (*Controller, error) {
localPeerID, err := conf.ParseLocalPeerID()
if err != nil {
return nil, err
}
var remotePeerIDs []peer.ID
for _, pid := range conf.GetRemotePeerIds() {
pi, err := peer.IDB58Decode(pid)
if err != nil {
return nil, err
}
remotePeerIDs = append(remotePeerIDs, pi)
}
pid := protocol.ID(conf.GetProtocolId())
if err := pid.Validate(); err != nil {
return nil, err
}
return &Controller{
le: le,
bus: bus,
conf: conf,
rpcCh: make(chan *queuedRPC),
localPeerID: localPeerID,
remotePeerIDs: remotePeerIDs,
protocolID: pid,
}, nil
}
// GetControllerInfo returns information about the controller.
func (c *Controller) GetControllerInfo() *controller.Info {
return controller.NewInfo(
ControllerID,
Version,
"accept controller",
)
}
// Execute executes the accept controller.
// Returning nil ends execution.
// Returning an error triggers a retry with backoff.
func (c *Controller) Execute(ctx context.Context) error {
return nil
}
// HandleDirective asks if the handler can resolve the directive.
// If it can, it returns a resolver. If not, returns nil.
// Any exceptional errors are returned for logging.
// It is safe to add a reference to the directive during this call.
func (c *Controller) HandleDirective(ctx context.Context, di directive.Instance) ([]directive.Resolver, error) {
dir := di.GetDirective()
// HandleMountedStream handler.
if d, ok := dir.(link.HandleMountedStream); ok {
return c.resolveHandleMountedStream(ctx, di, d)
}
return nil, nil
}
// queuedRPC is a queued RPC
type queuedRPC struct {
rpc stream_api_rpc.RPC
doneCb func(err error)
}
// AttachRPC attaches a RPC call to the controller.
func (c *Controller) AttachRPC(rpc stream_api_rpc.RPC) error {
ctx := rpc.Context()
errCh := make(chan error, 1)
if err := rpc.Send(&stream_api_rpc.Data{
State: stream_api_rpc.StreamState_StreamState_ESTABLISHING,
}); err != nil {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
case c.rpcCh <- &queuedRPC{
rpc: rpc,
doneCb: func(err error) {
select {
case errCh <- err:
default:
}
},
}:
return <-errCh
}
}
// resolveHandleMountedStream resolves a HandleMountedStream directive by dialing a target.
func (c *Controller) resolveHandleMountedStream(
ctx context.Context,
di directive.Instance,
dir link.HandleMountedStream,
) ([]directive.Resolver, error) {
if c.protocolID != dir.HandleMountedStreamProtocolID() {
return nil, nil
}
if localPeerID := c.localPeerID; localPeerID != peer.ID("") {
if lid := dir.HandleMountedStreamLocalPeerID(); lid != localPeerID {
c.le.Debugf(
"incoming stream %s != filtered %s",
lid.Pretty(),
localPeerID.Pretty(),
)
return nil, nil
}
}
if len(c.remotePeerIDs) != 0 {
remoteID := dir.HandleMountedStreamRemotePeerID()
var found bool
for _, rpid := range c.remotePeerIDs {
if rpid == remoteID {
found = true
break
}
}
if !found {
c.le.Debugf(
"incoming stream %s != filtered %v",
remoteID.Pretty(),
c.conf.GetRemotePeerIds(),
)
return nil, nil
}
}
return directive.Resolvers(c), nil
}
// Resolve resolves the values, emitting them to the handler.
func (c *Controller) Resolve(ctx context.Context, handler directive.ResolverHandler) error {
var rpc *queuedRPC
select {
case <-ctx.Done():
return ctx.Err()
case rpc = <-c.rpcCh:
}
h, err := NewMountedStreamHandler(c.le, c.bus, rpc)
if err != nil {
rpc.doneCb(err)
return err
}
handler.AddValue(link.MountedStreamHandler(h))
return nil
}
// Close releases any resources used by the controller.
// Error indicates any issue encountered releasing.
func (c *Controller) Close() error {
return nil
}
// _ is a type assertion
var _ controller.Controller = ((*Controller)(nil))
// _ is a type assertion
var _ directive.Resolver = ((*Controller)(nil))