go-libp2p-pubsub/notify.go

62 lines
1.2 KiB
Go
Raw Normal View History

package pubsub
import (
2019-05-26 17:19:03 +01:00
"github.com/libp2p/go-libp2p-core/network"
2021-04-01 23:45:09 +03:00
"github.com/libp2p/go-libp2p-core/peer"
2018-10-04 19:23:53 -04:00
ma "github.com/multiformats/go-multiaddr"
)
2019-05-26 17:19:03 +01:00
var _ network.Notifiee = (*PubSubNotif)(nil)
type PubSubNotif PubSub
2019-05-26 17:19:03 +01:00
func (p *PubSubNotif) OpenedStream(n network.Network, s network.Stream) {
}
2019-05-26 17:19:03 +01:00
func (p *PubSubNotif) ClosedStream(n network.Network, s network.Stream) {
}
2019-05-26 17:19:03 +01:00
func (p *PubSubNotif) Connected(n network.Network, c network.Conn) {
2021-04-01 23:45:09 +03:00
// ignore transient connections
if c.Stat().Transient {
return
}
go func() {
select {
case p.newPeers <- c.RemotePeer():
case <-p.ctx.Done():
}
}()
}
2019-05-26 17:19:03 +01:00
func (p *PubSubNotif) Disconnected(n network.Network, c network.Conn) {
}
2019-05-26 17:19:03 +01:00
func (p *PubSubNotif) Listen(n network.Network, _ ma.Multiaddr) {
}
2019-05-26 17:19:03 +01:00
func (p *PubSubNotif) ListenClose(n network.Network, _ ma.Multiaddr) {
}
func (p *PubSubNotif) Initialize() {
2021-04-01 23:45:09 +03:00
isTransient := func(pid peer.ID) bool {
for _, c := range p.host.Network().ConnsToPeer(pid) {
if !c.Stat().Transient {
return false
}
}
return true
}
for _, pid := range p.host.Network().Peers() {
if isTransient(pid) {
continue
}
select {
2021-04-01 23:45:09 +03:00
case p.newPeers <- pid:
case <-p.ctx.Done():
}
}
}