ipfs_impl.go 5.85 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
	}
34
	return &bitswapNetwork
35 36
}

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

	// inbound messages from the network are forwarded to the receiver
	receiver Receiver
45

46
	stats Stats
47 48
}

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

func (s *streamMessageSender) Close() error {
Steven Allen's avatar
Steven Allen committed
54
	return inet.FullClose(s.s)
Jeromy's avatar
Jeromy committed
55 56
}

57 58 59 60
func (s *streamMessageSender) Reset() error {
	return s.s.Reset()
}

61 62
func (s *streamMessageSender) SendMsg(ctx context.Context, msg bsmsg.BitSwapMessage) error {
	return msgToStream(ctx, s.s, msg)
63 64
}

65 66 67 68 69
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
70

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

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

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

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
}

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

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

	s, err := bsnet.newStreamToPeer(ctx, p)
115 116 117
	if err != nil {
		return err
	}
118

Steven Allen's avatar
Steven Allen committed
119
	if err = msgToStream(ctx, s, outgoing); err != nil {
120
		s.Reset()
Steven Allen's avatar
Steven Allen committed
121
		return err
122
	}
123 124
	atomic.AddUint64(&bsnet.stats.MessagesSent, 1)

125 126 127 128
	// TODO(https://github.com/libp2p/go-libp2p-net/issues/28): Avoid this goroutine.
	go inet.AwaitEOF(s)
	return s.Close()

129 130
}

131 132
func (bsnet *impl) SetDelegate(r Receiver) {
	bsnet.receiver = r
hannahhoward's avatar
hannahhoward committed
133 134 135 136 137 138
	bsnet.host.SetStreamHandler(ProtocolBitswap, bsnet.handleNewStream)
	bsnet.host.SetStreamHandler(ProtocolBitswapOne, bsnet.handleNewStream)
	bsnet.host.SetStreamHandler(ProtocolBitswapNoVers, bsnet.handleNewStream)
	bsnet.host.Network().Notify((*netNotifiee)(bsnet))
	// TODO: StopNotify.

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
func (bsnet *impl) Stats() Stats {
	return Stats{
206 207 208 209 210
		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) {}