This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
router.go
181 lines (159 loc) · 4.95 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
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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package ttn
import (
"context"
"sync"
"time"
"github.com/TheThingsNetwork/api/discovery"
"github.com/TheThingsNetwork/api/discovery/discoveryclient"
"github.com/TheThingsNetwork/api/router/routerclient"
"github.com/TheThingsNetwork/api/trace"
"github.com/TheThingsNetwork/gateway-connector-bridge/types"
"github.com/TheThingsNetwork/go-utils/grpc/auth"
"github.com/TheThingsNetwork/ttn/api"
"github.com/TheThingsNetwork/ttn/api/pool"
"github.com/apex/log"
"google.golang.org/grpc"
)
func init() {
api.WaitForStreams = 0
grpc.EnableTracing = false
}
// RouterConfig contains configuration for the TTN Router
type RouterConfig struct {
DiscoveryServer string
RouterID string
}
// Router side of the bridge
type Router struct {
config RouterConfig
Ctx log.Interface
conn *grpc.ClientConn
client *routerclient.Client
pool *pool.Pool
mu sync.Mutex
gateways map[string]*gatewayConn
}
// New sets up a new TTN Router
func New(config RouterConfig, ctx log.Interface, tokenFunc func(string) string) (*Router, error) {
router := &Router{
config: config,
Ctx: ctx.WithField("Connector", "TTN Router"),
pool: pool.NewPool(context.Background(), append(pool.DefaultDialOptions, auth.WithTokenFunc("id", tokenFunc).DialOption())...),
gateways: make(map[string]*gatewayConn),
}
return router, nil
}
// Connect to the TTN Router
func (r *Router) Connect() error {
r.mu.Lock()
defer r.mu.Unlock()
r.Ctx.WithFields(log.Fields{
"Discovery": r.config.DiscoveryServer,
"RouterID": r.config.RouterID,
}).Info("Discovering Router")
discovery, err := discoveryclient.NewClient(r.config.DiscoveryServer, &discovery.Announcement{
ServiceName: "bridge",
}, func() string { return "" })
if err != nil {
return err
}
announcement, err := discovery.Get("router", r.config.RouterID)
if err != nil {
return err
}
r.Ctx.WithFields(log.Fields{
"RouterID": r.config.RouterID,
"Address": announcement.NetAddress,
}).Info("Connecting with Router")
if announcement.GetCertificate() == "" {
r.conn, err = announcement.Dial(nil)
} else {
r.conn, err = announcement.Dial(r.pool)
}
if err != nil {
return err
}
r.client = routerclient.NewClient(routerclient.DefaultClientConfig)
r.client.AddServer(r.config.RouterID, r.conn)
return nil
}
// Disconnect from the TTN Router and clean up gateway connections
func (r *Router) Disconnect() error {
r.mu.Lock()
defer r.mu.Unlock()
r.gateways = make(map[string]*gatewayConn)
r.pool.Close()
return nil
}
type gatewayConn struct {
stream routerclient.GenericStream
lastActive time.Time
}
func (r *Router) getGateway(gatewayID string, downlinkActive bool) *gatewayConn {
r.mu.Lock()
defer r.mu.Unlock()
if gtw, ok := r.gateways[gatewayID]; ok {
gtw.lastActive = time.Now()
return gtw
}
r.gateways[gatewayID] = &gatewayConn{
stream: r.client.NewGatewayStreams(gatewayID, "", downlinkActive),
lastActive: time.Now(),
}
return r.gateways[gatewayID]
}
// CleanupGateway cleans up gateway clients that are no longer needed
func (r *Router) CleanupGateway(gatewayID string) {
r.mu.Lock()
defer r.mu.Unlock()
if gtw, ok := r.gateways[gatewayID]; ok {
gtw.stream.Close()
delete(r.gateways, gatewayID)
}
}
// PublishUplink publishes uplink messages to the TTN Router
func (r *Router) PublishUplink(message *types.UplinkMessage) error {
message.Message.Trace = message.Message.Trace.WithEvent(trace.ForwardEvent, "backend", "ttn")
r.getGateway(message.GatewayID, false).stream.Uplink(message.Message)
return nil
}
// PublishStatus publishes status messages to the TTN Router
func (r *Router) PublishStatus(message *types.StatusMessage) error {
r.getGateway(message.GatewayID, false).stream.Status(message.Message)
return nil
}
// SubscribeDownlink handles downlink messages for the given gateway ID
func (r *Router) SubscribeDownlink(gatewayID string) (<-chan *types.DownlinkMessage, error) {
downlink := make(chan *types.DownlinkMessage)
gtw := r.getGateway(gatewayID, true)
ctx := r.Ctx.WithField("GatewayID", gatewayID)
ch, err := gtw.stream.Downlink()
if err == routerclient.ErrDownlinkInactive {
ctx.Debug("Downlink inactive, restarting streams with downlink")
r.mu.Lock()
oldStream := gtw.stream
gtw.stream = r.client.NewGatewayStreams(gatewayID, "", true)
r.mu.Unlock()
oldStream.Close()
ch, err = gtw.stream.Downlink()
}
if err != nil {
return nil, err
}
go func() {
for in := range ch {
ctx.Debug("Downlink message received")
in.Trace = in.Trace.WithEvent(trace.ReceiveEvent, "backend", "ttn")
downlink <- &types.DownlinkMessage{GatewayID: gatewayID, Message: in}
}
close(downlink)
}()
return downlink, nil
}
// UnsubscribeDownlink should unsubscribe from downlink, but in practice just disconnects the entire gateway
func (r *Router) UnsubscribeDownlink(gatewayID string) error {
r.CleanupGateway(gatewayID)
return nil
}