2024-11-14 20:49:35 +00:00
|
|
|
//go:generate mockery --name RPC --inpackage --filename rpc_mock.go
|
|
|
|
|
2024-06-23 12:37:10 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-09-24 09:03:18 +00:00
|
|
|
"isle/bootstrap"
|
2024-11-11 14:32:15 +00:00
|
|
|
"isle/daemon/daecommon"
|
2024-09-07 11:52:32 +00:00
|
|
|
"isle/daemon/jsonrpc2"
|
2024-09-09 14:34:00 +00:00
|
|
|
"isle/daemon/network"
|
2024-07-12 13:30:21 +00:00
|
|
|
"isle/nebula"
|
2024-09-07 11:52:32 +00:00
|
|
|
"net/http"
|
2024-12-07 19:39:13 +00:00
|
|
|
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2024-06-23 12:37:10 +00:00
|
|
|
)
|
|
|
|
|
2024-12-17 10:33:19 +00:00
|
|
|
// CreateNetworkOpts are optional arguments to the CreateNetwork method. A nil
|
|
|
|
// value is equivalent to a zero value.
|
|
|
|
type CreateNetworkOpts struct {
|
|
|
|
// Config will be used as the NetworkConfig for the new Network, rather than
|
|
|
|
// picking one provided in the Daemon's Config.
|
|
|
|
Config *daecommon.NetworkConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *CreateNetworkOpts) withDefaults() *CreateNetworkOpts {
|
|
|
|
if o == nil {
|
|
|
|
o = new(CreateNetworkOpts)
|
|
|
|
}
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2024-09-07 11:52:32 +00:00
|
|
|
// RPC defines the methods which the Daemon exposes over RPC (via the RPCHandler
|
|
|
|
// or HTTPHandler methods). Method documentation can be found on the Daemon
|
|
|
|
// type.
|
2024-09-04 19:24:45 +00:00
|
|
|
type RPC interface {
|
|
|
|
CreateNetwork(
|
2024-09-04 20:25:38 +00:00
|
|
|
ctx context.Context,
|
2024-12-17 10:33:19 +00:00
|
|
|
creationParams bootstrap.CreationParams,
|
2024-09-04 20:25:38 +00:00
|
|
|
ipNet nebula.IPNet,
|
|
|
|
hostName nebula.HostName,
|
2024-12-17 10:33:19 +00:00
|
|
|
opts *CreateNetworkOpts,
|
2024-09-05 15:28:10 +00:00
|
|
|
) error
|
2024-09-04 19:24:45 +00:00
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
JoinNetwork(context.Context, network.JoiningBootstrap) error
|
2024-09-04 19:24:45 +00:00
|
|
|
|
2024-09-24 09:03:18 +00:00
|
|
|
GetNetworks(context.Context) ([]bootstrap.CreationParams, error)
|
|
|
|
|
2024-11-11 14:32:15 +00:00
|
|
|
// SetConfig extends the [network.RPC] method of the same name such that
|
2024-11-18 21:25:49 +00:00
|
|
|
// [ErrManagedNetworkConfig] is returned if the picked network is
|
2024-11-11 14:32:15 +00:00
|
|
|
// configured as part of the [daecommon.Config] which the Daemon was
|
|
|
|
// initialized with.
|
|
|
|
//
|
|
|
|
// See the `network.RPC` documentation in this interface for more usage
|
|
|
|
// details.
|
|
|
|
SetConfig(context.Context, daecommon.NetworkConfig) error
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
// All network.RPC methods are automatically implemented by Daemon using the
|
|
|
|
// currently joined network. If no network is joined then any call to these
|
|
|
|
// methods will return ErrNoNetwork.
|
2024-09-23 17:04:14 +00:00
|
|
|
//
|
2024-11-11 14:32:15 +00:00
|
|
|
// If more than one Network is joined then all calls to these methods must
|
|
|
|
// be accompanied with a context produced by WithNetwork, in order to choose
|
|
|
|
// the network. These methods may return these errors, in addition to those
|
|
|
|
// documented on the individual methods:
|
2024-09-23 17:04:14 +00:00
|
|
|
//
|
2024-09-24 09:03:18 +00:00
|
|
|
// Errors:
|
2024-11-09 20:13:33 +00:00
|
|
|
// - ErrNoNetwork
|
2024-09-23 17:04:14 +00:00
|
|
|
// - ErrNoMatchingNetworks
|
|
|
|
// - ErrMultipleMatchingNetworks
|
2024-09-09 14:34:00 +00:00
|
|
|
network.RPC
|
2024-09-04 19:24:45 +00:00
|
|
|
}
|
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
// NewRPCHandler returns a jsonrpc2.Handler which will use the given RPC to
|
|
|
|
// serve all methods defined on the interface.
|
|
|
|
func NewRPCHandler(
|
|
|
|
logger *mlog.Logger, rpc RPC,
|
|
|
|
) jsonrpc2.Handler {
|
2024-09-07 11:52:32 +00:00
|
|
|
return jsonrpc2.Chain(
|
2024-12-07 19:39:13 +00:00
|
|
|
jsonrpc2.NewMLogMiddleware(logger),
|
2024-09-07 11:52:32 +00:00
|
|
|
jsonrpc2.ExposeServerSideErrorsMiddleware,
|
|
|
|
)(
|
|
|
|
jsonrpc2.NewDispatchHandler(&rpc),
|
2024-07-09 09:43:17 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-09-07 11:52:32 +00:00
|
|
|
// HTTPRPCHandler returns an http.Handler which will use the Daemon to serve all
|
|
|
|
// methods defined on the RPC interface via the JSONRPC2 protocol.
|
|
|
|
func (d *Daemon) HTTPRPCHandler() http.Handler {
|
2024-12-07 19:39:13 +00:00
|
|
|
handler := NewRPCHandler(d.logger.WithNamespace("rpc"), d)
|
|
|
|
return jsonrpc2.NewHTTPHandler(handler)
|
2024-07-13 14:08:13 +00:00
|
|
|
}
|