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

import (
Jeromy's avatar
Jeromy committed
4 5
	"io"

6
	key "github.com/ipfs/go-ipfs/blocks/key"
7 8
	bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
	routing "github.com/ipfs/go-ipfs/routing"
Jeromy's avatar
Jeromy committed
9 10

	peer "gx/ipfs/QmQGwpJy9P4yXZySmqkZEXCmbBpJUb8xntCv8Ca4taZwDC/go-libp2p-peer"
Jeromy's avatar
Jeromy committed
11
	pstore "gx/ipfs/QmXHUpFsnpCmanRnacqYkFoLoFfEq5yS2nUgGkAjJ1Nj9j/go-libp2p-peerstore"
Jakub Sztandera's avatar
Jakub Sztandera committed
12
	logging "gx/ipfs/QmYtB7Qge8cJpXc4irsEp8zRqfnZMBeB7aTrMEkPk67DRv/go-log"
13
	ma "gx/ipfs/QmYzDkkgAEmrcNzFCiYo6L1dTX4EAG1gZkbtdbd9trL4vd/go-multiaddr"
Jeromy's avatar
Jeromy committed
14
	ggio "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/io"
Jeromy's avatar
Jeromy committed
15
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
Jeromy's avatar
Jeromy committed
16 17
	host "gx/ipfs/QmdBpVuSYuTGDA8Kn66CbKvEThXqKUh2nTANZEhzSxqrmJ/go-libp2p/p2p/host"
	inet "gx/ipfs/QmdBpVuSYuTGDA8Kn66CbKvEThXqKUh2nTANZEhzSxqrmJ/go-libp2p/p2p/net"
18 19
)

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

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

32
	return &bitswapNetwork
33 34
}

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

	// inbound messages from the network are forwarded to the receiver
	receiver Receiver
43 44
}

Jeromy's avatar
Jeromy committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
type streamMessageSender struct {
	s inet.Stream
}

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

func (s *streamMessageSender) SendMsg(msg bsmsg.BitSwapMessage) error {
	return msg.ToNet(s.s)
}

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
}

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

68 69
	// first, make sure we're connected.
	// if this fails, we cannot connect to given peer.
70
	//TODO(jbenet) move this into host.NewStream?
Jeromy's avatar
Jeromy committed
71
	if err := bsnet.host.Connect(ctx, pstore.PeerInfo{ID: p}); err != nil {
72
		return nil, err
73 74
	}

75
	return bsnet.host.NewStream(ctx, ProtocolBitswap, p)
76 77 78 79 80 81 82 83
}

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

	s, err := bsnet.newStreamToPeer(ctx, p)
84 85 86
	if err != nil {
		return err
	}
87 88
	defer s.Close()

89
	if err := outgoing.ToNet(s); err != nil {
90
		log.Debugf("error: %s", err)
91 92 93 94
		return err
	}

	return err
95 96
}

97
func (bsnet *impl) SendRequest(
98
	ctx context.Context,
99
	p peer.ID,
100 101
	outgoing bsmsg.BitSwapMessage) (bsmsg.BitSwapMessage, error) {

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

	if err := outgoing.ToNet(s); err != nil {
109
		log.Debugf("error: %s", err)
110 111
		return nil, err
	}
112

113 114
	incoming, err := bsmsg.FromNet(s)
	if err != nil {
115
		log.Debugf("error: %s", err)
116 117 118 119
		return incoming, err
	}

	return incoming, nil
120 121
}

122 123
func (bsnet *impl) SetDelegate(r Receiver) {
	bsnet.receiver = r
124
}
125

126
func (bsnet *impl) ConnectTo(ctx context.Context, p peer.ID) error {
Jeromy's avatar
Jeromy committed
127
	return bsnet.host.Connect(ctx, pstore.PeerInfo{ID: p})
128 129
}

130
// FindProvidersAsync returns a channel of providers for the given key
131
func (bsnet *impl) FindProvidersAsync(ctx context.Context, k key.Key, max int) <-chan peer.ID {
132 133 134 135 136 137 138 139 140

	// 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
141
	for _, id := range connectedPeers {
142 143 144
		if id == bsnet.host.ID() {
			continue // ignore self as provider
		}
145 146 147
		out <- id
	}

148 149 150 151
	go func() {
		defer close(out)
		providers := bsnet.routing.FindProvidersAsync(ctx, k, max)
		for info := range providers {
152 153
			if info.ID == bsnet.host.ID() {
				continue // ignore self as provider
154
			}
Jeromy's avatar
Jeromy committed
155
			bsnet.host.Peerstore().AddAddrs(info.ID, info.Addrs, pstore.TempAddrTTL)
156 157
			select {
			case <-ctx.Done():
158
				return
159 160 161 162 163
			case out <- info.ID:
			}
		}
	}()
	return out
164 165 166
}

// Provide provides the key to the network
167
func (bsnet *impl) Provide(ctx context.Context, k key.Key) error {
168 169 170
	return bsnet.routing.Provide(ctx, k)
}

171 172
// handleNewStream receives a new stream from the network.
func (bsnet *impl) handleNewStream(s inet.Stream) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
173
	defer s.Close()
174 175 176 177 178

	if bsnet.receiver == nil {
		return
	}

Jeromy's avatar
Jeromy committed
179
	reader := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
180
	for {
Jeromy's avatar
Jeromy committed
181
		received, err := bsmsg.FromPBReader(reader)
182
		if err != nil {
Jeromy's avatar
Jeromy committed
183 184 185 186
			if err != io.EOF {
				go bsnet.receiver.ReceiveError(err)
				log.Debugf("bitswap net handleNewStream from %s error: %s", s.Conn().RemotePeer(), err)
			}
187 188
			return
		}
189

190 191 192 193 194
		p := s.Conn().RemotePeer()
		ctx := context.Background()
		log.Debugf("bitswap net handleNewStream from %s", s.Conn().RemotePeer())
		bsnet.receiver.ReceiveMessage(ctx, p, received)
	}
195
}
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

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
213 214
func (nn *netNotifiee) Listen(n inet.Network, a ma.Multiaddr)      {}
func (nn *netNotifiee) ListenClose(n inet.Network, a ma.Multiaddr) {}