Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
ee1450bf
提交
ee1450bf
authored
2月 13, 2015
作者:
Juan Batiz-Benet
提交者:
Jeromy
2月 18, 2015
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
routed host stash
上级
d8d933c8
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
108 行增加
和
0 行删除
+108
-0
routed.go
p2p/host/routed/routed.go
+108
-0
没有找到文件。
p2p/host/routed/routed.go
0 → 100644
浏览文件 @
ee1450bf
package
routedhost
import
(
"fmt"
"time"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
ma
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
eventlog
"github.com/jbenet/go-ipfs/thirdparty/eventlog"
lgbl
"github.com/jbenet/go-ipfs/util/eventlog/loggables"
host
"github.com/jbenet/go-ipfs/p2p/host"
inet
"github.com/jbenet/go-ipfs/p2p/net"
peer
"github.com/jbenet/go-ipfs/p2p/peer"
protocol
"github.com/jbenet/go-ipfs/p2p/protocol"
routing
"github.com/jbenet/go-ipfs/routing"
)
var
log
=
eventlog
.
Logger
(
"p2p/host/routed"
)
// AddressTTL is the expiry time for our addresses.
// We expire them quickly.
const
AddressTTL
=
time
.
Second
*
10
// RoutedHost is a p2p Host that includes a routing system.
// This allows the Host to find the addresses for peers when
// it does not have them.
type
RoutedHost
struct
{
host
host
.
Host
// embedded other host.
route
routing
.
IpfsRouting
}
func
Wrap
(
h
host
.
Host
,
r
routing
.
IpfsRouting
)
*
RoutedHost
{
return
&
RoutedHost
{
h
,
r
}
}
// Connect ensures there is a connection between this host and the peer with
// given peer.ID. See (host.Host).Connect for more information.
//
// RoutedHost's Connect differs in that if the host has no addresses for a
// given peer, it will use its routing system to try to find some.
func
(
rh
*
RoutedHost
)
Connect
(
ctx
context
.
Context
,
pi
peer
.
PeerInfo
)
error
{
// first, check if we're already connected.
if
len
(
rh
.
Network
()
.
ConnsToPeer
(
pi
.
ID
))
>
0
{
return
nil
}
// if we were given some addresses, keep + use them.
if
len
(
pi
.
Addrs
)
>
0
{
rh
.
Peerstore
()
.
AddAddrs
(
pi
.
ID
,
pi
.
Addrs
,
peer
.
TempAddrTTL
)
}
// Check if we have some addresses in our recent memory.
addrs
:=
rh
.
Peerstore
()
.
Addrs
(
pi
.
ID
)
if
len
(
addrs
)
<
1
{
// no addrs? find some with the routing system.
pi2
,
err
:=
rh
.
route
.
FindPeer
(
ctx
,
pi
.
ID
)
if
err
!=
nil
{
return
err
// couldnt find any :(
}
if
pi2
.
ID
!=
pi
.
ID
{
err
=
fmt
.
Errorf
(
"routing failure: provided addrs for different peer"
)
logRoutingErrDifferentPeers
(
ctx
,
pi
.
ID
,
pi2
.
ID
,
err
)
return
err
}
addrs
=
pi2
.
Addrs
}
// if we're here, we got some addrs. let's use our wrapped host to connect.
pi
.
Addrs
=
addrs
return
rh
.
host
.
Connect
(
ctx
,
pi
)
}
func
logRoutingErrDifferentPeers
(
ctx
context
.
Context
,
wanted
,
got
peer
.
ID
,
err
error
)
{
lm
:=
make
(
lgbl
.
DeferredMap
)
lm
[
"error"
]
=
err
lm
[
"wantedPeer"
]
=
func
()
interface
{}
{
return
wanted
.
Pretty
()
}
lm
[
"gotPeer"
]
=
func
()
interface
{}
{
return
got
.
Pretty
()
}
log
.
Event
(
ctx
,
"routingError"
,
lm
)
}
func
(
rh
*
RoutedHost
)
ID
()
peer
.
ID
{
return
rh
.
host
.
ID
()
}
func
(
rh
*
RoutedHost
)
Peerstore
()
peer
.
Peerstore
{
return
rh
.
host
.
Peerstore
()
}
func
(
rh
*
RoutedHost
)
Addrs
()
[]
ma
.
Multiaddr
{
return
rh
.
host
.
Addrs
()
}
func
(
rh
*
RoutedHost
)
Network
()
inet
.
Network
{
return
rh
.
host
.
Network
()
}
func
(
rh
*
RoutedHost
)
Mux
()
*
protocol
.
Mux
{
return
rh
.
host
.
Mux
()
}
func
(
rh
*
RoutedHost
)
SetStreamHandler
(
pid
protocol
.
ID
,
handler
inet
.
StreamHandler
)
{
rh
.
host
.
SetStreamHandler
(
pid
,
handler
)
}
func
(
rh
*
RoutedHost
)
NewStream
(
pid
protocol
.
ID
,
p
peer
.
ID
)
(
inet
.
Stream
,
error
)
{
return
rh
.
host
.
NewStream
(
pid
,
p
)
}
func
(
rh
*
RoutedHost
)
Close
()
error
{
// no need to close IpfsRouting. we dont own it.
return
rh
.
host
.
Close
()
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论