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

import (
4
	"errors"
5
	"fmt"
6

7
	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
8 9
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
	blocks "github.com/jbenet/go-ipfs/blocks"
11
	blockstore "github.com/jbenet/go-ipfs/blockstore"
12 13 14 15 16
	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"
17
	peer "github.com/jbenet/go-ipfs/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19 20
)

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

25
	networkAdapter := bsnet.NetMessageAdapter(s, nil)
26 27 28 29 30
	bs := &bitswap{
		blockstore:    blockstore.NewBlockstore(d),
		notifications: notifications.New(),
		strategy:      strategy.New(),
		routing:       directory,
31
		sender:        networkAdapter,
32
	}
33
	networkAdapter.SetDelegate(bs)
34 35 36 37

	return bs
}

38 39
// bitswap instances implement the bitswap protocol.
type bitswap struct {
40

41
	// sender delivers messages on behalf of the session
42
	sender bsnet.Adapter
43

44
	// blockstore is the local database
45
	// NB: ensure threadsafety
46
	blockstore blockstore.Blockstore
47

48
	// routing interface for communication
49
	routing bsnet.Routing
50

51
	notifications notifications.PubSub
52

53
	// strategy listens to network traffic and makes decisions about how to
54
	// interact with partners.
55 56
	// TODO(brian): save the strategy's state to the datastore
	strategy strategy.Strategy
57 58
}

59 60
// GetBlock attempts to retrieve a particular block from peers within the
// deadline enforced by the context
61 62
//
// TODO ensure only one active request per key
63
func (bs *bitswap) Block(parent context.Context, k u.Key) (*blocks.Block, error) {
64

65
	ctx, cancelFunc := context.WithCancel(parent)
66
	// TODO add to wantlist
67
	promise := bs.notifications.Subscribe(ctx, k)
68

69 70 71 72 73 74 75
	// const maxProviders = 20
	// using non-async version for now.
	peersToQuery, err := bs.routing.FindProviders(ctx, k)
	if err != nil {
		return nil, fmt.Errorf("No providers found for %d (%v)", k, err)
	}

76
	go func() {
77 78
		message := bsmsg.New()
		message.AppendWanted(k)
79 80
		for _, iiiii := range peersToQuery {
			// u.DOut("bitswap got peersToQuery: %s\n", iiiii)
81
			go func(p *peer.Peer) {
82
				response, err := bs.sender.SendRequest(ctx, p, message)
83 84 85
				if err != nil {
					return
				}
86 87 88 89 90
				// 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)

91 92 93
				if response == nil {
					return
				}
94
				bs.ReceiveMessage(ctx, p, response)
95
			}(iiiii)
96 97
		}
	}()
98 99

	select {
100 101
	case block := <-promise:
		cancelFunc()
102
		// TODO remove from wantlist
103
		return &block, nil
104 105
	case <-parent.Done():
		return nil, parent.Err()
106 107 108
	}
}

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

116
// TODO(brian): handle errors
117
func (bs *bitswap) ReceiveMessage(ctx context.Context, p *peer.Peer, incoming bsmsg.BitSwapMessage) (
118
	*peer.Peer, bsmsg.BitSwapMessage, error) {
119

120 121 122 123 124 125
	if p == nil {
		return nil, nil, errors.New("Received nil Peer")
	}
	if incoming == nil {
		return nil, nil, errors.New("Received nil Message")
	}
126

127
	bs.strategy.MessageReceived(p, incoming) // FIRST
128

129
	for _, block := range incoming.Blocks() {
130 131 132
		// TODO verify blocks?
		if err := bs.blockstore.Put(block); err != nil {
			continue // FIXME(brian): err ignored
133
		}
134
		go bs.notifications.Publish(block)
135
		go func(block blocks.Block) {
136
			_ = bs.HasBlock(ctx, block) // FIXME err ignored
137
		}(block)
138 139
	}

140 141 142 143 144
	for _, key := range incoming.Wantlist() {
		if bs.strategy.ShouldSendBlockToPeer(key, p) {
			block, errBlockNotFound := bs.blockstore.Get(key)
			if errBlockNotFound != nil {
				return nil, nil, errBlockNotFound
145
			}
146 147 148 149
			message := bsmsg.New()
			message.AppendBlock(*block)
			defer bs.strategy.MessageSent(p, message)
			return p, message, nil
150 151
		}
	}
152
	return nil, nil, nil
153
}
154

155 156 157
// 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
158
	bs.sender.SendMessage(ctx, p, m)
159
	go bs.strategy.MessageSent(p, m)
160 161
}

162 163 164 165 166 167
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)
168
				go bs.send(ctx, p, message)
169 170 171 172
			}
		}
	}
}