github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/libnetwork/cmd/networkdb-test/dummyclient/dummyClient.go (about)

     1  package dummyclient
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  
     8  	"github.com/docker/docker/libnetwork/diagnostic"
     9  	"github.com/docker/docker/libnetwork/networkdb"
    10  	events "github.com/docker/go-events"
    11  	"github.com/sirupsen/logrus"
    12  )
    13  
    14  // DummyClientPaths2Func exported paths for the client
    15  var DummyClientPaths2Func = map[string]diagnostic.HTTPHandlerFunc{
    16  	"/watchtable":          watchTable,
    17  	"/watchedtableentries": watchTableEntries,
    18  }
    19  
    20  const (
    21  	missingParameter = "missing parameter"
    22  )
    23  
    24  type tableHandler struct {
    25  	cancelWatch func()
    26  	entries     map[string]string
    27  }
    28  
    29  var clientWatchTable = map[string]tableHandler{}
    30  
    31  func watchTable(ctx interface{}, w http.ResponseWriter, r *http.Request) {
    32  	r.ParseForm() //nolint:errcheck
    33  	diagnostic.DebugHTTPForm(r)
    34  	if len(r.Form["tname"]) < 1 {
    35  		rsp := diagnostic.WrongCommand(missingParameter, fmt.Sprintf("%s?tname=table_name", r.URL.Path))
    36  		diagnostic.HTTPReply(w, rsp, &diagnostic.JSONOutput{}) //nolint:errcheck
    37  		return
    38  	}
    39  
    40  	tableName := r.Form["tname"][0]
    41  	if _, ok := clientWatchTable[tableName]; ok {
    42  		fmt.Fprintf(w, "OK\n")
    43  		return
    44  	}
    45  
    46  	nDB, ok := ctx.(*networkdb.NetworkDB)
    47  	if ok {
    48  		ch, cancel := nDB.Watch(tableName, "", "")
    49  		clientWatchTable[tableName] = tableHandler{cancelWatch: cancel, entries: make(map[string]string)}
    50  		go handleTableEvents(tableName, ch)
    51  
    52  		fmt.Fprintf(w, "OK\n")
    53  	}
    54  }
    55  
    56  func watchTableEntries(ctx interface{}, w http.ResponseWriter, r *http.Request) {
    57  	r.ParseForm() //nolint:errcheck
    58  	diagnostic.DebugHTTPForm(r)
    59  	if len(r.Form["tname"]) < 1 {
    60  		rsp := diagnostic.WrongCommand(missingParameter, fmt.Sprintf("%s?tname=table_name", r.URL.Path))
    61  		diagnostic.HTTPReply(w, rsp, &diagnostic.JSONOutput{}) //nolint:errcheck
    62  		return
    63  	}
    64  
    65  	tableName := r.Form["tname"][0]
    66  	table, ok := clientWatchTable[tableName]
    67  	if !ok {
    68  		fmt.Fprintf(w, "Table %s not watched\n", tableName)
    69  		return
    70  	}
    71  
    72  	fmt.Fprintf(w, "total elements: %d\n", len(table.entries))
    73  	i := 0
    74  	for k, v := range table.entries {
    75  		fmt.Fprintf(w, "%d) k:`%s` -> v:`%s`\n", i, k, v)
    76  		i++
    77  	}
    78  }
    79  
    80  func handleTableEvents(tableName string, ch *events.Channel) {
    81  	var (
    82  		// nid   string
    83  		eid   string
    84  		value []byte
    85  		isAdd bool
    86  	)
    87  
    88  	logrus.Infof("Started watching table:%s", tableName)
    89  	for {
    90  		select {
    91  		case <-ch.Done():
    92  			logrus.Infof("End watching %s", tableName)
    93  			return
    94  
    95  		case evt := <-ch.C:
    96  			logrus.Infof("Recevied new event on:%s", tableName)
    97  			switch event := evt.(type) {
    98  			case networkdb.CreateEvent:
    99  				// nid = event.NetworkID
   100  				eid = event.Key
   101  				value = event.Value
   102  				isAdd = true
   103  			case networkdb.DeleteEvent:
   104  				// nid = event.NetworkID
   105  				eid = event.Key
   106  				value = event.Value
   107  				isAdd = false
   108  			default:
   109  				log.Fatalf("Unexpected table event = %#v", event)
   110  			}
   111  			if isAdd {
   112  				// logrus.Infof("Add %s %s", tableName, eid)
   113  				clientWatchTable[tableName].entries[eid] = string(value)
   114  			} else {
   115  				// logrus.Infof("Del %s %s", tableName, eid)
   116  				delete(clientWatchTable[tableName].entries, eid)
   117  			}
   118  		}
   119  	}
   120  }