Skip to content

Commit

Permalink
update to consolidated libp2p interface package (#21)
Browse files Browse the repository at this point in the history
and fix parsing of connection latencies

This commit was moved from ipfs/go-ipfs-http-client@fd5cce4
  • Loading branch information
godcong authored and Stebalien committed Jul 21, 2019
1 parent 2fabc8c commit 9345cfa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
31 changes: 15 additions & 16 deletions client/httpapi/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,37 @@ import (

caopts "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
"github.com/libp2p/go-libp2p-peer"
"github.com/libp2p/go-libp2p-peerstore"
notif "github.com/libp2p/go-libp2p-routing/notifications"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/routing"
)

type DhtAPI HttpApi

func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (peerstore.PeerInfo, error) {
func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
var out struct {
Type notif.QueryEventType
Responses []peerstore.PeerInfo
Type routing.QueryEventType
Responses []peer.AddrInfo
}
resp, err := api.core().Request("dht/findpeer", p.Pretty()).Send(ctx)
if err != nil {
return peerstore.PeerInfo{}, err
return peer.AddrInfo{}, err
}
if resp.Error != nil {
return peerstore.PeerInfo{}, resp.Error
return peer.AddrInfo{}, resp.Error
}
defer resp.Close()
dec := json.NewDecoder(resp.Output)
for {
if err := dec.Decode(&out); err != nil {
return peerstore.PeerInfo{}, err
return peer.AddrInfo{}, err
}
if out.Type == notif.FinalPeer {
if out.Type == routing.FinalPeer {
return out.Responses[0], nil
}
}
}

func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopts.DhtFindProvidersOption) (<-chan peerstore.PeerInfo, error) {
func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopts.DhtFindProvidersOption) (<-chan peer.AddrInfo, error) {
options, err := caopts.DhtFindProvidersOptions(opts...)
if err != nil {
return nil, err
Expand All @@ -57,7 +56,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopt
if resp.Error != nil {
return nil, resp.Error
}
res := make(chan peerstore.PeerInfo)
res := make(chan peer.AddrInfo)

go func() {
defer resp.Close()
Expand All @@ -67,18 +66,18 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopt
for {
var out struct {
Extra string
Type notif.QueryEventType
Responses []peerstore.PeerInfo
Type routing.QueryEventType
Responses []peer.AddrInfo
}

if err := dec.Decode(&out); err != nil {
return // todo: handle this somehow
}
if out.Type == notif.QueryError {
if out.Type == routing.QueryError {
return // usually a 'not found' error
// todo: handle other errors
}
if out.Type == notif.Provider {
if out.Type == routing.Provider {
for _, pi := range out.Responses {
select {
case res <- pi:
Expand Down
2 changes: 1 addition & 1 deletion client/httpapi/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/ipfs/interface-go-ipfs-core"
caopts "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
"github.com/libp2p/go-libp2p-peer"
"github.com/libp2p/go-libp2p-core/peer"
)

type KeyAPI HttpApi
Expand Down
2 changes: 1 addition & 1 deletion client/httpapi/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/ipfs/interface-go-ipfs-core"
caopts "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/libp2p/go-libp2p-peer"
"github.com/libp2p/go-libp2p-core/peer"
)

type PubsubAPI HttpApi
Expand Down
20 changes: 10 additions & 10 deletions client/httpapi/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import (
"time"

"github.com/ipfs/interface-go-ipfs-core"
inet "github.com/libp2p/go-libp2p-net"
"github.com/libp2p/go-libp2p-peer"
"github.com/libp2p/go-libp2p-peerstore"
"github.com/libp2p/go-libp2p-protocol"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/multiformats/go-multiaddr"
)

type SwarmAPI HttpApi

func (api *SwarmAPI) Connect(ctx context.Context, pi peerstore.PeerInfo) error {
func (api *SwarmAPI) Connect(ctx context.Context, pi peer.AddrInfo) error {
pidma, err := multiaddr.NewComponent("p2p", pi.ID.Pretty())
if err != nil {
return err
Expand All @@ -37,7 +36,7 @@ type connInfo struct {
peer peer.ID
latency time.Duration
muxer string
direction inet.Direction
direction network.Direction
streams []protocol.ID
}

Expand All @@ -49,7 +48,7 @@ func (c *connInfo) Address() multiaddr.Multiaddr {
return c.addr
}

func (c *connInfo) Direction() inet.Direction {
func (c *connInfo) Direction() network.Direction {
return c.direction
}

Expand All @@ -66,9 +65,9 @@ func (api *SwarmAPI) Peers(ctx context.Context) ([]iface.ConnectionInfo, error)
Peers []struct {
Addr string
Peer string
Latency time.Duration
Latency string
Muxer string
Direction inet.Direction
Direction network.Direction
Streams []struct {
Protocol string
}
Expand All @@ -85,8 +84,9 @@ func (api *SwarmAPI) Peers(ctx context.Context) ([]iface.ConnectionInfo, error)

res := make([]iface.ConnectionInfo, len(resp.Peers))
for i, conn := range resp.Peers {
latency, _ := time.ParseDuration(conn.Latency)
out := &connInfo{
latency: conn.Latency,
latency: latency,
muxer: conn.Muxer,
direction: conn.Direction,
}
Expand Down

0 comments on commit 9345cfa

Please sign in to comment.