-
Notifications
You must be signed in to change notification settings - Fork 12
/
backend.go
64 lines (49 loc) · 1.4 KB
/
backend.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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
package inprocess
import (
"context"
"cunicu.li/cunicu/pkg/crypto"
"cunicu.li/cunicu/pkg/log"
signalingproto "cunicu.li/cunicu/pkg/proto/signaling"
"cunicu.li/cunicu/pkg/signaling"
)
//nolint:gochecknoglobals
var subs = signaling.NewSubscriptionsRegistry()
func init() { //nolint:gochecknoinits
signaling.Backends["inprocess"] = &signaling.BackendPlugin{
New: NewBackend,
Description: "In-Process",
}
}
type Backend struct {
logger *log.Logger
}
func NewBackend(cfg *signaling.BackendConfig, logger *log.Logger) (signaling.Backend, error) {
b := &Backend{
logger: logger,
}
for _, h := range cfg.OnReady {
h.OnSignalingBackendReady(b)
}
return b, nil
}
func (b *Backend) Type() signalingproto.BackendType {
return signalingproto.BackendType_INPROCESS
}
func (b *Backend) Subscribe(_ context.Context, kp *crypto.KeyPair, h signaling.MessageHandler) (bool, error) {
return subs.Subscribe(kp, h)
}
func (b *Backend) Unsubscribe(_ context.Context, kp *crypto.KeyPair, h signaling.MessageHandler) (bool, error) {
return subs.Unsubscribe(kp, h)
}
func (b *Backend) Publish(_ context.Context, kp *crypto.KeyPair, msg *signaling.Message) error {
env, err := msg.Encrypt(kp)
if err != nil {
return err
}
return subs.NewMessage(env)
}
func (b *Backend) Close() error {
return nil
}