This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
/
pktfwd.go
167 lines (144 loc) · 4.42 KB
/
pktfwd.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
// 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 pktfwd
import (
"sync"
"time"
"github.com/TheThingsNetwork/gateway-connector-bridge/types"
"github.com/TheThingsNetwork/go-utils/log"
"github.com/brocaar/lorawan"
)
// Config contains configuration for PacketForwarder
type Config struct {
Bind string
Session time.Duration
LockIP bool
LockPort bool
}
// New returns a new Dummy backend
func New(config Config, ctx log.Interface) *PacketForwarder {
f := &PacketForwarder{
config: config,
ctx: ctx.WithField("Connector", "PacketForwarder"),
connect: make(chan *types.ConnectMessage),
disconnect: make(chan *types.DisconnectMessage),
uplink: make(map[string]chan *types.UplinkMessage),
status: make(map[string]chan *types.StatusMessage),
}
return f
}
// PacketForwarder backend based on github.com/brocaar/lora-gateway-bridge
type PacketForwarder struct {
config Config
backend *Backend
ctx log.Interface
mu sync.RWMutex
connect chan *types.ConnectMessage
disconnect chan *types.DisconnectMessage
uplink map[string]chan *types.UplinkMessage
status map[string]chan *types.StatusMessage
}
// Connect implements the Southbound interface
func (f *PacketForwarder) Connect() (err error) {
f.backend, err = NewBackend(f.config, f.onNew, f.onDelete, false)
if err != nil {
return err
}
f.backend.log = f.ctx
go func() {
for uplink := range f.backend.RXPacketChan() {
f.mu.RLock()
if ch, ok := f.uplink[uplink.GatewayID]; ok {
ch <- uplink
} else if ch, ok := f.uplink[""]; ok {
ch <- uplink
} else {
f.ctx.WithField("GatewayID", uplink.GatewayID).Debug("Dropping uplink for inactive gateway")
}
f.mu.RUnlock()
}
}()
go func() {
for status := range f.backend.StatsChan() {
status.Backend = "PacketForwarder"
f.mu.RLock()
if ch, ok := f.status[status.GatewayID]; ok {
ch <- status
} else if ch, ok := f.status[""]; ok {
ch <- status
} else {
f.ctx.WithField("GatewayID", status.GatewayID).Debug("Dropping status for inactive gateway")
}
f.mu.RUnlock()
}
}()
return
}
// Disconnect implements the Southbound interface
func (f *PacketForwarder) Disconnect() error {
return f.backend.Close()
}
func (f *PacketForwarder) onNew(mac lorawan.EUI64) error {
f.connect <- &types.ConnectMessage{GatewayID: getID(mac)}
return nil
}
func (f *PacketForwarder) onDelete(mac lorawan.EUI64) error {
f.disconnect <- &types.DisconnectMessage{GatewayID: getID(mac)}
return nil
}
// SubscribeConnect implements the Southbound interface
func (f *PacketForwarder) SubscribeConnect() (<-chan *types.ConnectMessage, error) {
return f.connect, nil
}
// UnsubscribeConnect implements the Southbound interface
func (f *PacketForwarder) UnsubscribeConnect() error {
close(f.connect)
return nil
}
// SubscribeDisconnect implements the Southbound interface
func (f *PacketForwarder) SubscribeDisconnect() (<-chan *types.DisconnectMessage, error) {
return f.disconnect, nil
}
// UnsubscribeDisconnect implements the Southbound interface
func (f *PacketForwarder) UnsubscribeDisconnect() error {
close(f.disconnect)
return nil
}
// SubscribeUplink implements the Southbound interface
func (f *PacketForwarder) SubscribeUplink(gatewayID string) (<-chan *types.UplinkMessage, error) {
f.mu.Lock()
defer f.mu.Unlock()
f.uplink[gatewayID] = make(chan *types.UplinkMessage)
return f.uplink[gatewayID], nil
}
// UnsubscribeUplink implements the Southbound interface
func (f *PacketForwarder) UnsubscribeUplink(gatewayID string) error {
f.mu.Lock()
defer f.mu.Unlock()
if ch, ok := f.uplink[gatewayID]; ok {
close(ch)
}
delete(f.uplink, gatewayID)
return nil
}
// SubscribeStatus implements the Southbound interface
func (f *PacketForwarder) SubscribeStatus(gatewayID string) (<-chan *types.StatusMessage, error) {
f.mu.Lock()
defer f.mu.Unlock()
f.status[gatewayID] = make(chan *types.StatusMessage)
return f.status[gatewayID], nil
}
// UnsubscribeStatus implements the Southbound interface
func (f *PacketForwarder) UnsubscribeStatus(gatewayID string) error {
f.mu.Lock()
defer f.mu.Unlock()
if ch, ok := f.status[gatewayID]; ok {
close(ch)
}
delete(f.status, gatewayID)
return nil
}
// PublishDownlink implements the Southbound interface
func (f *PacketForwarder) PublishDownlink(message *types.DownlinkMessage) error {
return f.backend.Send(message)
}