interface.go 3.24 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2
// Package mocknet provides a mock net.Network to test with.
//
3
// - a Mocknet has many network.Networks
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4
// - a Mocknet has many Links
5 6
// - a Link joins two network.Networks
// - network.Conns and network.Streams are created by network.Networks
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 8 9
package mocknet

import (
Karthik Bala's avatar
Karthik Bala committed
10 11
	"io"
	"time"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12

tavit ohanian's avatar
tavit ohanian committed
13 14 15 16 17
	ic "gitlab.dms3.io/p2p/go-p2p-core/crypto"
	"gitlab.dms3.io/p2p/go-p2p-core/host"
	"gitlab.dms3.io/p2p/go-p2p-core/network"
	"gitlab.dms3.io/p2p/go-p2p-core/peer"
	"gitlab.dms3.io/p2p/go-p2p-core/peerstore"
Jeromy's avatar
Jeromy committed
18

19
	ma "gitlab.dms3.io/mf/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20 21 22 23
)

type Mocknet interface {

24
	// GenPeer generates a peer and its network.Network in the Mocknet
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25
	GenPeer() (host.Host, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26 27 28

	// AddPeer adds an existing peer. we need both a privkey and addr.
	// ID is derived from PrivKey
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29
	AddPeer(ic.PrivKey, ma.Multiaddr) (host.Host, error)
30
	AddPeerWithPeerstore(peer.ID, peerstore.Peerstore) (host.Host, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31 32 33

	// retrieve things (with randomized iteration order)
	Peers() []peer.ID
34 35
	Net(peer.ID) network.Network
	Nets() []network.Network
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36 37
	Host(peer.ID) host.Host
	Hosts() []host.Host
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39
	Links() LinkMap
	LinksBetweenPeers(a, b peer.ID) []Link
40
	LinksBetweenNets(a, b network.Network) []Link
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41 42 43 44 45 46 47

	// Links are the **ability to connect**.
	// think of Links as the physical medium.
	// For p1 and p2 to connect, a link must exist between them.
	// (this makes it possible to test dial failures, and
	// things like relaying traffic)
	LinkPeers(peer.ID, peer.ID) (Link, error)
48
	LinkNets(network.Network, network.Network) (Link, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49 50
	Unlink(Link) error
	UnlinkPeers(peer.ID, peer.ID) error
51
	UnlinkNets(network.Network, network.Network) error
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52 53

	// LinkDefaults are the default options that govern links
Christian Muehlhaeuser's avatar
Christian Muehlhaeuser committed
54
	// if they do not have their own option set.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55 56 57 58 59
	SetLinkDefaults(LinkOptions)
	LinkDefaults() LinkOptions

	// Connections are the usual. Connecting means Dialing.
	// **to succeed, peers must be linked beforehand**
60 61
	ConnectPeers(peer.ID, peer.ID) (network.Conn, error)
	ConnectNets(network.Network, network.Network) (network.Conn, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62
	DisconnectPeers(peer.ID, peer.ID) error
63
	DisconnectNets(network.Network, network.Network) error
Karthik Bala's avatar
Karthik Bala committed
64
	LinkAll() error
rht's avatar
rht committed
65
	ConnectAllButSelf() error
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
66 67 68 69 70 71
}

// LinkOptions are used to change aspects of the links.
// Sorry but they dont work yet :(
type LinkOptions struct {
	Latency   time.Duration
Karthik Bala's avatar
Karthik Bala committed
72
	Bandwidth float64 // in bytes-per-second
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74 75 76 77 78 79 80 81
	// we can make these values distributions down the road.
}

// Link represents the **possibility** of a connection between
// two peers. Think of it like physical network links. Without
// them, the peers can try and try but they won't be able to
// connect. This allows constructing topologies where specific
// nodes cannot talk to each other directly. :)
type Link interface {
82
	Networks() []network.Network
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	Peers() []peer.ID

	SetOptions(LinkOptions)
	Options() LinkOptions

	// Metrics() Metrics
}

// LinkMap is a 3D map to give us an easy way to track links.
// (wow, much map. so data structure. how compose. ahhh pointer)
type LinkMap map[string]map[string]map[Link]struct{}

// Printer lets you inspect things :)
type Printer interface {
	// MocknetLinks shows the entire Mocknet's link table :)
	MocknetLinks(mn Mocknet)
99
	NetworkConns(ni network.Network)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100 101 102 103 104 105
}

// PrinterTo returns a Printer ready to write to w.
func PrinterTo(w io.Writer) Printer {
	return &printer{w}
}