2019-09-05 00:16:22 +01:00
|
|
|
// Package handler implements network RPC handler
|
2019-09-02 11:42:45 +01:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/errors"
|
|
|
|
"github.com/micro/go-micro/network"
|
|
|
|
pbNet "github.com/micro/go-micro/network/proto"
|
|
|
|
pbRtr "github.com/micro/go-micro/router/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Network implements network handler
|
|
|
|
type Network struct {
|
|
|
|
Network network.Network
|
|
|
|
}
|
|
|
|
|
2019-09-23 15:41:05 +01:00
|
|
|
func flatten(n network.Node, visited map[string]bool) []network.Node {
|
|
|
|
// if node is nil runaway
|
|
|
|
if n == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// set visisted
|
|
|
|
if visited == nil {
|
|
|
|
visited = make(map[string]bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if already visited
|
|
|
|
if visited[n.Id()] == true {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new list of nodes
|
|
|
|
var nodes []network.Node
|
|
|
|
|
|
|
|
// append the current node
|
|
|
|
nodes = append(nodes, n)
|
|
|
|
|
|
|
|
// set to visited
|
|
|
|
visited[n.Id()] = true
|
|
|
|
|
|
|
|
// visit the list of peers
|
|
|
|
for _, node := range n.Peers() {
|
|
|
|
nodes = append(nodes, flatten(node, visited)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nodes returns the list of nodes
|
|
|
|
func (n *Network) Nodes(ctx context.Context, req *pbNet.NodesRequest, resp *pbNet.NodesResponse) error {
|
|
|
|
depth := uint(req.Depth)
|
|
|
|
if depth <= 0 || depth > network.MaxDepth {
|
|
|
|
depth = network.MaxDepth
|
|
|
|
}
|
|
|
|
|
|
|
|
// root node
|
|
|
|
nodes := map[string]network.Node{}
|
|
|
|
|
|
|
|
// get peers encoded into protobuf
|
|
|
|
peers := flatten(n.Network, nil)
|
|
|
|
|
|
|
|
// walk all the peers
|
|
|
|
for _, peer := range peers {
|
|
|
|
if peer == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := nodes[peer.Id()]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// add to visited list
|
|
|
|
nodes[n.Network.Id()] = peer
|
|
|
|
|
|
|
|
resp.Nodes = append(resp.Nodes, &pbNet.Node{
|
|
|
|
Id: peer.Id(),
|
|
|
|
Address: peer.Address(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Graph returns the network graph from this root node
|
|
|
|
func (n *Network) Graph(ctx context.Context, req *pbNet.GraphRequest, resp *pbNet.GraphResponse) error {
|
2019-09-11 23:03:27 +01:00
|
|
|
depth := uint(req.Depth)
|
|
|
|
if depth <= 0 || depth > network.MaxDepth {
|
|
|
|
depth = network.MaxDepth
|
2019-09-02 17:06:21 +01:00
|
|
|
}
|
|
|
|
|
2019-09-11 23:56:57 +01:00
|
|
|
// get peers encoded into protobuf
|
2019-09-13 03:02:20 +01:00
|
|
|
peers := network.PeersToProto(n.Network, depth)
|
2019-09-11 23:03:27 +01:00
|
|
|
|
2019-09-23 15:41:05 +01:00
|
|
|
// set the root node
|
|
|
|
resp.Root = peers
|
2019-09-10 01:14:23 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-23 15:41:05 +01:00
|
|
|
// Routes returns a list of routing table routes
|
|
|
|
func (n *Network) Routes(ctx context.Context, req *pbNet.RoutesRequest, resp *pbNet.RoutesResponse) error {
|
2019-09-10 01:14:23 +01:00
|
|
|
routes, err := n.Network.Options().Router.Table().List()
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.network", "failed to list routes: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var respRoutes []*pbRtr.Route
|
|
|
|
for _, route := range routes {
|
|
|
|
respRoute := &pbRtr.Route{
|
|
|
|
Service: route.Service,
|
|
|
|
Address: route.Address,
|
|
|
|
Gateway: route.Gateway,
|
|
|
|
Network: route.Network,
|
|
|
|
Router: route.Router,
|
|
|
|
Link: route.Link,
|
|
|
|
Metric: int64(route.Metric),
|
|
|
|
}
|
|
|
|
respRoutes = append(respRoutes, respRoute)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Routes = respRoutes
|
2019-09-02 17:06:21 +01:00
|
|
|
|
2019-09-02 11:42:45 +01:00
|
|
|
return nil
|
|
|
|
}
|