Skip to content
Snippets Groups Projects
Commit 36606609 authored by Oliver Herms's avatar Oliver Herms
Browse files

Implemented hook to call UpdateNewClient on client registration

parent 446007bd
No related branches found
No related tags found
No related merge requests found
package adjRIBIn package adjRIBIn
import ( import (
"fmt"
"sync" "sync"
"github.com/bio-routing/bio-rd/net" "github.com/bio-routing/bio-rd/net"
...@@ -17,9 +18,16 @@ type AdjRIBIn struct { ...@@ -17,9 +18,16 @@ type AdjRIBIn struct {
// NewAdjRIBIn creates a new Adjacency RIB In // NewAdjRIBIn creates a new Adjacency RIB In
func NewAdjRIBIn() *AdjRIBIn { func NewAdjRIBIn() *AdjRIBIn {
return &AdjRIBIn{ a := &AdjRIBIn{
rt: routingtable.NewRoutingTable(), rt: routingtable.NewRoutingTable(),
} }
a.ClientManager = routingtable.NewClientManager(a)
return a
}
// UpdateNewClient sends current state to a new client
func (a *AdjRIBIn) UpdateNewClient(client routingtable.RouteTableClient) error {
return fmt.Errorf("Not implemented")
} }
// AddPath replaces the path for prefix `pfx`. If the prefix doesn't exist it is added. // AddPath replaces the path for prefix `pfx`. If the prefix doesn't exist it is added.
...@@ -33,13 +41,13 @@ func (a *AdjRIBIn) AddPath(pfx net.Prefix, p *route.Path) error { ...@@ -33,13 +41,13 @@ func (a *AdjRIBIn) AddPath(pfx net.Prefix, p *route.Path) error {
} }
// RemovePath removes the path for prefix `pfx` // RemovePath removes the path for prefix `pfx`
func (a *AdjRIBIn) RemovePath(pfx net.Prefix, p *route.Path) error { func (a *AdjRIBIn) RemovePath(pfx net.Prefix, p *route.Path) bool {
a.mu.Lock() a.mu.Lock()
defer a.mu.Unlock() defer a.mu.Unlock()
r := a.rt.Get(pfx) r := a.rt.Get(pfx)
if r == nil { if r == nil {
return nil return false
} }
oldPaths := r.Paths() oldPaths := r.Paths()
...@@ -48,7 +56,7 @@ func (a *AdjRIBIn) RemovePath(pfx net.Prefix, p *route.Path) error { ...@@ -48,7 +56,7 @@ func (a *AdjRIBIn) RemovePath(pfx net.Prefix, p *route.Path) error {
} }
a.removePathsFromClients(pfx, oldPaths) a.removePathsFromClients(pfx, oldPaths)
return nil return true
} }
func (a *AdjRIBIn) removePathsFromClients(pfx net.Prefix, paths []*route.Path) { func (a *AdjRIBIn) removePathsFromClients(pfx net.Prefix, paths []*route.Path) {
......
package adjRIBIn package adjRIBIn
import ( import (
"fmt"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/bio-routing/bio-rd/net" "github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route" "github.com/bio-routing/bio-rd/route"
"github.com/bio-routing/bio-rd/routingtable"
) )
type RTMockClient struct { type RTMockClient struct {
...@@ -21,15 +23,18 @@ func NewRTMockClient() *RTMockClient { ...@@ -21,15 +23,18 @@ func NewRTMockClient() *RTMockClient {
} }
func (m *RTMockClient) AddPath(pfx net.Prefix, p *route.Path) error { func (m *RTMockClient) AddPath(pfx net.Prefix, p *route.Path) error {
return nil return nil
} }
func (m *RTMockClient) UpdateNewClient(client routingtable.RouteTableClient) error {
return fmt.Errorf("Not implemented")
}
// RemovePath removes the path for prefix `pfx` // RemovePath removes the path for prefix `pfx`
func (m *RTMockClient) RemovePath(pfx net.Prefix, p *route.Path) error { func (m *RTMockClient) RemovePath(pfx net.Prefix, p *route.Path) bool {
m.removePathParams.pfx = pfx m.removePathParams.pfx = pfx
m.removePathParams.path = p m.removePathParams.path = p
return nil return true
} }
func TestAddPath(t *testing.T) { func TestAddPath(t *testing.T) {
......
...@@ -8,5 +8,6 @@ import ( ...@@ -8,5 +8,6 @@ import (
// RouteTableClient is the interface that every type of RIB must implement // RouteTableClient is the interface that every type of RIB must implement
type RouteTableClient interface { type RouteTableClient interface {
AddPath(net.Prefix, *route.Path) error AddPath(net.Prefix, *route.Path) error
RemovePath(net.Prefix, *route.Path) error RemovePath(net.Prefix, *route.Path) bool
UpdateNewClient(RouteTableClient) error
} }
...@@ -2,8 +2,16 @@ package routingtable ...@@ -2,8 +2,16 @@ package routingtable
// ClientManager manages clients of routing tables (observer pattern) // ClientManager manages clients of routing tables (observer pattern)
type ClientManager struct { type ClientManager struct {
clients map[RouteTableClient]struct{} // Ensures a client registers at most once clients map[RouteTableClient]struct{} // Ensures a client registers at most once
routingTable *RoutingTable rtc RouteTableClient
}
// NewClientManager creates and initializes a new client manager
func NewClientManager(rtc RouteTableClient) ClientManager {
return ClientManager{
clients: make(map[RouteTableClient]struct{}, 0),
rtc: rtc,
}
} }
// Register registers a client for updates // Register registers a client for updates
...@@ -12,7 +20,7 @@ func (c *ClientManager) Register(client RouteTableClient) { ...@@ -12,7 +20,7 @@ func (c *ClientManager) Register(client RouteTableClient) {
c.clients = make(map[RouteTableClient]struct{}, 0) c.clients = make(map[RouteTableClient]struct{}, 0)
} }
c.clients[client] = struct{}{} c.clients[client] = struct{}{}
//c.routingTable.updateNewClient(client) c.rtc.UpdateNewClient(client)
} }
// Unregister unregisters a client // Unregister unregisters a client
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment