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"
|
|
|
|
|
|
|
|
"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-12 01:03:27 +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 {
|
|
|
|
depth := uint(req.Depth)
|
|
|
|
if depth <= 0 || depth > network.MaxDepth {
|
|
|
|
depth = network.MaxDepth
|
2019-09-02 19:06:21 +03:00
|
|
|
}
|
|
|
|
|
2019-09-12 01:56:57 +03:00
|
|
|
// get peers encoded into protobuf
|
2019-09-13 05:02:20 +03:00
|
|
|
peers := network.PeersToProto(n.Network, depth)
|
2019-09-12 01:03:27 +03:00
|
|
|
|
|
|
|
resp.Peers = peers
|
2019-09-10 03:14:23 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|