Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
b28343cf
提交
b28343cf
authored
9月 11, 2014
作者:
Brian Tiger Chow
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(bitswap) meslistener -> notifications
上级
455c6582
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
24 行增加
和
28 行删除
+24
-28
bitswap.go
bitswap/bitswap.go
+24
-28
没有找到文件。
bitswap/bitswap.go
浏览文件 @
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.
...
...
@@ -60,15 +61,15 @@ type BitSwap struct {
// NewBitSwap creates a new BitSwap instance. It does not check its parameters.
func
NewBitSwap
(
p
*
peer
.
Peer
,
net
swarm
.
Network
,
d
ds
.
Datastore
,
r
routing
.
IpfsRouting
)
*
BitSwap
{
bs
:=
&
BitSwap
{
peer
:
p
,
net
:
net
,
datastore
:
d
,
partners
:
LedgerMap
{},
wantList
:
KeySet
{},
routing
:
r
.
(
*
dht
.
IpfsDHT
),
meschan
:
net
.
GetChannel
(
swarm
.
PBWrapper_BITSWAP
),
haltChan
:
make
(
chan
struct
{}),
listener
:
swarm
.
NewMessageListener
(),
peer
:
p
,
net
:
net
,
datastore
:
d
,
partners
:
LedgerMap
{},
wantList
:
KeySet
{},
routing
:
r
.
(
*
dht
.
IpfsDHT
),
meschan
:
net
.
GetChannel
(
swarm
.
PBWrapper_BITSWAP
),
haltChan
:
make
(
chan
struct
{}),
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
))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论