2019-06-06 18:37:40 +03:00
|
|
|
// Package router provides an interface for micro network routers
|
|
|
|
package router
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
var (
|
|
|
|
// DefaultRouter returns default micro router
|
|
|
|
DefaultRouter = NewRouter()
|
|
|
|
)
|
|
|
|
|
2019-06-06 18:37:40 +03:00
|
|
|
// Router is micro network router
|
|
|
|
type Router interface {
|
|
|
|
// Initi initializes Router with options
|
|
|
|
Init(...Option) error
|
|
|
|
// Options returns Router options
|
|
|
|
Options() Options
|
2019-06-07 01:29:24 +03:00
|
|
|
// Add adds new entry into routing table
|
|
|
|
Add(*Entry, ...RouteOption) error
|
|
|
|
// Remove removes entry from the routing table
|
|
|
|
Remove(*Entry) error
|
|
|
|
// Update updates entry in the routing table
|
|
|
|
Update(*Entry) error
|
|
|
|
// Lookup queries the routing table and returns matching entries
|
|
|
|
Lookup(Query) ([]*Entry, error)
|
|
|
|
// Table returns routing table
|
2019-06-07 15:29:09 +03:00
|
|
|
Table() Table
|
2019-06-07 01:29:24 +03:00
|
|
|
// Address is Router adddress
|
|
|
|
Address() string
|
2019-06-06 18:37:40 +03:00
|
|
|
// String implemens fmt.Stringer interface
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:29:09 +03:00
|
|
|
// RIB is Routing Information Base
|
|
|
|
type RIB interface {
|
|
|
|
// String returns debug info
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2019-06-06 18:37:40 +03:00
|
|
|
// Option used by the Router
|
|
|
|
type Option func(*Options)
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// RouteOption is used by Router for adding routing table entries
|
|
|
|
type RouteOption func(*RouteOptions)
|
2019-06-06 18:37:40 +03:00
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// QueryOption is used to defined routing table lookup query
|
|
|
|
type QueryOption func(*QueryOptions)
|
2019-06-06 18:37:40 +03:00
|
|
|
|
|
|
|
// NewRouter creates new Router and returns it
|
|
|
|
func NewRouter(opts ...Option) Router {
|
2019-06-07 15:29:09 +03:00
|
|
|
// set default options
|
2019-06-07 01:29:24 +03:00
|
|
|
ropts := Options{
|
2019-06-07 15:29:09 +03:00
|
|
|
Table: DefaultTable,
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
2019-06-07 01:29:24 +03:00
|
|
|
o(&ropts)
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return newRouter(opts...)
|
|
|
|
}
|