Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
03ffdbff
Commit
03ffdbff
authored
Sep 16, 2014
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refac(bitswap): privatize bitswap
parent
881447e6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
15 deletions
+15
-15
bitswap/bitswap.go
bitswap/bitswap.go
+15
-15
No files found.
bitswap/bitswap.go
View file @
03ffdbff
...
...
@@ -28,8 +28,8 @@ const PartnerWantListMax = 10
// access/lookups.
type
KeySet
map
[
u
.
Key
]
struct
{}
//
B
it
S
wap instances implement the bitswap protocol.
type
B
it
S
wap
struct
{
//
b
it
s
wap instances implement the bitswap protocol.
type
b
it
s
wap
struct
{
// peer is the identity of this (local) node.
peer
*
peer
.
Peer
...
...
@@ -62,10 +62,10 @@ type BitSwap struct {
}
// 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
{}
bs
:=
&
B
it
S
wap
{
bs
:=
&
b
it
s
wap
{
peer
:
p
,
blockstore
:
blockstore
.
NewBlockstore
(
d
),
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.
func
(
bs
*
B
it
S
wap
)
Block
(
k
u
.
Key
,
timeout
time
.
Duration
)
(
func
(
bs
*
b
it
s
wap
)
Block
(
k
u
.
Key
,
timeout
time
.
Duration
)
(
*
blocks
.
Block
,
error
)
{
u
.
DOut
(
"Bitswap GetBlock: '%s'
\n
"
,
k
.
Pretty
())
begin
:=
time
.
Now
()
...
...
@@ -118,7 +118,7 @@ func (bs *BitSwap) Block(k u.Key, timeout time.Duration) (
}
}
func
(
bs
*
B
it
S
wap
)
getBlock
(
k
u
.
Key
,
p
*
peer
.
Peer
,
timeout
time
.
Duration
)
(
*
blocks
.
Block
,
error
)
{
func
(
bs
*
b
it
s
wap
)
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
())
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
}
// HasBlock announces the existance of a block to
B
it
S
wap, potentially sending
// HasBlock announces the existance of a block to
b
it
s
wap, potentially sending
// it to peers (Partners) whose WantLists include it.
func
(
bs
*
B
it
S
wap
)
HasBlock
(
blk
blocks
.
Block
)
error
{
func
(
bs
*
b
it
s
wap
)
HasBlock
(
blk
blocks
.
Block
)
error
{
go
func
()
{
for
_
,
ledger
:=
range
bs
.
partners
{
if
ledger
.
WantListContains
(
blk
.
Key
())
{
...
...
@@ -153,7 +153,7 @@ func (bs *BitSwap) HasBlock(blk blocks.Block) error {
}
// TODO(brian): get a return value
func
(
bs
*
B
it
S
wap
)
SendBlock
(
p
*
peer
.
Peer
,
b
blocks
.
Block
)
{
func
(
bs
*
b
it
s
wap
)
SendBlock
(
p
*
peer
.
Peer
,
b
blocks
.
Block
)
{
u
.
DOut
(
"Sending block to peer.
\n
"
)
message
:=
bsmsg
.
New
()
// 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,
// and then if we do, check the ledger for whether or not we should send it.
func
(
bs
*
B
it
S
wap
)
peerWantsBlock
(
p
*
peer
.
Peer
,
wanted
u
.
Key
)
{
func
(
bs
*
b
it
s
wap
)
peerWantsBlock
(
p
*
peer
.
Peer
,
wanted
u
.
Key
)
{
u
.
DOut
(
"peer [%s] wants block [%s]
\n
"
,
p
.
ID
.
Pretty
(),
wanted
.
Pretty
())
ledger
:=
bs
.
getLedger
(
p
)
...
...
@@ -182,7 +182,7 @@ func (bs *BitSwap) peerWantsBlock(p *peer.Peer, wanted u.Key) {
}
// TODO(brian): return error
func
(
bs
*
B
it
S
wap
)
blockReceive
(
p
*
peer
.
Peer
,
blk
blocks
.
Block
)
{
func
(
bs
*
b
it
s
wap
)
blockReceive
(
p
*
peer
.
Peer
,
blk
blocks
.
Block
)
{
u
.
DOut
(
"blockReceive: %s
\n
"
,
blk
.
Key
()
.
Pretty
())
err
:=
bs
.
blockstore
.
Put
(
blk
)
if
err
!=
nil
{
...
...
@@ -196,7 +196,7 @@ func (bs *BitSwap) blockReceive(p *peer.Peer, blk blocks.Block) {
ledger
.
ReceivedBytes
(
len
(
blk
.
Data
))
}
func
(
bs
*
B
it
S
wap
)
getLedger
(
p
*
peer
.
Peer
)
*
Ledger
{
func
(
bs
*
b
it
s
wap
)
getLedger
(
p
*
peer
.
Peer
)
*
Ledger
{
l
,
ok
:=
bs
.
partners
[
p
.
Key
()]
if
ok
{
return
l
...
...
@@ -209,7 +209,7 @@ func (bs *BitSwap) getLedger(p *peer.Peer) *Ledger {
return
l
}
func
(
bs
*
B
it
S
wap
)
SendWantList
(
wl
KeySet
)
error
{
func
(
bs
*
b
it
s
wap
)
SendWantList
(
wl
KeySet
)
error
{
message
:=
bsmsg
.
New
()
for
k
,
_
:=
range
wl
{
message
.
AppendWanted
(
k
)
...
...
@@ -223,11 +223,11 @@ func (bs *BitSwap) SendWantList(wl KeySet) error {
return
nil
}
func
(
bs
*
B
it
S
wap
)
Halt
()
{
func
(
bs
*
b
it
s
wap
)
Halt
()
{
bs
.
haltChan
<-
struct
{}{}
}
func
(
bs
*
B
it
S
wap
)
ReceiveMessage
(
func
(
bs
*
b
it
s
wap
)
ReceiveMessage
(
ctx
context
.
Context
,
sender
*
peer
.
Peer
,
incoming
bsmsg
.
BitSwapMessage
)
(
*
peer
.
Peer
,
bsmsg
.
BitSwapMessage
,
error
)
{
if
incoming
.
Blocks
()
!=
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment