Skip to content

Commit

Permalink
add function to get subscribed vnis as a client
Browse files Browse the repository at this point in the history
  • Loading branch information
byteocean committed Nov 23, 2023
1 parent 113acdd commit 4540aa6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
15 changes: 13 additions & 2 deletions metalbond.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ type MetalBond struct {

keepaliveInterval uint32
shuttingDown bool

client Client
client Client

lis *net.Listener // for server only
isServer bool
Expand Down Expand Up @@ -310,6 +309,18 @@ func (m *MetalBond) GetRoutesForVni(vni VNI) error {
return nil
}

func (m *MetalBond) GetSubscribedVnis() []VNI {
m.mtxMySubscriptions.RLock()
defer m.mtxMySubscriptions.RUnlock()

vnis := make([]VNI, 0)
for vni := range m.mySubscriptions {
vnis = append(vnis, vni)
}

return vnis
}

func (m *MetalBond) addReceivedRoute(fromPeer *metalBondPeer, vni VNI, dest Destination, hop NextHop) error {
err := m.routeTable.AddNextHop(vni, dest, hop, fromPeer)
if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,42 @@ var _ = Describe("Peer", func() {
Expect(err).NotTo(HaveOccurred())
})

FIt("should subscribe", func() {
mbClient := NewMetalBond(Config{}, client)
localIP := net.ParseIP("127.0.0.1")
err := mbClient.AddPeer(serverAddress, localIP.String())
Expect(err).NotTo(HaveOccurred())

time.Sleep(1 * time.Second)
vni_one := VNI(200)
err = mbClient.Subscribe(vni_one)
if err != nil {
log.Errorf("subscribe failed: %v", err)
}
Expect(err).NotTo(HaveOccurred())

vni_two := VNI(300)
err = mbClient.Subscribe(vni_two)
if err != nil {
log.Errorf("subscribe failed: %v", err)
}
Expect(err).NotTo(HaveOccurred())

vnis := mbClient.GetSubscribedVnis()
Expect(len(vnis)).To(Equal(2))
Expect(vnis[0]).To(Equal(vni_one))
Expect(vnis[1]).To(Equal(vni_two))

err = mbClient.Unsubscribe(vni_one)
Expect(err).NotTo(HaveOccurred())

err = mbClient.Unsubscribe(vni_two)
Expect(err).NotTo(HaveOccurred())

vnis = mbClient.GetSubscribedVnis()
Expect(len(vnis)).To(Equal(0))
})

It("should announce", func() {
totalClients := 1000
var wg sync.WaitGroup
Expand Down

0 comments on commit 4540aa6

Please sign in to comment.