|
@@ -6,41 +6,42 @@ import (
|
|
|
"fmt"
|
|
"fmt"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+// CONSTANTS
|
|
|
|
|
+
|
|
|
type Watcher struct {
|
|
type Watcher struct {
|
|
|
- chanMap map[string][]chan int
|
|
|
|
|
|
|
+ chanMap map[string][]chan Notification
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+type Notification struct {
|
|
|
|
|
+ action int
|
|
|
|
|
+ key string
|
|
|
|
|
+ oldValue string
|
|
|
|
|
+ newValue string
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func createWatcher() *Watcher {
|
|
func createWatcher() *Watcher {
|
|
|
w := new(Watcher)
|
|
w := new(Watcher)
|
|
|
- w.chanMap = make(map[string][]chan int)
|
|
|
|
|
|
|
+ w.chanMap = make(map[string][]chan Notification)
|
|
|
return w
|
|
return w
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (w *Watcher) add(prefix string, c chan int) error {
|
|
|
|
|
|
|
+func (w *Watcher) add(prefix string, c chan Notification, f func(chan Notification)) error {
|
|
|
|
|
|
|
|
prefix = path.Clean(prefix)
|
|
prefix = path.Clean(prefix)
|
|
|
fmt.Println("Add ", prefix)
|
|
fmt.Println("Add ", prefix)
|
|
|
|
|
+
|
|
|
_, ok := w.chanMap[prefix]
|
|
_, ok := w.chanMap[prefix]
|
|
|
if !ok {
|
|
if !ok {
|
|
|
- w.chanMap[prefix] = make([]chan int, 0)
|
|
|
|
|
|
|
+ w.chanMap[prefix] = make([]chan Notification, 0)
|
|
|
w.chanMap[prefix] = append(w.chanMap[prefix], c)
|
|
w.chanMap[prefix] = append(w.chanMap[prefix], c)
|
|
|
} else {
|
|
} else {
|
|
|
w.chanMap[prefix] = append(w.chanMap[prefix], c)
|
|
w.chanMap[prefix] = append(w.chanMap[prefix], c)
|
|
|
}
|
|
}
|
|
|
- fmt.Println(len(w.chanMap[prefix]), "@", prefix)
|
|
|
|
|
- go wait(c)
|
|
|
|
|
- return nil
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func wait(c chan int) {
|
|
|
|
|
- result := <-c
|
|
|
|
|
|
|
|
|
|
- if result == 0 {
|
|
|
|
|
- fmt.Println("yes")
|
|
|
|
|
- } else {
|
|
|
|
|
- fmt.Println("no")
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ fmt.Println(len(w.chanMap[prefix]), "@", prefix)
|
|
|
|
|
|
|
|
|
|
+ go f(c)
|
|
|
|
|
+ return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (w *Watcher) notify(action int, key string, oldValue string, newValue string) error {
|
|
func (w *Watcher) notify(action int, key string, oldValue string, newValue string) error {
|
|
@@ -50,17 +51,30 @@ func (w *Watcher) notify(action int, key string, oldValue string, newValue strin
|
|
|
|
|
|
|
|
currPath := "/"
|
|
currPath := "/"
|
|
|
|
|
|
|
|
|
|
+ // walk through all the pathes
|
|
|
for _, segment := range segments {
|
|
for _, segment := range segments {
|
|
|
|
|
+
|
|
|
currPath := path.Join(currPath, segment)
|
|
currPath := path.Join(currPath, segment)
|
|
|
|
|
+
|
|
|
fmt.Println(currPath)
|
|
fmt.Println(currPath)
|
|
|
|
|
+
|
|
|
chans, ok := w.chanMap[currPath]
|
|
chans, ok := w.chanMap[currPath]
|
|
|
|
|
+
|
|
|
if ok {
|
|
if ok {
|
|
|
fmt.Println("found ", currPath)
|
|
fmt.Println("found ", currPath)
|
|
|
|
|
+
|
|
|
|
|
+ n := Notification {action, key, oldValue, newValue}
|
|
|
|
|
+ // notify all the watchers
|
|
|
for _, c := range chans {
|
|
for _, c := range chans {
|
|
|
- c <- 0
|
|
|
|
|
|
|
+ c <- n
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // we have notified all the watchers at this path
|
|
|
|
|
+ // delete the map
|
|
|
delete(w.chanMap, currPath)
|
|
delete(w.chanMap, currPath)
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|