relay.go 883 Bytes
Newer Older
vyzo's avatar
vyzo committed
1
2
3
4
5
package relay

import (
	"context"

6
7
	basic "github.com/libp2p/go-libp2p/p2p/host/basic"

vyzo's avatar
vyzo committed
8
9
	discovery "github.com/libp2p/go-libp2p-discovery"
	host "github.com/libp2p/go-libp2p-host"
10
	ma "github.com/multiformats/go-multiaddr"
vyzo's avatar
vyzo committed
11
12
13
14
)

// RelayHost is a Host that provides Relay services.
type RelayHost struct {
15
	*basic.BasicHost
vyzo's avatar
vyzo committed
16
	advertise discovery.Advertiser
17
	addrsF    basic.AddrsFactory
vyzo's avatar
vyzo committed
18
19
20
}

// New constructs a new RelayHost
21
22
23
24
25
26
27
func NewRelayHost(ctx context.Context, bhost *basic.BasicHost, advertise discovery.Advertiser) *RelayHost {
	h := &RelayHost{
		BasicHost: bhost,
		addrsF:    bhost.AddrsFactory,
		advertise: advertise,
	}
	bhost.AddrsFactory = h.hostAddrs
vyzo's avatar
vyzo committed
28
	discovery.Advertise(ctx, advertise, RelayRendezvous)
vyzo's avatar
vyzo committed
29
30
31
	return h
}

32
33
34
35
func (h *RelayHost) hostAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
	return filterUnspecificRelay(h.addrsF(addrs))
}

vyzo's avatar
vyzo committed
36
var _ host.Host = (*RelayHost)(nil)