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

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

Jeromy's avatar
Jeromy committed
10 11 12 13 14 15 16 17 18 19 20 21
	bsmsg "github.com/ipfs/go-bitswap/message"

	ggio "github.com/gogo/protobuf/io"
	cid "github.com/ipfs/go-cid"
	logging "github.com/ipfs/go-log"
	host "github.com/libp2p/go-libp2p-host"
	ifconnmgr "github.com/libp2p/go-libp2p-interface-connmgr"
	inet "github.com/libp2p/go-libp2p-net"
	peer "github.com/libp2p/go-libp2p-peer"
	pstore "github.com/libp2p/go-libp2p-peerstore"
	routing "github.com/libp2p/go-libp2p-routing"
	ma "github.com/multiformats/go-multiaddr"
22 23
)

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

26 27
var sendMessageTimeout = time.Minute * 10

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

40
	return &bitswapNetwork
41 42
}

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

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

	stats NetworkStats
53 54
}

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

func (s *streamMessageSender) Close() error {
Steven Allen's avatar
Steven Allen committed
60
	return inet.FullClose(s.s)
Jeromy's avatar
Jeromy committed
61 62
}

63 64 65 66
func (s *streamMessageSender) Reset() error {
	return s.s.Reset()
}

67 68
func (s *streamMessageSender) SendMsg(ctx context.Context, msg bsmsg.BitSwapMessage) error {
	return msgToStream(ctx, s.s, msg)
69 70
}

71 72 73 74 75
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
	}
Bob Potter's avatar
Bob Potter committed
76

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

81 82
	switch s.Protocol() {
	case ProtocolBitswap:
Bob Potter's avatar
Bob Potter committed
83
		if err := msg.ToNetV1(s); err != nil {
84 85 86 87
			log.Debugf("error: %s", err)
			return err
		}
	case ProtocolBitswapOne, ProtocolBitswapNoVers:
Bob Potter's avatar
Bob Potter committed
88
		if err := msg.ToNetV0(s); err != nil {
89 90 91 92 93 94
			log.Debugf("error: %s", err)
			return err
		}
	default:
		return fmt.Errorf("unrecognized protocol on remote: %s", s.Protocol())
	}
95 96 97 98

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

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
}

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

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

	s, err := bsnet.newStreamToPeer(ctx, p)
121 122 123
	if err != nil {
		return err
	}
124

Steven Allen's avatar
Steven Allen committed
125
	if err = msgToStream(ctx, s, outgoing); err != nil {
126
		s.Reset()
Steven Allen's avatar
Steven Allen committed
127
		return err
128
	}
129 130
	atomic.AddUint64(&bsnet.stats.MessagesSent, 1)

131 132 133 134
	// TODO(https://github.com/libp2p/go-libp2p-net/issues/28): Avoid this goroutine.
	go inet.AwaitEOF(s)
	return s.Close()

135 136
}

137 138
func (bsnet *impl) SetDelegate(r Receiver) {
	bsnet.receiver = r
139
}
140

141
func (bsnet *impl) ConnectTo(ctx context.Context, p peer.ID) error {
Jeromy's avatar
Jeromy committed
142
	return bsnet.host.Connect(ctx, pstore.PeerInfo{ID: p})
143 144
}

145
// FindProvidersAsync returns a channel of providers for the given key.
146
func (bsnet *impl) FindProvidersAsync(ctx context.Context, k cid.Cid, max int) <-chan peer.ID {
147
	out := make(chan peer.ID, max)
148 149
	go func() {
		defer close(out)
150
		providers := bsnet.routing.FindProvidersAsync(ctx, k, max)
151
		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 cid.Cid) error {
168
	return bsnet.routing.Provide(ctx, k, true)
169 170
}

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

	if bsnet.receiver == nil {
176
		s.Reset()
177 178 179
		return
	}

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

192 193 194 195
		p := s.Conn().RemotePeer()
		ctx := context.Background()
		log.Debugf("bitswap net handleNewStream from %s", s.Conn().RemotePeer())
		bsnet.receiver.ReceiveMessage(ctx, p, received)
196
		atomic.AddUint64(&bsnet.stats.MessagesRecvd, 1)
197
	}
198
}
199

200 201 202 203
func (bsnet *impl) ConnectionManager() ifconnmgr.ConnManager {
	return bsnet.host.ConnManager()
}

204 205 206 207 208 209 210
func (bsnet *impl) Stats() NetworkStats {
	return NetworkStats{
		MessagesRecvd: atomic.LoadUint64(&bsnet.stats.MessagesRecvd),
		MessagesSent:  atomic.LoadUint64(&bsnet.stats.MessagesSent),
	}
}

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