Add Solicit method to router interface

When calling Solicit, router lists all the routes and advertise them
straight away
This commit is contained in:
Milos Gajdos 2019-09-05 13:23:33 +01:00
parent a1ba1482c5
commit 9161b20d6b
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
2 changed files with 42 additions and 14 deletions

View File

@ -602,21 +602,10 @@ func (r *router) Advertise() (<-chan *Advert, error) {
r.subscribers[uuid.New().String()] = advertChan
return advertChan, nil
case Running:
// list routing table routes to announce
routes, err := r.table.List()
// list all the routes and pack them into even slice to advertise
events, err := r.flushRouteEvents()
if err != nil {
return nil, fmt.Errorf("failed listing 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
return nil, fmt.Errorf("failed to advertise routes: %s", err)
}
// create event channels
@ -687,6 +676,43 @@ func (r *router) Process(a *Advert) error {
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
func (r *router) Lookup(q Query) ([]Route, error) {
return r.table.Query(q)

View File

@ -28,6 +28,8 @@ type Router interface {
Advertise() (<-chan *Advert, error)
// Process processes incoming adverts
Process(*Advert) error
// Solicit advertises the whole routing table ot the network
Solicit() error
// Lookup queries routes in the routing table
Lookup(Query) ([]Route, error)
// Watch returns a watcher which tracks updates to the routing table