Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
1c36d520
提交
1c36d520
authored
11月 18, 2014
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #359 from jbenet/feat/swarm-cmd
Add swarm command
上级
21d2838d
81135f3d
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
192 行增加
和
0 行删除
+192
-0
root.go
core/commands2/root.go
+1
-0
swarm.go
core/commands2/swarm.go
+181
-0
interface.go
net/interface.go
+4
-0
net.go
net/net.go
+6
-0
没有找到文件。
core/commands2/root.go
浏览文件 @
1c36d520
...
@@ -79,6 +79,7 @@ var rootSubcommands = map[string]*cmds.Command{
...
@@ -79,6 +79,7 @@ var rootSubcommands = map[string]*cmds.Command{
"object"
:
objectCmd
,
"object"
:
objectCmd
,
"refs"
:
refsCmd
,
"refs"
:
refsCmd
,
"id"
:
idCmd
,
"id"
:
idCmd
,
"swarm"
:
swarmCmd
,
}
}
func
init
()
{
func
init
()
{
...
...
core/commands2/swarm.go
0 → 100644
浏览文件 @
1c36d520
package
commands
import
(
"bytes"
"fmt"
"path"
cmds
"github.com/jbenet/go-ipfs/commands"
internal
"github.com/jbenet/go-ipfs/core/commands2/internal"
peer
"github.com/jbenet/go-ipfs/peer"
errors
"github.com/jbenet/go-ipfs/util/debugerror"
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"
)
type
stringList
struct
{
Strings
[]
string
}
var
swarmCmd
=
&
cmds
.
Command
{
Helptext
:
cmds
.
HelpText
{
Tagline
:
"swarm inspection tool"
,
Synopsis
:
`
ipfs swarm peers - List peers with open connections
ipfs swarm connect <address> - Open connection to a given peer
`
,
ShortDescription
:
`
ipfs swarm is a tool to manipulate the network swarm. The swarm is the
component that opens, listens for, and maintains connections to other
ipfs peers in the internet.
`
,
},
Subcommands
:
map
[
string
]
*
cmds
.
Command
{
"peers"
:
swarmPeersCmd
,
"connect"
:
swarmConnectCmd
,
},
}
var
swarmPeersCmd
=
&
cmds
.
Command
{
Helptext
:
cmds
.
HelpText
{
Tagline
:
"List peers with open connections"
,
ShortDescription
:
`
ipfs swarm peers lists the set of peers this node is connected to.
`
,
},
Run
:
func
(
req
cmds
.
Request
)
(
interface
{},
error
)
{
log
.
Debug
(
"ipfs swarm peers"
)
n
,
err
:=
req
.
Context
()
.
GetNode
()
if
err
!=
nil
{
return
nil
,
err
}
if
n
.
Network
==
nil
{
return
nil
,
errNotOnline
}
conns
:=
n
.
Network
.
GetConnections
()
addrs
:=
make
([]
string
,
len
(
conns
))
for
i
,
c
:=
range
conns
{
pid
:=
c
.
RemotePeer
()
.
ID
()
addr
:=
c
.
RemoteMultiaddr
()
addrs
[
i
]
=
fmt
.
Sprintf
(
"%s/%s"
,
addr
,
pid
)
}
return
&
stringList
{
addrs
},
nil
},
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
stringListMarshaler
,
},
Type
:
&
stringList
{},
}
var
swarmConnectCmd
=
&
cmds
.
Command
{
Helptext
:
cmds
.
HelpText
{
Tagline
:
"Open connection to a given peer"
,
ShortDescription
:
`
'ipfs swarm connect' opens a connection to a peer address. The address format
is an ipfs multiaddr:
ipfs swarm connect /ip4/104.131.131.82/tcp/4001/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
`
,
},
Arguments
:
[]
cmds
.
Argument
{
cmds
.
StringArg
(
"address"
,
true
,
true
,
"address of peer to connect to"
),
},
Run
:
func
(
req
cmds
.
Request
)
(
interface
{},
error
)
{
ctx
:=
context
.
TODO
()
log
.
Debug
(
"ipfs swarm connect"
)
n
,
err
:=
req
.
Context
()
.
GetNode
()
if
err
!=
nil
{
return
nil
,
err
}
addrs
,
err
:=
internal
.
CastToStrings
(
req
.
Arguments
())
if
err
!=
nil
{
return
nil
,
cmds
.
ClientError
(
"Improperly formatted peer addresses: "
+
err
.
Error
())
}
if
n
.
Network
==
nil
{
return
nil
,
errNotOnline
}
peers
,
err
:=
peersWithAddresses
(
n
.
Peerstore
,
addrs
)
if
err
!=
nil
{
return
nil
,
err
}
output
:=
make
([]
string
,
len
(
peers
))
for
i
,
p
:=
range
peers
{
output
[
i
]
=
"connect "
+
p
.
ID
()
.
String
()
err
:=
n
.
Network
.
DialPeer
(
ctx
,
p
)
if
err
!=
nil
{
output
[
i
]
+=
" failure: "
+
err
.
Error
()
}
else
{
output
[
i
]
+=
" success"
}
}
return
&
stringList
{
output
},
nil
},
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
stringListMarshaler
,
},
Type
:
&
stringList
{},
}
func
stringListMarshaler
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
list
,
ok
:=
res
.
Output
()
.
(
*
stringList
)
if
!
ok
{
return
nil
,
errors
.
New
(
"failed to cast []string"
)
}
var
buf
bytes
.
Buffer
for
_
,
s
:=
range
list
.
Strings
{
buf
.
Write
([]
byte
(
s
))
buf
.
Write
([]
byte
(
"
\n
"
))
}
return
buf
.
Bytes
(),
nil
}
// splitAddresses is a function that takes in a slice of string peer addresses
// (multiaddr + peerid) and returns slices of multiaddrs and peerids.
func
splitAddresses
(
addrs
[]
string
)
(
maddrs
[]
ma
.
Multiaddr
,
pids
[]
peer
.
ID
,
err
error
)
{
maddrs
=
make
([]
ma
.
Multiaddr
,
len
(
addrs
))
pids
=
make
([]
peer
.
ID
,
len
(
addrs
))
for
i
,
addr
:=
range
addrs
{
a
,
err
:=
ma
.
NewMultiaddr
(
path
.
Dir
(
addr
))
if
err
!=
nil
{
return
nil
,
nil
,
cmds
.
ClientError
(
"invalid peer address: "
+
err
.
Error
())
}
maddrs
[
i
]
=
a
pids
[
i
]
=
peer
.
DecodePrettyID
(
path
.
Base
(
addr
))
}
return
}
// peersWithAddresses is a function that takes in a slice of string peer addresses
// (multiaddr + peerid) and returns a slice of properly constructed peers
func
peersWithAddresses
(
ps
peer
.
Peerstore
,
addrs
[]
string
)
([]
peer
.
Peer
,
error
)
{
maddrs
,
pids
,
err
:=
splitAddresses
(
addrs
)
if
err
!=
nil
{
return
nil
,
err
}
peers
:=
make
([]
peer
.
Peer
,
len
(
pids
))
for
i
,
pid
:=
range
pids
{
p
,
err
:=
ps
.
Get
(
pid
)
if
err
!=
nil
{
return
nil
,
err
}
p
.
AddAddress
(
maddrs
[
i
])
peers
[
i
]
=
p
}
return
peers
,
nil
}
net/interface.go
浏览文件 @
1c36d520
...
@@ -2,6 +2,7 @@ package net
...
@@ -2,6 +2,7 @@ package net
import
(
import
(
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
conn
"github.com/jbenet/go-ipfs/net/conn"
msg
"github.com/jbenet/go-ipfs/net/message"
msg
"github.com/jbenet/go-ipfs/net/message"
mux
"github.com/jbenet/go-ipfs/net/mux"
mux
"github.com/jbenet/go-ipfs/net/mux"
srv
"github.com/jbenet/go-ipfs/net/service"
srv
"github.com/jbenet/go-ipfs/net/service"
...
@@ -34,6 +35,9 @@ type Network interface {
...
@@ -34,6 +35,9 @@ type Network interface {
// GetPeerList returns the list of peers currently connected in this network.
// GetPeerList returns the list of peers currently connected in this network.
GetPeerList
()
[]
peer
.
Peer
GetPeerList
()
[]
peer
.
Peer
// GetConnections returns the list of connections currently open in this network.
GetConnections
()
[]
conn
.
Conn
// GetBandwidthTotals returns the total number of bytes passed through
// GetBandwidthTotals returns the total number of bytes passed through
// the network since it was instantiated
// the network since it was instantiated
GetBandwidthTotals
()
(
uint64
,
uint64
)
GetBandwidthTotals
()
(
uint64
,
uint64
)
...
...
net/net.go
浏览文件 @
1c36d520
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
package
net
package
net
import
(
import
(
conn
"github.com/jbenet/go-ipfs/net/conn"
msg
"github.com/jbenet/go-ipfs/net/message"
msg
"github.com/jbenet/go-ipfs/net/message"
mux
"github.com/jbenet/go-ipfs/net/mux"
mux
"github.com/jbenet/go-ipfs/net/mux"
swarm
"github.com/jbenet/go-ipfs/net/swarm"
swarm
"github.com/jbenet/go-ipfs/net/swarm"
...
@@ -99,6 +100,11 @@ func (n *IpfsNetwork) GetPeerList() []peer.Peer {
...
@@ -99,6 +100,11 @@ func (n *IpfsNetwork) GetPeerList() []peer.Peer {
return
n
.
swarm
.
GetPeerList
()
return
n
.
swarm
.
GetPeerList
()
}
}
// GetConnections returns the networks list of open connections
func
(
n
*
IpfsNetwork
)
GetConnections
()
[]
conn
.
Conn
{
return
n
.
swarm
.
Connections
()
}
// GetBandwidthTotals returns the total amount of bandwidth transferred
// GetBandwidthTotals returns the total amount of bandwidth transferred
func
(
n
*
IpfsNetwork
)
GetBandwidthTotals
()
(
in
uint64
,
out
uint64
)
{
func
(
n
*
IpfsNetwork
)
GetBandwidthTotals
()
(
in
uint64
,
out
uint64
)
{
return
n
.
muxer
.
GetBandwidthTotals
()
return
n
.
muxer
.
GetBandwidthTotals
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论