Skip to content

Commit

Permalink
Unregister a specific Notification Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Vandewilly Silva committed Nov 8, 2016
1 parent a35c46b commit 8fc3013
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
26 changes: 25 additions & 1 deletion client.go
Expand Up @@ -6,11 +6,13 @@ import (
"fmt"
"log"
"net"
"reflect"
"sync"

"os"

"github.com/cenk/rpc2"
"github.com/cenk/rpc2/jsonrpc"
"os"
)

// OvsdbClient is an OVSDB client
Expand Down Expand Up @@ -109,6 +111,28 @@ func (ovs *OvsdbClient) Register(handler NotificationHandler) {
ovs.handlers = append(ovs.handlers, handler)
}

//Get Handler by index
func getHandlerIndex(handler NotificationHandler, handlers []NotificationHandler) (int, error) {
for i, h := range handlers {
if reflect.DeepEqual(h, handler) {
return i, nil
}
}
return -1, errors.New("Handler not found")
}

// Unregister the supplied NotificationHandler to not recieve OVSDB Notifications anymore
func (ovs *OvsdbClient) Unregister(handler NotificationHandler) error {
ovs.handlersMutex.Lock()
defer ovs.handlersMutex.Unlock()
i, err := getHandlerIndex(handler, ovs.handlers)
if err != nil {
return err
}
ovs.handlers = append(ovs.handlers[:i], ovs.handlers[i+1:]...)
return nil
}

// NotificationHandler is the interface that must be implemented to receive notifcations
type NotificationHandler interface {
// RFC 7047 section 4.1.6 Update Notification
Expand Down
27 changes: 27 additions & 0 deletions ovs_integration_test.go
Expand Up @@ -281,6 +281,33 @@ func TestNotify(t *testing.T) {
ovs.Disconnect()
}

func TestRemoveNotify(t *testing.T) {
if testing.Short() {
t.Skip()
}

ovs, err := Connect(os.Getenv("DOCKER_IP"), int(6640))
if err != nil {
log.Fatal("Failed to Connect. error:", err)
panic(err)
}

notifyEchoChan := make(chan bool)

notifier := Notifier{notifyEchoChan}
ovs.Register(notifier)

lenIni := len(ovs.handlers)
ovs.Unregister(notifier)
lenEnd := len(ovs.handlers)

if lenIni == lenEnd {
log.Fatal("Failed to Unregister Notifier:")
}

ovs.Disconnect()
}

type Notifier struct {
echoChan chan bool
}
Expand Down

0 comments on commit 8fc3013

Please sign in to comment.