Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
6cbb9390
Commit
6cbb9390
authored
7 years ago
by
Lars Gierth
Browse files
Options
Download
Email Patches
Plain Diff
basichost: allow overriding Addrs()
parent
be827c6a
master
2018-Q4-OKR
feat/protobuf
feat/udp
fix/473
fix/no-custom-field
fix/reset-ping-stream
fix/revert-correct-external-addr
gx/update-jccl6u
gx/update-nza0mn
jenkinsfile
kevina/fix-go-vet
multistream-ping
punching
revert-276-update-go-detect-race
v6.0.23
v6.0.22
v6.0.21
v6.0.20
v6.0.19
v6.0.18
v6.0.17
v6.0.16
v6.0.15
v6.0.14
v6.0.13
v6.0.12
v6.0.11
v6.0.10
v6.0.9
v6.0.8
v6.0.7
v6.0.6
v6.0.5
v6.0.4
v6.0.3
v6.0.2
v6.0.1
v6.0.0
v5.0.21
v5.0.20
v5.0.19
v5.0.18
v5.0.17
v5.0.16
v5.0.15
v5.0.14
v5.0.13
v5.0.12
v5.0.11
v5.0.10
v5.0.9
v5.0.8
v5.0.7
v5.0.6
v5.0.5
v5.0.4
v5.0.3
v5.0.2
v5.0.1
v5.0.0
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.5
v4.4.4
v4.4.3
v4.4.2
v4.4.1
v4.4.0
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
p2p/host/basic/basic_host.go
+18
-2
p2p/host/basic/basic_host.go
p2p/host/basic/basic_host_test.go
+20
-0
p2p/host/basic/basic_host_test.go
with
38 additions
and
2 deletions
+38
-2
p2p/host/basic/basic_host.go
View file @
6cbb9390
...
...
@@ -23,6 +23,10 @@ var log = logging.Logger("basichost")
var
NegotiateTimeout
=
time
.
Second
*
60
// AddrsFactory functions can be passed to New in order to override
// addresses returned by Addrs.
type
AddrsFactory
func
([]
ma
.
Multiaddr
)
[]
ma
.
Multiaddr
// Option is a type used to pass in options to the host.
type
Option
int
...
...
@@ -45,6 +49,7 @@ type BasicHost struct {
mux
*
msmux
.
MultistreamMuxer
ids
*
identify
.
IDService
natmgr
*
natManager
addrs
AddrsFactory
NegotiateTimeout
time
.
Duration
...
...
@@ -72,6 +77,9 @@ func New(net inet.Network, opts ...interface{}) *BasicHost {
// setup host services
h
.
ids
=
identify
.
NewIDService
(
h
)
// default addresses factory, can be overridden via opts argument
h
.
addrs
=
func
(
addrs
[]
ma
.
Multiaddr
)
[]
ma
.
Multiaddr
{
return
addrs
}
for
_
,
o
:=
range
opts
{
switch
o
:=
o
.
(
type
)
{
case
Option
:
...
...
@@ -81,6 +89,8 @@ func New(net inet.Network, opts ...interface{}) *BasicHost {
}
case
metrics
.
Reporter
:
h
.
bwc
=
o
case
AddrsFactory
:
h
.
addrs
=
AddrsFactory
(
o
)
}
}
...
...
@@ -336,9 +346,15 @@ func (h *BasicHost) dialPeer(ctx context.Context, p peer.ID) error {
return
nil
}
// Addrs returns
all the
addresses
of BasicHost at this moment in time
.
//
It's ok to not include addresses if they're not available to be used now
.
// Addrs returns
listening
addresses
that are safe to announce to the network
.
//
The output is the same as AllAddrs, but processed by AddrsFactory
.
func
(
h
*
BasicHost
)
Addrs
()
[]
ma
.
Multiaddr
{
return
h
.
addrs
(
h
.
AllAddrs
())
}
// AllAddrs returns all the addresses of BasicHost at this moment in time.
// It's ok to not include addresses if they're not available to be used now.
func
(
h
*
BasicHost
)
AllAddrs
()
[]
ma
.
Multiaddr
{
addrs
,
err
:=
h
.
Network
()
.
InterfaceListenAddresses
()
if
err
!=
nil
{
log
.
Debug
(
"error retrieving network interface addrs"
)
...
...
This diff is collapsed.
Click to expand it.
p2p/host/basic/basic_host_test.go
View file @
6cbb9390
...
...
@@ -11,6 +11,7 @@ import (
inet
"github.com/libp2p/go-libp2p-net"
testutil
"github.com/libp2p/go-libp2p-netutil"
protocol
"github.com/libp2p/go-libp2p-protocol"
ma
"github.com/multiformats/go-multiaddr"
)
func
TestHostSimple
(
t
*
testing
.
T
)
{
...
...
@@ -63,6 +64,25 @@ func TestHostSimple(t *testing.T) {
}
}
func
TestHostAddrsFactory
(
t
*
testing
.
T
)
{
maddr
:=
ma
.
StringCast
(
"/ip4/1.2.3.4/tcp/1234"
)
addrsFactory
:=
func
(
addrs
[]
ma
.
Multiaddr
)
[]
ma
.
Multiaddr
{
return
[]
ma
.
Multiaddr
{
maddr
}
}
ctx
:=
context
.
Background
()
h
:=
New
(
testutil
.
GenSwarmNetwork
(
t
,
ctx
),
AddrsFactory
(
addrsFactory
))
defer
h
.
Close
()
addrs
:=
h
.
Addrs
()
if
len
(
addrs
)
!=
1
{
t
.
Fatalf
(
"expected 1 addr, got %d"
,
len
(
addrs
))
}
if
addrs
[
0
]
!=
maddr
{
t
.
Fatalf
(
"expected %s, got %s"
,
maddr
.
String
(),
addrs
[
0
]
.
String
())
}
}
func
getHostPair
(
ctx
context
.
Context
,
t
*
testing
.
T
)
(
host
.
Host
,
host
.
Host
)
{
h1
:=
New
(
testutil
.
GenSwarmNetwork
(
t
,
ctx
))
h2
:=
New
(
testutil
.
GenSwarmNetwork
(
t
,
ctx
))
...
...
This diff is collapsed.
Click to expand it.
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
Menu
Projects
Groups
Snippets
Help