Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
2b03664a
提交
2b03664a
authored
9月 14, 2014
作者:
Juan Batiz-Benet
提交者:
Brian Tiger Chow
9月 22, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
net interface
上级
d6e8e55f
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
153 行增加
和
33 行删除
+153
-33
interface.go
net/interface.go
+33
-0
net.go
net/net.go
+106
-0
net_test.go
net/net_test.go
+1
-0
service.go
net/service/service.go
+5
-0
swarm.go
net/swarm/swarm.go
+8
-33
没有找到文件。
net/interface.go
0 → 100644
浏览文件 @
2b03664a
package
net
import
(
msg
"github.com/jbenet/go-ipfs/net/message"
mux
"github.com/jbenet/go-ipfs/net/mux"
peer
"github.com/jbenet/go-ipfs/peer"
)
// Network is the interface IPFS uses for connecting to the world.
type
Network
interface
{
// Listen handles incoming connections on given Multiaddr.
// Listen(*ma.Muliaddr) error
// TODO: for now, only listen on addrs in local peer when initializing.
// DialPeer attempts to establish a connection to a given peer
DialPeer
(
*
peer
.
Peer
)
error
// ClosePeer connection to peer
ClosePeer
(
*
peer
.
Peer
)
error
// IsConnected returns whether a connection to given peer exists.
IsConnected
(
*
peer
.
Peer
)
(
bool
,
error
)
// GetProtocols returns the protocols registered in the network.
GetProtocols
()
*
mux
.
ProtocolMap
// SendMessage sends given Message out
SendMessage
(
*
msg
.
Message
)
error
// Close terminates all network operation
Close
()
error
}
net/net.go
0 → 100644
浏览文件 @
2b03664a
package
net
import
(
"errors"
msg
"github.com/jbenet/go-ipfs/net/message"
mux
"github.com/jbenet/go-ipfs/net/mux"
swarm
"github.com/jbenet/go-ipfs/net/swarm"
peer
"github.com/jbenet/go-ipfs/peer"
context
"code.google.com/p/go.net/context"
)
// IpfsNetwork implements the Network interface,
type
IpfsNetwork
struct
{
// local peer
local
*
peer
.
Peer
// protocol multiplexing
muxer
*
mux
.
Muxer
// peer connection multiplexing
swarm
*
swarm
.
Swarm
// network context
ctx
context
.
Context
cancel
context
.
CancelFunc
}
// NewIpfsNetwork is the structure that implements the network interface
func
NewIpfsNetwork
(
ctx
context
.
Context
,
local
*
peer
.
Peer
,
pmap
*
mux
.
ProtocolMap
)
(
*
IpfsNetwork
,
error
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
in
:=
&
IpfsNetwork
{
local
:
local
,
muxer
:
&
mux
.
Muxer
{
Protocols
:
*
pmap
},
ctx
:
ctx
,
cancel
:
cancel
,
}
err
:=
in
.
muxer
.
Start
(
ctx
)
if
err
!=
nil
{
cancel
()
return
nil
,
err
}
in
.
swarm
,
err
=
swarm
.
NewSwarm
(
ctx
,
local
)
if
err
!=
nil
{
cancel
()
return
nil
,
err
}
return
in
,
nil
}
// Listen handles incoming connections on given Multiaddr.
// func (n *IpfsNetwork) Listen(*ma.Muliaddr) error {}
// DialPeer attempts to establish a connection to a given peer
func
(
n
*
IpfsNetwork
)
DialPeer
(
p
*
peer
.
Peer
)
error
{
_
,
err
:=
n
.
swarm
.
Dial
(
p
)
return
err
}
// ClosePeer connection to peer
func
(
n
*
IpfsNetwork
)
ClosePeer
(
p
*
peer
.
Peer
)
error
{
return
n
.
swarm
.
CloseConnection
(
p
)
}
// IsConnected returns whether a connection to given peer exists.
func
(
n
*
IpfsNetwork
)
IsConnected
(
p
*
peer
.
Peer
)
(
bool
,
error
)
{
return
n
.
swarm
.
GetConnection
(
p
.
ID
)
!=
nil
,
nil
}
// GetProtocols returns the protocols registered in the network.
func
(
n
*
IpfsNetwork
)
GetProtocols
()
*
mux
.
ProtocolMap
{
// copy over because this map should be read only.
pmap
:=
mux
.
ProtocolMap
{}
for
id
,
proto
:=
range
n
.
muxer
.
Protocols
{
pmap
[
id
]
=
proto
}
return
&
pmap
}
// SendMessage sends given Message out
func
(
n
*
IpfsNetwork
)
SendMessage
(
m
*
msg
.
Message
)
error
{
n
.
swarm
.
Outgoing
<-
m
return
nil
}
// Close terminates all network operation
func
(
n
*
IpfsNetwork
)
Close
()
error
{
if
n
.
cancel
==
nil
{
return
errors
.
New
(
"Network already closed."
)
}
n
.
swarm
.
Close
()
n
.
muxer
.
Stop
()
n
.
cancel
()
n
.
cancel
=
nil
return
nil
}
net/net_test.go
0 → 100644
浏览文件 @
2b03664a
package
net
net/service/service.go
浏览文件 @
2b03664a
...
@@ -68,6 +68,11 @@ func (s *Service) Stop() {
...
@@ -68,6 +68,11 @@ func (s *Service) Stop() {
s
.
cancel
=
context
.
CancelFunc
(
nil
)
s
.
cancel
=
context
.
CancelFunc
(
nil
)
}
}
// GetPipe implements the mux.Protocol interface
func
(
s
*
Service
)
GetPipe
()
*
msg
.
Pipe
{
return
s
.
Pipe
}
// SendMessage sends a message out
// SendMessage sends a message out
func
(
s
*
Service
)
SendMessage
(
ctx
context
.
Context
,
m
*
msg
.
Message
,
rid
RequestID
)
error
{
func
(
s
*
Service
)
SendMessage
(
ctx
context
.
Context
,
m
*
msg
.
Message
,
rid
RequestID
)
error
{
...
...
net/swarm/swarm.go
浏览文件 @
2b03664a
...
@@ -110,15 +110,8 @@ func (s *Swarm) Dial(peer *peer.Peer) (*conn.Conn, error) {
...
@@ -110,15 +110,8 @@ func (s *Swarm) Dial(peer *peer.Peer) (*conn.Conn, error) {
return
nil
,
errors
.
New
(
"Attempted connection to self!"
)
return
nil
,
errors
.
New
(
"Attempted connection to self!"
)
}
}
k
:=
peer
.
Key
()
// check if we already have an open connection first
// check if we already have an open connection first
s
.
connsLock
.
RLock
()
c
:=
s
.
GetConnection
(
peer
.
ID
)
c
,
found
:=
s
.
conns
[
k
]
s
.
connsLock
.
RUnlock
()
if
found
{
return
c
,
nil
}
// open connection to peer
// open connection to peer
c
,
err
:=
conn
.
Dial
(
"tcp"
,
peer
)
c
,
err
:=
conn
.
Dial
(
"tcp"
,
peer
)
...
@@ -158,40 +151,22 @@ func (s *Swarm) DialAddr(addr *ma.Multiaddr) (*conn.Conn, error) {
...
@@ -158,40 +151,22 @@ func (s *Swarm) DialAddr(addr *ma.Multiaddr) (*conn.Conn, error) {
return
c
,
err
return
c
,
err
}
}
// Get
Peer returns the peer in the swarm with given key id.
// Get
Connection returns the connection in the swarm to given peer.ID
func
(
s
*
Swarm
)
Get
Peer
(
key
u
.
Key
)
*
peer
.
Peer
{
func
(
s
*
Swarm
)
Get
Connection
(
pid
peer
.
ID
)
*
conn
.
Conn
{
s
.
connsLock
.
RLock
()
s
.
connsLock
.
RLock
()
c
onn
,
found
:=
s
.
conns
[
key
]
c
,
found
:=
s
.
conns
[
u
.
Key
(
pid
)
]
s
.
connsLock
.
RUnlock
()
s
.
connsLock
.
RUnlock
()
if
!
found
{
if
!
found
{
return
nil
return
nil
}
}
return
conn
.
Peer
return
c
}
// GetConnection will check if we are already connected to the peer in question
// and only open a new connection if we arent already
func
(
s
*
Swarm
)
GetConnection
(
id
peer
.
ID
,
addr
*
ma
.
Multiaddr
)
(
*
peer
.
Peer
,
error
)
{
p
:=
&
peer
.
Peer
{
ID
:
id
,
Addresses
:
[]
*
ma
.
Multiaddr
{
addr
},
}
c
,
err
:=
s
.
Dial
(
p
)
if
err
!=
nil
{
return
nil
,
err
}
return
c
.
Peer
,
nil
}
}
// CloseConnection removes a given peer from swarm + closes the connection
// CloseConnection removes a given peer from swarm + closes the connection
func
(
s
*
Swarm
)
CloseConnection
(
p
*
peer
.
Peer
)
error
{
func
(
s
*
Swarm
)
CloseConnection
(
p
*
peer
.
Peer
)
error
{
s
.
connsLock
.
RLock
()
c
:=
s
.
GetConnection
(
p
.
ID
)
conn
,
found
:=
s
.
conns
[
u
.
Key
(
p
.
ID
)]
if
c
==
nil
{
s
.
connsLock
.
RUnlock
()
if
!
found
{
return
u
.
ErrNotFound
return
u
.
ErrNotFound
}
}
...
@@ -199,7 +174,7 @@ func (s *Swarm) CloseConnection(p *peer.Peer) error {
...
@@ -199,7 +174,7 @@ func (s *Swarm) CloseConnection(p *peer.Peer) error {
delete
(
s
.
conns
,
u
.
Key
(
p
.
ID
))
delete
(
s
.
conns
,
u
.
Key
(
p
.
ID
))
s
.
connsLock
.
Unlock
()
s
.
connsLock
.
Unlock
()
return
c
onn
.
Close
()
return
c
.
Close
()
}
}
func
(
s
*
Swarm
)
Error
(
e
error
)
{
func
(
s
*
Swarm
)
Error
(
e
error
)
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论