Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
56a5e727
提交
56a5e727
authored
1月 22, 2015
作者:
Jeromy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
implement dht findprovs and add error output to dht query
上级
e65afaf1
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
95 行增加
和
1 行删除
+95
-1
dht.go
core/commands/dht.go
+81
-0
query.go
notifications/query.go
+7
-0
lookup.go
routing/dht/lookup.go
+0
-1
query.go
routing/dht/query.go
+7
-0
没有找到文件。
core/commands/dht.go
浏览文件 @
56a5e727
...
...
@@ -9,6 +9,7 @@ import (
cmds
"github.com/jbenet/go-ipfs/commands"
notif
"github.com/jbenet/go-ipfs/notifications"
peer
"github.com/jbenet/go-ipfs/p2p/peer"
ipdht
"github.com/jbenet/go-ipfs/routing/dht"
u
"github.com/jbenet/go-ipfs/util"
)
...
...
@@ -21,6 +22,7 @@ var DhtCmd = &cmds.Command{
Subcommands
:
map
[
string
]
*
cmds
.
Command
{
"query"
:
queryDhtCmd
,
"findprovs"
:
findProvidersDhtCmd
,
},
}
...
...
@@ -97,6 +99,10 @@ var queryDhtCmd = &cmds.Command{
fmt
.
Fprintln
(
buf
)
case
notif
.
SendingQuery
:
fmt
.
Fprintf
(
buf
,
"* querying %s
\n
"
,
obj
.
ID
)
case
notif
.
QueryError
:
fmt
.
Fprintf
(
buf
,
"error: %s
\n
"
,
obj
.
Extra
)
default
:
fmt
.
Fprintf
(
buf
,
"unrecognized event type: %d
\n
"
,
obj
.
Type
)
}
return
buf
,
nil
}
...
...
@@ -109,3 +115,78 @@ var queryDhtCmd = &cmds.Command{
},
Type
:
notif
.
QueryEvent
{},
}
var
findProvidersDhtCmd
=
&
cmds
.
Command
{
Helptext
:
cmds
.
HelpText
{
Tagline
:
"Run a 'FindProviders' query through the DHT"
,
ShortDescription
:
`
FindProviders will return a list of peers who are able to provide the value requested.
`
,
},
Arguments
:
[]
cmds
.
Argument
{
cmds
.
StringArg
(
"key"
,
true
,
true
,
"The key to find providers for"
),
},
Options
:
[]
cmds
.
Option
{
cmds
.
BoolOption
(
"verbose"
,
"v"
,
"Write extra information"
),
},
Run
:
func
(
req
cmds
.
Request
)
(
interface
{},
error
)
{
n
,
err
:=
req
.
Context
()
.
GetNode
()
if
err
!=
nil
{
return
nil
,
err
}
dht
,
ok
:=
n
.
Routing
.
(
*
ipdht
.
IpfsDHT
)
if
!
ok
{
return
nil
,
errors
.
New
(
"Routing service was not a dht"
)
}
numProviders
:=
20
outChan
:=
make
(
chan
interface
{})
pchan
:=
dht
.
FindProvidersAsync
(
req
.
Context
()
.
Context
,
u
.
B58KeyDecode
(
req
.
Arguments
()[
0
]),
numProviders
)
go
func
()
{
defer
close
(
outChan
)
for
p
:=
range
pchan
{
np
:=
p
outChan
<-
&
np
}
}()
return
outChan
,
nil
},
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
outChan
,
ok
:=
res
.
Output
()
.
(
<-
chan
interface
{})
if
!
ok
{
return
nil
,
u
.
ErrCast
()
}
marshal
:=
func
(
v
interface
{})
(
io
.
Reader
,
error
)
{
obj
,
ok
:=
v
.
(
*
peer
.
PeerInfo
)
if
!
ok
{
return
nil
,
u
.
ErrCast
()
}
verbose
,
_
,
err
:=
res
.
Request
()
.
Option
(
"v"
)
.
Bool
()
if
err
!=
nil
{
return
nil
,
err
}
buf
:=
new
(
bytes
.
Buffer
)
if
verbose
{
fmt
.
Fprintf
(
buf
,
"%s
\n
"
,
obj
.
ID
.
Pretty
())
}
else
{
fmt
.
Fprintf
(
buf
,
"%s
\n
"
,
obj
.
ID
)
}
return
buf
,
nil
}
return
&
cmds
.
ChannelMarshaler
{
Channel
:
outChan
,
Marshaler
:
marshal
,
},
nil
},
},
Type
:
peer
.
PeerInfo
{},
}
notifications/query.go
浏览文件 @
56a5e727
...
...
@@ -15,12 +15,14 @@ const (
SendingQuery
QueryEventType
=
iota
PeerResponse
FinalPeer
QueryError
)
type
QueryEvent
struct
{
ID
peer
.
ID
Type
QueryEventType
Responses
[]
*
peer
.
PeerInfo
Extra
string
}
func
RegisterForQueryEvents
(
ctx
context
.
Context
,
ch
chan
<-
*
QueryEvent
)
context
.
Context
{
...
...
@@ -49,6 +51,7 @@ func (qe *QueryEvent) MarshalJSON() ([]byte, error) {
out
[
"ID"
]
=
peer
.
IDB58Encode
(
qe
.
ID
)
out
[
"Type"
]
=
int
(
qe
.
Type
)
out
[
"Responses"
]
=
qe
.
Responses
out
[
"Extra"
]
=
qe
.
Extra
return
json
.
Marshal
(
out
)
}
...
...
@@ -57,17 +60,21 @@ func (qe *QueryEvent) UnmarshalJSON(b []byte) error {
ID
string
Type
int
Responses
[]
*
peer
.
PeerInfo
Extra
string
}{}
err
:=
json
.
Unmarshal
(
b
,
&
temp
)
if
err
!=
nil
{
return
err
}
if
len
(
temp
.
ID
)
>
0
{
pid
,
err
:=
peer
.
IDB58Decode
(
temp
.
ID
)
if
err
!=
nil
{
return
err
}
qe
.
ID
=
pid
}
qe
.
Type
=
QueryEventType
(
temp
.
Type
)
qe
.
Responses
=
temp
.
Responses
qe
.
Extra
=
temp
.
Extra
return
nil
}
routing/dht/lookup.go
浏览文件 @
56a5e727
...
...
@@ -67,7 +67,6 @@ func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key u.Key) (<-chan peer
filtered
=
append
(
filtered
,
dht
.
peerstore
.
PeerInfo
(
clp
))
}
}
log
.
Errorf
(
"filtered: %v"
,
filtered
)
// For DHT query command
notif
.
PublishQueryEvent
(
ctx
,
&
notif
.
QueryEvent
{
...
...
routing/dht/query.go
浏览文件 @
56a5e727
...
...
@@ -3,6 +3,7 @@ package dht
import
(
"sync"
notif
"github.com/jbenet/go-ipfs/notifications"
peer
"github.com/jbenet/go-ipfs/p2p/peer"
queue
"github.com/jbenet/go-ipfs/p2p/peer/queue"
"github.com/jbenet/go-ipfs/routing"
...
...
@@ -230,6 +231,12 @@ func (r *dhtQueryRunner) queryPeer(cg ctxgroup.ContextGroup, p peer.ID) {
pi
:=
peer
.
PeerInfo
{
ID
:
p
}
if
err
:=
r
.
query
.
dht
.
host
.
Connect
(
cg
.
Context
(),
pi
);
err
!=
nil
{
log
.
Debugf
(
"Error connecting: %s"
,
err
)
notif
.
PublishQueryEvent
(
cg
.
Context
(),
&
notif
.
QueryEvent
{
Type
:
notif
.
QueryError
,
Extra
:
err
.
Error
(),
})
r
.
Lock
()
r
.
errs
=
append
(
r
.
errs
,
err
)
r
.
Unlock
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论