Addrs listen api

This commit is contained in:
Łukasz Magiera 2019-07-08 23:01:15 +02:00
parent f9b5343430
commit 9a244ebdf8
4 changed files with 33 additions and 4 deletions

View File

@ -2,7 +2,9 @@ package api
import ( import (
"context" "context"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
) )
// Version provides various build-time information // Version provides various build-time information
@ -35,6 +37,7 @@ type API interface {
NetPeers(context.Context) ([]peer.AddrInfo, error) // TODO: check serialization NetPeers(context.Context) ([]peer.AddrInfo, error) // TODO: check serialization
NetConnect(context.Context, peer.AddrInfo) error NetConnect(context.Context, peer.AddrInfo) error
NetAddrsListen(context.Context) ([]ma.Multiaddr, error)
// // ping // // ping
// Struct // Struct

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
) )
// Struct implements API passing calls to user-provided function values. // Struct implements API passing calls to user-provided function values.
@ -12,8 +13,9 @@ type Struct struct {
ID func(context.Context) (peer.ID, error) ID func(context.Context) (peer.ID, error)
Version func(context.Context) (Version, error) Version func(context.Context) (Version, error)
NetPeers func(context.Context) ([]peer.AddrInfo, error) NetPeers func(context.Context) ([]peer.AddrInfo, error)
NetConnect func(context.Context, peer.AddrInfo) error NetConnect func(context.Context, peer.AddrInfo) error
NetAddrsListen func(context.Context) ([]ma.Multiaddr, error)
} }
} }
@ -25,6 +27,10 @@ func (c *Struct) NetConnect(ctx context.Context, p peer.AddrInfo) error {
return c.Internal.NetConnect(ctx, p) return c.Internal.NetConnect(ctx, p)
} }
func (c *Struct) NetAddrsListen(ctx context.Context) ([]ma.Multiaddr, error) {
return c.Internal.NetAddrsListen(ctx)
}
// ID implements API.ID // ID implements API.ID
func (c *Struct) ID(ctx context.Context) (peer.ID, error) { func (c *Struct) ID(ctx context.Context) (peer.ID, error) {
return c.Internal.ID(ctx) return c.Internal.ID(ctx)

View File

@ -18,6 +18,7 @@ var netCmd = &cli.Command{
Subcommands: []*cli.Command{ Subcommands: []*cli.Command{
netPeers, netPeers,
netConnect, netConnect,
netListen,
}, },
} }
@ -32,6 +33,17 @@ var netPeers = &cli.Command{
}, },
} }
var netListen = &cli.Command{
Name: "listen",
Usage: "List listen addresses",
Action: func(ctx *cli.Context) error {
api := getApi(ctx)
fmt.Println(api.NetAddrsListen(context.Background()))
return nil
},
}
var netConnect = &cli.Command{ var netConnect = &cli.Command{
Name: "connect", Name: "connect",
Usage: "Connect to a peer", Usage: "Connect to a peer",

View File

@ -10,7 +10,7 @@ import (
"github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
"go.uber.org/fx" "go.uber.org/fx"
) )
@ -41,8 +41,10 @@ func apiOption(resAPI *api.Struct) fx.Option {
return fx.Options( return fx.Options(
provideApi(versionAPI, &in.Version), provideApi(versionAPI, &in.Version),
provideApi(idAPI, &in.ID), provideApi(idAPI, &in.ID),
provideApi(netPeersAPI, &in.NetPeers), provideApi(netPeersAPI, &in.NetPeers),
provideApi(netConnectAPI, &in.NetConnect), provideApi(netConnectAPI, &in.NetConnect),
provideApi(netAddrsListenAPI, &in.NetAddrsListen),
) )
} }
@ -68,7 +70,7 @@ func netPeersAPI(h host.Host) interface{} {
for i, conn := range conns { for i, conn := range conns {
out[i] = peer.AddrInfo{ out[i] = peer.AddrInfo{
ID: conn.RemotePeer(), ID: conn.RemotePeer(),
Addrs: []multiaddr.Multiaddr{ Addrs: []ma.Multiaddr{
conn.RemoteMultiaddr(), conn.RemoteMultiaddr(),
}, },
} }
@ -83,3 +85,9 @@ func netConnectAPI(h host.Host) interface{} {
return errors.New("nope") return errors.New("nope")
} }
} }
func netAddrsListenAPI(h host.Host) interface{} {
return func(context.Context) ([]ma.Multiaddr, error) {
return h.Addrs(), nil
}
}