-
Notifications
You must be signed in to change notification settings - Fork 301
/
packages.go
124 lines (111 loc) · 3.8 KB
/
packages.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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package packages
import (
"context"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"go.thethings.network/lorawan-stack/pkg/applicationserver/io"
"go.thethings.network/lorawan-stack/pkg/errors"
"go.thethings.network/lorawan-stack/pkg/log"
"go.thethings.network/lorawan-stack/pkg/rpcserver"
"go.thethings.network/lorawan-stack/pkg/ttnpb"
"go.thethings.network/lorawan-stack/pkg/unique"
"google.golang.org/grpc"
)
type server struct {
ctx context.Context
io io.Server
registry Registry
handlers map[string]ApplicationPackageHandler
}
// Server is an application packages frontend.
type Server interface {
rpcserver.Registerer
NewSubscription() *io.Subscription
}
// New returns an application packages server wrapping the given registries.
func New(ctx context.Context, io io.Server, registry Registry) (Server, error) {
ctx = log.NewContextWithField(ctx, "namespace", "applicationserver/io/packages")
s := &server{
ctx: ctx,
io: io,
registry: registry,
handlers: make(map[string]ApplicationPackageHandler),
}
for _, p := range registeredPackages {
s.handlers[p.Name] = p.new(io, registry)
}
return s, nil
}
func (s *server) handleUp(ctx context.Context, msg *ttnpb.ApplicationUp) error {
ctx = log.NewContextWithField(ctx, "device_uid", unique.ID(ctx, msg.EndDeviceIdentifiers))
switch up := msg.Up.(type) {
case *ttnpb.ApplicationUp_UplinkMessage:
association, err := s.registry.Get(ctx, ttnpb.ApplicationPackageAssociationIdentifiers{
EndDeviceIdentifiers: msg.EndDeviceIdentifiers,
FPort: up.UplinkMessage.FPort,
}, []string{
"data",
"ids.end_device_ids",
"ids.f_port",
"package_name",
})
if errors.IsNotFound(err) {
return nil
} else if err != nil {
return err
}
if handler, ok := s.handlers[association.PackageName]; ok {
ctx := log.NewContextWithField(ctx, "package", association.PackageName)
return handler.HandleUp(ctx, association, msg)
}
return errNotImplemented.WithAttributes("name", association.PackageName)
}
return nil
}
// Roles implements the rpcserver.Registerer interface.
func (s *server) Roles() []ttnpb.ClusterRole {
return nil
}
// RegisterServices registers the services of the registered application packages.
func (s *server) RegisterServices(gs *grpc.Server) {
ttnpb.RegisterApplicationPackageRegistryServer(gs, s)
for _, subsystem := range s.handlers {
subsystem.RegisterServices(gs)
}
}
// RegisterHandlers registers the handlers of the registered application packages.
func (s *server) RegisterHandlers(rs *runtime.ServeMux, conn *grpc.ClientConn) {
ttnpb.RegisterApplicationPackageRegistryHandler(s.ctx, rs, conn)
for _, subsystem := range s.handlers {
subsystem.RegisterHandlers(rs, conn)
}
}
// NewSubscription creates a new default subscription for upstream application packages traffic.
func (s *server) NewSubscription() *io.Subscription {
sub := io.NewSubscription(s.ctx, "applicationpackages", nil)
go func() {
for {
select {
case <-s.ctx.Done():
return
case up := <-sub.Up():
if err := s.handleUp(up.Context, up.ApplicationUp); err != nil {
log.FromContext(s.ctx).WithError(err).Warn("Failed to handle message")
}
}
}
}()
return sub
}