node.go (863B)
1 package debug 2 3 type Node struct { 4 Name string 5 Description string 6 conn []string 7 } 8 9 var ( 10 NodeIndex = make(map[string]Node) 11 MenuIndex = make(map[string]int) 12 ) 13 14 func (n *Node) haveConn(peer string) bool { 15 var v string 16 for _, v = range n.conn { 17 if peer == v { 18 return true 19 } 20 } 21 return false 22 } 23 24 func (n *Node) Connect(peer Node) bool { 25 var ok bool 26 _, ok = NodeIndex[peer.Name] 27 if !ok { 28 NodeIndex[peer.Name] = peer 29 } 30 if n.haveConn(peer.Name) { 31 return false 32 } 33 n.conn = append(n.conn, peer.Name) 34 return true 35 } 36 37 func (n *Node) String() string { 38 return n.Name 39 } 40 41 func (n *Node) Next() *Node { 42 if len(n.conn) == 0 { 43 return nil 44 } 45 r := NodeIndex[n.conn[0]] 46 n.conn = n.conn[1:] 47 return &r 48 } 49 50 func AddMenu(s string) int { 51 var ok bool 52 _, ok = MenuIndex[s] 53 if !ok { 54 MenuIndex[s] = 0 55 } 56 MenuIndex[s] += 1 57 return MenuIndex[s] 58 }