1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 02:30:39 +08:00

refac(bitswap): privatize bitswap

This commit is contained in:
Brian Tiger Chow
2014-09-16 05:41:19 -07:00
parent 881447e68e
commit 03ffdbffed

View File

@ -28,8 +28,8 @@ const PartnerWantListMax = 10
// access/lookups. // access/lookups.
type KeySet map[u.Key]struct{} type KeySet map[u.Key]struct{}
// BitSwap instances implement the bitswap protocol. // bitswap instances implement the bitswap protocol.
type BitSwap struct { type bitswap struct {
// peer is the identity of this (local) node. // peer is the identity of this (local) node.
peer *peer.Peer peer *peer.Peer
@ -62,10 +62,10 @@ type BitSwap struct {
} }
// NewSession initializes a bitswap session. // NewSession initializes a bitswap session.
func NewSession(parent context.Context, s bsnet.NetworkService, p *peer.Peer, d ds.Datastore, r routing.IpfsRouting) *BitSwap { func NewSession(parent context.Context, s bsnet.NetworkService, p *peer.Peer, d ds.Datastore, r routing.IpfsRouting) Exchange {
receiver := bsnet.Forwarder{} receiver := bsnet.Forwarder{}
bs := &BitSwap{ bs := &bitswap{
peer: p, peer: p,
blockstore: blockstore.NewBlockstore(d), blockstore: blockstore.NewBlockstore(d),
partners: LedgerMap{}, partners: LedgerMap{},
@ -82,7 +82,7 @@ func NewSession(parent context.Context, s bsnet.NetworkService, p *peer.Peer, d
} }
// GetBlock attempts to retrieve a particular block from peers, within timeout. // GetBlock attempts to retrieve a particular block from peers, within timeout.
func (bs *BitSwap) Block(k u.Key, timeout time.Duration) ( func (bs *bitswap) Block(k u.Key, timeout time.Duration) (
*blocks.Block, error) { *blocks.Block, error) {
u.DOut("Bitswap GetBlock: '%s'\n", k.Pretty()) u.DOut("Bitswap GetBlock: '%s'\n", k.Pretty())
begin := time.Now() begin := time.Now()
@ -118,7 +118,7 @@ func (bs *BitSwap) Block(k u.Key, timeout time.Duration) (
} }
} }
func (bs *BitSwap) getBlock(k u.Key, p *peer.Peer, timeout time.Duration) (*blocks.Block, error) { func (bs *bitswap) getBlock(k u.Key, p *peer.Peer, timeout time.Duration) (*blocks.Block, error) {
u.DOut("[%s] getBlock '%s' from [%s]\n", bs.peer.ID.Pretty(), k.Pretty(), p.ID.Pretty()) u.DOut("[%s] getBlock '%s' from [%s]\n", bs.peer.ID.Pretty(), k.Pretty(), p.ID.Pretty())
ctx, _ := context.WithTimeout(context.Background(), timeout) ctx, _ := context.WithTimeout(context.Background(), timeout)
@ -136,9 +136,9 @@ func (bs *BitSwap) getBlock(k u.Key, p *peer.Peer, timeout time.Duration) (*bloc
return &block, nil return &block, nil
} }
// HasBlock announces the existance of a block to BitSwap, potentially sending // HasBlock announces the existance of a block to bitswap, potentially sending
// it to peers (Partners) whose WantLists include it. // it to peers (Partners) whose WantLists include it.
func (bs *BitSwap) HasBlock(blk blocks.Block) error { func (bs *bitswap) HasBlock(blk blocks.Block) error {
go func() { go func() {
for _, ledger := range bs.partners { for _, ledger := range bs.partners {
if ledger.WantListContains(blk.Key()) { if ledger.WantListContains(blk.Key()) {
@ -153,7 +153,7 @@ func (bs *BitSwap) HasBlock(blk blocks.Block) error {
} }
// TODO(brian): get a return value // TODO(brian): get a return value
func (bs *BitSwap) SendBlock(p *peer.Peer, b blocks.Block) { func (bs *bitswap) SendBlock(p *peer.Peer, b blocks.Block) {
u.DOut("Sending block to peer.\n") u.DOut("Sending block to peer.\n")
message := bsmsg.New() message := bsmsg.New()
// TODO(brian): change interface to accept value instead of pointer // TODO(brian): change interface to accept value instead of pointer
@ -163,7 +163,7 @@ func (bs *BitSwap) SendBlock(p *peer.Peer, b blocks.Block) {
// peerWantsBlock will check if we have the block in question, // peerWantsBlock will check if we have the block in question,
// and then if we do, check the ledger for whether or not we should send it. // and then if we do, check the ledger for whether or not we should send it.
func (bs *BitSwap) peerWantsBlock(p *peer.Peer, wanted u.Key) { func (bs *bitswap) peerWantsBlock(p *peer.Peer, wanted u.Key) {
u.DOut("peer [%s] wants block [%s]\n", p.ID.Pretty(), wanted.Pretty()) u.DOut("peer [%s] wants block [%s]\n", p.ID.Pretty(), wanted.Pretty())
ledger := bs.getLedger(p) ledger := bs.getLedger(p)
@ -182,7 +182,7 @@ func (bs *BitSwap) peerWantsBlock(p *peer.Peer, wanted u.Key) {
} }
// TODO(brian): return error // TODO(brian): return error
func (bs *BitSwap) blockReceive(p *peer.Peer, blk blocks.Block) { func (bs *bitswap) blockReceive(p *peer.Peer, blk blocks.Block) {
u.DOut("blockReceive: %s\n", blk.Key().Pretty()) u.DOut("blockReceive: %s\n", blk.Key().Pretty())
err := bs.blockstore.Put(blk) err := bs.blockstore.Put(blk)
if err != nil { if err != nil {
@ -196,7 +196,7 @@ func (bs *BitSwap) blockReceive(p *peer.Peer, blk blocks.Block) {
ledger.ReceivedBytes(len(blk.Data)) ledger.ReceivedBytes(len(blk.Data))
} }
func (bs *BitSwap) getLedger(p *peer.Peer) *Ledger { func (bs *bitswap) getLedger(p *peer.Peer) *Ledger {
l, ok := bs.partners[p.Key()] l, ok := bs.partners[p.Key()]
if ok { if ok {
return l return l
@ -209,7 +209,7 @@ func (bs *BitSwap) getLedger(p *peer.Peer) *Ledger {
return l return l
} }
func (bs *BitSwap) SendWantList(wl KeySet) error { func (bs *bitswap) SendWantList(wl KeySet) error {
message := bsmsg.New() message := bsmsg.New()
for k, _ := range wl { for k, _ := range wl {
message.AppendWanted(k) message.AppendWanted(k)
@ -223,11 +223,11 @@ func (bs *BitSwap) SendWantList(wl KeySet) error {
return nil return nil
} }
func (bs *BitSwap) Halt() { func (bs *bitswap) Halt() {
bs.haltChan <- struct{}{} bs.haltChan <- struct{}{}
} }
func (bs *BitSwap) ReceiveMessage( func (bs *bitswap) ReceiveMessage(
ctx context.Context, sender *peer.Peer, incoming bsmsg.BitSwapMessage) ( ctx context.Context, sender *peer.Peer, incoming bsmsg.BitSwapMessage) (
*peer.Peer, bsmsg.BitSwapMessage, error) { *peer.Peer, bsmsg.BitSwapMessage, error) {
if incoming.Blocks() != nil { if incoming.Blocks() != nil {