Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
171f96b7
提交
171f96b7
authored
7月 29, 2014
作者:
Jeromy Johnson
提交者:
Juan Batiz-Benet
8月 07, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update messages and add some new code around handling/creating messages
上级
8bc80124
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
44 行增加
和
5 行删除
+44
-5
dht.go
routing/dht/dht.go
+32
-2
messages.proto
routing/dht/messages.proto
+5
-2
routing.go
routing/dht/routing.go
+7
-1
没有找到文件。
routing/dht/dht.go
浏览文件 @
171f96b7
...
...
@@ -2,6 +2,7 @@ package dht
import
(
swarm
"github.com/jbenet/go-ipfs/swarm"
"sync"
)
// TODO. SEE https://github.com/jbenet/node-ipfs/blob/master/submodules/ipfs-dht/index.js
...
...
@@ -10,13 +11,42 @@ import (
// IpfsDHT is an implementation of Kademlia with Coral and S/Kademlia modifications.
// It is used to implement the base IpfsRouting module.
type
IpfsDHT
struct
{
routes
RoutingTable
routes
RoutingTable
network
*
swarm
.
Swarm
network
*
swarm
.
Swarm
listeners
map
[
uint64
]
chan
swarm
.
Message
listenLock
sync
.
RWMutex
}
// Read in all messages from swarm and handle them appropriately
// NOTE: this function is just a quick sketch
func
(
dht
*
IpfsDHT
)
handleMessages
()
{
for
mes
:=
range
dht
.
network
.
Chan
.
Incoming
{
for
{
select
{
case
mes
:=
<-
dht
.
network
.
Chan
.
Incoming
:
// Unmarshal message
dht
.
listenLock
.
RLock
()
ch
,
ok
:=
dht
.
listeners
[
id
]
dht
.
listenLock
.
RUnlock
()
if
ok
{
// Send message to waiting goroutine
ch
<-
mes
}
//case closeChan: or something
}
}
}
}
// Register a handler for a specific message ID, used for getting replies
// to certain messages (i.e. response to a GET_VALUE message)
func
(
dht
*
IpfsDHT
)
ListenFor
(
mesid
uint64
)
<-
chan
swarm
.
Message
{
lchan
:=
make
(
chan
swarm
.
Message
)
dht
.
listenLock
.
Lock
()
dht
.
listeners
[
mesid
]
=
lchan
dht
.
listenLock
.
Unlock
()
return
lchan
}
routing/dht/messages.proto
浏览文件 @
171f96b7
...
...
@@ -6,11 +6,14 @@ message DHTMessage {
enum
MessageType
{
PUT_VALUE
=
0
;
GET_VALUE
=
1
;
PING
=
2
;
FIND_NODE
=
3
;
ADD_PROVIDER
=
2
;
GET_PROVIDERS
=
3
;
FIND_NODE
=
4
;
PING
=
5
;
}
required
MessageType
type
=
1
;
optional
string
key
=
2
;
optional
bytes
value
=
3
;
required
int64
id
=
4
;
}
routing/dht/routing.go
浏览文件 @
171f96b7
...
...
@@ -43,14 +43,20 @@ func (s *IpfsDHT) GetValue(key u.Key, timeout time.Duration) ([]byte, error) {
mes
.
Data
=
[]
byte
(
pmes
.
String
())
mes
.
Peer
=
p
response_chan
:=
s
.
network
.
ListenFor
(
pmes
.
Id
)
response_chan
:=
s
.
ListenFor
(
pmes
.
Id
)
// Wait for either the response or a timeout
timeup
:=
time
.
After
(
timeout
)
select
{
case
<-
timeup
:
// TODO: unregister listener
return
nil
,
timeoutError
case
resp
:=
<-
response_chan
:
return
resp
.
Data
,
nil
}
// Should never be hit
return
nil
,
nil
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论