Skip to content

Commit

Permalink
migrate to consolidated types. (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk authored May 26, 2019
1 parent 676df1b commit 0df8d18
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
24 changes: 12 additions & 12 deletions p2p/host/autonat/autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"sync"
"time"

host "github.com/libp2p/go-libp2p-host"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
ma "github.com/multiformats/go-multiaddr"
)

Expand Down Expand Up @@ -157,10 +157,10 @@ func (as *AmbientAutoNAT) autodetect() {

for _, pi := range peers[:probe] {
wg.Add(1)
go func(pi pstore.PeerInfo) {
go func(pi peer.AddrInfo) {
defer wg.Done()

as.host.Peerstore().AddAddrs(pi.ID, pi.Addrs, pstore.TempAddrTTL)
as.host.Peerstore().AddAddrs(pi.ID, pi.Addrs, peerstore.TempAddrTTL)
a, err := cli.DialBack(ctx, pi.ID)

switch {
Expand Down Expand Up @@ -217,21 +217,21 @@ func (as *AmbientAutoNAT) autodetect() {
as.mx.Unlock()
}

func (as *AmbientAutoNAT) getPeers() []pstore.PeerInfo {
func (as *AmbientAutoNAT) getPeers() []peer.AddrInfo {
as.mx.Lock()
defer as.mx.Unlock()

if len(as.peers) == 0 {
return nil
}

var connected, others []pstore.PeerInfo
var connected, others []peer.AddrInfo

for p, addrs := range as.peers {
if as.host.Network().Connectedness(p) == inet.Connected {
connected = append(connected, pstore.PeerInfo{ID: p, Addrs: addrs})
if as.host.Network().Connectedness(p) == network.Connected {
connected = append(connected, peer.AddrInfo{ID: p, Addrs: addrs})
} else {
others = append(others, pstore.PeerInfo{ID: p, Addrs: addrs})
others = append(others, peer.AddrInfo{ID: p, Addrs: addrs})
}
}

Expand All @@ -245,7 +245,7 @@ func (as *AmbientAutoNAT) getPeers() []pstore.PeerInfo {
}
}

func shufflePeers(peers []pstore.PeerInfo) {
func shufflePeers(peers []peer.AddrInfo) {
for i := range peers {
j := rand.Intn(i + 1)
peers[i], peers[j] = peers[j], peers[i]
Expand Down
13 changes: 7 additions & 6 deletions p2p/host/autonat/autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"time"

pb "github.com/libp2p/go-libp2p-autonat/pb"
"github.com/libp2p/go-libp2p-core/peer"

"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"

ggio "github.com/gogo/protobuf/io"
bhost "github.com/libp2p/go-libp2p-blankhost"
host "github.com/libp2p/go-libp2p-host"
inet "github.com/libp2p/go-libp2p-net"
pstore "github.com/libp2p/go-libp2p-peerstore"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
ma "github.com/multiformats/go-multiaddr"
)
Expand All @@ -36,7 +37,7 @@ func makeAutoNATServicePublic(ctx context.Context, t *testing.T) host.Host {
return h
}

func sayAutoNATPrivate(s inet.Stream) {
func sayAutoNATPrivate(s network.Stream) {
defer s.Close()
w := ggio.NewDelimitedWriter(s)
res := pb.Message{
Expand All @@ -46,7 +47,7 @@ func sayAutoNATPrivate(s inet.Stream) {
w.WriteMsg(&res)
}

func sayAutoNATPublic(s inet.Stream) {
func sayAutoNATPublic(s network.Stream) {
defer s.Close()
w := ggio.NewDelimitedWriter(s)
res := pb.Message{
Expand Down Expand Up @@ -80,7 +81,7 @@ func makeAutoNAT(ctx context.Context, t *testing.T, ash host.Host) (host.Host, A
}

func connect(t *testing.T, a, b host.Host) {
pinfo := pstore.PeerInfo{ID: a.ID(), Addrs: a.Addrs()}
pinfo := peer.AddrInfo{ID: a.ID(), Addrs: a.Addrs()}
err := b.Connect(context.Background(), pinfo)
if err != nil {
t.Fatal(err)
Expand Down
14 changes: 7 additions & 7 deletions p2p/host/autonat/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"fmt"

pb "github.com/libp2p/go-libp2p-autonat/pb"
"github.com/libp2p/go-libp2p-core/helpers"

ggio "github.com/gogo/protobuf/io"
host "github.com/libp2p/go-libp2p-host"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
)

Expand Down Expand Up @@ -51,12 +51,12 @@ func (c *client) DialBack(ctx context.Context, p peer.ID) (ma.Multiaddr, error)
}
// Might as well just reset the stream. Once we get to this point, we
// don't care about being nice.
defer inet.FullClose(s)
defer helpers.FullClose(s)

r := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
r := ggio.NewDelimitedReader(s, network.MessageSizeMax)
w := ggio.NewDelimitedWriter(s)

req := newDialMessage(pstore.PeerInfo{ID: c.h.ID(), Addrs: c.getAddrs()})
req := newDialMessage(peer.AddrInfo{ID: c.h.ID(), Addrs: c.getAddrs()})
err = w.WriteMsg(req)
if err != nil {
s.Reset()
Expand Down
17 changes: 9 additions & 8 deletions p2p/host/autonat/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package autonat
import (
"time"

inet "github.com/libp2p/go-libp2p-net"
"github.com/libp2p/go-libp2p-core/network"

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

var _ inet.Notifiee = (*AmbientAutoNAT)(nil)
var _ network.Notifiee = (*AmbientAutoNAT)(nil)

var AutoNATIdentifyDelay = 5 * time.Second

func (as *AmbientAutoNAT) Listen(net inet.Network, a ma.Multiaddr) {}
func (as *AmbientAutoNAT) ListenClose(net inet.Network, a ma.Multiaddr) {}
func (as *AmbientAutoNAT) OpenedStream(net inet.Network, s inet.Stream) {}
func (as *AmbientAutoNAT) ClosedStream(net inet.Network, s inet.Stream) {}
func (as *AmbientAutoNAT) Listen(net network.Network, a ma.Multiaddr) {}
func (as *AmbientAutoNAT) ListenClose(net network.Network, a ma.Multiaddr) {}
func (as *AmbientAutoNAT) OpenedStream(net network.Network, s network.Stream) {}
func (as *AmbientAutoNAT) ClosedStream(net network.Network, s network.Stream) {}

func (as *AmbientAutoNAT) Connected(net inet.Network, c inet.Conn) {
func (as *AmbientAutoNAT) Connected(net network.Network, c network.Conn) {
p := c.RemotePeer()

go func() {
Expand All @@ -38,4 +39,4 @@ func (as *AmbientAutoNAT) Connected(net inet.Network, c inet.Conn) {
}()
}

func (as *AmbientAutoNAT) Disconnected(net inet.Network, c inet.Conn) {}
func (as *AmbientAutoNAT) Disconnected(net network.Network, c network.Conn) {}
5 changes: 3 additions & 2 deletions p2p/host/autonat/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package autonat
import (
pb "github.com/libp2p/go-libp2p-autonat/pb"

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

logging "github.com/ipfs/go-log"
pstore "github.com/libp2p/go-libp2p-peerstore"
)

const AutoNATProto = "/libp2p/autonat/1.0.0"

var log = logging.Logger("autonat")

func newDialMessage(pi pstore.PeerInfo) *pb.Message {
func newDialMessage(pi peer.AddrInfo) *pb.Message {
msg := new(pb.Message)
msg.Type = pb.Message_DIAL.Enum()
msg.Dial = new(pb.Message_Dial)
Expand Down

0 comments on commit 0df8d18

Please sign in to comment.