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

import (
4 5 6 7 8 9 10 11 12
	ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
	bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
	host "github.com/ipfs/go-ipfs/p2p/host"
	inet "github.com/ipfs/go-ipfs/p2p/net"
	peer "github.com/ipfs/go-ipfs/p2p/peer"
	routing "github.com/ipfs/go-ipfs/routing"
	eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
	util "github.com/ipfs/go-ipfs/util"
13 14
)

15
var log = eventlog.Logger("bitswap_network")
Jeromy's avatar
Jeromy committed
16

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

27
	return &bitswapNetwork
28 29
}

30 31
// impl transforms the ipfs network interface, which sends and receives
// NetMessage objects, into the bitswap network interface.
32
type impl struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
	host    host.Host
34
	routing routing.IpfsRouting
35 36 37

	// inbound messages from the network are forwarded to the receiver
	receiver Receiver
38 39
}

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

42 43
	// first, make sure we're connected.
	// if this fails, we cannot connect to given peer.
44 45
	//TODO(jbenet) move this into host.NewStream?
	if err := bsnet.host.Connect(ctx, peer.PeerInfo{ID: p}); err != nil {
46
		return nil, err
47 48
	}

49 50 51 52 53 54 55 56 57
	return bsnet.host.NewStream(ProtocolBitswap, p)
}

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

	s, err := bsnet.newStreamToPeer(ctx, p)
58 59 60
	if err != nil {
		return err
	}
61 62
	defer s.Close()

63
	if err := outgoing.ToNet(s); err != nil {
64
		log.Debugf("error: %s", err)
65 66 67 68
		return err
	}

	return err
69 70
}

71
func (bsnet *impl) SendRequest(
72
	ctx context.Context,
73
	p peer.ID,
74 75
	outgoing bsmsg.BitSwapMessage) (bsmsg.BitSwapMessage, error) {

76
	s, err := bsnet.newStreamToPeer(ctx, p)
77 78 79
	if err != nil {
		return nil, err
	}
80 81 82
	defer s.Close()

	if err := outgoing.ToNet(s); err != nil {
83
		log.Debugf("error: %s", err)
84 85
		return nil, err
	}
86

87 88
	incoming, err := bsmsg.FromNet(s)
	if err != nil {
89
		log.Debugf("error: %s", err)
90 91 92 93
		return incoming, err
	}

	return incoming, nil
94 95
}

96 97
func (bsnet *impl) SetDelegate(r Receiver) {
	bsnet.receiver = r
98
}
99

100
// FindProvidersAsync returns a channel of providers for the given key
101
func (bsnet *impl) FindProvidersAsync(ctx context.Context, k util.Key, max int) <-chan peer.ID {
102 103 104 105 106 107 108 109 110

	// 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
111
	for _, id := range connectedPeers {
112 113 114
		if id == bsnet.host.ID() {
			continue // ignore self as provider
		}
115 116 117
		out <- id
	}

118 119 120 121
	go func() {
		defer close(out)
		providers := bsnet.routing.FindProvidersAsync(ctx, k, max)
		for info := range providers {
122 123
			if info.ID == bsnet.host.ID() {
				continue // ignore self as provider
124
			}
125
			bsnet.host.Peerstore().AddAddrs(info.ID, info.Addrs, peer.TempAddrTTL)
126 127
			select {
			case <-ctx.Done():
128
				return
129 130 131 132 133
			case out <- info.ID:
			}
		}
	}()
	return out
134 135 136 137 138 139 140
}

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

141 142
// handleNewStream receives a new stream from the network.
func (bsnet *impl) handleNewStream(s inet.Stream) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
143
	defer s.Close()
144 145 146 147 148

	if bsnet.receiver == nil {
		return
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
149 150 151
	received, err := bsmsg.FromNet(s)
	if err != nil {
		go bsnet.receiver.ReceiveError(err)
152
		log.Debugf("bitswap net handleNewStream from %s error: %s", s.Conn().RemotePeer(), err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
153 154
		return
	}
155

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
156 157
	p := s.Conn().RemotePeer()
	ctx := context.Background()
158
	log.Debugf("bitswap net handleNewStream from %s", s.Conn().RemotePeer())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
159
	bsnet.receiver.ReceiveMessage(ctx, p, received)
160
}
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

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