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

extract from 0.4.0

parent 5a0162c7
......@@ -19,11 +19,12 @@ import (
"crypto/sha512"
"hash"
proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
u "github.com/ipfs/go-ipfs/util"
logging "github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
pb "github.com/ipfs/go-libp2p/p2p/crypto/pb"
proto "github.com/gogo/protobuf/proto"
logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
u "util"
)
var log = logging.Logger("crypto")
......
......@@ -4,7 +4,7 @@ import (
. "github.com/ipfs/go-libp2p/p2p/crypto"
"bytes"
tu "github.com/ipfs/go-ipfs/util/testutil"
tu "util/testutil"
"testing"
)
......
......@@ -14,7 +14,7 @@ It has these top-level messages:
*/
package crypto_pb
import proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
import proto "github.com/golang/protobuf/proto"
import math "math"
// Reference imports to suppress errors if they are not otherwise used.
......
......@@ -8,7 +8,7 @@ import (
"crypto/x509"
"errors"
proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
proto "github.com/gogo/protobuf/proto"
pb "github.com/ipfs/go-libp2p/p2p/crypto/pb"
)
......
......@@ -13,7 +13,7 @@ import (
"crypto/sha512"
"hash"
bfish "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/crypto/blowfish"
bfish "golang.org/x/crypto/blowfish"
ci "github.com/ipfs/go-libp2p/p2p/crypto"
)
......
......@@ -6,8 +6,8 @@ import (
ci "github.com/ipfs/go-libp2p/p2p/crypto"
msgio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
msgio "github.com/jbenet/go-msgio"
context "golang.org/x/net/context"
peer "github.com/ipfs/go-libp2p/p2p/peer"
)
......
......@@ -14,7 +14,7 @@ It has these top-level messages:
*/
package spipe_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.
......
......@@ -9,13 +9,13 @@ import (
"sync"
"time"
msgio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
u "github.com/ipfs/go-ipfs/util"
logging "github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
msgio "github.com/jbenet/go-msgio"
context "golang.org/x/net/context"
ci "github.com/ipfs/go-libp2p/p2p/crypto"
pb "github.com/ipfs/go-libp2p/p2p/crypto/secio/pb"
peer "github.com/ipfs/go-libp2p/p2p/peer"
u "util"
logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
)
var log = logging.Logger("secio")
......
......@@ -9,10 +9,10 @@ import (
"crypto/hmac"
proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
msgio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
mpool "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio/mpool"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
proto "github.com/gogo/protobuf/proto"
msgio "github.com/jbenet/go-msgio"
mpool "github.com/jbenet/go-msgio/mpool"
context "golang.org/x/net/context"
)
const MaxMsgSize = 8 * 1024 * 1024
......
......@@ -9,13 +9,13 @@ import (
"sync"
"time"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/cryptix/mdns"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
"github.com/cryptix/mdns"
ma "github.com/jbenet/go-multiaddr"
manet "github.com/jbenet/go-multiaddr-net"
logging "github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
"github.com/ipfs/go-libp2p/p2p/host"
"github.com/ipfs/go-libp2p/p2p/peer"
logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
)
var log = logging.Logger("mdns")
......
......@@ -3,21 +3,23 @@ package basichost
import (
"io"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
goprocess "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
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"
metrics "github.com/ipfs/go-libp2p/util/metrics"
mstream "github.com/ipfs/go-libp2p/util/metrics/stream"
logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
metrics "github.com/ipfs/go-libp2p/p2p/metrics"
mstream "github.com/ipfs/go-libp2p/p2p/metrics/stream"
ma "github.com/jbenet/go-multiaddr"
goprocess "github.com/jbenet/goprocess"
context "golang.org/x/net/context"
inet "github.com/ipfs/go-libp2p/p2p/net"
peer "github.com/ipfs/go-libp2p/p2p/peer"
protocol "github.com/ipfs/go-libp2p/p2p/protocol"
identify "github.com/ipfs/go-libp2p/p2p/protocol/identify"
relay "github.com/ipfs/go-libp2p/p2p/protocol/relay"
msmux "github.com/whyrusleeping/go-multistream"
)
var log = logging.Logger("p2p/host/basic")
var log = logging.Logger("github.com/ipfs/go-libp2p/p2p/host/basic")
// Option is a type used to pass in options to the host.
type Option int
......@@ -39,7 +41,7 @@ const (
// * uses a nat service to establish NAT port mappings
type BasicHost struct {
network inet.Network
mux *protocol.Mux
mux *msmux.MultistreamMuxer
ids *identify.IDService
relay *relay.RelayService
natmgr *natManager
......@@ -53,7 +55,7 @@ type BasicHost struct {
func New(net inet.Network, opts ...interface{}) *BasicHost {
h := &BasicHost{
network: net,
mux: protocol.NewMux(),
mux: msmux.NewMultistreamMuxer(),
bwc: metrics.NewBandwidthCounter(),
}
......@@ -67,7 +69,12 @@ func New(net inet.Network, opts ...interface{}) *BasicHost {
// setup host services
h.ids = identify.NewIDService(h)
h.relay = relay.NewRelayService(h, h.Mux().HandleSync)
muxh := h.Mux().Handle
handle := func(s inet.Stream) {
muxh(s)
}
h.relay = relay.NewRelayService(h, handle)
for _, o := range opts {
switch o := o.(type) {
......@@ -95,7 +102,7 @@ func (h *BasicHost) newConnHandler(c inet.Conn) {
// newStreamHandler is the remote-opened stream handler for inet.Network
// TODO: this feels a bit wonky
func (h *BasicHost) newStreamHandler(s inet.Stream) {
protoID, handle, err := h.Mux().ReadHeader(s)
protoID, handle, err := h.Mux().Negotiate(s)
if err != nil {
if err == io.EOF {
log.Debugf("protocol EOF: %s", s.Conn().RemotePeer())
......@@ -105,7 +112,7 @@ func (h *BasicHost) newStreamHandler(s inet.Stream) {
return
}
logStream := mstream.WrapStream(s, protoID, h.bwc)
logStream := mstream.WrapStream(s, protocol.ID(protoID), h.bwc)
go handle(logStream)
}
......@@ -126,7 +133,7 @@ func (h *BasicHost) Network() inet.Network {
}
// Mux returns the Mux multiplexing incoming streams to protocol handlers
func (h *BasicHost) Mux() *protocol.Mux {
func (h *BasicHost) Mux() *msmux.MultistreamMuxer {
return h.mux
}
......@@ -140,12 +147,15 @@ func (h *BasicHost) IDService() *identify.IDService {
// host.Mux().SetHandler(proto, handler)
// (Threadsafe)
func (h *BasicHost) SetStreamHandler(pid protocol.ID, handler inet.StreamHandler) {
h.Mux().SetHandler(pid, handler)
h.Mux().AddHandler(string(pid), func(rwc io.ReadWriteCloser) error {
handler(rwc.(inet.Stream))
return nil
})
}
// RemoveStreamHandler returns ..
func (h *BasicHost) RemoveStreamHandler(pid protocol.ID) {
h.Mux().RemoveHandler(pid)
h.Mux().RemoveHandler(string(pid))
}
// NewStream opens a new stream to given peer p, and writes a p2p/protocol
......@@ -160,12 +170,11 @@ func (h *BasicHost) NewStream(pid protocol.ID, p peer.ID) (inet.Stream, error) {
logStream := mstream.WrapStream(s, pid, h.bwc)
if err := protocol.WriteHeader(logStream, pid); err != nil {
logStream.Close()
return nil, err
}
return logStream, nil
lzcon := msmux.NewMSSelect(logStream, string(pid))
return &streamWrapper{
Stream: logStream,
rw: lzcon,
}, nil
}
// Connect ensures there is a connection between this host and the peer with
......@@ -244,3 +253,16 @@ func (h *BasicHost) Close() error {
func (h *BasicHost) GetBandwidthReporter() metrics.Reporter {
return h.bwc
}
type streamWrapper struct {
inet.Stream
rw io.ReadWriter
}
func (s *streamWrapper) Read(b []byte) (int, error) {
return s.rw.Read(b)
}
func (s *streamWrapper) Write(b []byte) (int, error) {
return s.rw.Write(b)
}
......@@ -9,7 +9,7 @@ import (
protocol "github.com/ipfs/go-libp2p/p2p/protocol"
testutil "github.com/ipfs/go-libp2p/p2p/test/util"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
context "golang.org/x/net/context"
)
func TestHostSimple(t *testing.T) {
......
......@@ -3,13 +3,13 @@ package basichost
import (
"sync"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
goprocess "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
ma "github.com/jbenet/go-multiaddr"
goprocess "github.com/jbenet/goprocess"
context "golang.org/x/net/context"
inat "github.com/ipfs/go-libp2p/p2p/nat"
inet "github.com/ipfs/go-libp2p/p2p/net"
lgbl "github.com/ipfs/go-libp2p/util/eventlog/loggables"
lgbl "util/eventlog/loggables"
)
// natManager takes care of adding + removing port mappings to the nat.
......
package host
import (
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"
logging "github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
metrics "github.com/ipfs/go-libp2p/p2p/metrics"
inet "github.com/ipfs/go-libp2p/p2p/net"
peer "github.com/ipfs/go-libp2p/p2p/peer"
protocol "github.com/ipfs/go-libp2p/p2p/protocol"
metrics "github.com/ipfs/go-libp2p/util/metrics"
ma "github.com/jbenet/go-multiaddr"
context "golang.org/x/net/context"
msmux "github.com/whyrusleeping/go-multistream"
)
var log = logging.Logger("p2p/host")
var log = logging.Logger("github.com/ipfs/go-libp2p/p2p/host")
// Host is an object participating in a p2p network, which
// implements protocols or provides services. It handles
......@@ -31,7 +33,7 @@ type Host interface {
Network() inet.Network
// Mux returns the Mux multiplexing incoming streams to protocol handlers
Mux() *protocol.Mux
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
......
......@@ -4,20 +4,21 @@ import (
"fmt"
"time"
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"
logging "github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
lgbl "github.com/ipfs/go-libp2p/util/eventlog/loggables"
logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
ma "github.com/jbenet/go-multiaddr"
context "golang.org/x/net/context"
lgbl "util/eventlog/loggables"
routing "github.com/ipfs/go-ipfs/routing"
host "github.com/ipfs/go-libp2p/p2p/host"
metrics "github.com/ipfs/go-libp2p/p2p/metrics"
inet "github.com/ipfs/go-libp2p/p2p/net"
peer "github.com/ipfs/go-libp2p/p2p/peer"
protocol "github.com/ipfs/go-libp2p/p2p/protocol"
metrics "github.com/ipfs/go-libp2p/util/metrics"
msmux "github.com/whyrusleeping/go-multistream"
)
var log = logging.Logger("p2p/host/routed")
var log = logging.Logger("github.com/ipfs/go-libp2p/p2p/host/routed")
// AddressTTL is the expiry time for our addresses.
// We expire them quickly.
......@@ -28,10 +29,14 @@ const AddressTTL = time.Second * 10
// it does not have them.
type RoutedHost struct {
host host.Host // embedded other host.
route routing.IpfsRouting
route Routing
}
type Routing interface {
FindPeer(context.Context, peer.ID) (peer.PeerInfo, error)
}
func Wrap(h host.Host, r routing.IpfsRouting) *RoutedHost {
func Wrap(h host.Host, r Routing) *RoutedHost {
return &RoutedHost{h, r}
}
......@@ -97,7 +102,7 @@ func (rh *RoutedHost) Network() inet.Network {
return rh.host.Network()
}
func (rh *RoutedHost) Mux() *protocol.Mux {
func (rh *RoutedHost) Mux() *msmux.MultistreamMuxer {
return rh.host.Mux()
}
......
package metrics
import (
gm "github.com/whyrusleeping/go-metrics"
"sync"
gm "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/go-metrics"
peer "github.com/ipfs/go-libp2p/p2p/peer"
protocol "github.com/ipfs/go-libp2p/p2p/protocol"
)
......
package meterconn
import (
manet "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
metrics "github.com/ipfs/go-libp2p/util/metrics"
metrics "github.com/ipfs/go-libp2p/p2p/metrics"
transport "github.com/ipfs/go-libp2p/p2p/net/transport"
)
type MeteredConn struct {
mesRecv metrics.MeterCallback
mesSent metrics.MeterCallback
manet.Conn
transport.Conn
}
func WrapConn(bwc metrics.Reporter, c manet.Conn) manet.Conn {
func WrapConn(bwc metrics.Reporter, c transport.Conn) transport.Conn {
return newMeteredConn(c, bwc.LogRecvMessage, bwc.LogSentMessage)
}
func newMeteredConn(base manet.Conn, rcb metrics.MeterCallback, scb metrics.MeterCallback) manet.Conn {
func newMeteredConn(base transport.Conn, rcb metrics.MeterCallback, scb metrics.MeterCallback) transport.Conn {
return &MeteredConn{
Conn: base,
mesRecv: rcb,
......
package meterstream
import (
metrics "github.com/ipfs/go-libp2p/p2p/metrics"
inet "github.com/ipfs/go-libp2p/p2p/net"
peer "github.com/ipfs/go-libp2p/p2p/peer"
protocol "github.com/ipfs/go-libp2p/p2p/protocol"
metrics "github.com/ipfs/go-libp2p/util/metrics"
)
type meteredStream struct {
......
......@@ -5,7 +5,7 @@ import (
"io/ioutil"
"testing"
u "github.com/ipfs/go-ipfs/util"
randbo "github.com/dustin/randbo"
inet "github.com/ipfs/go-libp2p/p2p/net"
peer "github.com/ipfs/go-libp2p/p2p/peer"
protocol "github.com/ipfs/go-libp2p/p2p/protocol"
......@@ -43,8 +43,8 @@ func TestCallbacksWork(t *testing.T) {
toWrite := int64(100000)
toRead := int64(100000)
fake.ReadBuf = io.LimitReader(u.NewTimeSeededRand(), toRead)
writeData := io.LimitReader(u.NewTimeSeededRand(), toWrite)
fake.ReadBuf = io.LimitReader(randbo.New(), toRead)
writeData := io.LimitReader(randbo.New(), toWrite)
n, err := io.Copy(ms, writeData)
if err != nil {
......
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