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
51fd99e3
Commit
51fd99e3
authored
Nov 11, 2015
by
Jeromy
Browse files
extract from 0.4.0
parent
5a0162c7
Changes
125
Show whitespace changes
Inline
Side-by-side
p2p/peer/addr/addrsrcs.go
View file @
51fd99e3
...
...
@@ -2,7 +2,7 @@
package
addr
import
(
ma
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-multiaddr"
ma
"github.com/jbenet/go-multiaddr"
)
// AddrSource is a source of addresses. It allows clients to retrieve
...
...
p2p/peer/addr/addrsrcs_test.go
View file @
51fd99e3
...
...
@@ -4,7 +4,7 @@ import (
"fmt"
"testing"
ma
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-multiaddr"
ma
"github.com/jbenet/go-multiaddr"
)
func
newAddrOrFatal
(
t
*
testing
.
T
,
s
string
)
ma
.
Multiaddr
{
...
...
p2p/peer/addr_manager.go
View file @
51fd99e3
...
...
@@ -4,7 +4,7 @@ import (
"sync"
"time"
ma
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-multiaddr"
ma
"github.com/jbenet/go-multiaddr"
)
const
(
...
...
p2p/peer/addr_manager_test.go
View file @
51fd99e3
...
...
@@ -4,7 +4,7 @@ import (
"testing"
"time"
ma
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-multiaddr"
ma
"github.com/jbenet/go-multiaddr"
)
func
IDS
(
t
*
testing
.
T
,
ids
string
)
ID
{
...
...
p2p/peer/metrics_test.go
View file @
51fd99e3
...
...
@@ -6,8 +6,8 @@ import (
"testing"
"time"
testutil
"github.com/ipfs/go-ipfs/util/testutil"
peer
"github.com/ipfs/go-libp2p/p2p/peer"
testutil
"util/testutil"
)
func
TestLatencyEWMAFun
(
t
*
testing
.
T
)
{
...
...
p2p/peer/peer.go
View file @
51fd99e3
...
...
@@ -7,13 +7,13 @@ import (
"fmt"
"strings"
b58
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-base58"
ma
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-multiaddr"
mh
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-multihash"
b58
"github.com/jbenet/go-base58"
ma
"github.com/jbenet/go-multiaddr"
mh
"github.com/jbenet/go-multihash"
u
"github.com/ipfs/go-ipfs/util"
logging
"github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
logging
"QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
ic
"github.com/ipfs/go-libp2p/p2p/crypto"
u
"util"
)
var
log
=
logging
.
Logger
(
"peer"
)
...
...
p2p/peer/peer_test.go
View file @
51fd99e3
...
...
@@ -6,12 +6,12 @@ import (
"strings"
"testing"
u
"github.com/ipfs/go-ipfs/util"
tu
"github.com/ipfs/go-ipfs/util/testutil"
ic
"github.com/ipfs/go-libp2p/p2p/crypto"
.
"github.com/ipfs/go-libp2p/p2p/peer"
u
"util"
tu
"util/testutil"
b58
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-base58"
b58
"github.com/jbenet/go-base58"
)
var
gen1
keyset
// generated
...
...
p2p/peer/peerstore.go
View file @
51fd99e3
...
...
@@ -7,9 +7,9 @@ import (
ic
"github.com/ipfs/go-libp2p/p2p/crypto"
ds
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-datastore"
dssync
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-datastore/sync"
ma
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-multiaddr"
ds
"github.com/jbenet/go-datastore"
dssync
"github.com/jbenet/go-datastore/sync"
ma
"github.com/jbenet/go-multiaddr"
)
const
(
...
...
p2p/peer/queue/distance.go
View file @
51fd99e3
...
...
@@ -5,9 +5,9 @@ import (
"math/big"
"sync"
key
"github.com/ipfs/go-ipfs/blocks/key"
ks
"github.com/ipfs/go-ipfs/routing/keyspace"
ks
"Qma4vHVBYKDiKS5VpvtLNJHHDbL7S6VRsvxxmBnBFfKP3k/go-keyspace"
peer
"github.com/ipfs/go-libp2p/p2p/peer"
key
"github.com/whyrusleeping/go-key"
)
// peerMetric tracks a peer and its distance to something else.
...
...
p2p/peer/queue/queue_test.go
View file @
51fd99e3
...
...
@@ -6,11 +6,11 @@ import (
"testing"
"time"
key
"github.com/ipfs/go-ipfs/blocks/key"
u
"github.com/ipfs/go-ipfs/util"
peer
"github.com/ipfs/go-libp2p/p2p/peer"
key
"github.com/whyrusleeping/go-key"
u
"util"
context
"
github.com/ipfs/go-ipfs/Godeps/_workspace/src/
golang.org/x/net/context"
context
"golang.org/x/net/context"
)
func
TestQueue
(
t
*
testing
.
T
)
{
...
...
p2p/peer/queue/sync.go
View file @
51fd99e3
package
queue
import
(
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
logging
"github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
context
"golang.org/x/net/context"
peer
"github.com/ipfs/go-libp2p/p2p/peer"
logging
"QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
)
var
log
=
logging
.
Logger
(
"peerqueue"
)
...
...
p2p/protocol/identify/id.go
View file @
51fd99e3
...
...
@@ -4,31 +4,31 @@ import (
"strings"
"sync"
semver
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
ggio
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/io"
ma
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
semver
"github.com/coreos/go-semver/semver"
ggio
"github.com/gogo/protobuf/io"
ma
"github.com/jbenet/go-multiaddr"
msmux
"github.com/whyrusleeping/go-multistream"
context
"golang.org/x/net/context"
config
"github.com/ipfs/go-ipfs/repo/config"
logging
"github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
host
"github.com/ipfs/go-libp2p/p2p/host"
mstream
"github.com/ipfs/go-libp2p/p2p/metrics/stream"
inet
"github.com/ipfs/go-libp2p/p2p/net"
peer
"github.com/ipfs/go-libp2p/p2p/peer"
protocol
"github.com/ipfs/go-libp2p/p2p/protocol"
pb
"github.com/ipfs/go-libp2p/p2p/protocol/identify/pb"
lgbl
"github.com/ipfs/go-libp2p/util/eventlog/loggables"
mstream
"github.com/ipfs/go-libp2p/util/metrics/stream"
logging
"QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
lgbl
"util/eventlog/loggables"
)
var
log
=
logging
.
Logger
(
"net/identify"
)
// ID is the protocol.ID of the Identify Service.
const
ID
protocol
.
ID
=
"/ipfs/identify"
const
ID
=
"/ipfs/identify"
//
Ipfs
Version holds the current protocol version for a client running this code
//
LibP2P
Version holds the current protocol version for a client running this code
// TODO(jbenet): fix the versioning mess.
const
Ipfs
Version
=
"ipfs/0.1.0"
const
ClientVersion
=
"go-
ipfs/"
+
config
.
CurrentVersionNumber
const
LibP2P
Version
=
"ipfs/0.1.0"
const
ClientVersion
=
"go-
libp2p/0.1.0"
// IDService is a structure that implements ProtocolIdentify.
// It is a trivial service that gives the other peer some
...
...
@@ -87,15 +87,15 @@ func (ids *IDService) IdentifyConn(c inet.Conn) {
s
=
mstream
.
WrapStream
(
s
,
ID
,
bwc
)
// ok give the response to our handler.
if
err
:=
protocol
.
WriteHeader
(
s
,
ID
);
err
!=
nil
{
if
err
:=
msmux
.
SelectProtoOrFail
(
ID
,
s
);
err
!=
nil
{
log
.
Debugf
(
"error writing stream header for %s"
,
ID
)
log
.
Event
(
context
.
TODO
(),
"IdentifyOpenFailed"
,
c
.
RemotePeer
())
s
.
Close
()
c
.
Close
()
return
}
}
else
{
ids
.
ResponseHandler
(
s
)
}
}
ids
.
currmu
.
Lock
()
ch
,
found
:=
ids
.
currid
[
c
]
...
...
@@ -163,7 +163,7 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c inet.Conn) {
log
.
Debugf
(
"%s sent listen addrs to %s: %s"
,
c
.
LocalPeer
(),
c
.
RemotePeer
(),
laddrs
)
// set protocol versions
pv
:=
Ipfs
Version
pv
:=
LibP2P
Version
av
:=
ClientVersion
mes
.
ProtocolVersion
=
&
pv
mes
.
AgentVersion
=
&
av
...
...
@@ -202,7 +202,7 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c inet.Conn) {
// version check. if we shouldn't talk, bail.
// TODO: at this point, we've already exchanged information.
// move this into a first handshake before the connection can open streams.
if
!
protocolVersionsAreCompatible
(
pv
,
Ipfs
Version
)
{
if
!
protocolVersionsAreCompatible
(
pv
,
LibP2P
Version
)
{
logProtocolMismatchDisconnect
(
c
,
pv
,
av
)
c
.
Close
()
return
...
...
p2p/protocol/identify/id_test.go
View file @
51fd99e3
...
...
@@ -9,8 +9,8 @@ import (
identify
"github.com/ipfs/go-libp2p/p2p/protocol/identify"
testutil
"github.com/ipfs/go-libp2p/p2p/test/util"
ma
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-multiaddr"
context
"
github.com/ipfs/go-ipfs/Godeps/_workspace/src/
golang.org/x/net/context"
ma
"github.com/jbenet/go-multiaddr"
context
"golang.org/x/net/context"
)
func
subtestIDService
(
t
*
testing
.
T
,
postDialWait
time
.
Duration
)
{
...
...
@@ -79,7 +79,7 @@ func testHasProtocolVersions(t *testing.T, h host.Host, p peer.ID) {
t
.
Error
(
"no protocol version"
)
return
}
if
v
.
(
string
)
!=
identify
.
Ipfs
Version
{
if
v
.
(
string
)
!=
identify
.
LibP2P
Version
{
t
.
Error
(
"protocol mismatch"
,
err
)
}
v
,
err
=
h
.
Peerstore
()
.
Get
(
p
,
"AgentVersion"
)
...
...
p2p/protocol/identify/obsaddr.go
View file @
51fd99e3
...
...
@@ -6,7 +6,7 @@ import (
peer
"github.com/ipfs/go-libp2p/p2p/peer"
ma
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-multiaddr"
ma
"github.com/jbenet/go-multiaddr"
)
// ObservedAddr is an entry for an address reported by our peers.
...
...
p2p/protocol/identify/obsaddr_test.go
View file @
51fd99e3
...
...
@@ -4,7 +4,7 @@ import (
"testing"
"time"
ma
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
jbenet/go-multiaddr"
ma
"github.com/jbenet/go-multiaddr"
)
// TestObsAddrSet
...
...
p2p/protocol/identify/pb/identify.pb.go
View file @
51fd99e3
...
...
@@ -13,7 +13,7 @@ It has these top-level messages:
*/
package
identify_pb
import
proto
"github.com/
ipfs/go-ipfs/Godeps/_workspace/src/github.com/
gogo/protobuf/proto"
import
proto
"github.com/gogo/protobuf/proto"
import
math
"math"
// Reference imports to suppress errors if they are not otherwise used.
...
...
p2p/protocol/mux.go
deleted
100644 → 0
View file @
5a0162c7
package
protocol
import
(
"fmt"
"io"
"sync"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
logging
"github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
inet
"github.com/ipfs/go-libp2p/p2p/net"
lgbl
"github.com/ipfs/go-libp2p/util/eventlog/loggables"
)
var
log
=
logging
.
Logger
(
"net/mux"
)
type
streamHandlerMap
map
[
ID
]
inet
.
StreamHandler
// Mux provides simple stream multixplexing.
// It helps you precisely when:
// * You have many streams
// * You have function handlers
//
// It contains the handlers for each protocol accepted.
// It dispatches handlers for streams opened by remote peers.
type
Mux
struct
{
lock
sync
.
RWMutex
handlers
streamHandlerMap
defaultHandler
inet
.
StreamHandler
}
func
NewMux
()
*
Mux
{
return
&
Mux
{
handlers
:
streamHandlerMap
{},
}
}
// Protocols returns the list of protocols this muxer has handlers for
func
(
m
*
Mux
)
Protocols
()
[]
ID
{
m
.
lock
.
RLock
()
l
:=
make
([]
ID
,
0
,
len
(
m
.
handlers
))
for
p
:=
range
m
.
handlers
{
l
=
append
(
l
,
p
)
}
m
.
lock
.
RUnlock
()
return
l
}
// ReadHeader reads the stream and returns the next Handler function
// according to the muxer encoding.
func
(
m
*
Mux
)
ReadHeader
(
s
io
.
Reader
)
(
ID
,
inet
.
StreamHandler
,
error
)
{
p
,
err
:=
ReadHeader
(
s
)
if
err
!=
nil
{
return
""
,
nil
,
err
}
m
.
lock
.
RLock
()
defer
m
.
lock
.
RUnlock
()
h
,
found
:=
m
.
handlers
[
p
]
switch
{
case
!
found
&&
m
.
defaultHandler
!=
nil
:
return
p
,
m
.
defaultHandler
,
nil
case
!
found
&&
m
.
defaultHandler
==
nil
:
return
p
,
nil
,
fmt
.
Errorf
(
"%s no handler with name: %s (%d)"
,
m
,
p
,
len
(
p
))
default
:
return
p
,
h
,
nil
}
}
// String returns the muxer's printing representation
func
(
m
*
Mux
)
String
()
string
{
m
.
lock
.
RLock
()
defer
m
.
lock
.
RUnlock
()
return
fmt
.
Sprintf
(
"<Muxer %p %d>"
,
m
,
len
(
m
.
handlers
))
}
func
(
m
*
Mux
)
SetDefaultHandler
(
h
inet
.
StreamHandler
)
{
m
.
lock
.
Lock
()
m
.
defaultHandler
=
h
m
.
lock
.
Unlock
()
}
// SetHandler sets the protocol handler on the Network's Muxer.
// This operation is threadsafe.
func
(
m
*
Mux
)
SetHandler
(
p
ID
,
h
inet
.
StreamHandler
)
{
log
.
Debugf
(
"%s setting handler for protocol: %s (%d)"
,
m
,
p
,
len
(
p
))
m
.
lock
.
Lock
()
m
.
handlers
[
p
]
=
h
m
.
lock
.
Unlock
()
}
// RemoveHandler removes the protocol handler on the Network's Muxer.
// This operation is threadsafe.
func
(
m
*
Mux
)
RemoveHandler
(
p
ID
)
{
log
.
Debugf
(
"%s removing handler for protocol: %s (%d)"
,
m
,
p
,
len
(
p
))
m
.
lock
.
Lock
()
delete
(
m
.
handlers
,
p
)
m
.
lock
.
Unlock
()
}
// Handle reads the next name off the Stream, and calls a handler function
// This is done in its own goroutine, to avoid blocking the caller.
func
(
m
*
Mux
)
Handle
(
s
inet
.
Stream
)
{
go
m
.
HandleSync
(
s
)
}
// HandleSync reads the next name off the Stream, and calls a handler function
// This is done synchronously. The handler function will return before
// HandleSync returns.
func
(
m
*
Mux
)
HandleSync
(
s
inet
.
Stream
)
{
ctx
:=
context
.
Background
()
name
,
handler
,
err
:=
m
.
ReadHeader
(
s
)
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"protocol mux error: %s"
,
err
)
log
.
Event
(
ctx
,
"muxError"
,
lgbl
.
Error
(
err
))
s
.
Close
()
return
}
log
.
Debugf
(
"muxer handle protocol %s: %s"
,
s
.
Conn
()
.
RemotePeer
(),
name
)
handler
(
s
)
}
// ReadLengthPrefix reads the name from Reader with a length-byte-prefix.
func
ReadLengthPrefix
(
r
io
.
Reader
)
(
string
,
error
)
{
// c-string identifier
// the first byte is our length
l
:=
make
([]
byte
,
1
)
if
_
,
err
:=
io
.
ReadFull
(
r
,
l
);
err
!=
nil
{
return
""
,
err
}
length
:=
int
(
l
[
0
])
// the next are our identifier
name
:=
make
([]
byte
,
length
)
if
_
,
err
:=
io
.
ReadFull
(
r
,
name
);
err
!=
nil
{
return
""
,
err
}
return
string
(
name
),
nil
}
p2p/protocol/mux_test.go
deleted
100644 → 0
View file @
5a0162c7
package
protocol
import
(
"bytes"
"testing"
inet
"github.com/ipfs/go-libp2p/p2p/net"
)
var
testCases
=
map
[
string
]
string
{
"/bitswap"
:
"
\u0009
/bitswap
\n
"
,
"/dht"
:
"
\u0005
/dht
\n
"
,
"/ipfs"
:
"
\u0006
/ipfs
\n
"
,
"/ipfs/dksnafkasnfkdajfkdajfdsjadosiaaodj"
:
")/ipfs/dksnafkasnfkdajfkdajfdsjadosiaaodj
\n
"
,
}
func
TestWrite
(
t
*
testing
.
T
)
{
for
k
,
v
:=
range
testCases
{
buf
:=
new
(
bytes
.
Buffer
)
if
err
:=
WriteHeader
(
buf
,
ID
(
k
));
err
!=
nil
{
t
.
Fatal
(
err
)
}
v2
:=
buf
.
Bytes
()
if
!
bytes
.
Equal
(
v2
,
[]
byte
(
v
))
{
t
.
Errorf
(
"failed: %s - %v != %v"
,
k
,
[]
byte
(
v
),
v2
)
}
}
}
func
TestHandler
(
t
*
testing
.
T
)
{
outs
:=
make
(
chan
string
,
10
)
h
:=
func
(
n
string
)
func
(
s
inet
.
Stream
)
{
return
func
(
s
inet
.
Stream
)
{
outs
<-
n
}
}
m
:=
NewMux
()
m
.
SetDefaultHandler
(
h
(
"default"
))
m
.
SetHandler
(
"/dht"
,
h
(
"bitswap"
))
// m.Handlers["/ipfs"] = h("bitswap") // default!
m
.
SetHandler
(
"/bitswap"
,
h
(
"bitswap"
))
m
.
SetHandler
(
"/ipfs/dksnafkasnfkdajfkdajfdsjadosiaaodj"
,
h
(
"bitswap"
))
for
k
,
v
:=
range
testCases
{
buf
:=
new
(
bytes
.
Buffer
)
if
_
,
err
:=
buf
.
Write
([]
byte
(
v
));
err
!=
nil
{
t
.
Error
(
err
)
continue
}
name
,
err
:=
ReadHeader
(
buf
)
if
err
!=
nil
{
t
.
Error
(
err
)
continue
}
if
name
!=
ID
(
k
)
{
t
.
Errorf
(
"name mismatch: %s != %s"
,
k
,
name
)
continue
}
}
}
p2p/protocol/ping/ping.go
View file @
51fd99e3
...
...
@@ -6,13 +6,13 @@ import (
"io"
"time"
context
"
github.com/ipfs/go-ipfs/Godeps/_workspace/src/
golang.org/x/net/context"
context
"golang.org/x/net/context"
u
"github.com/ipfs/go-ipfs/util"
logging
"github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
host
"github.com/ipfs/go-libp2p/p2p/host"
inet
"github.com/ipfs/go-libp2p/p2p/net"
peer
"github.com/ipfs/go-libp2p/p2p/peer"
u
"util"
logging
"QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
)
var
log
=
logging
.
Logger
(
"ping"
)
...
...
p2p/protocol/ping/ping_test.go
View file @
51fd99e3
...
...
@@ -4,7 +4,7 @@ import (
"testing"
"time"
context
"
github.com/ipfs/go-ipfs/Godeps/_workspace/src/
golang.org/x/net/context"
context
"golang.org/x/net/context"
peer
"github.com/ipfs/go-libp2p/p2p/peer"
netutil
"github.com/ipfs/go-libp2p/p2p/test/util"
)
...
...
Prev
1
2
3
4
5
6
7
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