From f2cbe790a5c3c3e8cb59b592c252b2b84025bd91 Mon Sep 17 00:00:00 2001 From: Lukas Macko Date: Thu, 7 Sep 2017 10:00:26 +0200 Subject: Wait until vpp is ready + Update vendor Change-Id: I7c42d44d60f84fd21e55f9e4928d776f5466cc83 Signed-off-by: Lukas Macko --- core/core.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'core/core.go') diff --git a/core/core.go b/core/core.go index 8977902..3eea332 100644 --- a/core/core.go +++ b/core/core.go @@ -28,6 +28,7 @@ import ( "git.fd.io/govpp.git/adapter" "git.fd.io/govpp.git/api" "git.fd.io/govpp.git/core/bin_api/vpe" + "github.com/fsnotify/fsnotify" ) const ( @@ -52,6 +53,15 @@ const ( Disconnected = iota ) +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" +) + // ConnectionEvent is a notification about change in the VPP connection state. type ConnectionEvent struct { // Timestamp holds the time when the event has been generated. @@ -202,11 +212,48 @@ func (c *Connection) disconnectVPP() { } } +func fileExists(name string) bool { + if _, err := os.Stat(name); err != nil { + if os.IsNotExist(err) { + return false + } + } + return true +} + +// waitForVpp blocks until shared memory for sending bin api calls +// is present on the file system. +func waitForVpp() 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 +} + // connectLoop attempts to connect to VPP until it succeeds. // Then it continues with healthCheckLoop. func (c *Connection) connectLoop(connChan chan ConnectionEvent) { // loop until connected for { + waitForVpp() err := c.connectVPP() if err == nil { // signal connected event -- cgit 1.2.3-korg