-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
57 lines (47 loc) · 1.32 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
package kademlia
import (
"context"
"github.com/gauss-project/aurorafs/pkg/rpc"
"github.com/gauss-project/aurorafs/pkg/subscribe"
)
func (k *Kad) API() rpc.API {
return rpc.API{
Namespace: "p2p",
Version: "1.0",
Service: &apiService{kad: k},
Public: true,
}
}
type apiService struct {
kad *Kad
}
type Connected struct {
FullNodes int `json:"full_nodes"`
LightNodes uint `json:"light_nodes"`
BootNodes uint `json:"boot_nodes"`
}
type KadInfo struct {
Depth uint8 `json:"depth"`
Population int `json:"population"`
Connected Connected `json:"connected"`
}
func (a *apiService) KadInfo(ctx context.Context) (*rpc.Subscription, error) {
notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
sub := notifier.CreateSubscription()
iNotifier := subscribe.NewNotifierWithDelay(notifier, sub, 1, false)
a.kad.SubscribePeersChange(iNotifier)
return sub, nil
}
func (a *apiService) PeerState(ctx context.Context) (*rpc.Subscription, error) {
notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
sub := notifier.CreateSubscription()
iNotifier := subscribe.NewNotifier(notifier, sub)
a.kad.SubscribePeerState(iNotifier)
return sub, nil
}