Skip to content
GitLab
Menu
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
7081c172
Commit
7081c172
authored
Oct 04, 2016
by
Jeromy Johnson
Committed by
GitHub
Oct 04, 2016
Browse files
Merge pull request #131 from libp2p/feat/extracting-4
Feat/extracting 4
parents
a54a624e
1a53ad77
Changes
23
Hide whitespace changes
Inline
Side-by-side
examples/hosts/main.go
View file @
7081c172
...
...
@@ -8,9 +8,9 @@ import (
"log"
"strings"
host
"github.com/libp2p/go-libp2p-host"
inet
"github.com/libp2p/go-libp2p-net"
net
"github.com/libp2p/go-libp2p-net"
host
"github.com/libp2p/go-libp2p/p2p/host"
bhost
"github.com/libp2p/go-libp2p/p2p/host/basic"
swarm
"github.com/libp2p/go-libp2p/p2p/net/swarm"
...
...
p2p/discovery/mdns.go
View file @
7081c172
...
...
@@ -15,7 +15,7 @@ import (
logging
"github.com/ipfs/go-log"
ma
"github.com/jbenet/go-multiaddr"
manet
"github.com/jbenet/go-multiaddr-net"
"github.com/libp2p/go-libp2p
/p2p/
host"
"github.com/libp2p/go-libp2p
-
host"
"github.com/whyrusleeping/mdns"
)
...
...
p2p/discovery/mdns_test.go
View file @
7081c172
...
...
@@ -5,7 +5,7 @@ import (
"testing"
"time"
host
"github.com/libp2p/go-libp2p
/p2p/
host"
host
"github.com/libp2p/go-libp2p
-
host"
netutil
"github.com/libp2p/go-libp2p/p2p/test/util"
pstore
"github.com/ipfs/go-libp2p-peerstore"
...
...
p2p/host/basic/basic_host_test.go
View file @
7081c172
...
...
@@ -7,9 +7,9 @@ import (
"testing"
"time"
host
"github.com/libp2p/go-libp2p-host"
inet
"github.com/libp2p/go-libp2p-net"
protocol
"github.com/libp2p/go-libp2p-protocol"
host
"github.com/libp2p/go-libp2p/p2p/host"
testutil
"github.com/libp2p/go-libp2p/p2p/test/util"
)
...
...
p2p/host/host.go
deleted
100644 → 0
View file @
a54a624e
package
host
import
(
"context"
peer
"github.com/ipfs/go-libp2p-peer"
pstore
"github.com/ipfs/go-libp2p-peerstore"
logging
"github.com/ipfs/go-log"
ma
"github.com/jbenet/go-multiaddr"
metrics
"github.com/libp2p/go-libp2p-metrics"
inet
"github.com/libp2p/go-libp2p-net"
protocol
"github.com/libp2p/go-libp2p-protocol"
msmux
"github.com/whyrusleeping/go-multistream"
)
var
log
=
logging
.
Logger
(
"github.com/libp2p/go-libp2p/p2p/host"
)
// Host is an object participating in a p2p network, which
// implements protocols or provides services. It handles
// requests like a Server, and issues requests like a Client.
// It is called Host because it is both Server and Client (and Peer
// may be confusing).
type
Host
interface
{
// ID returns the (local) peer.ID associated with this Host
ID
()
peer
.
ID
// Peerstore returns the Host's repository of Peer Addresses and Keys.
Peerstore
()
pstore
.
Peerstore
// Returns the listen addresses of the Host
Addrs
()
[]
ma
.
Multiaddr
// Networks returns the Network interface of the Host
Network
()
inet
.
Network
// Mux returns the Mux multiplexing incoming streams to protocol handlers
Mux
()
*
msmux
.
MultistreamMuxer
// Connect ensures there is a connection between this host and the peer with
// given peer.ID. Connect will absorb the addresses in pi into its internal
// peerstore. If there is not an active connection, Connect will issue a
// h.Network.Dial, and block until a connection is open, or an error is
// returned. // TODO: Relay + NAT.
Connect
(
ctx
context
.
Context
,
pi
pstore
.
PeerInfo
)
error
// SetStreamHandler sets the protocol handler on the Host's Mux.
// This is equivalent to:
// host.Mux().SetHandler(proto, handler)
// (Threadsafe)
SetStreamHandler
(
pid
protocol
.
ID
,
handler
inet
.
StreamHandler
)
// SetStreamHandlerMatch sets the protocol handler on the Host's Mux
// using a matching function for protocol selection.
SetStreamHandlerMatch
(
protocol
.
ID
,
func
(
string
)
bool
,
inet
.
StreamHandler
)
// RemoveStreamHandler removes a handler on the mux that was set by
// SetStreamHandler
RemoveStreamHandler
(
pid
protocol
.
ID
)
// NewStream opens a new stream to given peer p, and writes a p2p/protocol
// header with given protocol.ID. If there is no connection to p, attempts
// to create one. If ProtocolID is "", writes no header.
// (Threadsafe)
NewStream
(
ctx
context
.
Context
,
p
peer
.
ID
,
pids
...
protocol
.
ID
)
(
inet
.
Stream
,
error
)
// Close shuts down the host, its Network, and services.
Close
()
error
GetBandwidthReporter
()
metrics
.
Reporter
}
p2p/host/match.go
deleted
100644 → 0
View file @
a54a624e
package
host
import
(
"github.com/libp2p/go-libp2p-protocol"
"strings"
semver
"github.com/coreos/go-semver/semver"
)
func
MultistreamSemverMatcher
(
base
protocol
.
ID
)
(
func
(
string
)
bool
,
error
)
{
parts
:=
strings
.
Split
(
string
(
base
),
"/"
)
vers
,
err
:=
semver
.
NewVersion
(
parts
[
len
(
parts
)
-
1
])
if
err
!=
nil
{
return
nil
,
err
}
return
func
(
check
string
)
bool
{
chparts
:=
strings
.
Split
(
check
,
"/"
)
if
len
(
chparts
)
!=
len
(
parts
)
{
return
false
}
for
i
,
v
:=
range
chparts
[
:
len
(
chparts
)
-
1
]
{
if
parts
[
i
]
!=
v
{
return
false
}
}
chvers
,
err
:=
semver
.
NewVersion
(
chparts
[
len
(
chparts
)
-
1
])
if
err
!=
nil
{
return
false
}
return
vers
.
Major
==
chvers
.
Major
&&
vers
.
Minor
>=
chvers
.
Minor
},
nil
}
p2p/host/match_test.go
deleted
100644 → 0
View file @
a54a624e
package
host
import
(
"testing"
)
func
TestSemverMatching
(
t
*
testing
.
T
)
{
m
,
err
:=
MultistreamSemverMatcher
(
"/testing/4.3.5"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
cases
:=
map
[
string
]
bool
{
"/testing/4.3.0"
:
true
,
"/testing/4.3.7"
:
true
,
"/testing/4.3.5"
:
true
,
"/testing/4.2.7"
:
true
,
"/testing/4.0.0"
:
true
,
"/testing/5.0.0"
:
false
,
"/cars/dogs/4.3.5"
:
false
,
"/foo/1.0.0"
:
false
,
""
:
false
,
"dogs"
:
false
,
"/foo"
:
false
,
"/foo/1.1.1.1"
:
false
,
}
for
p
,
ok
:=
range
cases
{
if
m
(
p
)
!=
ok
{
t
.
Fatalf
(
"expected %s to be %t"
,
p
,
ok
)
}
}
}
p2p/host/routed/routed.go
View file @
7081c172
...
...
@@ -5,7 +5,7 @@ import (
"fmt"
"time"
host
"github.com/libp2p/go-libp2p
/p2p/
host"
host
"github.com/libp2p/go-libp2p
-
host"
lgbl
"github.com/ipfs/go-libp2p-loggables"
peer
"github.com/ipfs/go-libp2p-peer"
...
...
p2p/net/mock/interface.go
View file @
7081c172
...
...
@@ -10,7 +10,7 @@ import (
"io"
"time"
host
"github.com/libp2p/go-libp2p
/p2p/
host"
host
"github.com/libp2p/go-libp2p
-
host"
ic
"github.com/ipfs/go-libp2p-crypto"
peer
"github.com/ipfs/go-libp2p-peer"
...
...
p2p/net/mock/mock_net.go
View file @
7081c172
...
...
@@ -6,7 +6,7 @@ import (
"sort"
"sync"
host
"github.com/libp2p/go-libp2p
/p2p/
host"
host
"github.com/libp2p/go-libp2p
-
host"
bhost
"github.com/libp2p/go-libp2p/p2p/host/basic"
p2putil
"github.com/libp2p/go-libp2p/p2p/test/util"
...
...
p2p/net/swarm/limiter.go
View file @
7081c172
...
...
@@ -7,11 +7,11 @@ import (
peer
"github.com/ipfs/go-libp2p-peer"
ma
"github.com/jbenet/go-multiaddr"
addrutil
"github.com/libp2p/go-addr-util"
conn
"github.com/libp2p/go-libp2p-conn"
i
conn
"github.com/libp2p/go-libp2p-
interface-
conn"
)
type
dialResult
struct
{
Conn
conn
.
Conn
Conn
i
conn
.
Conn
Err
error
}
...
...
@@ -38,14 +38,14 @@ type dialLimiter struct {
fdLimit
int
waitingOnFd
[]
*
dialJob
dialFunc
func
(
context
.
Context
,
peer
.
ID
,
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
dialFunc
func
(
context
.
Context
,
peer
.
ID
,
ma
.
Multiaddr
)
(
i
conn
.
Conn
,
error
)
activePerPeer
map
[
peer
.
ID
]
int
perPeerLimit
int
waitingOnPeerLimit
map
[
peer
.
ID
][]
*
dialJob
}
type
dialfunc
func
(
context
.
Context
,
peer
.
ID
,
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
type
dialfunc
func
(
context
.
Context
,
peer
.
ID
,
ma
.
Multiaddr
)
(
i
conn
.
Conn
,
error
)
func
newDialLimiter
(
df
dialfunc
)
*
dialLimiter
{
return
newDialLimiterWithParams
(
df
,
concurrentFdDials
,
defaultPerPeerRateLimit
)
...
...
p2p/net/swarm/limiter_test.go
View file @
7081c172
...
...
@@ -10,7 +10,7 @@ import (
peer
"github.com/ipfs/go-libp2p-peer"
ma
"github.com/jbenet/go-multiaddr"
conn
"github.com/libp2p/go-libp2p-conn"
i
conn
"github.com/libp2p/go-libp2p-
interface-
conn"
mafmt
"github.com/whyrusleeping/mafmt"
)
...
...
@@ -55,13 +55,13 @@ func tryDialAddrs(ctx context.Context, l *dialLimiter, p peer.ID, addrs []ma.Mul
}
func
hangDialFunc
(
hang
chan
struct
{})
dialfunc
{
return
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
a
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
{
return
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
a
ma
.
Multiaddr
)
(
i
conn
.
Conn
,
error
)
{
if
mafmt
.
UTP
.
Matches
(
a
)
{
return
conn
.
Conn
(
nil
),
nil
return
i
conn
.
Conn
(
nil
),
nil
}
if
tcpPortOver
(
a
,
10
)
{
return
conn
.
Conn
(
nil
),
nil
return
i
conn
.
Conn
(
nil
),
nil
}
<-
hang
...
...
@@ -171,9 +171,9 @@ func TestFDLimiting(t *testing.T) {
func
TestTokenRedistribution
(
t
*
testing
.
T
)
{
hangchs
:=
make
(
map
[
peer
.
ID
]
chan
struct
{})
df
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
a
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
{
df
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
a
ma
.
Multiaddr
)
(
i
conn
.
Conn
,
error
)
{
if
tcpPortOver
(
a
,
10
)
{
return
(
conn
.
Conn
)(
nil
),
nil
return
(
i
conn
.
Conn
)(
nil
),
nil
}
<-
hangchs
[
p
]
...
...
@@ -260,9 +260,9 @@ func TestTokenRedistribution(t *testing.T) {
}
func
TestStressLimiter
(
t
*
testing
.
T
)
{
df
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
a
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
{
df
:=
func
(
ctx
context
.
Context
,
p
peer
.
ID
,
a
ma
.
Multiaddr
)
(
i
conn
.
Conn
,
error
)
{
if
tcpPortOver
(
a
,
1000
)
{
return
conn
.
Conn
(
nil
),
nil
return
i
conn
.
Conn
(
nil
),
nil
}
time
.
Sleep
(
time
.
Millisecond
*
time
.
Duration
(
5
+
rand
.
Intn
(
100
)))
...
...
p2p/net/swarm/swarm_addr.go
View file @
7081c172
...
...
@@ -3,7 +3,7 @@ package swarm
import
(
ma
"github.com/jbenet/go-multiaddr"
addrutil
"github.com/libp2p/go-addr-util"
conn
"github.com/libp2p/go-libp2p-conn"
i
conn
"github.com/libp2p/go-libp2p-
interface-
conn"
)
// ListenAddresses returns a list of addresses at which this swarm listens.
...
...
@@ -11,7 +11,7 @@ func (s *Swarm) ListenAddresses() []ma.Multiaddr {
listeners
:=
s
.
swarm
.
Listeners
()
addrs
:=
make
([]
ma
.
Multiaddr
,
0
,
len
(
listeners
))
for
_
,
l
:=
range
listeners
{
if
l2
,
ok
:=
l
.
NetListener
()
.
(
conn
.
Listener
);
ok
{
if
l2
,
ok
:=
l
.
NetListener
()
.
(
i
conn
.
Listener
);
ok
{
addrs
=
append
(
addrs
,
l2
.
Multiaddr
())
}
}
...
...
p2p/net/swarm/swarm_conn.go
View file @
7081c172
...
...
@@ -4,13 +4,12 @@ import (
"context"
"fmt"
inet
"github.com/libp2p/go-libp2p-net"
ic
"github.com/ipfs/go-libp2p-crypto"
peer
"github.com/ipfs/go-libp2p-peer"
ma
"github.com/jbenet/go-multiaddr"
ps
"github.com/jbenet/go-peerstream"
conn
"github.com/libp2p/go-libp2p-conn"
iconn
"github.com/libp2p/go-libp2p-interface-conn"
inet
"github.com/libp2p/go-libp2p-net"
)
// Conn is a simple wrapper around a ps.Conn that also exposes
...
...
@@ -33,12 +32,12 @@ func (c *Conn) StreamConn() *ps.Conn {
return
(
*
ps
.
Conn
)(
c
)
}
func
(
c
*
Conn
)
RawConn
()
conn
.
Conn
{
func
(
c
*
Conn
)
RawConn
()
i
conn
.
Conn
{
// righly panic if these things aren't true. it is an expected
// invariant that these Conns are all of the typewe expect:
// ps.Conn wrapping a conn.Conn
// if we get something else it is programmer error.
return
(
*
ps
.
Conn
)(
c
)
.
NetConn
()
.
(
conn
.
Conn
)
return
(
*
ps
.
Conn
)(
c
)
.
NetConn
()
.
(
i
conn
.
Conn
)
}
func
(
c
*
Conn
)
String
()
string
{
...
...
@@ -94,7 +93,7 @@ func (c *Conn) Close() error {
func
wrapConn
(
psc
*
ps
.
Conn
)
(
*
Conn
,
error
)
{
// grab the underlying connection.
if
_
,
ok
:=
psc
.
NetConn
()
.
(
conn
.
Conn
);
!
ok
{
if
_
,
ok
:=
psc
.
NetConn
()
.
(
i
conn
.
Conn
);
!
ok
{
// this should never happen. if we see it ocurring it means that we added
// a Listener to the ps.Swarm that is NOT one of our net/conn.Listener.
return
nil
,
fmt
.
Errorf
(
"swarm connHandler: invalid conn (not a conn.Conn): %s"
,
psc
)
...
...
p2p/net/swarm/swarm_dial.go
View file @
7081c172
...
...
@@ -11,7 +11,7 @@ import (
peer
"github.com/ipfs/go-libp2p-peer"
ma
"github.com/jbenet/go-multiaddr"
addrutil
"github.com/libp2p/go-addr-util"
conn
"github.com/libp2p/go-libp2p-conn"
i
conn
"github.com/libp2p/go-libp2p-
interface-
conn"
)
// Diagram of dial sync:
...
...
@@ -284,7 +284,7 @@ func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) {
return
swarmC
,
nil
}
func
(
s
*
Swarm
)
dialAddrs
(
ctx
context
.
Context
,
p
peer
.
ID
,
remoteAddrs
<-
chan
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
{
func
(
s
*
Swarm
)
dialAddrs
(
ctx
context
.
Context
,
p
peer
.
ID
,
remoteAddrs
<-
chan
ma
.
Multiaddr
)
(
i
conn
.
Conn
,
error
)
{
log
.
Debugf
(
"%s swarm dialing %s %s"
,
s
.
local
,
p
,
remoteAddrs
)
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
...
...
@@ -344,7 +344,7 @@ func (s *Swarm) limitedDial(ctx context.Context, p peer.ID, a ma.Multiaddr, resp
})
}
func
(
s
*
Swarm
)
dialAddr
(
ctx
context
.
Context
,
p
peer
.
ID
,
addr
ma
.
Multiaddr
)
(
conn
.
Conn
,
error
)
{
func
(
s
*
Swarm
)
dialAddr
(
ctx
context
.
Context
,
p
peer
.
ID
,
addr
ma
.
Multiaddr
)
(
i
conn
.
Conn
,
error
)
{
log
.
Debugf
(
"%s swarm dialing %s %s"
,
s
.
local
,
p
,
addr
)
connC
,
err
:=
s
.
dialer
.
Dial
(
ctx
,
addr
,
p
)
...
...
@@ -376,7 +376,7 @@ var ConnSetupTimeout = time.Minute * 5
// dialConnSetup is the setup logic for a connection from the dial side. it
// needs to add the Conn to the StreamSwarm, then run newConnSetup
func
dialConnSetup
(
ctx
context
.
Context
,
s
*
Swarm
,
connC
conn
.
Conn
)
(
*
Conn
,
error
)
{
func
dialConnSetup
(
ctx
context
.
Context
,
s
*
Swarm
,
connC
i
conn
.
Conn
)
(
*
Conn
,
error
)
{
deadline
,
ok
:=
ctx
.
Deadline
()
if
!
ok
{
...
...
p2p/net/swarm/swarm_listen.go
View file @
7081c172
...
...
@@ -8,6 +8,7 @@ import (
ma
"github.com/jbenet/go-multiaddr"
ps
"github.com/jbenet/go-peerstream"
conn
"github.com/libp2p/go-libp2p-conn"
iconn
"github.com/libp2p/go-libp2p-interface-conn"
mconn
"github.com/libp2p/go-libp2p-metrics/conn"
inet
"github.com/libp2p/go-libp2p-net"
transport
"github.com/libp2p/go-libp2p-transport"
...
...
@@ -98,7 +99,7 @@ func (s *Swarm) addListener(tptlist transport.Listener) error {
return
s
.
addConnListener
(
list
)
}
func
(
s
*
Swarm
)
addConnListener
(
list
conn
.
Listener
)
error
{
func
(
s
*
Swarm
)
addConnListener
(
list
i
conn
.
Listener
)
error
{
// AddListener to the peerstream Listener. this will begin accepting connections
// and streams!
sl
,
err
:=
s
.
swarm
.
AddListener
(
list
)
...
...
p2p/protocol/identify/id.go
View file @
7081c172
...
...
@@ -5,7 +5,6 @@ import (
"strings"
"sync"
host
"github.com/libp2p/go-libp2p/p2p/host"
pb
"github.com/libp2p/go-libp2p/p2p/protocol/identify/pb"
semver
"github.com/coreos/go-semver/semver"
...
...
@@ -16,6 +15,7 @@ import (
pstore
"github.com/ipfs/go-libp2p-peerstore"
logging
"github.com/ipfs/go-log"
ma
"github.com/jbenet/go-multiaddr"
host
"github.com/libp2p/go-libp2p-host"
mstream
"github.com/libp2p/go-libp2p-metrics/stream"
inet
"github.com/libp2p/go-libp2p-net"
msmux
"github.com/whyrusleeping/go-multistream"
...
...
p2p/protocol/identify/id_test.go
View file @
7081c172
package
identify_test
import
(
"context"
"testing"
"time"
ic
"github.com/ipfs/go-libp2p-crypto"
peer
"github.com/ipfs/go-libp2p-peer"
host
"github.com/libp2p/go-libp2p/p2p/host"
identify
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
testutil
"github.com/libp2p/go-libp2p/p2p/test/util"
"context"
ma
"github.com/jbenet/go-multiaddr"
host
"github.com/libp2p/go-libp2p-host"
)
func
subtestIDService
(
t
*
testing
.
T
,
postDialWait
time
.
Duration
)
{
...
...
p2p/protocol/ping/ping.go
View file @
7081c172
...
...
@@ -10,8 +10,8 @@ import (
u
"github.com/ipfs/go-ipfs-util"
peer
"github.com/ipfs/go-libp2p-peer"
logging
"github.com/ipfs/go-log"
host
"github.com/libp2p/go-libp2p-host"
inet
"github.com/libp2p/go-libp2p-net"
host
"github.com/libp2p/go-libp2p/p2p/host"
)
var
log
=
logging
.
Logger
(
"ping"
)
...
...
p2p/protocol/relay/relay.go
View file @
7081c172
...
...
@@ -6,7 +6,7 @@ import (
"io"
"time"
host
"github.com/libp2p/go-libp2p
/p2p/
host"
host
"github.com/libp2p/go-libp2p
-
host"
peer
"github.com/ipfs/go-libp2p-peer"
logging
"github.com/ipfs/go-log"
...
...
Prev
1
2
Next
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