2019-06-10 01:09:38 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
// AddPolicy defines routing table addition policy
|
|
|
|
type AddPolicy int
|
|
|
|
|
|
|
|
const (
|
2019-06-10 15:34:23 +03:00
|
|
|
// Override overrides existing routing table route
|
2019-06-10 01:09:38 +03:00
|
|
|
OverrideIfExists AddPolicy = iota
|
2019-06-10 15:34:23 +03:00
|
|
|
// ErrIfExists returns error if the route already exists
|
2019-06-10 01:09:38 +03:00
|
|
|
ErrIfExists
|
|
|
|
)
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
// RouteOptions defines micro network routing table route options
|
|
|
|
type RouteOptions struct {
|
2019-06-10 01:09:38 +03:00
|
|
|
// DestAddr is destination address
|
|
|
|
DestAddr string
|
2019-06-12 01:59:25 +03:00
|
|
|
// Gateway is the next route hop
|
|
|
|
Gateway Router
|
2019-06-10 21:50:54 +03:00
|
|
|
// Network defines micro network
|
|
|
|
Network string
|
2019-06-10 01:09:38 +03:00
|
|
|
// Metric is route cost metric
|
|
|
|
Metric int
|
2019-06-10 15:34:23 +03:00
|
|
|
// Policy defines route addition policy
|
2019-06-10 01:09:38 +03:00
|
|
|
Policy AddPolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
// DestAddr sets destination address
|
2019-06-10 15:34:23 +03:00
|
|
|
func DestAddr(a string) RouteOption {
|
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 01:09:38 +03:00
|
|
|
o.DestAddr = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
// Gateway sets the route gateway
|
|
|
|
func Gateway(r Router) RouteOption {
|
2019-06-10 15:34:23 +03:00
|
|
|
return func(o *RouteOptions) {
|
2019-06-12 01:59:25 +03:00
|
|
|
o.Gateway = r
|
2019-06-10 01:09:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 21:50:54 +03:00
|
|
|
// Network sets micro network
|
|
|
|
func Network(n string) RouteOption {
|
2019-06-10 15:34:23 +03:00
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 21:50:54 +03:00
|
|
|
o.Network = n
|
2019-06-10 01:09:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
// Metric sets route metric
|
|
|
|
func Metric(m int) RouteOption {
|
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 01:09:38 +03:00
|
|
|
o.Metric = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
// RoutePolicy sets add route policy
|
|
|
|
func RoutePolicy(p AddPolicy) RouteOption {
|
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 01:09:38 +03:00
|
|
|
o.Policy = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
// Route is routing table route
|
|
|
|
type Route interface {
|
|
|
|
// Options returns route options
|
|
|
|
Options() RouteOptions
|
2019-06-10 01:09:38 +03:00
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
type route struct {
|
|
|
|
opts RouteOptions
|
2019-06-10 01:09:38 +03:00
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
// NewRoute returns new routing table route
|
|
|
|
func NewRoute(opts ...RouteOption) Route {
|
|
|
|
eopts := RouteOptions{}
|
2019-06-10 01:09:38 +03:00
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&eopts)
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
return &route{
|
2019-06-10 01:09:38 +03:00
|
|
|
opts: eopts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
// Options returns route options
|
|
|
|
func (r *route) Options() RouteOptions {
|
|
|
|
return r.opts
|
2019-06-10 01:09:38 +03:00
|
|
|
}
|