bitswap.go 4.77 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package bitswap

import (
4
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
5 6
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7
	blocks "github.com/jbenet/go-ipfs/blocks"
8
	blockstore "github.com/jbenet/go-ipfs/blockstore"
9 10 11 12 13
	exchange "github.com/jbenet/go-ipfs/exchange"
	bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
	bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
	notifications "github.com/jbenet/go-ipfs/exchange/bitswap/notifications"
	strategy "github.com/jbenet/go-ipfs/exchange/bitswap/strategy"
14
	peer "github.com/jbenet/go-ipfs/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16 17
)

18 19 20
// TODO rename -> Router?
type Routing interface {
	// FindProvidersAsync returns a channel of providers for the given key
21
	FindProvidersAsync(context.Context, u.Key, int) <-chan *peer.Peer
22 23 24 25 26

	// Provide provides the key to the network
	Provide(key u.Key) error
}

27 28 29
// NetMessageSession initializes a BitSwap session that communicates over the
// provided NetMessage service
func NetMessageSession(parent context.Context, s bsnet.NetMessageService, p *peer.Peer, d ds.Datastore, directory Routing) exchange.Interface {
30

31
	networkAdapter := bsnet.NetMessageAdapter(s, nil)
32 33 34 35 36
	bs := &bitswap{
		blockstore:    blockstore.NewBlockstore(d),
		notifications: notifications.New(),
		strategy:      strategy.New(),
		routing:       directory,
37
		sender:        networkAdapter,
38
	}
39
	networkAdapter.SetDelegate(bs)
40 41 42 43

	return bs
}

44 45
// bitswap instances implement the bitswap protocol.
type bitswap struct {
46

47
	// sender delivers messages on behalf of the session
48
	sender bsnet.Adapter
49

50
	// blockstore is the local database
51
	// NB: ensure threadsafety
52
	blockstore blockstore.Blockstore
53

54
	// routing interface for communication
55
	routing Routing
56

57
	notifications notifications.PubSub
58

59
	// strategy listens to network traffic and makes decisions about how to
60
	// interact with partners.
61 62
	// TODO(brian): save the strategy's state to the datastore
	strategy strategy.Strategy
63 64
}

65 66
// GetBlock attempts to retrieve a particular block from peers within the
// deadline enforced by the context
67 68
//
// TODO ensure only one active request per key
69
func (bs *bitswap) Block(parent context.Context, k u.Key) (*blocks.Block, error) {
70

71 72
	ctx, cancelFunc := context.WithCancel(parent)
	promise := bs.notifications.Subscribe(ctx, k)
73

74
	go func() {
75 76 77 78 79 80 81
		const maxProviders = 20
		peersToQuery := bs.routing.FindProvidersAsync(ctx, k, maxProviders)
		message := bsmsg.New()
		message.AppendWanted(k)
		for i := range peersToQuery {
			go func(p *peer.Peer) {
				response, err := bs.sender.SendRequest(ctx, p, message)
82 83 84
				if err != nil {
					return
				}
85 86 87 88 89 90 91
				// FIXME ensure accounting is handled correctly when
				// communication fails. May require slightly different API to
				// get better guarantees. May need shared sequence numbers.
				bs.strategy.MessageSent(p, message)

				bs.ReceiveMessage(ctx, p, response)
			}(i)
92 93
		}
	}()
94 95

	select {
96 97
	case block := <-promise:
		cancelFunc()
98
		return &block, nil
99 100
	case <-parent.Done():
		return nil, parent.Err()
101 102 103
	}
}

104
// HasBlock announces the existance of a block to bitswap, potentially sending
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
105
// it to peers (Partners) whose WantLists include it.
106
func (bs *bitswap) HasBlock(ctx context.Context, blk blocks.Block) error {
107
	go bs.sendToPeersThatWant(ctx, blk)
108 109 110
	return bs.routing.Provide(blk.Key())
}

111
// TODO(brian): handle errors
112
func (bs *bitswap) ReceiveMessage(
113
	ctx context.Context, p *peer.Peer, incoming bsmsg.BitSwapMessage) (
114
	*peer.Peer, bsmsg.BitSwapMessage, error) {
115

116
	bs.strategy.MessageReceived(p, incoming)
117

118 119
	if incoming.Blocks() != nil {
		for _, block := range incoming.Blocks() {
120 121
			go bs.blockstore.Put(block) // FIXME(brian): err ignored
			go bs.notifications.Publish(block)
122
			go bs.HasBlock(ctx, block) // FIXME err ignored
123 124 125 126
		}
	}

	if incoming.Wantlist() != nil {
127
		for _, key := range incoming.Wantlist() {
128
			if bs.strategy.ShouldSendBlockToPeer(key, p) {
129 130 131 132 133
				block, errBlockNotFound := bs.blockstore.Get(key)
				if errBlockNotFound != nil {
					// TODO(brian): log/return the error
					continue
				}
134 135 136
				message := bsmsg.New()
				message.AppendBlock(*block)
				go bs.send(ctx, p, message)
137
			}
138 139
		}
	}
140
	return nil, nil, nil
141
}
142

143 144 145
// send strives to ensure that accounting is always performed when a message is
// sent
func (bs *bitswap) send(ctx context.Context, p *peer.Peer, m bsmsg.BitSwapMessage) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
146
	bs.sender.SendMessage(ctx, p, m)
147
	bs.strategy.MessageSent(p, m)
148 149
}

150 151 152
func numBytes(b blocks.Block) int {
	return len(b.Data)
}
153 154 155 156 157 158 159 160 161 162 163 164

func (bs *bitswap) sendToPeersThatWant(ctx context.Context, block blocks.Block) {
	for _, p := range bs.strategy.Peers() {
		if bs.strategy.BlockIsWantedByPeer(block.Key(), p) {
			if bs.strategy.ShouldSendBlockToPeer(block.Key(), p) {
				message := bsmsg.New()
				message.AppendBlock(block)
				go bs.send(ctx, p, message)
			}
		}
	}
}