Skip to content

Commit

Permalink
Clean up client dial API. Add vat.NewClientHost.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Jul 15, 2022
1 parent c86f7fe commit ba17b32
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 43 deletions.
14 changes: 3 additions & 11 deletions examples/dial-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 5 additions & 11 deletions examples/pubsub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 2 additions & 21 deletions pkg/client/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions pkg/vat/util.go
Original file line number Diff line number Diff line change
@@ -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...)
}

0 comments on commit ba17b32

Please sign in to comment.