Skip to content

Commit

Permalink
Make Listener, Conn and Addr private.
Browse files Browse the repository at this point in the history
They aren't meant to be used directly, only as implementations
of net.Listener, net.Conn and net.Addr. Accessible via Dial()
and Listen().
  • Loading branch information
hsanjuan committed Oct 19, 2017
1 parent 8b11532 commit 5fc7f01
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 34 deletions.
8 changes: 4 additions & 4 deletions p2p/net/gostream/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package gostream

import peer "github.com/libp2p/go-libp2p-peer"

// Addr implements net.Addr and holds a libp2p peer ID.
type Addr struct{ id peer.ID }
// addr implements net.Addr and holds a libp2p peer ID.
type addr struct{ id peer.ID }

// Network returns the name of the network that this address belongs to
// (libp2p).
func (a *Addr) Network() string { return "libp2p" }
func (a *addr) Network() string { return Network }

// String returns the peer ID of this address in string form
// (B58-encoded).
func (a *Addr) String() string { return a.id.Pretty() }
func (a *addr) String() string { return a.id.Pretty() }
34 changes: 17 additions & 17 deletions p2p/net/gostream/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,68 @@ import (
protocol "github.com/libp2p/go-libp2p-protocol"
)

// Conn is an implementation of net.Conn which wraps
// conn is an implementation of net.Conn which wraps
// libp2p streams.
type Conn struct {
type conn struct {
s pnet.Stream
}

// NewConn creates a Conn given a libp2p stream
func NewConn(s pnet.Stream) net.Conn {
return &Conn{s}
// newConn creates a conn given a libp2p stream
func newConn(s pnet.Stream) net.Conn {
return &conn{s}
}

// Read reads data from the connection.
func (c *Conn) Read(b []byte) (n int, err error) {
func (c *conn) Read(b []byte) (n int, err error) {
return c.s.Read(b)
}

// Write writes data to the connection.
func (c *Conn) Write(b []byte) (n int, err error) {
func (c *conn) Write(b []byte) (n int, err error) {
return c.s.Write(b)
}

// Close closes the connection.
// Any blocked Read or Write operations will be unblocked and return errors.
func (c *Conn) Close() error {
func (c *conn) Close() error {
return c.s.Close()
}

// LocalAddr returns the local network address.
func (c *Conn) LocalAddr() net.Addr {
return &Addr{c.s.Conn().LocalPeer()}
func (c *conn) LocalAddr() net.Addr {
return &addr{c.s.Conn().LocalPeer()}
}

// RemoteAddr returns the remote network address.
func (c *Conn) RemoteAddr() net.Addr {
return &Addr{c.s.Conn().RemotePeer()}
func (c *conn) RemoteAddr() net.Addr {
return &addr{c.s.Conn().RemotePeer()}
}

// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
// See https://golang.org/pkg/net/#Conn for more details.
func (c *Conn) SetDeadline(t time.Time) error {
func (c *conn) SetDeadline(t time.Time) error {
return c.s.SetDeadline(t)
}

// SetReadDeadline sets the deadline for future Read calls.
// A zero value for t means Read will not time out.
func (c *Conn) SetReadDeadline(t time.Time) error {
func (c *conn) SetReadDeadline(t time.Time) error {
return c.s.SetReadDeadline(t)
}

// SetWriteDeadline sets the deadline for future Write calls.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
func (c *Conn) SetWriteDeadline(t time.Time) error {
func (c *conn) SetWriteDeadline(t time.Time) error {
return c.s.SetWriteDeadline(t)
}

// Dial opens a stream to the destination address
// (which should parseable to a peer ID) using the given
// host and returns it.
// host and returns it as a standard net.Conn.
func Dial(h host.Host, pid peer.ID, tag protocol.ID) (net.Conn, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
Expand All @@ -81,5 +81,5 @@ func Dial(h host.Host, pid peer.ID, tag protocol.ID) (net.Conn, error) {
if err != nil {
return nil, err
}
return NewConn(s), nil
return newConn(s), nil
}
9 changes: 7 additions & 2 deletions p2p/net/gostream/gostream.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package `gostream` allows to replace the standard net stack in Go
// Package gostream allows to replace the standard net stack in Go
// with [LibP2P](https://github.com/libp2p/libp2p) streams.
//
// Given a libp2p.Host, `gostream` provides Dial() and Listen() methods which
// Given a libp2p.Host, gostream provides Dial() and Listen() methods which
// return implementations of net.Conn and net.Listener.
//
// Instead of the regular "host:port" addressing, `gostream` uses a Peer ID,
Expand All @@ -12,3 +12,8 @@
// Note that LibP2P hosts cannot dial to themselves, so there is no possibility
// of using the same Host as server and as client.
package gostream

// Network is the "net.Addr.Network()" name returned by
// addresses used by gostream connections. In turn, the "net.Addr.String()" will
// be a peer ID.
var Network = "libp2p"
4 changes: 4 additions & 0 deletions p2p/net/gostream/gostream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func TestServerClient(t *testing.T) {
t.Fatal("Bad RemoteAddr")
}

if clientConn.LocalAddr().Network() != Network {
t.Fatal("Bad Network()")
}

err = clientConn.SetDeadline(time.Now().Add(time.Second))
if err != nil {
t.Fatal(err)
Expand Down
25 changes: 14 additions & 11 deletions p2p/net/gostream/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,49 @@ import (
protocol "github.com/libp2p/go-libp2p-protocol"
)

// Listener is an implementation of net.Listener which handles
// listener is an implementation of net.Listener which handles
// http-tagged streams from a libp2p connection.
// A listener can be built with Listen()
type Listener struct {
type listener struct {
host host.Host
ctx context.Context
tag protocol.ID
cancel func()
streamCh chan pnet.Stream
}

// Accept returns a connection from this listener. It blocks if there
// are no connections.
func (l *Listener) Accept() (net.Conn, error) {
// Accept returns the next a connection to this listener.
// It blocks if there are no connections. Under the hood,
// connections are libp2p streams.
func (l *listener) Accept() (net.Conn, error) {
select {
case s := <-l.streamCh:
return NewConn(s), nil
return newConn(s), nil
case <-l.ctx.Done():
return nil, l.ctx.Err()
}
}

// Close terminates this listener. It will no longer handle any
// incoming streams
func (l *Listener) Close() error {
func (l *listener) Close() error {
l.cancel()
l.host.RemoveStreamHandler(l.tag)
return nil
}

// Addr returns the address for this listener, which is its libp2p Peer ID.
func (l *Listener) Addr() net.Addr {
return &Addr{l.host.ID()}
func (l *listener) Addr() net.Addr {
return &addr{l.host.ID()}
}

// Listen creates a new listener ready to accept streams received by a host.
// Listen provides a standard net.Listener ready to accept "connections".
// Under the hood, these connections are libp2p streams tagged with the
// given protocol.ID.
func Listen(h host.Host, tag protocol.ID) (net.Listener, error) {
ctx, cancel := context.WithCancel(context.Background())

l := &Listener{
l := &listener{
host: h,
ctx: ctx,
cancel: cancel,
Expand Down

0 comments on commit 5fc7f01

Please sign in to comment.