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-swarm
Commits
49ab9018
Commit
49ab9018
authored
Sep 03, 2016
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
swarm: refactor to make modular construction easier
parent
aa0e1e6e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
31 deletions
+64
-31
swarm.go
swarm.go
+33
-6
swarm_listen.go
swarm_listen.go
+31
-25
No files found.
swarm.go
View file @
49ab9018
...
@@ -10,6 +10,14 @@ import (
...
@@ -10,6 +10,14 @@ import (
"sync"
"sync"
"time"
"time"
metrics
"github.com/libp2p/go-libp2p/p2p/metrics"
mconn
"github.com/libp2p/go-libp2p/p2p/metrics/conn"
inet
"github.com/libp2p/go-libp2p/p2p/net"
conn
"github.com/libp2p/go-libp2p/p2p/net/conn"
filter
"github.com/libp2p/go-libp2p/p2p/net/filter"
addrutil
"github.com/libp2p/go-libp2p/p2p/net/swarm/addr"
ci
"github.com/ipfs/go-libp2p-crypto"
peer
"github.com/ipfs/go-libp2p-peer"
peer
"github.com/ipfs/go-libp2p-peer"
pstore
"github.com/ipfs/go-libp2p-peerstore"
pstore
"github.com/ipfs/go-libp2p-peerstore"
transport
"github.com/ipfs/go-libp2p-transport"
transport
"github.com/ipfs/go-libp2p-transport"
...
@@ -19,12 +27,6 @@ import (
...
@@ -19,12 +27,6 @@ import (
pst
"github.com/jbenet/go-stream-muxer"
pst
"github.com/jbenet/go-stream-muxer"
"github.com/jbenet/goprocess"
"github.com/jbenet/goprocess"
goprocessctx
"github.com/jbenet/goprocess/context"
goprocessctx
"github.com/jbenet/goprocess/context"
metrics
"github.com/libp2p/go-libp2p/p2p/metrics"
mconn
"github.com/libp2p/go-libp2p/p2p/metrics/conn"
inet
"github.com/libp2p/go-libp2p/p2p/net"
conn
"github.com/libp2p/go-libp2p/p2p/net/conn"
filter
"github.com/libp2p/go-libp2p/p2p/net/filter"
addrutil
"github.com/libp2p/go-libp2p/p2p/net/swarm/addr"
psmss
"github.com/whyrusleeping/go-smux-multistream"
psmss
"github.com/whyrusleeping/go-smux-multistream"
spdy
"github.com/whyrusleeping/go-smux-spdystream"
spdy
"github.com/whyrusleeping/go-smux-spdystream"
yamux
"github.com/whyrusleeping/go-smux-yamux"
yamux
"github.com/whyrusleeping/go-smux-yamux"
...
@@ -143,6 +145,31 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
...
@@ -143,6 +145,31 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
return
s
,
nil
return
s
,
nil
}
}
func
NewBlankSwarm
(
ctx
context
.
Context
,
id
peer
.
ID
,
privkey
ci
.
PrivKey
)
*
Swarm
{
s
:=
&
Swarm
{
swarm
:
ps
.
NewSwarm
(
PSTransport
),
local
:
id
,
peers
:
pstore
.
NewPeerstore
(),
ctx
:
ctx
,
dialT
:
DialTimeout
,
notifs
:
make
(
map
[
inet
.
Notifiee
]
ps
.
Notifiee
),
fdRateLimit
:
make
(
chan
struct
{},
concurrentFdDials
),
Filters
:
filter
.
NewFilters
(),
dialer
:
conn
.
NewDialer
(
id
,
privkey
,
nil
),
}
// configure Swarm
s
.
limiter
=
newDialLimiter
(
s
.
dialAddr
)
s
.
proc
=
goprocessctx
.
WithContextAndTeardown
(
ctx
,
s
.
teardown
)
s
.
SetConnHandler
(
nil
)
// make sure to setup our own conn handler.
return
s
}
func
(
s
*
Swarm
)
AddTransport
(
t
transport
.
Transport
)
{
s
.
transports
=
append
(
s
.
transports
,
t
)
}
func
(
s
*
Swarm
)
teardown
()
error
{
func
(
s
*
Swarm
)
teardown
()
error
{
return
s
.
swarm
.
Close
()
return
s
.
swarm
.
Close
()
}
}
...
...
swarm_listen.go
View file @
49ab9018
...
@@ -13,37 +13,42 @@ import (
...
@@ -13,37 +13,42 @@ import (
context
"golang.org/x/net/context"
context
"golang.org/x/net/context"
)
)
func
(
s
*
Swarm
)
AddListenAddr
(
a
ma
.
Multiaddr
)
error
{
tpt
:=
s
.
transportForAddr
(
a
)
if
tpt
==
nil
{
return
fmt
.
Errorf
(
"no transport for address: %s"
,
a
)
}
d
,
err
:=
tpt
.
Dialer
(
a
,
transport
.
TimeoutOpt
(
DialTimeout
),
transport
.
ReusePorts
)
if
err
!=
nil
{
return
err
}
s
.
dialer
.
AddDialer
(
d
)
list
,
err
:=
tpt
.
Listen
(
a
)
if
err
!=
nil
{
return
err
}
err
=
s
.
addListener
(
list
)
if
err
!=
nil
{
return
err
}
return
nil
}
// Open listeners and reuse-dialers for the given addresses
// Open listeners and reuse-dialers for the given addresses
func
(
s
*
Swarm
)
setupInterfaces
(
addrs
[]
ma
.
Multiaddr
)
error
{
func
(
s
*
Swarm
)
setupInterfaces
(
addrs
[]
ma
.
Multiaddr
)
error
{
errs
:=
make
([]
error
,
len
(
addrs
))
errs
:=
make
([]
error
,
len
(
addrs
))
var
succeeded
int
var
succeeded
int
for
i
,
a
:=
range
addrs
{
for
i
,
a
:=
range
addrs
{
tpt
:=
s
.
transportForAddr
(
a
)
if
err
:=
s
.
AddListenAddr
(
a
);
err
!=
nil
{
if
tpt
==
nil
{
errs
[
i
]
=
fmt
.
Errorf
(
"no transport for address: %s"
,
a
)
continue
}
d
,
err
:=
tpt
.
Dialer
(
a
,
transport
.
TimeoutOpt
(
DialTimeout
),
transport
.
ReusePorts
)
if
err
!=
nil
{
errs
[
i
]
=
err
continue
}
s
.
dialer
.
AddDialer
(
d
)
list
,
err
:=
tpt
.
Listen
(
a
)
if
err
!=
nil
{
errs
[
i
]
=
err
errs
[
i
]
=
err
continue
}
else
{
succeeded
++
}
}
err
=
s
.
addListener
(
list
)
if
err
!=
nil
{
errs
[
i
]
=
err
continue
}
succeeded
++
}
}
for
i
,
e
:=
range
errs
{
for
i
,
e
:=
range
errs
{
...
@@ -51,6 +56,7 @@ func (s *Swarm) setupInterfaces(addrs []ma.Multiaddr) error {
...
@@ -51,6 +56,7 @@ func (s *Swarm) setupInterfaces(addrs []ma.Multiaddr) error {
log
.
Warning
(
"listen on %s failed: %s"
,
addrs
[
i
],
errs
[
i
])
log
.
Warning
(
"listen on %s failed: %s"
,
addrs
[
i
],
errs
[
i
])
}
}
}
}
if
succeeded
==
0
&&
len
(
addrs
)
>
0
{
if
succeeded
==
0
&&
len
(
addrs
)
>
0
{
return
fmt
.
Errorf
(
"failed to listen on any addresses: %s"
,
errs
)
return
fmt
.
Errorf
(
"failed to listen on any addresses: %s"
,
errs
)
}
}
...
@@ -83,7 +89,7 @@ func (s *Swarm) addListener(tptlist transport.Listener) error {
...
@@ -83,7 +89,7 @@ func (s *Swarm) addListener(tptlist transport.Listener) error {
list
.
SetAddrFilters
(
s
.
Filters
)
list
.
SetAddrFilters
(
s
.
Filters
)
if
cw
,
ok
:=
list
.
(
conn
.
ListenerConnWrapper
);
ok
{
if
cw
,
ok
:=
list
.
(
conn
.
ListenerConnWrapper
);
ok
&&
s
.
bwc
!=
nil
{
cw
.
SetConnWrapper
(
func
(
c
transport
.
Conn
)
transport
.
Conn
{
cw
.
SetConnWrapper
(
func
(
c
transport
.
Conn
)
transport
.
Conn
{
return
mconn
.
WrapConn
(
s
.
bwc
,
c
)
return
mconn
.
WrapConn
(
s
.
bwc
,
c
)
})
})
...
...
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