Improve compatibility checking
[govpp.git] / adapter / vppapiclient / vppapiclient.go
index d8465d7..977f32d 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
 
@@ -40,7 +40,7 @@ import (
 var (
        // MaxWaitReady defines maximum duration before waiting for shared memory
        // segment times out
-       MaxWaitReady = time.Second * 15
+       MaxWaitReady = time.Second * 10
 )
 
 const (
@@ -116,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
@@ -148,12 +148,11 @@ func (a *vppClient) WaitReady() error {
                path = filepath.Join(shmDir, a.shmPrefix+"-"+vppShmFile)
        }
 
-       // check if file at the path already 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
@@ -168,18 +167,19 @@ func (a *vppClient) WaitReady() error {
                return err
        }
 
+       timeout := time.NewTimer(MaxWaitReady)
        for {
                select {
-               case <-time.After(MaxWaitReady):
-                       return fmt.Errorf("waiting for shared memory segment timed out (%s)", MaxWaitReady)
+               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 {
-                               if (ev.Op & fsnotify.Create) == fsnotify.Create {
-                                       // file was created, we are ready
-                                       return nil
-                               }
+                       if ev.Name == path && (ev.Op&fsnotify.Create) == fsnotify.Create {
+                               // file created, we are ready
+                               return nil
                        }
                }
        }