diff options
author | Ondrej Fabry <ofabry@cisco.com> | 2018-08-23 22:51:56 +0200 |
---|---|---|
committer | Ondrej Fabry <ofabry@cisco.com> | 2018-08-24 12:43:05 +0200 |
commit | 6b350c65fe0ec845cecf58bfb41ffc63dc9c04f7 (patch) | |
tree | 6255495854f43ec2f2d11f88990369aadb48db3f /examples/cmd/stats-client | |
parent | 892683bef86cacc2ccda2b4df2b079171bd92164 (diff) |
Simplify subscribing to events and fix events
- there is no need for sending subscription requests through channels,
since all the messages are registered and no communication with VPP
is needed
Change-Id: Ibc29957be02a32e26309f66c369a071559b822a9
Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'examples/cmd/stats-client')
-rw-r--r-- | examples/cmd/stats-client/stats_client.go | 31 |
1 files changed, 14 insertions, 17 deletions
diff --git a/examples/cmd/stats-client/stats_client.go b/examples/cmd/stats-client/stats_client.go index 4ea4659..f61f975 100644 --- a/examples/cmd/stats-client/stats_client.go +++ b/examples/cmd/stats-client/stats_client.go @@ -18,6 +18,7 @@ package main import ( "fmt" + "log" "os" "os/signal" @@ -28,45 +29,41 @@ import ( ) func main() { - fmt.Println("Starting stats VPP client...") + fmt.Println("Starting stats VPP client..") // async connect to VPP conn, statCh, err := govpp.AsyncConnect("") if err != nil { - fmt.Println("Error:", err) - os.Exit(1) + log.Fatalln("Error:", err) } defer conn.Disconnect() // create an API channel that will be used in the examples ch, err := conn.NewAPIChannel() if err != nil { - fmt.Println("Error:", err) - os.Exit(1) + log.Fatalln("Error:", err) } - defer fmt.Println("calling close") defer ch.Close() // create channel for Interrupt signal sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, os.Interrupt) - var simpleCountersSubs *api.NotifSubscription - var combinedCountersSubs *api.NotifSubscription var notifChan chan api.Message + var simpleSub api.SubscriptionCtx + var combinedSub api.SubscriptionCtx // loop until Interrupt signal is received loop: for { select { - case connEvent := <-statCh: // VPP connection state change switch connEvent.State { case core.Connected: fmt.Println("VPP connected.") - if simpleCountersSubs == nil { - simpleCountersSubs, combinedCountersSubs, notifChan = subscribeNotifications(ch) + if notifChan == nil { + simpleSub, combinedSub, notifChan = subscribeNotifications(ch) } requestStatistics(ch) @@ -93,24 +90,24 @@ loop: } } - ch.UnsubscribeNotification(simpleCountersSubs) - ch.UnsubscribeNotification(combinedCountersSubs) + simpleSub.Unsubscribe() + combinedSub.Unsubscribe() } // subscribeNotifications subscribes for interface counters notifications. -func subscribeNotifications(ch api.Channel) (*api.NotifSubscription, *api.NotifSubscription, chan api.Message) { +func subscribeNotifications(ch api.Channel) (api.SubscriptionCtx, api.SubscriptionCtx, chan api.Message) { notifChan := make(chan api.Message, 100) - simpleCountersSubs, err := ch.SubscribeNotification(notifChan, stats.NewVnetInterfaceSimpleCounters) + simpleSub, err := ch.SubscribeNotification(notifChan, &stats.VnetInterfaceSimpleCounters{}) if err != nil { panic(err) } - combinedCountersSubs, err := ch.SubscribeNotification(notifChan, stats.NewVnetInterfaceCombinedCounters) + combinedSub, err := ch.SubscribeNotification(notifChan, &stats.VnetInterfaceCombinedCounters{}) if err != nil { panic(err) } - return simpleCountersSubs, combinedCountersSubs, notifChan + return simpleSub, combinedSub, notifChan } // requestStatistics requests interface counters notifications from VPP. |