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

import (
10
	"io"
Juan Batiz-Benet's avatar
mock2  
Juan Batiz-Benet committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
	"time"

	inet "github.com/jbenet/go-ipfs/net"
	peer "github.com/jbenet/go-ipfs/peer"
)

type Mocknet interface {
	GenPeer() (inet.Network, error)
	AddPeer(peer.ID) (inet.Network, error)

	// retrieve things
	Peer(peer.ID) peer.Peer
	Peers() []peer.Peer
	Net(peer.ID) inet.Network
	Nets() []inet.Network
	LinksBetweenPeers(a, b peer.Peer) []Link
	LinksBetweenNets(a, b inet.Network) []Link

29 30
	PrintLinkMap(io.Writer)

Juan Batiz-Benet's avatar
mock2  
Juan Batiz-Benet committed
31 32 33 34 35 36 37 38 39 40 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.Peer, peer.Peer) (Link, error)
	LinkNets(inet.Network, inet.Network) (Link, error)
	Unlink(Link) error
	UnlinkPeers(peer.Peer, peer.Peer) error
	UnlinkNets(inet.Network, inet.Network) error

	// LinkDefaults are the default options that govern links
	// if they do not have thier own option set.
	SetLinkDefaults(LinkOptions)
	LinkDefaults() LinkOptions

	// Connections are the usual. Connecting means Dialing.
48
	// **to succeed, peers must be linked beforehand**
Juan Batiz-Benet's avatar
mock2  
Juan Batiz-Benet committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	ConnectPeers(peer.Peer, peer.Peer) error
	ConnectNets(inet.Network, inet.Network) error
	DisconnectPeers(peer.Peer, peer.Peer) error
	DisconnectNets(inet.Network, inet.Network) error
}

type LinkOptions struct {
	Latency   time.Duration
	Bandwidth int // in bytes-per-second
	// we can make these values distributions down the road.
}

type Link interface {
	Networks() []inet.Network
	Peers() []peer.Peer

	SetOptions(LinkOptions)
	Options() LinkOptions

	// Metrics() Metrics
}