From 67482a4f8c3a3a7cf17b4edf82e61c28b318ffd9 Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Tue, 28 Nov 2017 19:53:14 +0100 Subject: Add WaitReady to VppAdapter - this fixes issue with mocked adapter during AsyncConnect Change-Id: I952025fec865422f9e83cec9383d96f79a639c90 Signed-off-by: Ondrej Fabry --- adapter/vppapiclient/vppapiclient_adapter.go | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'adapter/vppapiclient/vppapiclient_adapter.go') diff --git a/adapter/vppapiclient/vppapiclient_adapter.go b/adapter/vppapiclient/vppapiclient_adapter.go index 74f0118..dfdd973 100644 --- a/adapter/vppapiclient/vppapiclient_adapter.go +++ b/adapter/vppapiclient/vppapiclient_adapter.go @@ -79,10 +79,21 @@ import "C" import ( "errors" "fmt" + "os" "reflect" "unsafe" "git.fd.io/govpp.git/adapter" + "github.com/fsnotify/fsnotify" +) + +const ( + // watchedFolder is a folder where vpp's shared memory is supposed to be created. + // File system events are monitored in this folder. + watchedFolder = "/dev/shm/" + // watchedFile is a name of the file in the watchedFolder. Once the file is present + // the vpp is ready to accept a new connection. + watchedFile = watchedFolder + "vpe-api" ) // vppAPIClientAdapter is the opaque context of the adapter. @@ -139,6 +150,44 @@ func (a *vppAPIClientAdapter) SetMsgCallback(cb func(context uint32, msgID uint1 a.callback = cb } +// WaitReady returns func which blocks until shared memory +// for sending bin api calls is present on the file system. +func (a *vppAPIClientAdapter) WaitReady() func() error { + return func() error { + watcher, err := fsnotify.NewWatcher() + if err != nil { + return err + } + defer watcher.Close() + + err = watcher.Add(watchedFolder) + if err != nil { + return err + } + + if fileExists(watchedFile) { + return nil + } + + for { + ev := <-watcher.Events + if ev.Name == watchedFile && (ev.Op&fsnotify.Create) == fsnotify.Create { + break + } + } + return nil + } +} + +func fileExists(name string) bool { + if _, err := os.Stat(name); err != nil { + if os.IsNotExist(err) { + return false + } + } + return true +} + //export go_msg_callback func go_msg_callback(msgID C.uint16_t, context C.uint32_t, data unsafe.Pointer, size C.size_t) { // convert unsafe.Pointer to byte slice -- cgit 1.2.3-korg