@cryptotaxi247 / kubo / commits / 03ffdbffe

refac(bitswap): privatize bitswap

Brian Tiger Chow committed Sep 16, 2014 at 05:41 UTC 03ffdbffedde2519ec14c6aba9cc663eaee19a1e
1 file changed +15 -15
bitswap/bitswap.go
+15 -15
@@ -28,8 +28,8 @@ const PartnerWantListMax = 10
28 // access/lookups.
29 type KeySet map[u.Key]struct{}
30
31 -// BitSwap instances implement the bitswap protocol.
32 -type BitSwap struct {
31 +// bitswap instances implement the bitswap protocol.
32 +type bitswap struct {
33 // peer is the identity of this (local) node.
34 peer *peer.Peer
35
@@ -62,10 +62,10 @@ type BitSwap struct {
62 }
63
64 // NewSession initializes a bitswap session.
65 -func NewSession(parent context.Context, s bsnet.NetworkService, p *peer.Peer, d ds.Datastore, r routing.IpfsRouting) *BitSwap {
65 +func NewSession(parent context.Context, s bsnet.NetworkService, p *peer.Peer, d ds.Datastore, r routing.IpfsRouting) Exchange {
66
67 receiver := bsnet.Forwarder{}
68 - bs := &BitSwap{
68 + bs := &bitswap{
69 peer: p,
70 blockstore: blockstore.NewBlockstore(d),
71 partners: LedgerMap{},
@@ -82,7 +82,7 @@ func NewSession(parent context.Context, s bsnet.NetworkService, p *peer.Peer, d
82 }
83
84 // GetBlock attempts to retrieve a particular block from peers, within timeout.
85 -func (bs *BitSwap) Block(k u.Key, timeout time.Duration) (
85 +func (bs *bitswap) Block(k u.Key, timeout time.Duration) (
86 *blocks.Block, error) {
87 u.DOut("Bitswap GetBlock: '%s'\n", k.Pretty())
88 begin := time.Now()
@@ -118,7 +118,7 @@ func (bs *BitSwap) Block(k u.Key, timeout time.Duration) (
118 }
119 }
120
121 -func (bs *BitSwap) getBlock(k u.Key, p *peer.Peer, timeout time.Duration) (*blocks.Block, error) {
121 +func (bs *bitswap) getBlock(k u.Key, p *peer.Peer, timeout time.Duration) (*blocks.Block, error) {
122 u.DOut("[%s] getBlock '%s' from [%s]\n", bs.peer.ID.Pretty(), k.Pretty(), p.ID.Pretty())
123
124 ctx, _ := context.WithTimeout(context.Background(), timeout)
@@ -136,9 +136,9 @@ func (bs *BitSwap) getBlock(k u.Key, p *peer.Peer, timeout time.Duration) (*bloc
136 return &block, nil
137 }
138
139 -// HasBlock announces the existance of a block to BitSwap, potentially sending
139 +// HasBlock announces the existance of a block to bitswap, potentially sending
140 // it to peers (Partners) whose WantLists include it.
141 -func (bs *BitSwap) HasBlock(blk blocks.Block) error {
141 +func (bs *bitswap) HasBlock(blk blocks.Block) error {
142 go func() {
143 for _, ledger := range bs.partners {
144 if ledger.WantListContains(blk.Key()) {
@@ -153,7 +153,7 @@ func (bs *BitSwap) HasBlock(blk blocks.Block) error {
153 }
154
155 // TODO(brian): get a return value
156 -func (bs *BitSwap) SendBlock(p *peer.Peer, b blocks.Block) {
156 +func (bs *bitswap) SendBlock(p *peer.Peer, b blocks.Block) {
157 u.DOut("Sending block to peer.\n")
158 message := bsmsg.New()
159 // TODO(brian): change interface to accept value instead of pointer
@@ -163,7 +163,7 @@ func (bs *BitSwap) SendBlock(p *peer.Peer, b blocks.Block) {
163
164 // peerWantsBlock will check if we have the block in question,
165 // and then if we do, check the ledger for whether or not we should send it.
166 -func (bs *BitSwap) peerWantsBlock(p *peer.Peer, wanted u.Key) {
166 +func (bs *bitswap) peerWantsBlock(p *peer.Peer, wanted u.Key) {
167 u.DOut("peer [%s] wants block [%s]\n", p.ID.Pretty(), wanted.Pretty())
168
169 ledger := bs.getLedger(p)
@@ -182,7 +182,7 @@ func (bs *BitSwap) peerWantsBlock(p *peer.Peer, wanted u.Key) {
182 }
183
184 // TODO(brian): return error
185 -func (bs *BitSwap) blockReceive(p *peer.Peer, blk blocks.Block) {
185 +func (bs *bitswap) blockReceive(p *peer.Peer, blk blocks.Block) {
186 u.DOut("blockReceive: %s\n", blk.Key().Pretty())
187 err := bs.blockstore.Put(blk)
188 if err != nil {
@@ -196,7 +196,7 @@ func (bs *BitSwap) blockReceive(p *peer.Peer, blk blocks.Block) {
196 ledger.ReceivedBytes(len(blk.Data))
197 }
198
199 -func (bs *BitSwap) getLedger(p *peer.Peer) *Ledger {
199 +func (bs *bitswap) getLedger(p *peer.Peer) *Ledger {
200 l, ok := bs.partners[p.Key()]
201 if ok {
202 return l
@@ -209,7 +209,7 @@ func (bs *BitSwap) getLedger(p *peer.Peer) *Ledger {
209 return l
210 }
211
212 -func (bs *BitSwap) SendWantList(wl KeySet) error {
212 +func (bs *bitswap) SendWantList(wl KeySet) error {
213 message := bsmsg.New()
214 for k, _ := range wl {
215 message.AppendWanted(k)
@@ -223,11 +223,11 @@ func (bs *BitSwap) SendWantList(wl KeySet) error {
223 return nil
224 }
225
226 -func (bs *BitSwap) Halt() {
226 +func (bs *bitswap) Halt() {
227 bs.haltChan <- struct{}{}
228 }
229
230 -func (bs *BitSwap) ReceiveMessage(
230 +func (bs *bitswap) ReceiveMessage(
231 ctx context.Context, sender *peer.Peer, incoming bsmsg.BitSwapMessage) (
232 *peer.Peer, bsmsg.BitSwapMessage, error) {
233 if incoming.Blocks() != nil {