Renamed variables, options and functions

This commit is contained in:
Milos Gajdos
2019-06-19 21:22:14 +01:00
parent 4e5fbbf7eb
commit 10a3636a9f
8 changed files with 158 additions and 260 deletions

View File

@@ -14,18 +14,18 @@ var (
DefaultNetworkMetric = 10
)
// AddPolicy defines routing table addition policy
type AddPolicy int
// RoutePolicy defines routing table addition policy
type RoutePolicy int
const (
// OverrideIfExists overrides route if it already exists
OverrideIfExists AddPolicy = iota
OverrideIfExists RoutePolicy = iota
// IgnoreIfExists does not modify existing route
IgnoreIfExists
)
// String returns human reprensentation of policy
func (p AddPolicy) String() string {
func (p RoutePolicy) String() string {
switch p {
case OverrideIfExists:
return "OVERRIDE"
@@ -36,100 +36,34 @@ func (p AddPolicy) String() string {
}
}
// RouteOption is used to set routing table entry options
type RouteOption func(*RouteOptions)
// RouteOptions are route options
type RouteOptions struct {
// DestAddr is destination address
DestAddr string
// Gateway is the next route hop
Gateway Router
// Network defines micro network
// Route is network route
type Route struct {
// Destination is destination address
Destination string
// Router is the network router
Router Router
// Network is micro network address
Network string
// Metric is route cost metric
// Metric is the route cost metric
Metric int
// Policy defines route addition policy
Policy AddPolicy
}
// DestAddr sets destination address
func DestAddr(a string) RouteOption {
return func(o *RouteOptions) {
o.DestAddr = a
}
}
// Gateway sets the route gateway
func Gateway(r Router) RouteOption {
return func(o *RouteOptions) {
o.Gateway = r
}
}
// Network sets micro network
func Network(n string) RouteOption {
return func(o *RouteOptions) {
o.Network = n
}
}
// Metric sets route metric
func Metric(m int) RouteOption {
return func(o *RouteOptions) {
o.Metric = m
}
}
// RoutePolicy sets add route policy
func RoutePolicy(p AddPolicy) RouteOption {
return func(o *RouteOptions) {
o.Policy = p
}
}
// Route is routing table route
type Route interface {
// Options returns route options
Options() RouteOptions
}
type route struct {
opts RouteOptions
}
// NewRoute returns new routing table route
func NewRoute(opts ...RouteOption) Route {
eopts := RouteOptions{}
for _, o := range opts {
o(&eopts)
}
return &route{
opts: eopts,
}
}
// Options returns route options
func (r *route) Options() RouteOptions {
return r.opts
// Policy defines route policy
Policy RoutePolicy
}
// String allows to print the route
func (r *route) String() string {
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", "Network", "Metric"})
table.SetHeader([]string{"Destination", "Router", "Network", "Metric"})
strRoute := []string{
r.opts.DestAddr,
r.opts.Gateway.Address(),
r.opts.Network,
fmt.Sprintf("%d", r.opts.Metric),
r.Destination,
r.Router.Address(),
r.Network,
fmt.Sprintf("%d", r.Metric),
}
table.Append(strRoute)