micro/network/router/route.go
Milos Gajdos 9d7420658d
Changed router interface. Added table watcher. Advertise routes
* Changed router interface to return Advertisement channel
* Added default gateway route to the routing table if supplied
* Watch table for updates and advertise to the network
* We hash the routes on 3-tuple (Destination, Gateway, Network)
2019-07-01 15:46:25 +01:00

82 lines
1.7 KiB
Go

package router
import (
"fmt"
"strings"
"github.com/olekukonko/tablewriter"
)
var (
// DefaultLocalMetric is default route cost for local network
DefaultLocalMetric = 1
// DefaultNetworkMetric is default route cost for micro network
DefaultNetworkMetric = 10
)
// RoutePolicy defines routing table addition policy
type RoutePolicy int
const (
// OverrideIfExists overrides route if it already exists
OverrideIfExists RoutePolicy = iota
// AddIfNotExist adds the route if it does not exist
AddIfNotExists
// IgnoreIfExists instructs to not modify existing route
IgnoreIfExists
)
// String returns human reprensentation of policy
func (p RoutePolicy) String() string {
switch p {
case OverrideIfExists:
return "OVERRIDE_IF_EXISTS"
case AddIfNotExists:
return "ADD_IF_NOT_EXISTS"
case IgnoreIfExists:
return "IGNORE_IF_EXISTS"
default:
return "UNKNOWN"
}
}
// Route is network route
type Route struct {
// Destination is destination address
Destination string
// Gateway is route gateway
Gateway string
// Router is the network router address
Router string
// Network is micro network address
Network string
// Metric is the route cost metric
Metric int
// Policy defines route policy
Policy RoutePolicy
}
// String allows to print the route
func (r *Route) 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", "Gateway", "Router", "Network", "Metric"})
strRoute := []string{
r.Destination,
r.Gateway,
r.Router,
r.Network,
fmt.Sprintf("%d", r.Metric),
}
table.Append(strRoute)
// render table into sb
table.Render()
return sb.String()
}