2019-07-08 18:51:55 +03:00
|
|
|
package table
|
2019-06-10 01:09:38 +03:00
|
|
|
|
2019-06-18 20:33:05 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-05 21:15:32 +03:00
|
|
|
"hash/fnv"
|
2019-06-18 20:33:05 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
)
|
2019-06-13 00:30:42 +03:00
|
|
|
|
2019-06-13 17:12:07 +03:00
|
|
|
var (
|
2019-07-09 17:45:42 +03:00
|
|
|
// DefaultLink is default network link
|
|
|
|
DefaultLink = "local"
|
2019-07-08 18:16:50 +03:00
|
|
|
// DefaultLocalMetric is default route cost metric for the local network
|
2019-06-13 17:12:07 +03:00
|
|
|
DefaultLocalMetric = 1
|
2019-07-08 18:16:50 +03:00
|
|
|
// DefaultNetworkMetric is default route cost metric for the micro network
|
2019-06-13 17:12:07 +03:00
|
|
|
DefaultNetworkMetric = 10
|
|
|
|
)
|
|
|
|
|
2019-06-19 23:22:14 +03:00
|
|
|
// Route is network route
|
|
|
|
type Route struct {
|
2019-07-09 17:45:42 +03:00
|
|
|
// Service is destination service name
|
|
|
|
Service string
|
|
|
|
// Address is service node address
|
|
|
|
Address string
|
2019-06-26 18:03:19 +03:00
|
|
|
// Gateway is route gateway
|
|
|
|
Gateway string
|
2019-07-08 18:16:50 +03:00
|
|
|
// Network is network address
|
2019-06-10 21:50:54 +03:00
|
|
|
Network string
|
2019-07-09 17:45:42 +03:00
|
|
|
// Link is network link
|
|
|
|
Link string
|
2019-06-19 23:22:14 +03:00
|
|
|
// Metric is the route cost metric
|
2019-06-10 01:09:38 +03:00
|
|
|
Metric int
|
|
|
|
}
|
2019-06-18 20:33:05 +03:00
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
// Hash returns route hash sum.
|
|
|
|
func (r *Route) Hash() uint64 {
|
|
|
|
h := fnv.New64()
|
|
|
|
h.Reset()
|
2019-07-09 17:45:42 +03:00
|
|
|
h.Write([]byte(r.Service + r.Address + r.Gateway + r.Network + r.Link))
|
2019-07-05 21:15:32 +03:00
|
|
|
|
|
|
|
return h.Sum64()
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// String returns human readable route
|
2019-07-05 21:15:32 +03:00
|
|
|
func (r Route) String() string {
|
2019-06-18 20:33:05 +03:00
|
|
|
// this will help us build routing table string
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
|
|
|
// create nice table printing structure
|
|
|
|
table := tablewriter.NewWriter(sb)
|
2019-07-09 17:45:42 +03:00
|
|
|
table.SetHeader([]string{"Service", "Address", "Gateway", "Network", "Link", "Metric"})
|
2019-06-18 20:33:05 +03:00
|
|
|
|
|
|
|
strRoute := []string{
|
2019-07-09 17:45:42 +03:00
|
|
|
r.Service,
|
|
|
|
r.Address,
|
2019-06-26 18:03:19 +03:00
|
|
|
r.Gateway,
|
2019-06-19 23:22:14 +03:00
|
|
|
r.Network,
|
2019-07-09 17:45:42 +03:00
|
|
|
r.Link,
|
2019-06-19 23:22:14 +03:00
|
|
|
fmt.Sprintf("%d", r.Metric),
|
2019-06-18 20:33:05 +03:00
|
|
|
}
|
|
|
|
table.Append(strRoute)
|
|
|
|
|
|
|
|
// render table into sb
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
return sb.String()
|
|
|
|
}
|