Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
reorg code; see below.
Browse files Browse the repository at this point in the history
* flatten out slim packages in routing.
* simplify naming in discovery.
* break up big monolithic files into constituents (network, pnet).
* renames for consistency.
  • Loading branch information
raulk committed Apr 17, 2019
1 parent 4e1f238 commit 47396eb
Show file tree
Hide file tree
Showing 24 changed files with 238 additions and 225 deletions.
42 changes: 2 additions & 40 deletions discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,17 @@ import (
// Advertiser is an interface for advertising services
type Advertiser interface {
// Advertise advertises a service
Advertise(ctx context.Context, ns string, opts ...DiscoveryOpt) (time.Duration, error)
Advertise(ctx context.Context, ns string, opts ...Option) (time.Duration, error)
}

// Discoverer is an interface for peer discovery
type Discoverer interface {
// FindPeers discovers peers providing a service
FindPeers(ctx context.Context, ns string, opts ...DiscoveryOpt) (<-chan peer.Info, error)
FindPeers(ctx context.Context, ns string, opts ...Option) (<-chan peer.Info, error)
}

// Discovery is an interface that combines service advertisement and peer discovery
type Discovery interface {
Advertiser
Discoverer
}

// DiscoveryOpt is a single discovery option.
type DiscoveryOpt func(opts *DiscoveryOpts) error

// DiscoveryOpts is a set of discovery options.
type DiscoveryOpts struct {
Ttl time.Duration
Limit int

// Other (implementation-specific) options
Other map[interface{}]interface{}
}

// Apply applies the given options to this DiscoveryOpts
func (opts *DiscoveryOpts) Apply(options ...DiscoveryOpt) error {
for _, o := range options {
if err := o(opts); err != nil {
return err
}
}
return nil
}

// TTL is an option that provides a hint for the duration of an advertisement
func TTL(ttl time.Duration) DiscoveryOpt {
return func(opts *DiscoveryOpts) error {
opts.Ttl = ttl
return nil
}
}

// Limit is an option that provides an upper bound on the peer count for discovery
func Limit(limit int) DiscoveryOpt {
return func(opts *DiscoveryOpts) error {
opts.Limit = limit
return nil
}
}
41 changes: 41 additions & 0 deletions discovery/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package discovery

import "time"

// DiscoveryOpt is a single discovery option.
type Option func(opts *Options) error

// DiscoveryOpts is a set of discovery options.
type Options struct {
Ttl time.Duration
Limit int

// Other (implementation-specific) options
Other map[interface{}]interface{}
}

// Apply applies the given options to this DiscoveryOpts
func (opts *Options) Apply(options ...Option) error {
for _, o := range options {
if err := o(opts); err != nil {
return err
}
}
return nil
}

// TTL is an option that provides a hint for the duration of an advertisement
func TTL(ttl time.Duration) Option {
return func(opts *Options) error {
opts.Ttl = ttl
return nil
}
}

// Limit is an option that provides an upper bound on the peer count for discovery
func Limit(limit int) Option {
return func(opts *Options) error {
opts.Limit = limit
return nil
}
}
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions mux/muxer.go → mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
var ErrReset = errors.New("stream reset")

// Stream is a bidirectional io pipe within a connection.
type MuxStream interface {
type MuxedStream interface {
io.Reader
io.Writer

Expand All @@ -29,7 +29,7 @@ type MuxStream interface {
}

// NoOpHandler do nothing. Resets streams as soon as they are opened.
var NoOpHandler = func(s MuxStream) { s.Reset() }
var NoOpHandler = func(s MuxedStream) { s.Reset() }

// MuxedConn is a stream-multiplexing connection to a remote peer.
type MuxedConn interface {
Expand All @@ -41,10 +41,10 @@ type MuxedConn interface {
IsClosed() bool

// OpenStream creates a new stream.
OpenStream() (MuxStream, error)
OpenStream() (MuxedStream, error)

// AcceptStream accepts a stream opened by the other side.
AcceptStream() (MuxStream, error)
AcceptStream() (MuxedStream, error)
}

// Transport constructs go-stream-muxer compatible connections.
Expand Down
58 changes: 58 additions & 0 deletions network/conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package network

import (
"io"

"github.com/libp2p/go-libp2p-core/peer"
ic "github.com/libp2p/go-libp2p-crypto"

ma "github.com/multiformats/go-multiaddr"
)

// Conn is a connection to a remote peer. It multiplexes streams.
// Usually there is no need to use a Conn directly, but it may
// be useful to get information about the peer on the other side:
// stream.Conn().RemotePeer()
type Conn interface {
io.Closer

ConnSecurity
ConnMultiaddrs

// NewStream constructs a new Stream over this conn.
NewStream() (Stream, error)

// GetStreams returns all open streams over this conn.
GetStreams() []Stream

// Stat stores metadata pertaining to this conn.
Stat() Stat
}

// ConnSecurity is the interface that one can mix into a connection interface to
// give it the security methods.
type ConnSecurity interface {
// LocalPeer returns our peer ID
LocalPeer() peer.ID

// LocalPrivateKey returns our private key
LocalPrivateKey() ic.PrivKey

// RemotePeer returns the peer ID of the remote peer.
RemotePeer() peer.ID

// RemotePublicKey returns the public key of the remote peer.
RemotePublicKey() ic.PubKey
}

// ConnMultiaddrs is an interface mixin for connection types that provide multiaddr
// addresses for the endpoints.
type ConnMultiaddrs interface {
// LocalMultiaddr returns the local Multiaddr associated
// with this connection
LocalMultiaddr() ma.Multiaddr

// RemoteMultiaddr returns the remote Multiaddr associated
// with this connection
RemoteMultiaddr() ma.Multiaddr
}
10 changes: 10 additions & 0 deletions network/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package network

import "errors"

// There are no addresses associated with a peer when they were needed.
var ErrNoRemoteAddrs = errors.New("no remote addresses")

// ErrNoConn is returned when attempting to open a stream to a peer with the NoDial
// option and no usable connection is available.
var ErrNoConn = errors.New("no usable connection to peer")
132 changes: 19 additions & 113 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ package network

import (
"context"
"errors"
"io"

"github.com/jbenet/goprocess"
"github.com/libp2p/go-libp2p-core/mux"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/protocol"

ic "github.com/libp2p/go-libp2p-crypto"

ma "github.com/multiformats/go-multiaddr"
)
Expand All @@ -22,24 +17,6 @@ import (
// a single, large serialized object.
const MessageSizeMax = 1 << 22 // 4 MB

// Stream represents a bidirectional channel between two agents in
// a libp2p network. "agent" is as granular as desired, potentially
// being a "request -> reply" pair, or whole protocols.
//
// Streams are backed by a multiplexer underneath the hood.
type Stream interface {
mux.MuxStream

Protocol() protocol.ID
SetProtocol(id protocol.ID)

// Stat returns metadata pertaining to this stream.
Stat() Stat

// Conn returns the connection this stream is part of.
Conn() Conn
}

// Direction represents which peer in a stream initiated a connection.
type Direction int

Expand All @@ -52,6 +29,25 @@ const (
DirOutbound
)

// Connectedness signals the capacity for a connection with a given node.
// It is used to signal to services and other peers whether a node is reachable.
type Connectedness int

const (
// NotConnected means no connection to peer, and no extra information (default)
NotConnected Connectedness = iota

// Connected means has an open, live connection to peer
Connected

// CanConnect means recently connected to peer, terminated gracefully
CanConnect

// CannotConnect means recently attempted connecting but failed to connect.
// (should signal "made effort, failed")
CannotConnect
)

// Stat stores metadata pertaining to a given Stream/Conn.
type Stat struct {
Direction Direction
Expand All @@ -62,54 +58,6 @@ type Stat struct {
// streams opened by the remote side.
type StreamHandler func(Stream)

// ConnSecurity is the interface that one can mix into a connection interface to
// give it the security methods.
type ConnSecurity interface {
// LocalPeer returns our peer ID
LocalPeer() peer.ID

// LocalPrivateKey returns our private key
LocalPrivateKey() ic.PrivKey

// RemotePeer returns the peer ID of the remote peer.
RemotePeer() peer.ID

// RemotePublicKey returns the public key of the remote peer.
RemotePublicKey() ic.PubKey
}

// ConnMultiaddrs is an interface mixin for connection types that provide multiaddr
// addresses for the endpoints.
type ConnMultiaddrs interface {
// LocalMultiaddr returns the local Multiaddr associated
// with this connection
LocalMultiaddr() ma.Multiaddr

// RemoteMultiaddr returns the remote Multiaddr associated
// with this connection
RemoteMultiaddr() ma.Multiaddr
}

// Conn is a connection to a remote peer. It multiplexes streams.
// Usually there is no need to use a Conn directly, but it may
// be useful to get information about the peer on the other side:
// stream.Conn().RemotePeer()
type Conn interface {
io.Closer

ConnSecurity
ConnMultiaddrs

// NewStream constructs a new Stream over this conn.
NewStream() (Stream, error)

// GetStreams returns all open streams over this conn.
GetStreams() []Stream

// Stat stores metadata pertaining to this conn.
Stat() Stat
}

// ConnHandler is the type of function used to listen for
// connections opened by the remote side.
type ConnHandler func(Conn)
Expand Down Expand Up @@ -149,18 +97,10 @@ type Network interface {
Process() goprocess.Process
}

// There are no addresses associated with a peer when they were needed.
var ErrNoRemoteAddrs = errors.New("no remote addresses")

// ErrNoConn is returned when attempting to open a stream to a peer with the NoDial
// option and no usable connection is available.
var ErrNoConn = errors.New("no usable connection to peer")

// Dialer represents a service that can dial out to peers
// (this is usually just a Network, but other services may not need the whole
// stack, and thus it becomes easier to mock)
type Dialer interface {

// Peerstore returns the internal peerstore
// This is useful to tell the dialer about a new address for a peer.
// Or use one of the public keys found out over the network.
Expand Down Expand Up @@ -191,37 +131,3 @@ type Dialer interface {
Notify(Notifiee)
StopNotify(Notifiee)
}

// Connectedness signals the capacity for a connection with a given node.
// It is used to signal to services and other peers whether a node is reachable.
type Connectedness int

const (
// NotConnected means no connection to peer, and no extra information (default)
NotConnected Connectedness = iota

// Connected means has an open, live connection to peer
Connected

// CanConnect means recently connected to peer, terminated gracefully
CanConnect

// CannotConnect means recently attempted connecting but failed to connect.
// (should signal "made effort, failed")
CannotConnect
)

// Notifiee is an interface for an object wishing to receive
// notifications from a Network.
type Notifiee interface {
Listen(Network, ma.Multiaddr) // called when network starts listening on an addr
ListenClose(Network, ma.Multiaddr) // called when network stops listening on an addr
Connected(Network, Conn) // called when a connection opened
Disconnected(Network, Conn) // called when a connection closed
OpenedStream(Network, Stream) // called when a stream opened
ClosedStream(Network, Stream) // called when a stream closed

// TODO
// PeerConnected(Network, peer.ID) // called when a peer connected
// PeerDisconnected(Network, peer.ID) // called when a peer disconnected
}
Loading

0 comments on commit 47396eb

Please sign in to comment.