aboutsummaryrefslogtreecommitdiffstats
path: root/adapter
diff options
context:
space:
mode:
authorOndrej Fabry <ofabry@cisco.com>2017-11-28 19:53:14 +0100
committerOndrej Fabry <ofabry@cisco.com>2017-11-28 19:53:14 +0100
commit67482a4f8c3a3a7cf17b4edf82e61c28b318ffd9 (patch)
treef08519f399f345439bd3b3cfd4f61c1c63e34889 /adapter
parent3f1edad4e6ba0a7876750aea55507fae14d8badf (diff)
Add WaitReady to VppAdapter
- this fixes issue with mocked adapter during AsyncConnect Change-Id: I952025fec865422f9e83cec9383d96f79a639c90 Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'adapter')
-rw-r--r--adapter/adapter.go3
-rw-r--r--adapter/mock/mock_adapter.go5
-rw-r--r--adapter/vppapiclient/empty_adapter.go4
-rw-r--r--adapter/vppapiclient/vppapiclient_adapter.go49
4 files changed, 61 insertions, 0 deletions
diff --git a/adapter/adapter.go b/adapter/adapter.go
index 2843d2c..d8f29c9 100644
--- a/adapter/adapter.go
+++ b/adapter/adapter.go
@@ -30,4 +30,7 @@ type VppAdapter interface {
// SetMsgCallback sets a callback function that will be called by the adapter whenever a message comes from VPP.
SetMsgCallback(func(context uint32, msgId uint16, data []byte))
+
+ // WaitReady returns func which waits until adapter is ready.
+ WaitReady() func() error
}
diff --git a/adapter/mock/mock_adapter.go b/adapter/mock/mock_adapter.go
index 8c88030..f85f17d 100644
--- a/adapter/mock/mock_adapter.go
+++ b/adapter/mock/mock_adapter.go
@@ -294,6 +294,11 @@ func (a *VppAdapter) SetMsgCallback(cb func(context uint32, msgID uint16, data [
a.callback = cb
}
+// WaitReady mocks waiting for VPP
+func (a *VppAdapter) WaitReady() func() error {
+ return func() error { return nil }
+}
+
// MockReply stores a message to be returned when the next request comes. It is a FIFO queue - multiple replies
// can be pushed into it, the first one will be popped when some request comes.
// Using of this method automatically switches the mock into th useRepliesQueue mode.
diff --git a/adapter/vppapiclient/empty_adapter.go b/adapter/vppapiclient/empty_adapter.go
index 14f804d..7249864 100644
--- a/adapter/vppapiclient/empty_adapter.go
+++ b/adapter/vppapiclient/empty_adapter.go
@@ -50,3 +50,7 @@ func (a *vppAPIClientAdapter) SendMsg(clientID uint32, data []byte) error {
func (a *vppAPIClientAdapter) SetMsgCallback(cb func(context uint32, msgID uint16, data []byte)) {
// no op
}
+
+func (a *vppAPIClientAdapter) WaitReady() func() error {
+ return func() error { return nil }
+}
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