Add Solicit method to router interface
When calling Solicit, router lists all the routes and advertise them straight away
This commit is contained in:
parent
a1ba1482c5
commit
9161b20d6b
@ -602,21 +602,10 @@ func (r *router) Advertise() (<-chan *Advert, error) {
|
|||||||
r.subscribers[uuid.New().String()] = advertChan
|
r.subscribers[uuid.New().String()] = advertChan
|
||||||
return advertChan, nil
|
return advertChan, nil
|
||||||
case Running:
|
case Running:
|
||||||
// list routing table routes to announce
|
// list all the routes and pack them into even slice to advertise
|
||||||
routes, err := r.table.List()
|
events, err := r.flushRouteEvents()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed listing routes: %s", err)
|
return nil, fmt.Errorf("failed to advertise routes: %s", err)
|
||||||
}
|
|
||||||
|
|
||||||
// collect all the added routes before we attempt to add default gateway
|
|
||||||
events := make([]*Event, len(routes))
|
|
||||||
for i, route := range routes {
|
|
||||||
event := &Event{
|
|
||||||
Type: Create,
|
|
||||||
Timestamp: time.Now(),
|
|
||||||
Route: route,
|
|
||||||
}
|
|
||||||
events[i] = event
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// create event channels
|
// create event channels
|
||||||
@ -687,6 +676,43 @@ func (r *router) Process(a *Advert) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// flushRouteEvents lists all the routes and builds a slice of events
|
||||||
|
func (r *router) flushRouteEvents() ([]*Event, error) {
|
||||||
|
// list all routes
|
||||||
|
routes, err := r.table.List()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed listing routes: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// build a list of events to advertise
|
||||||
|
events := make([]*Event, len(routes))
|
||||||
|
for i, route := range routes {
|
||||||
|
event := &Event{
|
||||||
|
Type: Create,
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
Route: route,
|
||||||
|
}
|
||||||
|
events[i] = event
|
||||||
|
}
|
||||||
|
|
||||||
|
return events, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Solicit advertises all of its routes to the network
|
||||||
|
// It returns error if the router fails to list the routes
|
||||||
|
func (r *router) Solicit() error {
|
||||||
|
events, err := r.flushRouteEvents()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed solicit routes: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// advertise the routes
|
||||||
|
r.advertWg.Add(1)
|
||||||
|
go r.publishAdvert(RouteUpdate, events)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Lookup routes in the routing table
|
// Lookup routes in the routing table
|
||||||
func (r *router) Lookup(q Query) ([]Route, error) {
|
func (r *router) Lookup(q Query) ([]Route, error) {
|
||||||
return r.table.Query(q)
|
return r.table.Query(q)
|
||||||
|
@ -28,6 +28,8 @@ type Router interface {
|
|||||||
Advertise() (<-chan *Advert, error)
|
Advertise() (<-chan *Advert, error)
|
||||||
// Process processes incoming adverts
|
// Process processes incoming adverts
|
||||||
Process(*Advert) error
|
Process(*Advert) error
|
||||||
|
// Solicit advertises the whole routing table ot the network
|
||||||
|
Solicit() error
|
||||||
// Lookup queries routes in the routing table
|
// Lookup queries routes in the routing table
|
||||||
Lookup(Query) ([]Route, error)
|
Lookup(Query) ([]Route, error)
|
||||||
// Watch returns a watcher which tracks updates to the routing table
|
// Watch returns a watcher which tracks updates to the routing table
|
||||||
|
Loading…
x
Reference in New Issue
Block a user