2019-06-21 16:17:12 +01:00
|
|
|
// Package router provides a network routing control plane
|
2019-06-06 16:37:40 +01:00
|
|
|
package router
|
|
|
|
|
2019-06-28 11:53:55 +01:00
|
|
|
import "time"
|
|
|
|
|
2019-06-27 22:52:51 +01:00
|
|
|
var (
|
|
|
|
// DefaultRouter is default network router
|
|
|
|
DefaultRouter = NewRouter()
|
|
|
|
)
|
|
|
|
|
2019-06-21 16:17:12 +01:00
|
|
|
// Router is an interface for a routing control plane
|
2019-06-06 16:37:40 +01:00
|
|
|
type Router interface {
|
2019-06-12 22:30:42 +01:00
|
|
|
// Init initializes the router with options
|
2019-06-06 16:37:40 +01:00
|
|
|
Init(...Option) error
|
2019-06-12 22:30:42 +01:00
|
|
|
// Options returns the router options
|
2019-06-06 16:37:40 +01:00
|
|
|
Options() Options
|
2019-06-20 13:04:58 +01:00
|
|
|
// ID returns the id of the router
|
2019-06-16 23:09:59 +01:00
|
|
|
ID() string
|
2019-06-20 13:04:58 +01:00
|
|
|
// Table returns the routing table
|
2019-06-07 13:29:09 +01:00
|
|
|
Table() Table
|
2019-06-13 12:09:49 +01:00
|
|
|
// Address returns the router adddress
|
2019-06-06 23:29:24 +01:00
|
|
|
Address() string
|
2019-06-20 13:04:58 +01:00
|
|
|
// Network returns the network address of the router
|
2019-06-07 17:20:22 +01:00
|
|
|
Network() string
|
2019-06-27 22:52:51 +01:00
|
|
|
// Advertise starts advertising routes to the network
|
2019-06-28 11:53:55 +01:00
|
|
|
Advertise() (<-chan *Update, error)
|
2019-06-27 22:52:51 +01:00
|
|
|
// Update updates the routing table
|
2019-06-28 11:53:55 +01:00
|
|
|
Update(*Update) error
|
2019-06-28 18:35:53 +01:00
|
|
|
// Status returns router status
|
|
|
|
Status() Status
|
2019-06-12 22:30:42 +01:00
|
|
|
// Stop stops the router
|
2019-06-11 23:59:25 +01:00
|
|
|
Stop() error
|
2019-06-07 13:29:09 +01:00
|
|
|
// String returns debug info
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2019-06-28 11:53:55 +01:00
|
|
|
// Update is sent by the router to the network
|
|
|
|
type Update struct {
|
2019-06-28 18:35:53 +01:00
|
|
|
// ID is the router ID
|
2019-06-27 22:52:51 +01:00
|
|
|
ID string
|
2019-06-28 11:53:55 +01:00
|
|
|
// Timestamp marks the time when update is sent
|
|
|
|
Timestamp time.Time
|
2019-06-27 22:52:51 +01:00
|
|
|
// Event defines advertisement even
|
|
|
|
Event *Event
|
|
|
|
}
|
|
|
|
|
2019-06-28 18:35:53 +01:00
|
|
|
// StatusCode defines router status
|
|
|
|
type StatusCode int
|
|
|
|
|
|
|
|
// Status is router status
|
|
|
|
type Status struct {
|
|
|
|
// Error is router error
|
|
|
|
Error error
|
|
|
|
// Code defines router status
|
|
|
|
Code StatusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2019-06-29 00:46:22 +01:00
|
|
|
// Init means the rotuer has just been initialized
|
|
|
|
Init StatusCode = iota
|
|
|
|
// Running means the router is running
|
|
|
|
Running
|
2019-06-28 18:35:53 +01:00
|
|
|
// Error means the router has crashed with error
|
|
|
|
Error
|
|
|
|
// Stopped means the router has stopped
|
|
|
|
Stopped
|
|
|
|
)
|
|
|
|
|
|
|
|
// String returns human readable status code
|
|
|
|
func (sc StatusCode) String() string {
|
|
|
|
switch sc {
|
2019-06-29 00:46:22 +01:00
|
|
|
case Init:
|
|
|
|
return "INITIALIZED"
|
2019-06-28 18:35:53 +01:00
|
|
|
case Running:
|
|
|
|
return "RUNNING"
|
|
|
|
case Error:
|
|
|
|
return "ERROR"
|
|
|
|
case Stopped:
|
|
|
|
return "STOPPED"
|
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 23:09:38 +01:00
|
|
|
// Option used by the router
|
2019-06-06 16:37:40 +01:00
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
// NewRouter creates new Router and returns it
|
|
|
|
func NewRouter(opts ...Option) Router {
|
|
|
|
return newRouter(opts...)
|
|
|
|
}
|