summaryrefslogtreecommitdiff
path: root/libnetwork/cmd/networkdb-test/dummyclient/dummyClient.go
blob: 92498c7b25ebd5a0b3fcf3b57a192053f0edc3da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package dummyclient

import (
	"fmt"
	"log"
	"net/http"

	"github.com/docker/docker/libnetwork/diagnostic"
	"github.com/docker/docker/libnetwork/networkdb"
	events "github.com/docker/go-events"
	"github.com/sirupsen/logrus"
)

// DummyClientPaths2Func exported paths for the client
var DummyClientPaths2Func = map[string]diagnostic.HTTPHandlerFunc{
	"/watchtable":          watchTable,
	"/watchedtableentries": watchTableEntries,
}

const (
	missingParameter = "missing parameter"
)

type tableHandler struct {
	cancelWatch func()
	entries     map[string]string
}

var clientWatchTable = map[string]tableHandler{}

func watchTable(ctx interface{}, w http.ResponseWriter, r *http.Request) {
	r.ParseForm() //nolint:errcheck
	diagnostic.DebugHTTPForm(r)
	if len(r.Form["tname"]) < 1 {
		rsp := diagnostic.WrongCommand(missingParameter, fmt.Sprintf("%s?tname=table_name", r.URL.Path))
		diagnostic.HTTPReply(w, rsp, &diagnostic.JSONOutput{}) //nolint:errcheck
		return
	}

	tableName := r.Form["tname"][0]
	if _, ok := clientWatchTable[tableName]; ok {
		fmt.Fprintf(w, "OK\n")
		return
	}

	nDB, ok := ctx.(*networkdb.NetworkDB)
	if ok {
		ch, cancel := nDB.Watch(tableName, "", "")
		clientWatchTable[tableName] = tableHandler{cancelWatch: cancel, entries: make(map[string]string)}
		go handleTableEvents(tableName, ch)

		fmt.Fprintf(w, "OK\n")
	}
}

func watchTableEntries(ctx interface{}, w http.ResponseWriter, r *http.Request) {
	r.ParseForm() //nolint:errcheck
	diagnostic.DebugHTTPForm(r)
	if len(r.Form["tname"]) < 1 {
		rsp := diagnostic.WrongCommand(missingParameter, fmt.Sprintf("%s?tname=table_name", r.URL.Path))
		diagnostic.HTTPReply(w, rsp, &diagnostic.JSONOutput{}) //nolint:errcheck
		return
	}

	tableName := r.Form["tname"][0]
	table, ok := clientWatchTable[tableName]
	if !ok {
		fmt.Fprintf(w, "Table %s not watched\n", tableName)
		return
	}

	fmt.Fprintf(w, "total elements: %d\n", len(table.entries))
	i := 0
	for k, v := range table.entries {
		fmt.Fprintf(w, "%d) k:`%s` -> v:`%s`\n", i, k, v)
		i++
	}
}

func handleTableEvents(tableName string, ch *events.Channel) {
	var (
		// nid   string
		eid   string
		value []byte
		isAdd bool
	)

	logrus.Infof("Started watching table:%s", tableName)
	for {
		select {
		case <-ch.Done():
			logrus.Infof("End watching %s", tableName)
			return

		case evt := <-ch.C:
			logrus.Infof("Recevied new event on:%s", tableName)
			switch event := evt.(type) {
			case networkdb.CreateEvent:
				// nid = event.NetworkID
				eid = event.Key
				value = event.Value
				isAdd = true
			case networkdb.DeleteEvent:
				// nid = event.NetworkID
				eid = event.Key
				value = event.Value
				isAdd = false
			default:
				log.Fatalf("Unexpected table event = %#v", event)
			}
			if isAdd {
				// logrus.Infof("Add %s %s", tableName, eid)
				clientWatchTable[tableName].entries[eid] = string(value)
			} else {
				// logrus.Infof("Del %s %s", tableName, eid)
				delete(clientWatchTable[tableName].entries, eid)
			}
		}
	}
}