strategy.go 2.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
package strategy

import (
	"errors"

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

// TODO declare thread-safe datastore
func New() Strategy {
	return &strategist{
		ledgerMap:    ledgerMap{},
		strategyFunc: yesManStrategy,
	}
}

type strategist struct {
	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
func (s *strategist) Peers() []*peer.Peer {
	response := make([]*peer.Peer, 0)
	for _, ledger := range s.ledgerMap {
		response = append(response, ledger.Partner)
	}
	return response
}

func (s *strategist) BlockIsWantedByPeer(k u.Key, p *peer.Peer) bool {
	ledger := s.ledger(p)
	return ledger.WantListContains(k)
}

func (s *strategist) ShouldSendBlockToPeer(k u.Key, p *peer.Peer) bool {
	ledger := s.ledger(p)
	return ledger.ShouldSend()
}

func (s *strategist) Seed(int64) {
	// TODO
}

func (s *strategist) MessageReceived(p *peer.Peer, m bsmsg.BitSwapMessage) error {
	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

func (s *strategist) MessageSent(p *peer.Peer, m bsmsg.BitSwapMessage) error {
	l := s.ledger(p)
	for _, block := range m.Blocks() {
		l.SentBytes(len(block.Data))
	}
	return nil
}

79 80 81 82 83 84 85 86
func (s *strategist) NumBytesSentTo(p *peer.Peer) uint64 {
	return s.ledger(p).Accounting.BytesSent
}

func (s *strategist) NumBytesReceivedFrom(p *peer.Peer) uint64 {
	return s.ledger(p).Accounting.BytesRecv
}

87 88 89 90 91 92 93 94 95
// ledger lazily instantiates a ledger
func (s *strategist) ledger(p *peer.Peer) *ledger {
	l, ok := s.ledgerMap[peerKey(p.Key())]
	if !ok {
		l = newLedger(p, s.strategyFunc)
		s.ledgerMap[peerKey(p.Key())] = l
	}
	return l
}