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
b28343cf
Commit
b28343cf
authored
Sep 11, 2014
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(bitswap) meslistener -> notifications
parent
455c6582
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
28 deletions
+24
-28
bitswap/bitswap.go
bitswap/bitswap.go
+24
-28
No files found.
bitswap/bitswap.go
View file @
b28343cf
...
...
@@ -3,6 +3,7 @@ package bitswap
import
(
"time"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
proto
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
ds
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
...
...
@@ -38,7 +39,7 @@ type BitSwap struct {
// routing interface for communication
routing
*
dht
.
IpfsDHT
listener
*
swarm
.
MessageListener
notifications
*
notifications
// partners is a map of currently active bitswap relationships.
// The Ledger has the peer.ID, and the peer connection works through net.
...
...
@@ -68,7 +69,7 @@ func NewBitSwap(p *peer.Peer, net swarm.Network, d ds.Datastore, r routing.IpfsR
routing
:
r
.
(
*
dht
.
IpfsDHT
),
meschan
:
net
.
GetChannel
(
swarm
.
PBWrapper_BITSWAP
),
haltChan
:
make
(
chan
struct
{}),
listener
:
swarm
.
NewMessageListener
(),
notifications
:
newNotifications
(),
}
go
bs
.
handleMessages
()
...
...
@@ -83,7 +84,7 @@ func (bs *BitSwap) GetBlock(k u.Key, timeout time.Duration) (
tleft
:=
timeout
-
time
.
Now
()
.
Sub
(
begin
)
provs_ch
:=
bs
.
routing
.
FindProvidersAsync
(
k
,
20
,
timeout
)
valchan
:=
make
(
chan
[]
byte
)
blockChannel
:=
make
(
chan
*
blocks
.
Block
)
after
:=
time
.
After
(
tleft
)
// TODO: when the data is received, shut down this for loop ASAP
...
...
@@ -96,7 +97,7 @@ func (bs *BitSwap) GetBlock(k u.Key, timeout time.Duration) (
return
}
select
{
case
valchan
<-
blk
:
case
blockChannel
<-
blk
:
default
:
}
}(
p
)
...
...
@@ -104,31 +105,30 @@ func (bs *BitSwap) GetBlock(k u.Key, timeout time.Duration) (
}()
select
{
case
bl
kdata
:=
<-
valchan
:
close
(
valchan
)
return
block
s
.
NewBlock
(
blkdata
)
case
bl
ock
:=
<-
blockChannel
:
close
(
blockChannel
)
return
block
,
nil
case
<-
after
:
return
nil
,
u
.
ErrTimeout
}
}
func
(
bs
*
BitSwap
)
getBlock
(
k
u
.
Key
,
p
*
peer
.
Peer
,
timeout
time
.
Duration
)
(
[]
byte
,
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
())
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
timeout
)
blockChannel
:=
bs
.
notifications
.
Subscribe
(
ctx
,
k
)
message
:=
newMessage
()
message
.
AppendWanted
(
k
)
after
:=
time
.
After
(
timeout
)
resp
:=
bs
.
listener
.
Listen
(
string
(
k
),
1
,
timeout
)
bs
.
meschan
.
Outgoing
<-
message
.
ToSwarm
(
p
)
select
{
case
resp_mes
:=
<-
resp
:
return
resp_mes
.
Data
,
nil
case
<-
after
:
block
,
ok
:=
<-
blockChannel
if
!
ok
{
u
.
PErr
(
"getBlock for '%s' timed out.
\n
"
,
k
.
Pretty
())
return
nil
,
u
.
ErrTimeout
}
return
block
,
nil
}
// HaveBlock announces the existance of a block to BitSwap, potentially sending
...
...
@@ -229,11 +229,7 @@ func (bs *BitSwap) blockReceive(p *peer.Peer, blk *blocks.Block) {
return
}
mes
:=
&
swarm
.
Message
{
Peer
:
p
,
Data
:
blk
.
Data
,
}
bs
.
listener
.
Respond
(
string
(
blk
.
Key
()),
mes
)
bs
.
notifications
.
Publish
(
blk
)
ledger
:=
bs
.
getLedger
(
p
)
ledger
.
ReceivedBytes
(
len
(
blk
.
Data
))
...
...
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