Change module name to go.fd.io/govpp
[govpp.git] / adapter / vppapiclient / vppapiclient.go
index cac8c71..3ab460d 100644 (file)
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-// +build !windows,!darwin
+// +build !windows,!darwin,!novpp
 
 package vppapiclient
 
@@ -20,56 +20,7 @@ package vppapiclient
 #cgo CFLAGS: -DPNG_DEBUG=1
 #cgo LDFLAGS: -lvppapiclient
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <arpa/inet.h>
-#include <vpp-api/client/vppapiclient.h>
-
-extern void go_msg_callback(uint16_t msg_id, void* data, size_t size);
-
-typedef struct __attribute__((__packed__)) _req_header {
-    uint16_t msg_id;
-    uint32_t client_index;
-    uint32_t context;
-} req_header_t;
-
-typedef struct __attribute__((__packed__)) _reply_header {
-    uint16_t msg_id;
-} reply_header_t;
-
-static void
-govpp_msg_callback(unsigned char *data, int size)
-{
-    reply_header_t *header = ((reply_header_t *)data);
-    go_msg_callback(ntohs(header->msg_id), data, size);
-}
-
-static int
-govpp_send(uint32_t context, void *data, size_t size)
-{
-       req_header_t *header = ((req_header_t *)data);
-       header->context = htonl(context);
-    return vac_write(data, size);
-}
-
-static int
-govpp_connect(char *shm, int rx_qlen)
-{
-    return vac_connect("govpp", shm, govpp_msg_callback, rx_qlen);
-}
-
-static int
-govpp_disconnect()
-{
-    return vac_disconnect();
-}
-
-static uint32_t
-govpp_get_msg_index(char *name_and_crc)
-{
-    return vac_get_msg_index(name_and_crc);
-}
+#include "vppapiclient_wrapper.h"
 */
 import "C"
 
@@ -78,10 +29,19 @@ import (
        "os"
        "path/filepath"
        "reflect"
+       "sync/atomic"
+       "time"
        "unsafe"
 
-       "git.fd.io/govpp.git/adapter"
        "github.com/fsnotify/fsnotify"
+
+       "go.fd.io/govpp/adapter"
+)
+
+var (
+       // MaxWaitReady defines maximum duration before waiting for shared memory
+       // segment times out
+       MaxWaitReady = time.Second * 10
 )
 
 const (
@@ -93,7 +53,7 @@ const (
 
 // global VPP binary API client, library vppapiclient only supports
 // single connection at a time
-var globalVppClient *vppClient
+var globalVppClient unsafe.Pointer
 
 // stubVppClient is the default implementation of the VppAPI.
 type vppClient struct {
@@ -117,7 +77,8 @@ func NewVppClientWithInputQueueSize(shmPrefix string, inputQueueSize uint16) ada
 
 // Connect connects the process to VPP.
 func (a *vppClient) Connect() error {
-       if globalVppClient != nil {
+       h := (*vppClient)(atomic.LoadPointer(&globalVppClient))
+       if h != nil {
                return fmt.Errorf("already connected to binary API, disconnect first")
        }
 
@@ -133,19 +94,17 @@ func (a *vppClient) Connect() error {
                return fmt.Errorf("connecting to VPP binary API failed (rc=%v)", rc)
        }
 
-       globalVppClient = a
+       atomic.StorePointer(&globalVppClient, unsafe.Pointer(a))
        return nil
 }
 
 // Disconnect disconnects the process from VPP.
 func (a *vppClient) Disconnect() error {
-       globalVppClient = nil
-
+       atomic.StorePointer(&globalVppClient, nil)
        rc := C.govpp_disconnect()
        if rc != 0 {
                return fmt.Errorf("disconnecting from VPP binary API failed (rc=%v)", rc)
        }
-
        return nil
 }
 
@@ -157,7 +116,7 @@ func (a *vppClient) GetMsgID(msgName string, msgCrc string) (uint16, error) {
        msgID := uint16(C.govpp_get_msg_index(nameAndCrc))
        if msgID == ^uint16(0) {
                // VPP does not know this message
-               return msgID, fmt.Errorf("unknown message: %v (crc: %v)", msgName, msgCrc)
+               return msgID, &adapter.UnknownMsgError{msgName, msgCrc}
        }
 
        return msgID, nil
@@ -181,21 +140,19 @@ func (a *vppClient) SetMsgCallback(cb adapter.MsgCallback) {
 // WaitReady blocks until shared memory for sending
 // binary api calls is present on the file system.
 func (a *vppClient) WaitReady() error {
-       var path string
-
        // join the path to the shared memory segment
+       var path string
        if a.shmPrefix == "" {
                path = filepath.Join(shmDir, vppShmFile)
        } else {
                path = filepath.Join(shmDir, a.shmPrefix+"-"+vppShmFile)
        }
 
-       // check if file at the path exists
+       // check if file already exists
        if _, err := os.Stat(path); err == nil {
-               // file exists, we are ready
-               return nil
+               return nil // file exists, we are ready
        } else if !os.IsNotExist(err) {
-               return err
+               return err // some other error occurred
        }
 
        // file does not exist, start watching folder
@@ -205,28 +162,37 @@ func (a *vppClient) WaitReady() error {
        }
        defer watcher.Close()
 
+       // start watching directory
        if err := watcher.Add(shmDir); err != nil {
                return err
        }
 
+       timeout := time.NewTimer(MaxWaitReady)
        for {
-               ev := <-watcher.Events
-               if ev.Name == path {
-                       if (ev.Op & fsnotify.Create) == fsnotify.Create {
-                               // file was created, we are ready
-                               break
+               select {
+               case <-timeout.C:
+                       return fmt.Errorf("timeout waiting (%s) for shm file: %s", MaxWaitReady, path)
+
+               case e := <-watcher.Errors:
+                       return e
+
+               case ev := <-watcher.Events:
+                       if ev.Name == path && (ev.Op&fsnotify.Create) == fsnotify.Create {
+                               // file created, we are ready
+                               return nil
                        }
                }
        }
-
-       return nil
 }
 
 //export go_msg_callback
 func go_msg_callback(msgID C.uint16_t, data unsafe.Pointer, size C.size_t) {
+       h := (*vppClient)(atomic.LoadPointer(&globalVppClient))
+       if h == nil {
+               return
+       }
        // convert unsafe.Pointer to byte slice
        sliceHeader := &reflect.SliceHeader{Data: uintptr(data), Len: int(size), Cap: int(size)}
        byteSlice := *(*[]byte)(unsafe.Pointer(sliceHeader))
-
-       globalVppClient.msgCallback(uint16(msgID), byteSlice)
+       h.msgCallback(uint16(msgID), byteSlice)
 }