2019-09-10 01:01:41 +02:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2019-09-10 02:14:23 +02:00
|
|
|
"container/list"
|
2019-09-16 20:22:55 +02:00
|
|
|
"errors"
|
2019-09-10 01:01:41 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-12-10 13:46:13 +02:00
|
|
|
pb "github.com/micro/go-micro/network/service/proto"
|
2019-09-10 01:01:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// MaxDepth defines max depth of peer topology
|
2019-09-10 19:32:25 +02:00
|
|
|
MaxDepth uint = 3
|
2019-09-10 01:01:41 +02:00
|
|
|
)
|
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
var (
|
|
|
|
// ErrPeerExists is returned when adding a peer which already exists
|
|
|
|
ErrPeerExists = errors.New("peer already exists")
|
|
|
|
// ErrPeerNotFound is returned when a peer could not be found in node topology
|
|
|
|
ErrPeerNotFound = errors.New("peer not found")
|
|
|
|
)
|
|
|
|
|
2020-01-14 20:12:36 +02:00
|
|
|
// nerr tracks node errors
|
|
|
|
type nerr struct {
|
2020-01-14 00:22:12 +02:00
|
|
|
sync.RWMutex
|
|
|
|
count int
|
2020-01-14 20:12:36 +02:00
|
|
|
msg error
|
2020-01-14 00:22:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increment increments node error count
|
2020-01-14 20:12:36 +02:00
|
|
|
func (e *nerr) Update(err error) {
|
|
|
|
e.Lock()
|
|
|
|
defer e.Unlock()
|
2020-01-14 00:22:12 +02:00
|
|
|
|
2020-01-14 20:12:36 +02:00
|
|
|
e.count++
|
|
|
|
e.msg = err
|
2020-01-14 00:22:12 +02:00
|
|
|
}
|
|
|
|
|
2020-01-14 20:12:36 +02:00
|
|
|
// Count returns node error count
|
|
|
|
func (e *nerr) Count() int {
|
|
|
|
e.RLock()
|
|
|
|
defer e.RUnlock()
|
2020-01-14 00:22:12 +02:00
|
|
|
|
2020-01-14 20:12:36 +02:00
|
|
|
return e.count
|
2020-01-14 00:22:12 +02:00
|
|
|
}
|
|
|
|
|
2020-01-14 20:12:36 +02:00
|
|
|
func (e *nerr) Msg() string {
|
|
|
|
e.RLock()
|
|
|
|
defer e.RUnlock()
|
|
|
|
|
|
|
|
if e.msg != nil {
|
|
|
|
return e.msg.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
2020-01-14 00:22:12 +02:00
|
|
|
|
2020-01-14 20:12:36 +02:00
|
|
|
// status returns node status
|
|
|
|
type status struct {
|
|
|
|
sync.RWMutex
|
|
|
|
err *nerr
|
|
|
|
}
|
|
|
|
|
|
|
|
// newStatus creates
|
|
|
|
func newStatus() *status {
|
|
|
|
return &status{
|
|
|
|
err: new(nerr),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:48:42 +02:00
|
|
|
func newPeerStatus(peer *pb.Peer) *status {
|
|
|
|
return &status{
|
|
|
|
err: &nerr{
|
|
|
|
count: int(peer.Node.Status.Error.Count),
|
|
|
|
msg: errors.New(peer.Node.Status.Error.Msg),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:12:36 +02:00
|
|
|
func (s *status) Error() Error {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
return &nerr{
|
|
|
|
count: s.err.count,
|
|
|
|
msg: s.err.msg,
|
|
|
|
}
|
2020-01-14 00:22:12 +02:00
|
|
|
}
|
|
|
|
|
2019-09-10 01:01:41 +02:00
|
|
|
// node is network node
|
|
|
|
type node struct {
|
|
|
|
sync.RWMutex
|
|
|
|
// id is node id
|
|
|
|
id string
|
|
|
|
// address is node address
|
|
|
|
address string
|
2019-12-10 00:56:26 +02:00
|
|
|
// link on which we communicate with the peer
|
|
|
|
link string
|
2019-09-10 02:14:23 +02:00
|
|
|
// peers are nodes with direct link to this node
|
|
|
|
peers map[string]*node
|
2019-09-10 01:01:41 +02:00
|
|
|
// network returns the node network
|
|
|
|
network Network
|
2019-09-10 02:14:23 +02:00
|
|
|
// lastSeen keeps track of node lifetime and updates
|
2019-09-10 01:01:41 +02:00
|
|
|
lastSeen time.Time
|
2020-01-10 12:43:07 +02:00
|
|
|
// lastSync keeps track of node last sync request
|
|
|
|
lastSync time.Time
|
2020-01-14 20:12:36 +02:00
|
|
|
// err tracks node status
|
|
|
|
status *status
|
2019-09-10 01:01:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Id is node ide
|
|
|
|
func (n *node) Id() string {
|
|
|
|
return n.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns node address
|
|
|
|
func (n *node) Address() string {
|
|
|
|
return n.address
|
|
|
|
}
|
|
|
|
|
|
|
|
// Network returns node network
|
|
|
|
func (n *node) Network() Network {
|
|
|
|
return n.network
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:12:36 +02:00
|
|
|
// Status returns node status
|
|
|
|
func (n *node) Status() Status {
|
|
|
|
n.RLock()
|
|
|
|
defer n.RUnlock()
|
|
|
|
|
|
|
|
return &status{
|
|
|
|
err: &nerr{
|
|
|
|
count: n.status.err.count,
|
|
|
|
msg: n.status.err.msg,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
// walk walks the node graph until some condition is met
|
|
|
|
func (n *node) walk(until func(peer *node) bool, action func(parent, peer *node)) map[string]*node {
|
|
|
|
// track the visited nodes
|
|
|
|
visited := make(map[string]*node)
|
|
|
|
// queue of the nodes to visit
|
|
|
|
queue := list.New()
|
2019-09-13 04:02:20 +02:00
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
// push node to the back of queue
|
|
|
|
queue.PushBack(n)
|
|
|
|
// mark the node as visited
|
|
|
|
visited[n.id] = n
|
|
|
|
|
|
|
|
// keep iterating over the queue until its empty
|
|
|
|
for queue.Len() > 0 {
|
|
|
|
// pop the node from the front of the queue
|
|
|
|
qnode := queue.Front()
|
|
|
|
if until(qnode.Value.(*node)) {
|
|
|
|
return visited
|
|
|
|
}
|
|
|
|
// iterate through all of the node peers
|
|
|
|
// mark the visited nodes; enqueue the non-visted
|
|
|
|
for id, peer := range qnode.Value.(*node).peers {
|
2020-01-13 22:02:56 +02:00
|
|
|
action(qnode.Value.(*node), peer)
|
2019-09-16 20:22:55 +02:00
|
|
|
if _, ok := visited[id]; !ok {
|
|
|
|
visited[id] = peer
|
|
|
|
queue.PushBack(peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove the node from the queue
|
|
|
|
queue.Remove(qnode)
|
2019-09-13 04:02:20 +02:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
return visited
|
2019-09-13 04:02:20 +02:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
// AddPeer adds a new peer to node topology
|
|
|
|
// It returns false if the peer already exists
|
|
|
|
func (n *node) AddPeer(peer *node) error {
|
2019-09-13 04:02:20 +02:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
2020-01-14 20:12:36 +02:00
|
|
|
// get node topology: we need to check if the peer
|
|
|
|
// we are trying to add is already in our graph
|
|
|
|
top := n.getTopology(MaxDepth)
|
|
|
|
|
|
|
|
untilFoundPeer := func(n *node) bool {
|
|
|
|
return n.id == peer.id
|
|
|
|
}
|
|
|
|
|
|
|
|
justWalk := func(paent, node *node) {}
|
|
|
|
|
|
|
|
visited := top.walk(untilFoundPeer, justWalk)
|
|
|
|
|
|
|
|
peerNode, inTop := visited[peer.id]
|
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
if _, ok := n.peers[peer.id]; !ok {
|
2020-01-14 20:12:36 +02:00
|
|
|
if inTop {
|
|
|
|
// just create a new edge to the existing peer
|
|
|
|
// but make sure you update the peer link
|
|
|
|
peerNode.link = peer.link
|
|
|
|
n.peers[peer.id] = peerNode
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-13 04:02:20 +02:00
|
|
|
n.peers[peer.id] = peer
|
2019-09-16 20:22:55 +02:00
|
|
|
return nil
|
2019-09-13 04:02:20 +02:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
return ErrPeerExists
|
2019-09-13 04:02:20 +02:00
|
|
|
}
|
|
|
|
|
2019-09-15 13:16:08 +02:00
|
|
|
// DeletePeer deletes a peer from node peers
|
2019-09-16 20:22:55 +02:00
|
|
|
// It returns true if the peer has been deleted
|
2019-09-15 13:16:08 +02:00
|
|
|
func (n *node) DeletePeer(id string) bool {
|
2019-09-13 04:02:20 +02:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
delete(n.peers, id)
|
2019-09-15 13:16:08 +02:00
|
|
|
|
|
|
|
return true
|
2019-09-13 04:02:20 +02:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
// UpdatePeer updates a peer if it already exists
|
|
|
|
// It returns error if the peer does not exist
|
|
|
|
func (n *node) UpdatePeer(peer *node) error {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
if _, ok := n.peers[peer.id]; ok {
|
|
|
|
n.peers[peer.id] = peer
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-13 19:46:24 +02:00
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
return ErrPeerNotFound
|
2019-09-13 19:46:24 +02:00
|
|
|
}
|
|
|
|
|
2020-01-10 12:43:07 +02:00
|
|
|
// RefreshPeer updates node last seen timestamp
|
2019-09-13 19:46:24 +02:00
|
|
|
// It returns false if the peer has not been found.
|
2019-12-10 00:56:26 +02:00
|
|
|
func (n *node) RefreshPeer(id, link string, now time.Time) error {
|
2019-09-13 04:02:20 +02:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
peer, ok := n.peers[id]
|
|
|
|
if !ok {
|
2019-09-16 20:22:55 +02:00
|
|
|
return ErrPeerNotFound
|
2019-09-13 04:02:20 +02:00
|
|
|
}
|
|
|
|
|
2019-12-10 00:56:26 +02:00
|
|
|
// set peer link
|
|
|
|
peer.link = link
|
2019-12-13 17:27:47 +02:00
|
|
|
// set last seen
|
|
|
|
peer.lastSeen = now
|
2019-09-13 04:02:20 +02:00
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
return nil
|
2019-09-13 19:46:24 +02:00
|
|
|
}
|
|
|
|
|
2020-01-10 12:43:07 +02:00
|
|
|
// RefreshSync refreshes nodes sync time
|
|
|
|
func (n *node) RefreshSync(now time.Time) error {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
n.lastSync = now
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-15 13:16:08 +02:00
|
|
|
// Nodes returns a slice of all nodes in the whole node topology
|
2019-09-13 19:46:24 +02:00
|
|
|
func (n *node) Nodes() []Node {
|
|
|
|
// we need to freeze the network graph here
|
|
|
|
// otherwise we might get inconsisten results
|
|
|
|
n.RLock()
|
|
|
|
defer n.RUnlock()
|
|
|
|
|
|
|
|
// NOTE: this should never be true
|
2019-09-16 20:22:55 +02:00
|
|
|
untilNoMorePeers := func(node *node) bool {
|
|
|
|
return node == nil
|
2019-09-13 19:46:24 +02:00
|
|
|
}
|
2019-09-16 20:22:55 +02:00
|
|
|
justWalk := func(parent, node *node) {}
|
2019-09-13 19:46:24 +02:00
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
visited := n.walk(untilNoMorePeers, justWalk)
|
2019-09-13 19:46:24 +02:00
|
|
|
|
2019-11-11 02:03:51 +02:00
|
|
|
nodes := make([]Node, 0, len(visited))
|
2019-09-10 02:14:23 +02:00
|
|
|
// collect all the nodes and return them
|
|
|
|
for _, node := range visited {
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2019-09-15 13:16:08 +02:00
|
|
|
// GetPeerNode returns a node from node MaxDepth topology
|
|
|
|
// It returns nil if the peer was not found
|
2019-09-13 19:46:24 +02:00
|
|
|
func (n *node) GetPeerNode(id string) *node {
|
|
|
|
// get node topology up to MaxDepth
|
|
|
|
top := n.Topology(MaxDepth)
|
|
|
|
|
|
|
|
untilFoundPeer := func(n *node) bool {
|
|
|
|
return n.id == id
|
|
|
|
}
|
2019-09-16 20:22:55 +02:00
|
|
|
justWalk := func(paent, node *node) {}
|
2019-09-13 19:46:24 +02:00
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
visited := top.walk(untilFoundPeer, justWalk)
|
2019-09-13 19:46:24 +02:00
|
|
|
|
|
|
|
peerNode, ok := visited[id]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return peerNode
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
// DeletePeerNode removes peer node from node topology
|
|
|
|
func (n *node) DeletePeerNode(id string) error {
|
|
|
|
n.Lock()
|
2019-09-19 12:53:13 +02:00
|
|
|
defer n.Unlock()
|
2019-09-16 20:22:55 +02:00
|
|
|
|
|
|
|
untilNoMorePeers := func(node *node) bool {
|
|
|
|
return node == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
deleted := make(map[string]*node)
|
|
|
|
deletePeer := func(parent, node *node) {
|
|
|
|
if node.id != n.id && node.id == id {
|
|
|
|
delete(parent.peers, node.id)
|
|
|
|
deleted[node.id] = node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n.walk(untilNoMorePeers, deletePeer)
|
|
|
|
|
|
|
|
if _, ok := deleted[id]; !ok {
|
|
|
|
return ErrPeerNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-13 22:02:56 +02:00
|
|
|
// PrunePeer prunes the peers with the given id
|
|
|
|
func (n *node) PrunePeer(id string) {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
untilNoMorePeers := func(node *node) bool {
|
|
|
|
return node == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
prunePeer := func(parent, node *node) {
|
|
|
|
if node.id != n.id && node.id == id {
|
|
|
|
delete(parent.peers, node.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n.walk(untilNoMorePeers, prunePeer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PruneStalePeerNodes prunes the peers that have not been seen for longer than pruneTime
|
2019-09-16 20:22:55 +02:00
|
|
|
// It returns a map of the the nodes that got pruned
|
2019-12-07 21:54:29 +02:00
|
|
|
func (n *node) PruneStalePeers(pruneTime time.Duration) map[string]*node {
|
2019-09-16 20:22:55 +02:00
|
|
|
n.Lock()
|
2019-09-19 12:53:13 +02:00
|
|
|
defer n.Unlock()
|
2019-09-16 20:22:55 +02:00
|
|
|
|
|
|
|
untilNoMorePeers := func(node *node) bool {
|
|
|
|
return node == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pruned := make(map[string]*node)
|
|
|
|
pruneStalePeer := func(parent, node *node) {
|
|
|
|
if node.id != n.id && time.Since(node.lastSeen) > PruneTime {
|
|
|
|
delete(parent.peers, node.id)
|
|
|
|
pruned[node.id] = node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n.walk(untilNoMorePeers, pruneStalePeer)
|
|
|
|
|
|
|
|
return pruned
|
|
|
|
}
|
|
|
|
|
2019-10-18 12:23:28 +02:00
|
|
|
// getTopology traverses node graph and builds node topology
|
|
|
|
// NOTE: this function is not thread safe
|
|
|
|
func (n *node) getTopology(depth uint) *node {
|
2019-09-11 01:23:37 +02:00
|
|
|
// make a copy of yourself
|
|
|
|
node := &node{
|
2019-09-13 04:02:20 +02:00
|
|
|
id: n.id,
|
|
|
|
address: n.address,
|
|
|
|
peers: make(map[string]*node),
|
|
|
|
network: n.network,
|
2020-01-14 20:12:36 +02:00
|
|
|
status: n.status,
|
2019-09-13 04:02:20 +02:00
|
|
|
lastSeen: n.lastSeen,
|
2019-09-11 01:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// return if we reach requested depth or we have no more peers
|
|
|
|
if depth == 0 || len(n.peers) == 0 {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2019-09-12 00:03:27 +02:00
|
|
|
// decrement the depth
|
2019-09-11 01:23:37 +02:00
|
|
|
depth--
|
|
|
|
|
2019-09-12 00:03:27 +02:00
|
|
|
// iterate through our peers and update the node peers
|
2019-09-11 01:23:37 +02:00
|
|
|
for _, peer := range n.peers {
|
2019-10-18 12:23:28 +02:00
|
|
|
nodePeer := peer.getTopology(depth)
|
2019-09-12 00:03:27 +02:00
|
|
|
if _, ok := node.peers[nodePeer.id]; !ok {
|
|
|
|
node.peers[nodePeer.id] = nodePeer
|
|
|
|
}
|
2019-09-10 19:32:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-11 01:23:37 +02:00
|
|
|
return node
|
2019-09-10 19:32:25 +02:00
|
|
|
}
|
|
|
|
|
2019-10-18 12:23:28 +02:00
|
|
|
// Topology returns a copy of the node topology down to given depth
|
|
|
|
// NOTE: the returned node is a node graph - not a single node
|
|
|
|
func (n *node) Topology(depth uint) *node {
|
|
|
|
n.RLock()
|
|
|
|
defer n.RUnlock()
|
|
|
|
|
|
|
|
return n.getTopology(depth)
|
|
|
|
}
|
|
|
|
|
2019-09-13 19:46:24 +02:00
|
|
|
// Peers returns node peers up to MaxDepth
|
2019-09-12 00:03:27 +02:00
|
|
|
func (n *node) Peers() []Node {
|
|
|
|
n.RLock()
|
2019-09-13 19:46:24 +02:00
|
|
|
defer n.RUnlock()
|
|
|
|
|
2019-11-05 19:43:12 +02:00
|
|
|
peers := make([]Node, 0, len(n.peers))
|
2019-09-12 00:03:27 +02:00
|
|
|
for _, nodePeer := range n.peers {
|
2019-10-18 12:23:28 +02:00
|
|
|
peer := nodePeer.getTopology(MaxDepth)
|
2019-09-12 00:03:27 +02:00
|
|
|
peers = append(peers, peer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return peers
|
|
|
|
}
|
|
|
|
|
2019-09-13 04:02:20 +02:00
|
|
|
// UnpackPeerTopology unpacks pb.Peer into node topology of given depth
|
|
|
|
func UnpackPeerTopology(pbPeer *pb.Peer, lastSeen time.Time, depth uint) *node {
|
2019-09-10 02:14:23 +02:00
|
|
|
peerNode := &node{
|
2020-01-14 20:12:36 +02:00
|
|
|
id: pbPeer.Node.Id,
|
|
|
|
address: pbPeer.Node.Address,
|
|
|
|
peers: make(map[string]*node),
|
|
|
|
status: &status{
|
|
|
|
err: &nerr{
|
|
|
|
count: int(pbPeer.Node.Status.Error.Count),
|
|
|
|
msg: errors.New(pbPeer.Node.Status.Error.Msg),
|
|
|
|
},
|
|
|
|
},
|
2019-09-13 04:02:20 +02:00
|
|
|
lastSeen: lastSeen,
|
2019-09-10 01:01:41 +02:00
|
|
|
}
|
|
|
|
|
2019-09-10 02:14:23 +02:00
|
|
|
// return if have either reached the depth or have no more peers
|
|
|
|
if depth == 0 || len(pbPeer.Peers) == 0 {
|
|
|
|
return peerNode
|
2019-09-10 01:01:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// decrement the depth
|
|
|
|
depth--
|
|
|
|
|
2019-09-10 02:14:23 +02:00
|
|
|
peers := make(map[string]*node)
|
|
|
|
for _, pbPeer := range pbPeer.Peers {
|
2019-09-13 04:02:20 +02:00
|
|
|
peer := UnpackPeerTopology(pbPeer, lastSeen, depth)
|
2019-09-10 02:14:23 +02:00
|
|
|
peers[pbPeer.Node.Id] = peer
|
2019-09-10 01:01:41 +02:00
|
|
|
}
|
|
|
|
|
2019-09-10 02:14:23 +02:00
|
|
|
peerNode.peers = peers
|
2019-09-10 01:01:41 +02:00
|
|
|
|
2019-09-10 02:14:23 +02:00
|
|
|
return peerNode
|
2019-09-10 01:01:41 +02:00
|
|
|
}
|
|
|
|
|
2019-09-13 19:46:24 +02:00
|
|
|
func peerProtoTopology(peer Node, depth uint) *pb.Peer {
|
2019-09-12 00:56:57 +02:00
|
|
|
node := &pb.Node{
|
|
|
|
Id: peer.Id(),
|
|
|
|
Address: peer.Address(),
|
2020-01-14 20:12:36 +02:00
|
|
|
Status: &pb.Status{
|
|
|
|
Error: &pb.Error{
|
|
|
|
Count: uint32(peer.Status().Error().Count()),
|
|
|
|
Msg: peer.Status().Error().Msg(),
|
|
|
|
},
|
|
|
|
},
|
2019-09-12 00:56:57 +02:00
|
|
|
}
|
2019-09-11 19:56:28 +02:00
|
|
|
|
2019-10-14 23:26:23 +02:00
|
|
|
// set the network name if network is not nil
|
|
|
|
if peer.Network() != nil {
|
|
|
|
node.Network = peer.Network().Name()
|
|
|
|
}
|
|
|
|
|
2019-09-12 00:56:57 +02:00
|
|
|
pbPeers := &pb.Peer{
|
|
|
|
Node: node,
|
|
|
|
Peers: make([]*pb.Peer, 0),
|
2019-09-10 01:01:41 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 00:56:57 +02:00
|
|
|
// return if we reached the end of topology or depth
|
|
|
|
if depth == 0 || len(peer.Peers()) == 0 {
|
|
|
|
return pbPeers
|
|
|
|
}
|
2019-09-10 02:14:23 +02:00
|
|
|
|
2019-09-12 00:56:57 +02:00
|
|
|
// decrement the depth
|
|
|
|
depth--
|
2019-09-10 01:01:41 +02:00
|
|
|
|
2019-09-12 00:56:57 +02:00
|
|
|
// iterate through peers of peers aka pops
|
|
|
|
for _, pop := range peer.Peers() {
|
2019-09-13 19:46:24 +02:00
|
|
|
peer := peerProtoTopology(pop, depth)
|
2019-09-12 00:56:57 +02:00
|
|
|
pbPeers.Peers = append(pbPeers.Peers, peer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pbPeers
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeersToProto returns node peers graph encoded into protobuf
|
2019-09-13 04:02:20 +02:00
|
|
|
func PeersToProto(node Node, depth uint) *pb.Peer {
|
2019-09-12 00:56:57 +02:00
|
|
|
// network node aka root node
|
2019-09-13 04:02:20 +02:00
|
|
|
pbNode := &pb.Node{
|
|
|
|
Id: node.Id(),
|
|
|
|
Address: node.Address(),
|
2020-01-14 20:12:36 +02:00
|
|
|
Status: &pb.Status{
|
|
|
|
Error: &pb.Error{
|
|
|
|
Count: uint32(node.Status().Error().Count()),
|
|
|
|
Msg: node.Status().Error().Msg(),
|
|
|
|
},
|
|
|
|
},
|
2019-09-12 00:56:57 +02:00
|
|
|
}
|
2019-10-14 23:26:23 +02:00
|
|
|
|
|
|
|
// set the network name if network is not nil
|
|
|
|
if node.Network() != nil {
|
|
|
|
pbNode.Network = node.Network().Name()
|
|
|
|
}
|
|
|
|
|
2019-09-12 00:56:57 +02:00
|
|
|
// we will build proto topology into this
|
|
|
|
pbPeers := &pb.Peer{
|
2019-09-13 04:02:20 +02:00
|
|
|
Node: pbNode,
|
2019-09-12 00:56:57 +02:00
|
|
|
Peers: make([]*pb.Peer, 0),
|
|
|
|
}
|
|
|
|
|
2019-09-13 04:02:20 +02:00
|
|
|
for _, peer := range node.Peers() {
|
2019-09-13 19:46:24 +02:00
|
|
|
pbPeer := peerProtoTopology(peer, depth)
|
2019-09-12 00:56:57 +02:00
|
|
|
pbPeers.Peers = append(pbPeers.Peers, pbPeer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pbPeers
|
2019-09-10 01:01:41 +02:00
|
|
|
}
|