ipfs_impl.go 5.92 KB
Newer Older
1 2 3
package network

import (
4
	"context"
5
	"fmt"
Jeromy's avatar
Jeromy committed
6 7
	"io"

8
	bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
Jeromy's avatar
Jeromy committed
9

10
	routing "gx/ipfs/QmQKEgGgYCDyk8VNY6A65FpuE4YwbspvjXHco1rdb75PVc/go-libp2p-routing"
Jeromy's avatar
Jeromy committed
11
	logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
12
	ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
Jeromy's avatar
Jeromy committed
13
	host "gx/ipfs/QmWf338UyG5DKyemvoFiomDPtkVNHLsw3GAt9XXHX5ZtsM/go-libp2p-host"
14
	pstore "gx/ipfs/QmXXCcQ7CLg5a81Ui9TTR35QcR4y7ZyihxwfjqaHfUVcVo/go-libp2p-peerstore"
15
	cid "gx/ipfs/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid"
Jeromy's avatar
Jeromy committed
16
	ggio "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/io"
Jeromy's avatar
Jeromy committed
17
	inet "gx/ipfs/QmdysBu77i3YaagNtMAjiCJdeWWvds18ho5XEB784guQ41/go-libp2p-net"
18
	peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
19 20
)

Jeromy's avatar
Jeromy committed
21
var log = logging.Logger("bitswap_network")
Jeromy's avatar
Jeromy committed
22

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23
// NewFromIpfsHost returns a BitSwapNetwork supported by underlying IPFS host
24
func NewFromIpfsHost(host host.Host, r routing.ContentRouting) BitSwapNetwork {
25
	bitswapNetwork := impl{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26
		host:    host,
27
		routing: r,
28
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29
	host.SetStreamHandler(ProtocolBitswap, bitswapNetwork.handleNewStream)
30 31
	host.SetStreamHandler(ProtocolBitswapOne, bitswapNetwork.handleNewStream)
	host.SetStreamHandler(ProtocolBitswapNoVers, bitswapNetwork.handleNewStream)
32 33 34
	host.Network().Notify((*netNotifiee)(&bitswapNetwork))
	// TODO: StopNotify.

35
	return &bitswapNetwork
36 37
}

38 39
// impl transforms the ipfs network interface, which sends and receives
// NetMessage objects, into the bitswap network interface.
40
type impl struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
	host    host.Host
42
	routing routing.ContentRouting
43 44 45

	// inbound messages from the network are forwarded to the receiver
	receiver Receiver
46 47
}

Jeromy's avatar
Jeromy committed
48 49 50 51 52 53 54 55 56
type streamMessageSender struct {
	s inet.Stream
}

func (s *streamMessageSender) Close() error {
	return s.s.Close()
}

func (s *streamMessageSender) SendMsg(msg bsmsg.BitSwapMessage) error {
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	return msgToStream(s.s, msg)
}

func msgToStream(s inet.Stream, msg bsmsg.BitSwapMessage) error {
	switch s.Protocol() {
	case ProtocolBitswap:
		if err := msg.ToNetV1(s); err != nil {
			log.Debugf("error: %s", err)
			return err
		}
	case ProtocolBitswapOne, ProtocolBitswapNoVers:
		if err := msg.ToNetV0(s); err != nil {
			log.Debugf("error: %s", err)
			return err
		}
	default:
		return fmt.Errorf("unrecognized protocol on remote: %s", s.Protocol())
	}
	return nil
Jeromy's avatar
Jeromy committed
76 77 78 79 80 81 82 83 84 85 86
}

func (bsnet *impl) NewMessageSender(ctx context.Context, p peer.ID) (MessageSender, error) {
	s, err := bsnet.newStreamToPeer(ctx, p)
	if err != nil {
		return nil, err
	}

	return &streamMessageSender{s: s}, nil
}

87
func (bsnet *impl) newStreamToPeer(ctx context.Context, p peer.ID) (inet.Stream, error) {
88

89 90
	// first, make sure we're connected.
	// if this fails, we cannot connect to given peer.
91
	//TODO(jbenet) move this into host.NewStream?
Jeromy's avatar
Jeromy committed
92
	if err := bsnet.host.Connect(ctx, pstore.PeerInfo{ID: p}); err != nil {
93
		return nil, err
94 95
	}

96
	return bsnet.host.NewStream(ctx, p, ProtocolBitswap, ProtocolBitswapOne, ProtocolBitswapNoVers)
97 98 99 100 101 102 103 104
}

func (bsnet *impl) SendMessage(
	ctx context.Context,
	p peer.ID,
	outgoing bsmsg.BitSwapMessage) error {

	s, err := bsnet.newStreamToPeer(ctx, p)
105 106 107
	if err != nil {
		return err
	}
108 109
	defer s.Close()

110
	return msgToStream(s, outgoing)
111 112
}

113 114
func (bsnet *impl) SetDelegate(r Receiver) {
	bsnet.receiver = r
115
}
116

117
func (bsnet *impl) ConnectTo(ctx context.Context, p peer.ID) error {
Jeromy's avatar
Jeromy committed
118
	return bsnet.host.Connect(ctx, pstore.PeerInfo{ID: p})
119 120
}

121
// FindProvidersAsync returns a channel of providers for the given key
122
func (bsnet *impl) FindProvidersAsync(ctx context.Context, k *cid.Cid, max int) <-chan peer.ID {
123 124 125 126 127 128 129 130 131

	// Since routing queries are expensive, give bitswap the peers to which we
	// have open connections. Note that this may cause issues if bitswap starts
	// precisely tracking which peers provide certain keys. This optimization
	// would be misleading. In the long run, this may not be the most
	// appropriate place for this optimization, but it won't cause any harm in
	// the short term.
	connectedPeers := bsnet.host.Network().Peers()
	out := make(chan peer.ID, len(connectedPeers)) // just enough buffer for these connectedPeers
132
	for _, id := range connectedPeers {
133 134 135
		if id == bsnet.host.ID() {
			continue // ignore self as provider
		}
136 137 138
		out <- id
	}

139 140
	go func() {
		defer close(out)
141
		providers := bsnet.routing.FindProvidersAsync(ctx, k, max)
142
		for info := range providers {
143 144
			if info.ID == bsnet.host.ID() {
				continue // ignore self as provider
145
			}
Jeromy's avatar
Jeromy committed
146
			bsnet.host.Peerstore().AddAddrs(info.ID, info.Addrs, pstore.TempAddrTTL)
147 148
			select {
			case <-ctx.Done():
149
				return
150 151 152 153 154
			case out <- info.ID:
			}
		}
	}()
	return out
155 156 157
}

// Provide provides the key to the network
158 159
func (bsnet *impl) Provide(ctx context.Context, k *cid.Cid) error {
	return bsnet.routing.Provide(ctx, k)
160 161
}

162 163
// handleNewStream receives a new stream from the network.
func (bsnet *impl) handleNewStream(s inet.Stream) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
164
	defer s.Close()
165 166 167 168 169

	if bsnet.receiver == nil {
		return
	}

Jeromy's avatar
Jeromy committed
170
	reader := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
171
	for {
Jeromy's avatar
Jeromy committed
172
		received, err := bsmsg.FromPBReader(reader)
173
		if err != nil {
Jeromy's avatar
Jeromy committed
174 175 176 177
			if err != io.EOF {
				go bsnet.receiver.ReceiveError(err)
				log.Debugf("bitswap net handleNewStream from %s error: %s", s.Conn().RemotePeer(), err)
			}
178 179
			return
		}
180

181 182 183 184 185
		p := s.Conn().RemotePeer()
		ctx := context.Background()
		log.Debugf("bitswap net handleNewStream from %s", s.Conn().RemotePeer())
		bsnet.receiver.ReceiveMessage(ctx, p, received)
	}
186
}
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

type netNotifiee impl

func (nn *netNotifiee) impl() *impl {
	return (*impl)(nn)
}

func (nn *netNotifiee) Connected(n inet.Network, v inet.Conn) {
	nn.impl().receiver.PeerConnected(v.RemotePeer())
}

func (nn *netNotifiee) Disconnected(n inet.Network, v inet.Conn) {
	nn.impl().receiver.PeerDisconnected(v.RemotePeer())
}

func (nn *netNotifiee) OpenedStream(n inet.Network, v inet.Stream) {}
func (nn *netNotifiee) ClosedStream(n inet.Network, v inet.Stream) {}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
204 205
func (nn *netNotifiee) Listen(n inet.Network, a ma.Multiaddr)      {}
func (nn *netNotifiee) ListenClose(n inet.Network, a ma.Multiaddr) {}