From 3660660913aac081167759200d2aa8b4902b2dec Mon Sep 17 00:00:00 2001
From: Oliver Herms <oliver.herms@exaring.de>
Date: Sat, 5 May 2018 03:21:54 +0200
Subject: [PATCH] Implemented hook to call UpdateNewClient on client
 registration

---
 routingtable/adjRIBIn/adj_rib_in.go      | 16 ++++++++++++----
 routingtable/adjRIBIn/adj_rib_in_test.go | 11 ++++++++---
 routingtable/client_interface.go         |  3 ++-
 routingtable/client_manager.go           | 14 +++++++++++---
 4 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/routingtable/adjRIBIn/adj_rib_in.go b/routingtable/adjRIBIn/adj_rib_in.go
index 06755e7b..776be1e2 100644
--- a/routingtable/adjRIBIn/adj_rib_in.go
+++ b/routingtable/adjRIBIn/adj_rib_in.go
@@ -1,6 +1,7 @@
 package adjRIBIn
 
 import (
+	"fmt"
 	"sync"
 
 	"github.com/bio-routing/bio-rd/net"
@@ -17,9 +18,16 @@ type AdjRIBIn struct {
 
 // NewAdjRIBIn creates a new Adjacency RIB In
 func NewAdjRIBIn() *AdjRIBIn {
-	return &AdjRIBIn{
+	a := &AdjRIBIn{
 		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.
@@ -33,13 +41,13 @@ func (a *AdjRIBIn) AddPath(pfx net.Prefix, p *route.Path) error {
 }
 
 // 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()
 	defer a.mu.Unlock()
 
 	r := a.rt.Get(pfx)
 	if r == nil {
-		return nil
+		return false
 	}
 
 	oldPaths := r.Paths()
@@ -48,7 +56,7 @@ func (a *AdjRIBIn) RemovePath(pfx net.Prefix, p *route.Path) error {
 	}
 
 	a.removePathsFromClients(pfx, oldPaths)
-	return nil
+	return true
 }
 
 func (a *AdjRIBIn) removePathsFromClients(pfx net.Prefix, paths []*route.Path) {
diff --git a/routingtable/adjRIBIn/adj_rib_in_test.go b/routingtable/adjRIBIn/adj_rib_in_test.go
index 4ae96263..514830fb 100644
--- a/routingtable/adjRIBIn/adj_rib_in_test.go
+++ b/routingtable/adjRIBIn/adj_rib_in_test.go
@@ -1,12 +1,14 @@
 package adjRIBIn
 
 import (
+	"fmt"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
 
 	"github.com/bio-routing/bio-rd/net"
 	"github.com/bio-routing/bio-rd/route"
+	"github.com/bio-routing/bio-rd/routingtable"
 )
 
 type RTMockClient struct {
@@ -21,15 +23,18 @@ func NewRTMockClient() *RTMockClient {
 }
 
 func (m *RTMockClient) AddPath(pfx net.Prefix, p *route.Path) error {
-
 	return nil
 }
 
+func (m *RTMockClient) UpdateNewClient(client routingtable.RouteTableClient) error {
+	return fmt.Errorf("Not implemented")
+}
+
 // 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.path = p
-	return nil
+	return true
 }
 
 func TestAddPath(t *testing.T) {
diff --git a/routingtable/client_interface.go b/routingtable/client_interface.go
index 7c235c7a..63ba81e8 100644
--- a/routingtable/client_interface.go
+++ b/routingtable/client_interface.go
@@ -8,5 +8,6 @@ import (
 // RouteTableClient is the interface that every type of RIB must implement
 type RouteTableClient interface {
 	AddPath(net.Prefix, *route.Path) error
-	RemovePath(net.Prefix, *route.Path) error
+	RemovePath(net.Prefix, *route.Path) bool
+	UpdateNewClient(RouteTableClient) error
 }
diff --git a/routingtable/client_manager.go b/routingtable/client_manager.go
index 8f93a94b..cac27244 100644
--- a/routingtable/client_manager.go
+++ b/routingtable/client_manager.go
@@ -2,8 +2,16 @@ package routingtable
 
 // ClientManager manages clients of routing tables (observer pattern)
 type ClientManager struct {
-	clients      map[RouteTableClient]struct{} // Ensures a client registers at most once
-	routingTable *RoutingTable
+	clients map[RouteTableClient]struct{} // Ensures a client registers at most once
+	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
@@ -12,7 +20,7 @@ func (c *ClientManager) Register(client RouteTableClient) {
 		c.clients = make(map[RouteTableClient]struct{}, 0)
 	}
 	c.clients[client] = struct{}{}
-	//c.routingTable.updateNewClient(client)
+	c.rtc.UpdateNewClient(client)
 }
 
 // Unregister unregisters a client
-- 
GitLab