Documentation
¶
Index ¶
- type Clock
- type ClockController
- type FilterFunc
- type HostFactory
- type MapFunc
- type Op
- func (op Op) Bind(fn func(SelectFunc) Op) Op
- func (op Op) Err(ctx context.Context, hs Selection) (err error)
- func (op Op) ErrArgs(ctx context.Context, hs ...host.Host) error
- func (op Op) Eval(ctx context.Context, hs Selection) (out Selection, err error)
- func (op Op) EvalArgs(ctx context.Context, hs ...host.Host) (Selection, error)
- func (op Op) Go(f MapFunc) Op
- func (op Op) Map(f MapFunc) Op
- func (op Op) Must(ctx context.Context, hs Selection) Selection
- func (op Op) MustArgs(ctx context.Context, hs ...host.Host) Selection
- func (op Op) Then(f SelectFunc) Op
- type Option
- type Partition
- type SelectFunc
- type Selection
- type Simulation
- func (s Simulation) Clock() Clock
- func (s Simulation) MustHost(ctx context.Context, opt ...config.Option) host.Host
- func (s Simulation) MustHostSet(ctx context.Context, n int, opt ...config.Option) Selection
- func (s Simulation) NewDiscovery(h host.Host, t netsim.Topology) *netsim.DiscoveryService
- func (s Simulation) NewHost(ctx context.Context, opt ...config.Option) (host.Host, error)
- func (s Simulation) NewHostSet(ctx context.Context, n int, opt ...config.Option) (Selection, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Clock ¶
type Clock interface {
// Timestep returns the increment with which the clock is
// advanced at each time-step. This is effectively the
// temporal precision with which events are simulated.
Timestep() time.Duration
// After is analogous to time.After in the Go standard library.
// The supplied function will be called exactly once after the
// specified duration has elapsed, unless 'cancel' is called.
After(time.Duration, func()) (cancel func())
// Ticker is analogous to time.NewTicker in the Go standard
// library. The supplied function will be called periodically,
// until 'cancel' is called.
Ticker(time.Duration, func()) (cancel func())
}
type ClockController ¶
type HostFactory ¶
type MapFunc ¶
type Op ¶
type Op func(Op) (SelectFunc, Op)
func Filter ¶
func Filter(f FilterFunc) Op
Filter returns a new selection that contains the elements of the current selection for which f(element) == true.
func Select ¶
func Select(f SelectFunc) Op
func (Op) Then ¶
func (op Op) Then(f SelectFunc) Op
type Option ¶
type Option func(sim *Simulation)
func WithClock ¶
func WithClock(c ClockController) Option
WithClock sets the simulation clock. If c == nil, a default clock with 10ms accuracy is used.
func WithHostFactory ¶
func WithHostFactory(f HostFactory) Option
WithHostFactory sets the host factory for the simulation. If f == nil, a default factory is used that constructs hosts using an in-process transport.
func WithNamespaceFactory ¶
func WithNamespaceFactory(f func(Clock) netsim.NamespaceProvider) Option
WithNamespaceFactory sets the namespace implementation for the simulation. If ns == nil, a default namespace implementation is used.
type Partition ¶
type Partition int
NewPartition returns a partition of n subsets based on the index number of the current selection.
type SelectFunc ¶
func Fail ¶
func Fail(err error) SelectFunc
Fail aborts the Op pipeline and returns the supplied error.
func Just ¶
func Just(hs Selection) SelectFunc
Just discards the current selection and replaces it with hs.
func (SelectFunc) Bind ¶
func (f SelectFunc) Bind(fn SelectFunc) Op
type Selection ¶
Selection is a set of hosts.
func (Selection) Filter ¶
func (hs Selection) Filter(f FilterFunc) Selection
type Simulation ¶
type Simulation struct {
// contains filtered or unexported fields
}
Example ¶
package main
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"github.com/libp2p/go-libp2p-core/network"
mx "github.com/wetware/matrix/pkg"
"github.com/wetware/matrix/pkg/netsim"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
const (
ns = "matrix.test"
echo = "/echo"
)
var (
sim = mx.New(ctx)
h0 = sim.MustHost(ctx)
h1 = sim.MustHost(ctx)
)
h0.SetStreamHandler(echo, func(s network.Stream) {
defer s.Close()
if _, err := io.Copy(s, s); err != nil {
panic(err)
}
})
mx.Topology(sim, netsim.SelectRing{}, ns).
MustArgs(ctx, h0, h1)
s, err := h1.NewStream(ctx, h0.ID(), echo)
if err != nil {
panic(err)
}
defer s.Close()
_, err = io.Copy(s, strings.NewReader("Hello, world!"))
if err != nil {
panic(err)
}
s.CloseWrite()
var buf bytes.Buffer
_, err = io.Copy(&buf, s)
if err != nil {
panic(err)
}
fmt.Println(buf.String())
}
Output: Hello, world!
func (Simulation) Clock ¶
func (s Simulation) Clock() Clock
func (Simulation) MustHostSet ¶
MustHostSet calls NewHostSet with the supplied parameters and panics if an error is encountered.
func (Simulation) NewDiscovery ¶
func (s Simulation) NewDiscovery(h host.Host, t netsim.Topology) *netsim.DiscoveryService
NewDiscovery returns a discovery.Discovery implementation that supports the Simulation's in-process network.
The topology parameter t can be used to specify an initial connection topology. All peers must use the same instance of t in order to obtain the desired topology.
If t == nil, the topology defaults to netsim.SelectAll.
func (Simulation) NewHost ¶
NewHost assembles and creates a new libp2p host that uses the simulation's network.
The simulation configures hosts to use an in-process network, overriding the following options:
- libp2p.Transport - libp2p.NoTransports - libp2p.ListenAddr - libp2p.ListenAddrStrings - libp2p.NoListenAddrs
Users SHOULD NOT pass any of the above options to NewHost.
func (Simulation) NewHostSet ¶
NewHostSet builds and configures n hosts with identical parameters.
See NewHost.