github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/p2p/net/mock/interface.go (about) 1 // Package mocknet provides a mock net.Network to test with. 2 // 3 // - a Mocknet has many inet.Networks 4 // - a Mocknet has many Links 5 // - a Link joins two inet.Networks 6 // - inet.Conns and inet.Streams are created by inet.Networks 7 package mocknet 8 9 import ( 10 ic "github.com/ipfs/go-ipfs/p2p/crypto" 11 host "github.com/ipfs/go-ipfs/p2p/host" 12 inet "github.com/ipfs/go-ipfs/p2p/net" 13 peer "github.com/ipfs/go-ipfs/p2p/peer" 14 "io" 15 "time" 16 17 ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" 18 ) 19 20 type Mocknet interface { 21 22 // GenPeer generates a peer and its inet.Network in the Mocknet 23 GenPeer() (host.Host, error) 24 25 // AddPeer adds an existing peer. we need both a privkey and addr. 26 // ID is derived from PrivKey 27 AddPeer(ic.PrivKey, ma.Multiaddr) (host.Host, error) 28 AddPeerWithPeerstore(peer.ID, peer.Peerstore) (host.Host, error) 29 30 // retrieve things (with randomized iteration order) 31 Peers() []peer.ID 32 Net(peer.ID) inet.Network 33 Nets() []inet.Network 34 Host(peer.ID) host.Host 35 Hosts() []host.Host 36 Links() LinkMap 37 LinksBetweenPeers(a, b peer.ID) []Link 38 LinksBetweenNets(a, b inet.Network) []Link 39 40 // Links are the **ability to connect**. 41 // think of Links as the physical medium. 42 // For p1 and p2 to connect, a link must exist between them. 43 // (this makes it possible to test dial failures, and 44 // things like relaying traffic) 45 LinkPeers(peer.ID, peer.ID) (Link, error) 46 LinkNets(inet.Network, inet.Network) (Link, error) 47 Unlink(Link) error 48 UnlinkPeers(peer.ID, peer.ID) error 49 UnlinkNets(inet.Network, inet.Network) error 50 51 // LinkDefaults are the default options that govern links 52 // if they do not have thier own option set. 53 SetLinkDefaults(LinkOptions) 54 LinkDefaults() LinkOptions 55 56 // Connections are the usual. Connecting means Dialing. 57 // **to succeed, peers must be linked beforehand** 58 ConnectPeers(peer.ID, peer.ID) (inet.Conn, error) 59 ConnectNets(inet.Network, inet.Network) (inet.Conn, error) 60 DisconnectPeers(peer.ID, peer.ID) error 61 DisconnectNets(inet.Network, inet.Network) error 62 LinkAll() error 63 ConnectAllButSelf() error 64 } 65 66 // LinkOptions are used to change aspects of the links. 67 // Sorry but they dont work yet :( 68 type LinkOptions struct { 69 Latency time.Duration 70 Bandwidth float64 // in bytes-per-second 71 // we can make these values distributions down the road. 72 } 73 74 // Link represents the **possibility** of a connection between 75 // two peers. Think of it like physical network links. Without 76 // them, the peers can try and try but they won't be able to 77 // connect. This allows constructing topologies where specific 78 // nodes cannot talk to each other directly. :) 79 type Link interface { 80 Networks() []inet.Network 81 Peers() []peer.ID 82 83 SetOptions(LinkOptions) 84 Options() LinkOptions 85 86 // Metrics() Metrics 87 } 88 89 // LinkMap is a 3D map to give us an easy way to track links. 90 // (wow, much map. so data structure. how compose. ahhh pointer) 91 type LinkMap map[string]map[string]map[Link]struct{} 92 93 // Printer lets you inspect things :) 94 type Printer interface { 95 // MocknetLinks shows the entire Mocknet's link table :) 96 MocknetLinks(mn Mocknet) 97 NetworkConns(ni inet.Network) 98 } 99 100 // PrinterTo returns a Printer ready to write to w. 101 func PrinterTo(w io.Writer) Printer { 102 return &printer{w} 103 }