Use new default binapi socket with fallback to legacy
[govpp.git] / examples / simple-client / simple_client.go
index 08d4da6..c857768 100644 (file)
@@ -17,6 +17,7 @@
 package main
 
 import (
+       "flag"
        "fmt"
        "log"
        "net"
@@ -24,26 +25,41 @@ import (
        "strings"
 
        "git.fd.io/govpp.git"
+       "git.fd.io/govpp.git/adapter/socketclient"
        "git.fd.io/govpp.git/api"
-       "git.fd.io/govpp.git/examples/bin_api/acl"
-       "git.fd.io/govpp.git/examples/bin_api/interfaces"
-       "git.fd.io/govpp.git/examples/bin_api/ip"
+       "git.fd.io/govpp.git/core"
+       "git.fd.io/govpp.git/examples/binapi/acl"
+       "git.fd.io/govpp.git/examples/binapi/interfaces"
+       "git.fd.io/govpp.git/examples/binapi/ip"
+)
+
+var (
+       sockAddr = flag.String("sock", socketclient.DefaultSocketName, "Path to VPP binary API socket file")
 )
 
 func main() {
-       fmt.Println("Starting simple VPP client...")
+       flag.Parse()
+
+       fmt.Println("Starting simple client example")
 
        // connect to VPP
-       conn, err := govpp.Connect("")
+       conn, conev, err := govpp.AsyncConnect(*sockAddr, core.DefaultMaxReconnectAttempts, core.DefaultReconnectInterval)
        if err != nil {
                log.Fatalln("ERROR:", err)
        }
        defer conn.Disconnect()
 
+       select {
+       case e := <-conev:
+               if e.State != core.Connected {
+                       log.Fatalln("ERROR: connecting to VPP failed:", e.Error)
+               }
+       }
+
        // create an API channel that will be used in the examples
        ch, err := conn.NewAPIChannel()
        if err != nil {
-               log.Fatalln("ERROR:", err)
+               log.Fatalln("ERROR: creating channel failed:", err)
        }
        defer ch.Close()
 
@@ -107,10 +123,6 @@ func aclConfig(ch api.Channel) {
                fmt.Println("ERROR:", err)
                return
        }
-       if reply.Retval != 0 {
-               fmt.Println("Retval:", reply.Retval)
-               return
-       }
 
        fmt.Printf("ACL add replace reply: %+v\n", reply)
 
@@ -146,6 +158,7 @@ func interfaceDump(ch api.Channel) {
                }
                if err != nil {
                        fmt.Println("ERROR:", err)
+                       return
                }
                ifaceName := strings.TrimFunc(string(msg.InterfaceName), func(r rune) bool {
                        return r == 0x00
@@ -170,6 +183,7 @@ func ipAddressDump(ch api.Channel) {
                }
                if err != nil {
                        fmt.Println("ERROR:", err)
+                       return
                }
                fmt.Printf("ip address details: %d %+v\n", msg.SwIfIndex, msg)
        }
@@ -206,6 +220,7 @@ func ipUnnumberedDump(ch api.Channel) {
                }
                if err != nil {
                        fmt.Println("ERROR:", err)
+                       return
                }
                fmt.Printf("IP unnumbered details: %+v\n", msg)
        }