From ba17b32dd5a806cac5408a708ee4edb21b465b00 Mon Sep 17 00:00:00 2001 From: Louis Thibault Date: Fri, 15 Jul 2022 17:55:02 +0200 Subject: [PATCH] Clean up client dial API. Add vat.NewClientHost. --- examples/dial-client/main.go | 14 +++----------- examples/pubsub/main.go | 16 +++++----------- pkg/client/dialer.go | 23 ++--------------------- pkg/vat/util.go | 25 +++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 43 deletions(-) create mode 100644 pkg/vat/util.go diff --git a/examples/dial-client/main.go b/examples/dial-client/main.go index 46613f1e..b7a74dbd 100644 --- a/examples/dial-client/main.go +++ b/examples/dial-client/main.go @@ -6,24 +6,16 @@ import ( "io" "log" - "github.com/libp2p/go-libp2p" - libp2pquic "github.com/libp2p/go-libp2p-quic-transport" bootutil "github.com/wetware/casm/pkg/boot/util" "github.com/wetware/ww/pkg/client" "github.com/wetware/ww/pkg/vat" ) -func main() { - ctx := context.Background() +var ctx = context.Background() +func main() { // Instantiate the libp2p host that the Wetware client will use. - host, err := libp2p.New( - // Don't listen for incoming network connections. This is - // common practice for Wetware clients. - libp2p.NoListenAddrs, - // Wetware uses the QUIC transport, so let's enable it. - libp2p.Transport(libp2pquic.NewTransport), - ) + host, err := vat.NewClientHost() if err != nil { log.Fatal(err) } diff --git a/examples/pubsub/main.go b/examples/pubsub/main.go index 6c4f9ffe..386c3c76 100644 --- a/examples/pubsub/main.go +++ b/examples/pubsub/main.go @@ -7,24 +7,16 @@ import ( "log" "time" - "github.com/libp2p/go-libp2p" - libp2pquic "github.com/libp2p/go-libp2p-quic-transport" bootutil "github.com/wetware/casm/pkg/boot/util" "github.com/wetware/ww/pkg/client" "github.com/wetware/ww/pkg/vat" ) -func main() { - ctx := context.Background() +var ctx = context.Background() +func main() { // Instantiate the libp2p host that the Wetware client will use. - host, err := libp2p.New( - // Don't listen for incoming network connections. This is - // common practice for Wetware clients. - libp2p.NoListenAddrs, - // Wetware uses the QUIC transport, so let's enable it. - libp2p.Transport(libp2pquic.NewTransport), - ) + host, err := vat.NewClientHost() if err != nil { log.Fatal(err) } @@ -91,6 +83,8 @@ func main() { } defer sub.Cancel() + // Print messages received on the subscription. This will + // include messages we have sent ourselves. for { b, err := sub.Next(ctx) if err != nil { diff --git a/pkg/client/dialer.go b/pkg/client/dialer.go index 99d497d8..cb0e0281 100644 --- a/pkg/client/dialer.go +++ b/pkg/client/dialer.go @@ -9,38 +9,19 @@ import ( "github.com/libp2p/go-libp2p-core/peer" "github.com/lthibault/log" - "github.com/wetware/casm/pkg/boot" "github.com/wetware/ww/pkg/vat" "github.com/wetware/ww/pkg/vat/cap/cluster" "github.com/wetware/ww/pkg/vat/cap/pubsub" ) -type Addr string - -func (addr Addr) FindPeers(ctx context.Context, ns string, opt ...discovery.Option) (<-chan peer.AddrInfo, error) { - info, err := peer.AddrInfoFromString(string(addr)) - if err != nil { - return nil, err - } - - return boot.StaticAddrs{*info}.FindPeers(ctx, ns, opt...) -} - +// Dialer is a factory type for Node. It uses Boot to join the +// cluster identified by Vat.NS, and returns a Node. type Dialer struct { Log log.Logger Vat vat.Network Boot discovery.Discoverer } -// Dial is a convenience function that joins a cluster using the -// supplied address string. -// -// See Dialer.Dial for an important notice about the lifetime of -// ctx. -func Dial(ctx context.Context, vat vat.Network, a Addr) (*Node, error) { - return Dialer{Vat: vat, Boot: a}.Dial(ctx) -} - // Dial creates a client and connects it to a cluster. func (d Dialer) Dial(ctx context.Context) (*Node, error) { if d.Log == nil { diff --git a/pkg/vat/util.go b/pkg/vat/util.go new file mode 100644 index 00000000..9d32887d --- /dev/null +++ b/pkg/vat/util.go @@ -0,0 +1,25 @@ +// Package vatutil provides utilities for creating and configuring network vats. +package vat + +import ( + "github.com/libp2p/go-libp2p" + "github.com/libp2p/go-libp2p-core/host" + libp2pquic "github.com/libp2p/go-libp2p-quic-transport" +) + +// NewClientHost returns a libp2p.Host suitable for use in a Wetware +// client vat. By default, the returned host uses the QUIC transport, +// and does not accept incoming network connections. +// +// Callers can override these defaults by passing libp2p.Options. +func NewClientHost(opt ...libp2p.Option) (host.Host, error) { + return libp2p.New(withClientDefault(opt)...) +} + +func withClientDefault(opt []libp2p.Option) []libp2p.Option { + return append([]libp2p.Option{ + libp2p.NoTransports, + libp2p.NoListenAddrs, + libp2p.Transport(libp2pquic.NewTransport), + }, opt...) +}