2019-09-05 02:16:22 +03:00
|
|
|
// Package handler implements network RPC handler
|
2019-09-02 13:42:45 +03:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-02 19:06:21 +03:00
|
|
|
"sort"
|
2019-09-02 13:42:45 +03:00
|
|
|
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListNodes returns a list of all accessible nodes in the network
|
|
|
|
func (n *Network) ListNodes(ctx context.Context, req *pbNet.ListRequest, resp *pbNet.ListResponse) error {
|
2019-09-02 19:06:21 +03:00
|
|
|
nodes := n.Network.Nodes()
|
|
|
|
|
|
|
|
var respNodes []*pbNet.Node
|
|
|
|
for _, node := range nodes {
|
|
|
|
respNode := &pbNet.Node{
|
|
|
|
Id: node.Id(),
|
|
|
|
Address: node.Address(),
|
|
|
|
}
|
|
|
|
respNodes = append(respNodes, respNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Nodes = respNodes
|
|
|
|
|
2019-09-02 13:42:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
// ListPeers returns a list of all the nodes the node has a direct link with
|
|
|
|
func (n *Network) ListPeers(ctx context.Context, req *pbNet.PeerRequest, resp *pbNet.PeerResponse) error {
|
2019-09-02 19:06:21 +03:00
|
|
|
// extract the id of the node to query
|
|
|
|
id := req.Id
|
|
|
|
// if no id is passed, we assume local node
|
|
|
|
if id == "" {
|
|
|
|
id = n.Network.Id()
|
|
|
|
}
|
|
|
|
|
|
|
|
// get all the nodes in the network
|
|
|
|
nodes := n.Network.Nodes()
|
|
|
|
|
2019-09-02 22:00:52 +03:00
|
|
|
// sort the slice of nodes
|
|
|
|
sort.Slice(nodes, func(i, j int) bool { return nodes[i].Id() <= nodes[j].Id() })
|
2019-09-02 19:06:21 +03:00
|
|
|
// find a node with a given id
|
2019-09-02 22:00:52 +03:00
|
|
|
i := sort.Search(len(nodes), func(j int) bool { return nodes[j].Id() >= id })
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
var nodePeers []*pbNet.Node
|
|
|
|
// collect all the node peers into slice
|
2019-09-02 19:06:21 +03:00
|
|
|
if i < len(nodes) && nodes[i].Id() == id {
|
2019-09-10 03:14:23 +03:00
|
|
|
for _, peer := range nodes[i].Peers() {
|
2019-09-03 18:39:27 +03:00
|
|
|
// don't return yourself in response
|
2019-09-10 03:14:23 +03:00
|
|
|
if peer.Id() == n.Network.Id() {
|
2019-09-03 18:39:27 +03:00
|
|
|
continue
|
2019-09-02 19:06:21 +03:00
|
|
|
}
|
2019-09-10 03:14:23 +03:00
|
|
|
pbPeer := &pbNet.Node{
|
|
|
|
Id: peer.Id(),
|
|
|
|
Address: peer.Address(),
|
2019-09-02 19:06:21 +03:00
|
|
|
}
|
2019-09-10 03:14:23 +03:00
|
|
|
nodePeers = append(nodePeers, pbPeer)
|
2019-09-02 19:06:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
// requested node
|
2019-09-03 18:39:27 +03:00
|
|
|
node := &pbNet.Node{
|
|
|
|
Id: nodes[i].Id(),
|
|
|
|
Address: nodes[i].Address(),
|
|
|
|
}
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
// creaate peers answer
|
|
|
|
peers := &pbNet.Peers{
|
|
|
|
Node: node,
|
|
|
|
Peers: nodePeers,
|
2019-09-03 18:39:27 +03:00
|
|
|
}
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
resp.Peers = peers
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListRoutes returns a list of routing table routes
|
|
|
|
func (n *Network) ListRoutes(ctx context.Context, req *pbRtr.Request, resp *pbRtr.ListResponse) error {
|
|
|
|
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 19:06:21 +03:00
|
|
|
|
2019-09-02 13:42:45 +03:00
|
|
|
return nil
|
|
|
|
}
|