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
p2p
go-p2p-kad-dht
Commits
2c1700b4
Commit
2c1700b4
authored
Dec 08, 2014
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(dht) remove extraneous return value
License: MIT Signed-off-by:
Brian Tiger Chow
<
brian@perfmode.com
>
parent
5e61ef67
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
29 deletions
+20
-29
dht.go
dht.go
+4
-13
dht_test.go
dht_test.go
+16
-16
No files found.
dht.go
View file @
2c1700b4
...
...
@@ -97,32 +97,23 @@ func NewDHT(ctx context.Context, p peer.Peer, ps peer.Peerstore, dialer inet.Dia
}
// Connect to a new peer at the given address, ping and add to the routing table
func
(
dht
*
IpfsDHT
)
Connect
(
ctx
context
.
Context
,
npeer
peer
.
Peer
)
(
peer
.
Peer
,
error
)
{
// TODO(jbenet,whyrusleeping)
//
// Connect should take in a Peer (with ID). In a sense, we shouldn't be
// allowing connections to random multiaddrs without knowing who we're
// speaking to (i.e. peer.ID). In terms of moving around simple addresses
// -- instead of an (ID, Addr) pair -- we can use:
//
// /ip4/10.20.30.40/tcp/1234/ipfs/Qxhxxchxzcncxnzcnxzcxzm
//
func
(
dht
*
IpfsDHT
)
Connect
(
ctx
context
.
Context
,
npeer
peer
.
Peer
)
error
{
err
:=
dht
.
dialer
.
DialPeer
(
ctx
,
npeer
)
if
err
!=
nil
{
return
nil
,
err
return
err
}
// Ping new peer to register in their routing table
// NOTE: this should be done better...
err
=
dht
.
Ping
(
ctx
,
npeer
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to ping newly connected peer: %s
\n
"
,
err
)
return
fmt
.
Errorf
(
"failed to ping newly connected peer: %s
\n
"
,
err
)
}
log
.
Event
(
ctx
,
"connect"
,
dht
.
self
,
npeer
)
dht
.
Update
(
ctx
,
npeer
)
return
npeer
,
nil
return
nil
}
// HandleMessage implements the inet.Handler interface.
...
...
dht_test.go
View file @
2c1700b4
...
...
@@ -101,7 +101,7 @@ func TestPing(t *testing.T) {
defer
dhtA
.
dialer
.
(
inet
.
Network
)
.
Close
()
defer
dhtB
.
dialer
.
(
inet
.
Network
)
.
Close
()
_
,
err
=
dhtA
.
Connect
(
ctx
,
peerB
)
err
=
dhtA
.
Connect
(
ctx
,
peerB
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -151,7 +151,7 @@ func TestValueGetSet(t *testing.T) {
defer
dhtA
.
dialer
.
(
inet
.
Network
)
.
Close
()
defer
dhtB
.
dialer
.
(
inet
.
Network
)
.
Close
()
_
,
err
=
dhtA
.
Connect
(
ctx
,
peerB
)
err
=
dhtA
.
Connect
(
ctx
,
peerB
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -194,17 +194,17 @@ func TestProvides(t *testing.T) {
}
}()
_
,
err
:=
dhts
[
0
]
.
Connect
(
ctx
,
peers
[
1
])
err
:=
dhts
[
0
]
.
Connect
(
ctx
,
peers
[
1
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
2
])
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
2
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
3
])
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
3
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -256,17 +256,17 @@ func TestProvidesAsync(t *testing.T) {
}
}()
_
,
err
:=
dhts
[
0
]
.
Connect
(
ctx
,
peers
[
1
])
err
:=
dhts
[
0
]
.
Connect
(
ctx
,
peers
[
1
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
2
])
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
2
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
3
])
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
3
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -321,17 +321,17 @@ func TestLayeredGet(t *testing.T) {
}
}()
_
,
err
:=
dhts
[
0
]
.
Connect
(
ctx
,
peers
[
1
])
err
:=
dhts
[
0
]
.
Connect
(
ctx
,
peers
[
1
])
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to connect: %s"
,
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
2
])
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
2
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
3
])
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
3
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -376,17 +376,17 @@ func TestFindPeer(t *testing.T) {
}
}()
_
,
err
:=
dhts
[
0
]
.
Connect
(
ctx
,
peers
[
1
])
err
:=
dhts
[
0
]
.
Connect
(
ctx
,
peers
[
1
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
2
])
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
2
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
3
])
err
=
dhts
[
1
]
.
Connect
(
ctx
,
peers
[
3
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -435,14 +435,14 @@ func TestConnectCollision(t *testing.T) {
done
:=
make
(
chan
struct
{})
go
func
()
{
_
,
err
:=
dhtA
.
Connect
(
ctx
,
peerB
)
err
:=
dhtA
.
Connect
(
ctx
,
peerB
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
done
<-
struct
{}{}
}()
go
func
()
{
_
,
err
:=
dhtB
.
Connect
(
ctx
,
peerA
)
err
:=
dhtB
.
Connect
(
ctx
,
peerA
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
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