2019-06-07 01:29:24 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
import "errors"
|
|
|
|
|
|
|
|
var (
|
2019-06-07 15:29:09 +03:00
|
|
|
// DefaultRouter returns default micro router
|
|
|
|
DefaultTable = NewTable()
|
|
|
|
// ErrRouteNotFound is returned when no route was found
|
2019-06-07 01:29:24 +03:00
|
|
|
ErrRouteNotFound = errors.New("route not found")
|
2019-06-07 15:29:09 +03:00
|
|
|
// ErrDuplicateRoute is return when route already exists
|
|
|
|
ErrDuplicateRoute = errors.New("duplicate route")
|
2019-06-07 01:29:24 +03:00
|
|
|
)
|
|
|
|
|
2019-06-07 15:29:09 +03:00
|
|
|
// Table is routing table
|
|
|
|
type Table interface {
|
|
|
|
// Add adds new route to the table
|
|
|
|
Add(*Entry) error
|
|
|
|
// Remove removes route from the table
|
|
|
|
Remove(*Entry) error
|
|
|
|
// Update updates route in the table
|
|
|
|
Update(*Entry) error
|
|
|
|
// Lookup looks up routes in the table
|
|
|
|
Lookup(Query) ([]*Entry, error)
|
|
|
|
// Size returns the size of the table
|
|
|
|
Size() int
|
|
|
|
// String prints the routing table
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// Entry is micro network routing table entry
|
|
|
|
type Entry struct {
|
2019-06-07 15:29:09 +03:00
|
|
|
// DestAddr is destination address
|
|
|
|
DestAddr string
|
2019-06-07 01:29:24 +03:00
|
|
|
// NetID is micro network ID
|
|
|
|
NetID string
|
|
|
|
// Hop is the next route hop
|
|
|
|
Hop Router
|
|
|
|
// Metric is route cost metric
|
|
|
|
Metric int
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:29:09 +03:00
|
|
|
// table is routing table
|
2019-06-07 01:29:24 +03:00
|
|
|
// It maps service name to routes
|
2019-06-07 15:29:09 +03:00
|
|
|
type table struct {
|
2019-06-07 01:29:24 +03:00
|
|
|
// m stores routing table map
|
|
|
|
m map[string][]Entry
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTable creates new routing table and returns it
|
2019-06-07 15:29:09 +03:00
|
|
|
func NewTable() Table {
|
|
|
|
return &table{
|
2019-06-07 01:29:24 +03:00
|
|
|
m: make(map[string][]Entry),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:29:09 +03:00
|
|
|
// Add adds new routing entry
|
|
|
|
func (t *table) Add(e *Entry) error {
|
|
|
|
return nil
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes entry from the routing table
|
2019-06-07 15:29:09 +03:00
|
|
|
func (t *table) Remove(e *Entry) error {
|
2019-06-07 01:29:24 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates routin entry
|
2019-06-07 15:29:09 +03:00
|
|
|
func (t *table) Update(e *Entry) error {
|
2019-06-07 01:29:24 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:29:09 +03:00
|
|
|
// Lookup looks up entry in the routing table
|
|
|
|
func (t *table) Lookup(q Query) ([]*Entry, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// Size returns the size of the routing table
|
2019-06-07 15:29:09 +03:00
|
|
|
func (t *table) Size() int {
|
|
|
|
return len(t.m)
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns text representation of routing table
|
2019-06-07 15:29:09 +03:00
|
|
|
func (t *table) String() string {
|
2019-06-07 01:29:24 +03:00
|
|
|
return ""
|
|
|
|
}
|