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

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

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

Steven Allen's avatar
Steven Allen committed
11 12 13
	host "gx/ipfs/QmP46LGWhzVZTMmt5akNNLfoV8qL4h5wTwmzQxLyDafggd/go-libp2p-host"
	routing "gx/ipfs/QmPCGUjMRuBcPybZFpjhzpifwPP9wPRoiy5geTQKU4vqWA/go-libp2p-routing"
	ifconnmgr "gx/ipfs/QmSAJm4QdTJ3EGF2cvgNcQyXTEbxqWSW1x4kCVV1aJQUQr/go-libp2p-interface-connmgr"
Jeromy's avatar
Jeromy committed
14
	logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
Steven Allen's avatar
Steven Allen committed
15 16 17 18
	inet "gx/ipfs/QmU4vCDZTPLDqSDKguWbHCiUe46mZUtmM2g2suBZ9NE8ko/go-libp2p-net"
	ma "gx/ipfs/QmW8s4zTsUoX1Q6CeYxVKPyqSKbF7H1YDUyTostBtZ8DaG/go-multiaddr"
	peer "gx/ipfs/QmWNY7dV54ZDYmTA1ykVdwNCqC11mpU4zSUp6XDpLTH9eG/go-libp2p-peer"
	pstore "gx/ipfs/QmYijbtjCxFEjSXaudaQAUz3LN5VKLssm8WCUsRoqzXmQR/go-libp2p-peerstore"
Jeromy's avatar
Jeromy committed
19
	ggio "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/io"
Steven Allen's avatar
Steven Allen committed
20
	cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
21 22
)

Jeromy's avatar
Jeromy committed
23
var log = logging.Logger("bitswap_network")
Jeromy's avatar
Jeromy committed
24

25 26
var sendMessageTimeout = time.Minute * 10

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

39
	return &bitswapNetwork
40 41
}

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

	// inbound messages from the network are forwarded to the receiver
	receiver Receiver
50 51
}

Jeromy's avatar
Jeromy committed
52 53 54 55 56 57 58 59
type streamMessageSender struct {
	s inet.Stream
}

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

60 61 62 63
func (s *streamMessageSender) Reset() error {
	return s.s.Reset()
}

64 65
func (s *streamMessageSender) SendMsg(ctx context.Context, msg bsmsg.BitSwapMessage) error {
	return msgToStream(ctx, s.s, msg)
66 67
}

68 69 70 71 72 73 74 75 76 77
func msgToStream(ctx context.Context, s inet.Stream, msg bsmsg.BitSwapMessage) error {
	deadline := time.Now().Add(sendMessageTimeout)
	if dl, ok := ctx.Deadline(); ok {
		deadline = dl
	}

	if err := s.SetWriteDeadline(deadline); err != nil {
		log.Warningf("error setting deadline: %s", err)
	}

78 79 80 81 82 83 84 85 86 87 88 89 90 91
	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())
	}
92 93 94 95

	if err := s.SetWriteDeadline(time.Time{}); err != nil {
		log.Warningf("error resetting deadline: %s", err)
	}
96
	return nil
Jeromy's avatar
Jeromy committed
97 98 99 100 101 102 103 104 105 106 107
}

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
}

108
func (bsnet *impl) newStreamToPeer(ctx context.Context, p peer.ID) (inet.Stream, error) {
109
	return bsnet.host.NewStream(ctx, p, ProtocolBitswap, ProtocolBitswapOne, ProtocolBitswapNoVers)
110 111 112 113 114 115 116 117
}

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

	s, err := bsnet.newStreamToPeer(ctx, p)
118 119 120
	if err != nil {
		return err
	}
121

122 123 124 125 126 127 128
	err = msgToStream(ctx, s, outgoing)
	if err != nil {
		s.Reset()
	} else {
		s.Close()
	}
	return err
129 130
}

131 132
func (bsnet *impl) SetDelegate(r Receiver) {
	bsnet.receiver = r
133
}
134

135
func (bsnet *impl) ConnectTo(ctx context.Context, p peer.ID) error {
Jeromy's avatar
Jeromy committed
136
	return bsnet.host.Connect(ctx, pstore.PeerInfo{ID: p})
137 138
}

139
// FindProvidersAsync returns a channel of providers for the given key
140
func (bsnet *impl) FindProvidersAsync(ctx context.Context, k *cid.Cid, max int) <-chan peer.ID {
141 142 143 144 145 146 147 148 149

	// 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
150
	for _, id := range connectedPeers {
151 152 153
		if id == bsnet.host.ID() {
			continue // ignore self as provider
		}
154 155 156
		out <- id
	}

157 158
	go func() {
		defer close(out)
159
		providers := bsnet.routing.FindProvidersAsync(ctx, k, max)
160
		for info := range providers {
161 162
			if info.ID == bsnet.host.ID() {
				continue // ignore self as provider
163
			}
Jeromy's avatar
Jeromy committed
164
			bsnet.host.Peerstore().AddAddrs(info.ID, info.Addrs, pstore.TempAddrTTL)
165 166
			select {
			case <-ctx.Done():
167
				return
168 169 170 171 172
			case out <- info.ID:
			}
		}
	}()
	return out
173 174 175
}

// Provide provides the key to the network
176
func (bsnet *impl) Provide(ctx context.Context, k *cid.Cid) error {
177
	return bsnet.routing.Provide(ctx, k, true)
178 179
}

180 181
// handleNewStream receives a new stream from the network.
func (bsnet *impl) handleNewStream(s inet.Stream) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
182
	defer s.Close()
183 184

	if bsnet.receiver == nil {
185
		s.Reset()
186 187 188
		return
	}

Jeromy's avatar
Jeromy committed
189
	reader := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
190
	for {
Jeromy's avatar
Jeromy committed
191
		received, err := bsmsg.FromPBReader(reader)
192
		if err != nil {
Jeromy's avatar
Jeromy committed
193
			if err != io.EOF {
194
				s.Reset()
Jeromy's avatar
Jeromy committed
195 196 197
				go bsnet.receiver.ReceiveError(err)
				log.Debugf("bitswap net handleNewStream from %s error: %s", s.Conn().RemotePeer(), err)
			}
198 199
			return
		}
200

201 202 203 204 205
		p := s.Conn().RemotePeer()
		ctx := context.Background()
		log.Debugf("bitswap net handleNewStream from %s", s.Conn().RemotePeer())
		bsnet.receiver.ReceiveMessage(ctx, p, received)
	}
206
}
207

208 209 210 211
func (bsnet *impl) ConnectionManager() ifconnmgr.ConnManager {
	return bsnet.host.ConnManager()
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
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
228 229
func (nn *netNotifiee) Listen(n inet.Network, a ma.Multiaddr)      {}
func (nn *netNotifiee) ListenClose(n inet.Network, a ma.Multiaddr) {}