-
Notifications
You must be signed in to change notification settings - Fork 0
/
downlink.go
68 lines (54 loc) · 1.87 KB
/
downlink.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
// 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 broker
import (
"strings"
"time"
pb "github.com/TheThingsNetwork/ttn/api/broker"
"github.com/TheThingsNetwork/ttn/api/fields"
"github.com/TheThingsNetwork/ttn/api/trace"
"github.com/TheThingsNetwork/ttn/utils/errors"
)
// ByScore is used to sort a list of DownlinkOptions based on Score
type ByScore []*pb.DownlinkOption
func (a ByScore) Len() int { return len(a) }
func (a ByScore) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByScore) Less(i, j int) bool { return a[i].Score < a[j].Score }
func (b *broker) HandleDownlink(downlink *pb.DownlinkMessage) error {
ctx := b.Ctx.WithFields(fields.Get(downlink))
var err error
start := time.Now()
defer func() {
if err != nil {
ctx.WithError(err).Warn("Could not handle downlink")
} else {
ctx.WithField("Duration", time.Now().Sub(start)).Info("Handled downlink")
}
if downlink != nil {
for _, monitor := range b.Monitors.BrokerClients() {
ctx.Debug("Sending downlink to monitor")
go monitor.SendDownlink(downlink)
}
}
}()
b.status.downlink.Mark(1)
downlink.Trace = downlink.Trace.WithEvent(trace.ReceiveEvent)
downlink, err = b.ns.Downlink(b.Component.GetContext(b.nsToken), downlink)
if err != nil {
return errors.Wrap(errors.FromGRPCError(err), "NetworkServer did not handle downlink")
}
var routerID string
if id := strings.Split(downlink.DownlinkOption.Identifier, ":"); len(id) == 2 {
routerID = id[0]
} else {
return errors.NewErrInvalidArgument("DownlinkOption Identifier", "invalid format")
}
ctx = ctx.WithField("RouterID", routerID)
router, err := b.getRouter(routerID)
if err != nil {
return err
}
downlink.Trace = downlink.Trace.WithEvent(trace.ForwardEvent, "router", routerID)
router <- downlink
return nil
}