/
api.go
81 lines (71 loc) · 1.91 KB
/
api.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
package multicast
import (
"context"
"github.com/FavorLabs/favorX/pkg/boson"
"github.com/FavorLabs/favorX/pkg/rpc"
)
func (s *Service) API() rpc.API {
return rpc.API{
Namespace: "group",
Version: "1.0",
Service: &apiService{s: s},
Public: true,
}
}
type apiService struct {
s *Service
}
// Message subscribe the group message
func (a *apiService) Message(ctx context.Context, name string) (*rpc.Subscription, error) {
notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
sub := notifier.CreateSubscription()
gid, err := boson.ParseHexAddress(name)
if err != nil {
gid = GenerateGID(name)
}
err = a.s.SubscribeGroupMessage(notifier, sub, gid)
if err != nil {
return nil, err
}
return sub, nil
}
// Multicast subscribe the group multicast message
func (a *apiService) Multicast(ctx context.Context, name string) (*rpc.Subscription, error) {
notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
sub := notifier.CreateSubscription()
gid, err := boson.ParseHexAddress(name)
if err != nil {
gid = GenerateGID(name)
}
err = a.s.SubscribeMulticastMsg(notifier, sub, gid)
if err != nil {
return nil, err
}
return sub, nil
}
func (a *apiService) Peers(ctx context.Context, name string) (*rpc.Subscription, error) {
notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
sub := notifier.CreateSubscription()
gid, err := boson.ParseHexAddress(name)
if err != nil {
gid = GenerateGID(name)
}
err = a.s.subscribeGroupPeers(notifier, sub, gid)
if err != nil {
return nil, err
}
return sub, nil
}
// Reply to the group message to give the session ID
func (a *apiService) Reply(sessionID string, data []byte) error {
return a.s.replyGroupMessage(sessionID, data)
}