Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
81135f3d
Commit
81135f3d
authored
Nov 18, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added ipfs swarm connect
parent
4d78c2d7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
102 additions
and
2 deletions
+102
-2
core/commands2/swarm.go
core/commands2/swarm.go
+102
-2
No files found.
core/commands2/swarm.go
View file @
81135f3d
...
...
@@ -3,9 +3,15 @@ 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
{
...
...
@@ -26,8 +32,8 @@ ipfs peers in the internet.
`
,
},
Subcommands
:
map
[
string
]
*
cmds
.
Command
{
"peers"
:
swarmPeersCmd
,
//
"connect": swarmConnectCmd,
"peers"
:
swarmPeersCmd
,
"connect"
:
swarmConnectCmd
,
},
}
...
...
@@ -66,6 +72,62 @@ ipfs swarm peers lists the set of peers this node is connected to.
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
{
...
...
@@ -79,3 +141,41 @@ func stringListMarshaler(res cmds.Response) ([]byte, error) {
}
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment