Commit 51fd99e3 authored by Jeromy's avatar Jeromy
Browse files

extract from 0.4.0

parent 5a0162c7
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
package addr package addr
import ( 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 // AddrSource is a source of addresses. It allows clients to retrieve
......
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"testing" "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 { func newAddrOrFatal(t *testing.T, s string) ma.Multiaddr {
......
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"sync" "sync"
"time" "time"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ma "github.com/jbenet/go-multiaddr"
) )
const ( const (
......
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"testing" "testing"
"time" "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 { func IDS(t *testing.T, ids string) ID {
......
...@@ -6,8 +6,8 @@ import ( ...@@ -6,8 +6,8 @@ import (
"testing" "testing"
"time" "time"
testutil "github.com/ipfs/go-ipfs/util/testutil"
peer "github.com/ipfs/go-libp2p/p2p/peer" peer "github.com/ipfs/go-libp2p/p2p/peer"
testutil "util/testutil"
) )
func TestLatencyEWMAFun(t *testing.T) { func TestLatencyEWMAFun(t *testing.T) {
......
...@@ -7,13 +7,13 @@ import ( ...@@ -7,13 +7,13 @@ import (
"fmt" "fmt"
"strings" "strings"
b58 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58" b58 "github.com/jbenet/go-base58"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ma "github.com/jbenet/go-multiaddr"
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" mh "github.com/jbenet/go-multihash"
u "github.com/ipfs/go-ipfs/util" logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
logging "github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
ic "github.com/ipfs/go-libp2p/p2p/crypto" ic "github.com/ipfs/go-libp2p/p2p/crypto"
u "util"
) )
var log = logging.Logger("peer") var log = logging.Logger("peer")
......
...@@ -6,12 +6,12 @@ import ( ...@@ -6,12 +6,12 @@ import (
"strings" "strings"
"testing" "testing"
u "github.com/ipfs/go-ipfs/util"
tu "github.com/ipfs/go-ipfs/util/testutil"
ic "github.com/ipfs/go-libp2p/p2p/crypto" ic "github.com/ipfs/go-libp2p/p2p/crypto"
. "github.com/ipfs/go-libp2p/p2p/peer" . "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 var gen1 keyset // generated
......
...@@ -7,9 +7,9 @@ import ( ...@@ -7,9 +7,9 @@ import (
ic "github.com/ipfs/go-libp2p/p2p/crypto" ic "github.com/ipfs/go-libp2p/p2p/crypto"
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore" ds "github.com/jbenet/go-datastore"
dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync" dssync "github.com/jbenet/go-datastore/sync"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ma "github.com/jbenet/go-multiaddr"
) )
const ( const (
......
...@@ -5,9 +5,9 @@ import ( ...@@ -5,9 +5,9 @@ import (
"math/big" "math/big"
"sync" "sync"
key "github.com/ipfs/go-ipfs/blocks/key" ks "Qma4vHVBYKDiKS5VpvtLNJHHDbL7S6VRsvxxmBnBFfKP3k/go-keyspace"
ks "github.com/ipfs/go-ipfs/routing/keyspace"
peer "github.com/ipfs/go-libp2p/p2p/peer" peer "github.com/ipfs/go-libp2p/p2p/peer"
key "github.com/whyrusleeping/go-key"
) )
// peerMetric tracks a peer and its distance to something else. // peerMetric tracks a peer and its distance to something else.
......
...@@ -6,11 +6,11 @@ import ( ...@@ -6,11 +6,11 @@ import (
"testing" "testing"
"time" "time"
key "github.com/ipfs/go-ipfs/blocks/key"
u "github.com/ipfs/go-ipfs/util"
peer "github.com/ipfs/go-libp2p/p2p/peer" 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) { func TestQueue(t *testing.T) {
......
package queue package queue
import ( import (
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "golang.org/x/net/context"
logging "github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
peer "github.com/ipfs/go-libp2p/p2p/peer" peer "github.com/ipfs/go-libp2p/p2p/peer"
logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
) )
var log = logging.Logger("peerqueue") var log = logging.Logger("peerqueue")
......
...@@ -4,31 +4,31 @@ import ( ...@@ -4,31 +4,31 @@ import (
"strings" "strings"
"sync" "sync"
semver "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver" semver "github.com/coreos/go-semver/semver"
ggio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/io" ggio "github.com/gogo/protobuf/io"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ma "github.com/jbenet/go-multiaddr"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" 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" 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" inet "github.com/ipfs/go-libp2p/p2p/net"
peer "github.com/ipfs/go-libp2p/p2p/peer" 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" 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") var log = logging.Logger("net/identify")
// ID is the protocol.ID of the Identify Service. // ID is the protocol.ID of the Identify Service.
const ID protocol.ID = "/ipfs/identify" const ID = "/ipfs/identify"
// IpfsVersion holds the current protocol version for a client running this code // LibP2PVersion holds the current protocol version for a client running this code
// TODO(jbenet): fix the versioning mess. // TODO(jbenet): fix the versioning mess.
const IpfsVersion = "ipfs/0.1.0" const LibP2PVersion = "ipfs/0.1.0"
const ClientVersion = "go-ipfs/" + config.CurrentVersionNumber const ClientVersion = "go-libp2p/0.1.0"
// IDService is a structure that implements ProtocolIdentify. // IDService is a structure that implements ProtocolIdentify.
// It is a trivial service that gives the other peer some // It is a trivial service that gives the other peer some
...@@ -87,14 +87,14 @@ func (ids *IDService) IdentifyConn(c inet.Conn) { ...@@ -87,14 +87,14 @@ func (ids *IDService) IdentifyConn(c inet.Conn) {
s = mstream.WrapStream(s, ID, bwc) s = mstream.WrapStream(s, ID, bwc)
// ok give the response to our handler. // 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.Debugf("error writing stream header for %s", ID)
log.Event(context.TODO(), "IdentifyOpenFailed", c.RemotePeer()) log.Event(context.TODO(), "IdentifyOpenFailed", c.RemotePeer())
s.Close() s.Close()
c.Close()
return return
} else {
ids.ResponseHandler(s)
} }
ids.ResponseHandler(s)
} }
ids.currmu.Lock() ids.currmu.Lock()
...@@ -163,7 +163,7 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c inet.Conn) { ...@@ -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) log.Debugf("%s sent listen addrs to %s: %s", c.LocalPeer(), c.RemotePeer(), laddrs)
// set protocol versions // set protocol versions
pv := IpfsVersion pv := LibP2PVersion
av := ClientVersion av := ClientVersion
mes.ProtocolVersion = &pv mes.ProtocolVersion = &pv
mes.AgentVersion = &av mes.AgentVersion = &av
...@@ -202,7 +202,7 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c inet.Conn) { ...@@ -202,7 +202,7 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c inet.Conn) {
// version check. if we shouldn't talk, bail. // version check. if we shouldn't talk, bail.
// TODO: at this point, we've already exchanged information. // TODO: at this point, we've already exchanged information.
// move this into a first handshake before the connection can open streams. // move this into a first handshake before the connection can open streams.
if !protocolVersionsAreCompatible(pv, IpfsVersion) { if !protocolVersionsAreCompatible(pv, LibP2PVersion) {
logProtocolMismatchDisconnect(c, pv, av) logProtocolMismatchDisconnect(c, pv, av)
c.Close() c.Close()
return return
......
...@@ -9,8 +9,8 @@ import ( ...@@ -9,8 +9,8 @@ import (
identify "github.com/ipfs/go-libp2p/p2p/protocol/identify" identify "github.com/ipfs/go-libp2p/p2p/protocol/identify"
testutil "github.com/ipfs/go-libp2p/p2p/test/util" testutil "github.com/ipfs/go-libp2p/p2p/test/util"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ma "github.com/jbenet/go-multiaddr"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "golang.org/x/net/context"
) )
func subtestIDService(t *testing.T, postDialWait time.Duration) { func subtestIDService(t *testing.T, postDialWait time.Duration) {
...@@ -79,7 +79,7 @@ func testHasProtocolVersions(t *testing.T, h host.Host, p peer.ID) { ...@@ -79,7 +79,7 @@ func testHasProtocolVersions(t *testing.T, h host.Host, p peer.ID) {
t.Error("no protocol version") t.Error("no protocol version")
return return
} }
if v.(string) != identify.IpfsVersion { if v.(string) != identify.LibP2PVersion {
t.Error("protocol mismatch", err) t.Error("protocol mismatch", err)
} }
v, err = h.Peerstore().Get(p, "AgentVersion") v, err = h.Peerstore().Get(p, "AgentVersion")
......
...@@ -6,7 +6,7 @@ import ( ...@@ -6,7 +6,7 @@ import (
peer "github.com/ipfs/go-libp2p/p2p/peer" 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. // ObservedAddr is an entry for an address reported by our peers.
......
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"testing" "testing"
"time" "time"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ma "github.com/jbenet/go-multiaddr"
) )
// TestObsAddrSet // TestObsAddrSet
......
...@@ -13,7 +13,7 @@ It has these top-level messages: ...@@ -13,7 +13,7 @@ It has these top-level messages:
*/ */
package identify_pb 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" import math "math"
// Reference imports to suppress errors if they are not otherwise used. // Reference imports to suppress errors if they are not otherwise used.
......
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
}
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
}
}
}
...@@ -6,13 +6,13 @@ import ( ...@@ -6,13 +6,13 @@ import (
"io" "io"
"time" "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" host "github.com/ipfs/go-libp2p/p2p/host"
inet "github.com/ipfs/go-libp2p/p2p/net" inet "github.com/ipfs/go-libp2p/p2p/net"
peer "github.com/ipfs/go-libp2p/p2p/peer" peer "github.com/ipfs/go-libp2p/p2p/peer"
u "util"
logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
) )
var log = logging.Logger("ping") var log = logging.Logger("ping")
......
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"testing" "testing"
"time" "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" peer "github.com/ipfs/go-libp2p/p2p/peer"
netutil "github.com/ipfs/go-libp2p/p2p/test/util" netutil "github.com/ipfs/go-libp2p/p2p/test/util"
) )
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment