Route per service node. No Network Registry for now.

This commit is contained in:
Milos Gajdos
2019-06-26 16:03:19 +01:00
parent 4cad7697cc
commit fe84a2d726
6 changed files with 110 additions and 222 deletions

View File

@@ -1,11 +1,18 @@
package router
import (
"fmt"
"strings"
"github.com/olekukonko/tablewriter"
)
// LookupPolicy defines query policy
type LookupPolicy int
const (
// DiscardNoRoute discards query when no route is found
DiscardNoRoute LookupPolicy = iota
// DiscardIfNone discards query when no route is found
DiscardIfNone LookupPolicy = iota
// ClosestMatch returns closest match to supplied query
ClosestMatch
)
@@ -13,7 +20,7 @@ const (
// String returns human representation of LookupPolicy
func (lp LookupPolicy) String() string {
switch lp {
case DiscardNoRoute:
case DiscardIfNone:
return "DISCARD"
case ClosestMatch:
return "CLOSEST"
@@ -29,10 +36,10 @@ type QueryOption func(*QueryOptions)
type QueryOptions struct {
// Destination is destination address
Destination string
// Router is router address
Router string
// Network is network address
Network string
// Router is gateway address
Router Router
// Metric is route metric
Metric int
// Policy is query lookup policy
@@ -54,7 +61,7 @@ func QueryNetwork(a string) QueryOption {
}
// QueryRouter sets query gateway address
func QueryRouter(r Router) QueryOption {
func QueryRouter(r string) QueryOption {
return func(o *QueryOptions) {
o.Router = r
}
@@ -88,17 +95,14 @@ type query struct {
// NewQuery creates new query and returns it
func NewQuery(opts ...QueryOption) Query {
// default gateway for wildcard router
r := newRouter(ID("*"))
// default options
// NOTE: by default we use DefaultNetworkMetric
qopts := QueryOptions{
Destination: "*",
Router: "*",
Network: "*",
Router: r,
Metric: DefaultNetworkMetric,
Policy: DiscardNoRoute,
Policy: DiscardIfNone,
}
for _, o := range opts {
@@ -114,3 +118,27 @@ func NewQuery(opts ...QueryOption) Query {
func (q *query) Options() QueryOptions {
return q.opts
}
// String prints routing table query in human readable form
func (q query) String() string {
// this will help us build routing table string
sb := &strings.Builder{}
// create nice table printing structure
table := tablewriter.NewWriter(sb)
table.SetHeader([]string{"Destination", "Router", "Network", "Metric", "Policy"})
strQuery := []string{
q.opts.Destination,
q.opts.Router,
q.opts.Network,
fmt.Sprintf("%d", q.opts.Metric),
fmt.Sprintf("%s", q.opts.Policy),
}
table.Append(strQuery)
// render table into sb
table.Render()
return sb.String()
}