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

import (
4
	"errors"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
5

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

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

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

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

30 31 32
// NewSession initializes a bitswap session.
func NewSession(parent context.Context, s bsnet.NetworkService, p *peer.Peer, d ds.Datastore, directory Routing) exchange.Interface {

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

	return bs
}

46 47
// bitswap instances implement the bitswap protocol.
type bitswap struct {
48

49
	// sender delivers messages on behalf of the session
50
	sender bsnet.NetworkAdapter
51

52
	// blockstore is the local database
53
	// NB: ensure threadsafety
54
	blockstore blockstore.Blockstore
55

56
	// routing interface for communication
57
	routing Routing
58

59
	notifications notifications.PubSub
60

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

67 68 69
// GetBlock attempts to retrieve a particular block from peers within the
// deadline enforced by the context
func (bs *bitswap) Block(ctx context.Context, k u.Key) (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70
	*blocks.Block, error) {
71

72
	provs_ch := bs.routing.FindProvidersAsync(ctx, k, 20)
73

74
	blockChannel := make(chan blocks.Block)
75

76
	// TODO: when the data is received, shut down this for loop ASAP
77 78 79
	go func() {
		for p := range provs_ch {
			go func(pr *peer.Peer) {
80
				blk, err := bs.getBlock(ctx, k, pr)
81 82 83 84
				if err != nil {
					return
				}
				select {
85
				case blockChannel <- *blk:
86 87 88 89 90
				default:
				}
			}(p)
		}
	}()
91 92

	select {
93 94
	case block := <-blockChannel:
		close(blockChannel)
95
		return &block, nil
96 97
	case <-ctx.Done():
		return nil, ctx.Err()
98
	}
99 100
}

101
func (bs *bitswap) getBlock(ctx context.Context, k u.Key, p *peer.Peer) (*blocks.Block, error) {
102

103 104
	blockChannel := bs.notifications.Subscribe(ctx, k)

105
	message := bsmsg.New()
106
	message.AppendWanted(k)
107

108
	bs.send(ctx, p, message)
109

110 111
	block, ok := <-blockChannel
	if !ok {
112 113
		return nil, u.ErrTimeout
	}
114
	return &block, nil
115 116
}

117
func (bs *bitswap) sendToPeersThatWant(ctx context.Context, block blocks.Block) {
118 119 120
	for _, p := range bs.strategy.Peers() {
		if bs.strategy.BlockIsWantedByPeer(block.Key(), p) {
			if bs.strategy.ShouldSendBlockToPeer(block.Key(), p) {
121 122 123
				message := bsmsg.New()
				message.AppendBlock(block)
				go bs.send(ctx, p, message)
124 125 126 127 128
			}
		}
	}
}

129
// HasBlock announces the existance of a block to bitswap, potentially sending
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
130
// it to peers (Partners) whose WantLists include it.
131
func (bs *bitswap) HasBlock(ctx context.Context, blk blocks.Block) error {
132
	go bs.sendToPeersThatWant(ctx, blk)
133 134 135
	return bs.routing.Provide(blk.Key())
}

136
// TODO(brian): handle errors
137
func (bs *bitswap) ReceiveMessage(
138
	ctx context.Context, p *peer.Peer, incoming bsmsg.BitSwapMessage) (
139
	*peer.Peer, bsmsg.BitSwapMessage, error) {
140

141
	bs.strategy.MessageReceived(p, incoming)
142

143 144
	if incoming.Blocks() != nil {
		for _, block := range incoming.Blocks() {
145 146
			go bs.blockstore.Put(block) // FIXME(brian): err ignored
			go bs.notifications.Publish(block)
147 148 149 150
		}
	}

	if incoming.Wantlist() != nil {
151
		for _, key := range incoming.Wantlist() {
152
			if bs.strategy.ShouldSendBlockToPeer(key, p) {
153 154 155 156 157
				block, errBlockNotFound := bs.blockstore.Get(key)
				if errBlockNotFound != nil {
					// TODO(brian): log/return the error
					continue
				}
158 159 160
				message := bsmsg.New()
				message.AppendBlock(*block)
				go bs.send(ctx, p, message)
161
			}
162 163
		}
	}
164 165
	return nil, nil, errors.New("TODO implement")
}
166

167 168 169
// 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
170
	bs.sender.SendMessage(ctx, p, m)
171
	bs.strategy.MessageSent(p, m)
172 173
}

174 175 176
func numBytes(b blocks.Block) int {
	return len(b.Data)
}