-
Notifications
You must be signed in to change notification settings - Fork 8
/
factory.go
86 lines (74 loc) · 1.99 KB
/
factory.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
package xbee
import (
"context"
"github.com/aperturerobotics/bifrost/transport"
tc "github.com/aperturerobotics/bifrost/transport/controller"
"github.com/aperturerobotics/controllerbus/bus"
"github.com/aperturerobotics/controllerbus/config"
"github.com/aperturerobotics/controllerbus/controller"
"github.com/blang/semver"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/sirupsen/logrus"
)
// Factory constructs a XBee transport.
type Factory struct {
// bus is the controller bus
bus bus.Bus
}
// NewFactory builds a XBee transport factory.
func NewFactory(bus bus.Bus) *Factory {
return &Factory{bus: bus}
}
// GetConfigID returns the configuration ID for the controller.
func (t *Factory) GetConfigID() string {
return ConfigID
}
// GetControllerID returns the unique ID for the controller.
func (t *Factory) GetControllerID() string {
return ControllerID
}
// ConstructConfig constructs an instance of the controller configuration.
func (t *Factory) ConstructConfig() config.Config {
return &Config{}
}
// Construct constructs the associated controller given configuration.
// The transport's identity (private key) comes from a GetNode lookup.
func (t *Factory) Construct(
conf config.Config,
opts controller.ConstructOpts,
) (controller.Controller, error) {
le := opts.GetLogger()
cc := conf.(*Config)
peerIDConstraint, err := cc.ParseNodePeerID()
if err != nil {
return nil, err
}
// Construct the transport controller.
return tc.NewController(
le,
t.bus,
controller.NewInfo(ControllerID, Version, "xbee transport"),
peerIDConstraint,
func(
ctx context.Context,
le *logrus.Entry,
pkey crypto.PrivKey,
handler transport.TransportHandler,
) (transport.Transport, error) {
return NewXBee(
ctx,
le,
cc,
pkey,
handler,
)
},
cc.GetDialers(),
), nil
}
// GetVersion returns the version of this controller.
func (t *Factory) GetVersion() semver.Version {
return Version
}
// _ is a type assertion
var _ controller.Factory = ((*Factory)(nil))