// 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
}
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.
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 }
+}
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.
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
for j := 0; j < types.At(i).Len(); j++ {
field := types.At(i).At(j)
if jsongo.TypeArray == field.GetType() {
- err := processMessageField(testCtx, &fields, field, otherMessage)
+ err := processMessageField(testCtx, &fields, field, false)
Expect(err).ShouldNot(HaveOccurred())
Expect(fields[j-1]).To(BeEquivalentTo(expectedTypes[j-1]))
}
specificFieldName == "client_index" || specificFieldName == "context" {
continue
}
- err := processMessageField(testCtx, &fields, field, requestMessage)
+ err := processMessageField(testCtx, &fields, field, false)
Expect(err).ShouldNot(HaveOccurred())
Expect(fields[customIndex]).To(BeEquivalentTo(expectedTypes[customIndex]))
customIndex++
"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 (
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.
return nil, errors.New("only one connection per process is supported")
}
- conn = &Connection{vpp: vppAdapter, codec: &MsgCodec{}}
- conn.channels = make(map[uint32]*api.Channel)
- conn.msgIDs = make(map[string]uint16)
- conn.notifSubscriptions = make(map[uint16][]*api.NotifSubscription)
+ conn = &Connection{
+ vpp: vppAdapter,
+ codec: &MsgCodec{},
+ channels: make(map[uint32]*api.Channel),
+ msgIDs: make(map[string]uint16),
+ notifSubscriptions: make(map[uint16][]*api.NotifSubscription),
+ }
conn.vpp.SetMsgCallback(msgCallback)
return conn, nil
}
}
-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
+ waitForVpp := c.vpp.WaitReady()
for {
waitForVpp()
- err := c.connectVPP()
- if err == nil {
+ if err := c.connectVPP(); err == nil {
// signal connected event
connChan <- ConnectionEvent{Timestamp: time.Now(), State: Connected}
break