MCPcopy Create free account
hub / github.com/CPChain/chain / PeerEvents

Method PeerEvents

node/api.go:67–101  ·  view source on GitHub ↗

PeerEvents creates an RPC subscription which receives peer events from the node's p2p.Server

(ctx context.Context)

Source from the content-addressed store, hash-verified

65// PeerEvents creates an RPC subscription which receives peer events from the
66// node's p2p.Server
67func (api *PrivateAdminAPI) PeerEvents(ctx context.Context) (*rpc.Subscription, error) {
68 // Make sure the server is running, fail otherwise
69 server := api.node.Server()
70 if server == nil {
71 return nil, ErrNodeStopped
72 }
73
74 // Create the subscription
75 notifier, supported := rpc.NotifierFromContext(ctx)
76 if !supported {
77 return nil, rpc.ErrNotificationsUnsupported
78 }
79 rpcSub := notifier.CreateSubscription()
80
81 go func() {
82 events := make(chan *p2p.PeerEvent)
83 sub := server.SubscribeEvents(events)
84 defer sub.Unsubscribe()
85
86 for {
87 select {
88 case event := <-events:
89 notifier.Notify(rpcSub.ID, event)
90 case <-sub.Err():
91 return
92 case <-rpcSub.Err():
93 return
94 case <-notifier.Closed():
95 return
96 }
97 }
98 }()
99
100 return rpcSub, nil
101}
102
103// StartRPC starts the HTTP RPC API server.
104func (api *PrivateAdminAPI) StartRPC(host *string, port *int, cors *string, apis *string, vhosts *string) (bool, error) {

Callers

nothing calls this directly

Calls 6

ServerMethod · 0.80
CreateSubscriptionMethod · 0.80
UnsubscribeMethod · 0.65
ErrMethod · 0.65
ClosedMethod · 0.65
NotifyMethod · 0.45

Tested by

no test coverage detected