@cryptotaxi247 / kubo / commits / f1fc09cce

mocknet: printing internal mocknet state

Juan Batiz-Benet committed Dec 17, 2014 at 11:08 UTC f1fc09ccee7f10d98f554d9b02197f6177f3e768
2 files changed +45 -17
net/mock/interface.go
+24 -2
@@ -23,11 +23,10 @@ type Mocknet interface {
23 Peers() []peer.Peer
24 Net(peer.ID) inet.Network
25 Nets() []inet.Network
26 + Links() LinkMap
27 LinksBetweenPeers(a, b peer.Peer) []Link
28 LinksBetweenNets(a, b inet.Network) []Link
29
29 - PrintLinkMap(io.Writer)
30 -
30 // Links are the **ability to connect**.
31 // think of Links as the physical medium.
32 // For p1 and p2 to connect, a link must exist between them.
@@ -52,12 +51,19 @@ type Mocknet interface {
51 DisconnectNets(inet.Network, inet.Network) error
52 }
53
54 +// LinkOptions are used to change aspects of the links.
55 +// Sorry but they dont work yet :(
56 type LinkOptions struct {
57 Latency time.Duration
58 Bandwidth int // in bytes-per-second
59 // we can make these values distributions down the road.
60 }
61
62 +// Link represents the **possibility** of a connection between
63 +// two peers. Think of it like physical network links. Without
64 +// them, the peers can try and try but they won't be able to
65 +// connect. This allows constructing topologies where specific
66 +// nodes cannot talk to each other directly. :)
67 type Link interface {
68 Networks() []inet.Network
69 Peers() []peer.Peer
@@ -67,3 +73,19 @@ type Link interface {
73
74 // Metrics() Metrics
75 }
76 +
77 +// LinkMap is a 3D map to give us an easy way to track links.
78 +// (wow, much map. so data structure. how compose. ahhh pointer)
79 +type LinkMap map[string]map[string]map[Link]struct{}
80 +
81 +// Printer lets you inspect things :)
82 +type Printer interface {
83 + // MocknetLinks shows the entire Mocknet's link table :)
84 + MocknetLinks(mn Mocknet)
85 + NetworkConns(ni inet.Network)
86 +}
87 +
88 +// PrinterTo returns a Printer ready to write to w.
89 +func PrinterTo(w io.Writer) Printer {
90 + return &printer{w}
91 +}
net/mock/mock_net.go
+21 -15
@@ -2,7 +2,6 @@ package mocknet
2
3 import (
4 "fmt"
5 - "io"
5 "sync"
6
7 inet "github.com/jbenet/go-ipfs/net"
@@ -120,6 +119,27 @@ func (mn *mocknet) Nets() []inet.Network {
119 return cp
120 }
121
122 +// Links returns a copy of the internal link state map.
123 +// (wow, much map. so data structure. how compose. ahhh pointer)
124 +func (mn *mocknet) Links() LinkMap {
125 + mn.RLock()
126 + defer mn.RUnlock()
127 +
128 + links := map[string]map[string]map[Link]struct{}{}
129 + for p1, lm := range mn.links {
130 + sp1 := string(p1)
131 + links[sp1] = map[string]map[Link]struct{}{}
132 + for p2, ls := range lm {
133 + sp2 := string(p2)
134 + links[sp1][sp2] = map[Link]struct{}{}
135 + for l := range ls {
136 + links[sp1][sp2][l] = struct{}{}
137 + }
138 + }
139 + }
140 + return links
141 +}
142 +
143 func (mn *mocknet) LinkAll() error {
144 nets := mn.Nets()
145 for _, n1 := range nets {
@@ -309,17 +329,3 @@ func (mn *mocknet) LinkDefaults() LinkOptions {
329 defer mn.RUnlock()
330 return mn.linkDefaults
331 }
312 -
313 -func (mn *mocknet) PrintLinkMap(w io.Writer) {
314 - mn.RLock()
315 - defer mn.RUnlock()
316 -
317 - fmt.Fprintf(w, "Mocknet link map:\n")
318 - for p1, lm := range mn.links {
319 - fmt.Fprintf(w, "\t%s linked to:\n", peer.ID(p1))
320 - for p2, l := range lm {
321 - fmt.Fprintf(w, "\t\t%s (%d links)\n", peer.ID(p2), len(l))
322 - }
323 - }
324 - fmt.Fprintf(w, "\n")
325 -}