/ node.go
node.go
1 package main 2 3 import ( 4 "github.com/ethereum/go-ethereum/p2p" 5 ) 6 7 // Node represents single node information to be used in Graph. 8 type Node struct { 9 ID_ string `json:"id"` 10 Group_ int `json:"group"` 11 Weight_ int `json:"weight"` 12 13 meta *Metadata 14 } 15 16 // NewNode creates new Node object for the given peerinfo. 17 func NewNode(id, name string) *Node { 18 meta := NewMetadata(name) 19 return &Node{ 20 ID_: id, 21 Group_: group(meta), 22 Weight_: weight(meta), 23 24 meta: meta, 25 } 26 } 27 28 // IsClient returns true if node is identified as a mobile client, rather then server. 29 func (n *Node) IsClient() bool { 30 return n.meta.Name == "StatusIM" 31 } 32 33 // group returns group id based in server type. 34 func group(meta *Metadata) int { 35 switch meta.Name { 36 case "StatusIM": 37 return 1 38 case "Statusd": 39 return 2 40 default: 41 return 3 42 43 } 44 return 3 45 } 46 47 // weight returns weight based in server type. 48 func weight(meta *Metadata) int { 49 switch meta.Name { 50 case "StatusIM": 51 return 1 52 case "Statusd": 53 return 2 54 default: 55 return 1 56 57 } 58 return 1 59 } 60 61 // ClusterNode represents single cluster node information. 62 type ClusterNode struct { 63 IP string 64 ID string 65 Type string // name field in JSON (statusd or statusIM) 66 } 67 68 // NewClusterNode creates new Node object for the given peerinfo. 69 func NewClusterNode(ip string, peer *p2p.NodeInfo) *ClusterNode { 70 return &ClusterNode{ 71 IP: ip, 72 ID: peer.ID, 73 Type: peer.Name, 74 } 75 } 76 77 // PeersToNodes converts PeerInfo to Nodes. 78 func PeersToNodes(peers []*p2p.PeerInfo) ([]*Node, error) { 79 ret := make([]*Node, len(peers)) 80 for i, peer := range peers { 81 ret[i] = NewNode(peer.ID, peer.Name) 82 } 83 return ret, nil 84 } 85 86 // ID returns ID of the node. Satisfies graph.Node interface. 87 func (n *Node) ID() string { 88 return n.ID_ 89 } 90 91 // Group returns group of the node. Satisfies graph.GroupedNode interface. 92 func (n *Node) Group() int { 93 return n.Group_ 94 } 95 96 // Weight returns group of the node. Satisfies graph.WeightedNode interface. 97 func (n *Node) Weight() int { 98 return n.Weight_ 99 } 100 101 // Link represents link between two nodes. 102 type Link struct { 103 FromID, ToID string 104 } 105 106 // NewLinks creates link for the given IDs. 107 func NewLink(fromID, toID string) *Link { 108 return &Link{fromID, toID} 109 }