strategy.go 3.09 KB
Newer Older
1 2 3 4
package strategy

import (
	"errors"
5
	"sync"
6 7 8 9 10 11

	bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
	"github.com/jbenet/go-ipfs/peer"
	u "github.com/jbenet/go-ipfs/util"
)

12 13 14 15 16 17 18 19 20 21
// TODO niceness should be on a per-peer basis. Use-case: Certain peers are
// "trusted" and/or controlled by a single human user. The user may want for
// these peers to exchange data freely
func New(nice bool) Strategy {
	var stratFunc strategyFunc
	if nice {
		stratFunc = yesManStrategy
	} else {
		stratFunc = standardStrategy
	}
22 23
	return &strategist{
		ledgerMap:    ledgerMap{},
24
		strategyFunc: stratFunc,
25 26 27 28
	}
}

type strategist struct {
29
	lock sync.RWMutex
30 31 32 33 34 35 36 37 38 39 40
	ledgerMap
	strategyFunc
}

// LedgerMap lists Ledgers by their Partner key.
type ledgerMap map[peerKey]*ledger

// FIXME share this externally
type peerKey u.Key

// Peers returns a list of peers
41
func (s *strategist) Peers() []peer.Peer {
42 43 44
	s.lock.RLock()
	defer s.lock.RUnlock()

45
	response := make([]peer.Peer, 0)
46 47 48 49 50 51
	for _, ledger := range s.ledgerMap {
		response = append(response, ledger.Partner)
	}
	return response
}

52
func (s *strategist) BlockIsWantedByPeer(k u.Key, p peer.Peer) bool {
53 54 55
	s.lock.RLock()
	defer s.lock.RUnlock()

56 57 58 59
	ledger := s.ledger(p)
	return ledger.WantListContains(k)
}

60
func (s *strategist) ShouldSendBlockToPeer(k u.Key, p peer.Peer) bool {
61 62 63
	s.lock.RLock()
	defer s.lock.RUnlock()

64 65 66 67 68
	ledger := s.ledger(p)
	return ledger.ShouldSend()
}

func (s *strategist) Seed(int64) {
69 70 71
	s.lock.Lock()
	defer s.lock.Unlock()

72 73 74
	// TODO
}

75
func (s *strategist) MessageReceived(p peer.Peer, m bsmsg.BitSwapMessage) error {
76 77 78
	s.lock.Lock()
	defer s.lock.Unlock()

Brian Tiger Chow's avatar
Brian Tiger Chow committed
79 80 81 82 83 84 85
	// TODO find a more elegant way to handle this check
	if p == nil {
		return errors.New("Strategy received nil peer")
	}
	if m == nil {
		return errors.New("Strategy received nil message")
	}
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	l := s.ledger(p)
	for _, key := range m.Wantlist() {
		l.Wants(key)
	}
	for _, block := range m.Blocks() {
		// FIXME extract blocks.NumBytes(block) or block.NumBytes() method
		l.ReceivedBytes(len(block.Data))
	}
	return errors.New("TODO")
}

// TODO add contents of m.WantList() to my local wantlist? NB: could introduce
// race conditions where I send a message, but MessageSent gets handled after
// MessageReceived. The information in the local wantlist could become
// inconsistent. Would need to ensure that Sends and acknowledgement of the
// send happen atomically

103
func (s *strategist) MessageSent(p peer.Peer, m bsmsg.BitSwapMessage) error {
104 105 106
	s.lock.Lock()
	defer s.lock.Unlock()

107 108 109 110
	l := s.ledger(p)
	for _, block := range m.Blocks() {
		l.SentBytes(len(block.Data))
	}
111 112 113

	// TODO remove these blocks from peer's want list

114 115 116
	return nil
}

117
func (s *strategist) NumBytesSentTo(p peer.Peer) uint64 {
118 119 120
	s.lock.RLock()
	defer s.lock.RUnlock()

121 122 123
	return s.ledger(p).Accounting.BytesSent
}

124
func (s *strategist) NumBytesReceivedFrom(p peer.Peer) uint64 {
125 126 127
	s.lock.RLock()
	defer s.lock.RUnlock()

128 129 130
	return s.ledger(p).Accounting.BytesRecv
}

131
// ledger lazily instantiates a ledger
132
func (s *strategist) ledger(p peer.Peer) *ledger {
133 134 135 136 137 138 139
	l, ok := s.ledgerMap[peerKey(p.Key())]
	if !ok {
		l = newLedger(p, s.strategyFunc)
		s.ledgerMap[peerKey(p.Key())] = l
	}
	return l
}