From 28df7c855784e784fb0e614c1cca0e565a7ef75f Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Mon, 22 Oct 2018 14:45:21 +0200 Subject: Introduce StatsAPI and it's initial implementation - this implementation is basically Go wrapper around VPP's vppapiclient C library Change-Id: I6f53dc3e228868834bf3a8a00c686ad05e22f3dd Signed-off-by: Ondrej Fabry --- core/connection.go | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'core/connection.go') diff --git a/core/connection.go b/core/connection.go index 7d014ce..c4048f0 100644 --- a/core/connection.go +++ b/core/connection.go @@ -72,8 +72,9 @@ var ( // Connection represents a shared memory connection to VPP via vppAdapter. type Connection struct { - vpp adapter.VppAdapter // VPP adapter - connected uint32 // non-zero if the adapter is connected to VPP + vppClient adapter.VppAPI // VPP binary API client adapter + + vppConnected uint32 // non-zero if the adapter is connected to VPP codec *codec.MsgCodec // message codec msgIDs map[string]uint16 // map of message IDs indexed by message name + CRC @@ -93,24 +94,24 @@ type Connection struct { lastReply time.Time // time of the last received reply from VPP } -func newConnection(vpp adapter.VppAdapter) *Connection { +func newConnection(binapi adapter.VppAPI) *Connection { c := &Connection{ - vpp: vpp, + vppClient: binapi, codec: &codec.MsgCodec{}, msgIDs: make(map[string]uint16), msgMap: make(map[uint16]api.Message), channels: make(map[uint16]*Channel), subscriptions: make(map[uint16][]*subscriptionCtx), } - vpp.SetMsgCallback(c.msgCallback) + binapi.SetMsgCallback(c.msgCallback) return c } // Connect connects to VPP using specified VPP adapter and returns the connection handle. // This call blocks until VPP is connected, or an error occurs. Only one connection attempt will be performed. -func Connect(vppAdapter adapter.VppAdapter) (*Connection, error) { +func Connect(binapi adapter.VppAPI) (*Connection, error) { // create new connection handle - c, err := createConnection(vppAdapter) + c, err := createConnection(binapi) if err != nil { return nil, err } @@ -127,9 +128,9 @@ func Connect(vppAdapter adapter.VppAdapter) (*Connection, error) { // and ConnectionState channel. This call does not block until connection is established, it // returns immediately. The caller is supposed to watch the returned ConnectionState channel for // Connected/Disconnected events. In case of disconnect, the library will asynchronously try to reconnect. -func AsyncConnect(vppAdapter adapter.VppAdapter) (*Connection, chan ConnectionEvent, error) { +func AsyncConnect(binapi adapter.VppAPI) (*Connection, chan ConnectionEvent, error) { // create new connection handle - c, err := createConnection(vppAdapter) + c, err := createConnection(binapi) if err != nil { return nil, nil, err } @@ -150,14 +151,14 @@ func (c *Connection) Disconnect() { connLock.Lock() defer connLock.Unlock() - if c.vpp != nil { + if c.vppClient != nil { c.disconnectVPP() } conn = nil } // newConnection returns new connection handle. -func createConnection(vppAdapter adapter.VppAdapter) (*Connection, error) { +func createConnection(binapi adapter.VppAPI) (*Connection, error) { connLock.Lock() defer connLock.Unlock() @@ -165,7 +166,7 @@ func createConnection(vppAdapter adapter.VppAdapter) (*Connection, error) { return nil, errors.New("only one connection per process is supported") } - conn = newConnection(vppAdapter) + conn = newConnection(binapi) return conn, nil } @@ -175,19 +176,19 @@ func (c *Connection) connectVPP() error { log.Debug("Connecting to VPP..") // blocking connect - if err := c.vpp.Connect(); err != nil { + if err := c.vppClient.Connect(); err != nil { return err } log.Debugf("Connected to VPP.") if err := c.retrieveMessageIDs(); err != nil { - c.vpp.Disconnect() + c.vppClient.Disconnect() return fmt.Errorf("VPP is incompatible: %v", err) } // store connected state - atomic.StoreUint32(&c.connected, 1) + atomic.StoreUint32(&c.vppConnected, 1) return nil } @@ -272,7 +273,7 @@ func (c *Connection) retrieveMessageIDs() (err error) { msgs := api.GetAllMessages() for name, msg := range msgs { - msgID, err := c.vpp.GetMsgID(msg.GetMessageName(), msg.GetCrcString()) + msgID, err := c.vppClient.GetMsgID(msg.GetMessageName(), msg.GetCrcString()) if err != nil { return err } @@ -296,14 +297,14 @@ func (c *Connection) retrieveMessageIDs() (err error) { // fallback for control ping when vpe package is not imported if c.pingReqID == 0 { - c.pingReqID, err = c.vpp.GetMsgID(msgControlPing.GetMessageName(), msgControlPing.GetCrcString()) + c.pingReqID, err = c.vppClient.GetMsgID(msgControlPing.GetMessageName(), msgControlPing.GetCrcString()) if err != nil { return err } addMsg(c.pingReqID, msgControlPing) } if c.pingReplyID == 0 { - c.pingReplyID, err = c.vpp.GetMsgID(msgControlPingReply.GetMessageName(), msgControlPingReply.GetCrcString()) + c.pingReplyID, err = c.vppClient.GetMsgID(msgControlPingReply.GetMessageName(), msgControlPingReply.GetCrcString()) if err != nil { return err } @@ -315,8 +316,8 @@ func (c *Connection) retrieveMessageIDs() (err error) { // disconnectVPP disconnects from VPP in case it is connected. func (c *Connection) disconnectVPP() { - if atomic.CompareAndSwapUint32(&c.connected, 1, 0) { - c.vpp.Disconnect() + if atomic.CompareAndSwapUint32(&c.vppConnected, 1, 0) { + c.vppClient.Disconnect() } } @@ -325,7 +326,7 @@ func (c *Connection) disconnectVPP() { func (c *Connection) connectLoop(connChan chan ConnectionEvent) { // loop until connected for { - if err := c.vpp.WaitReady(); err != nil { + if err := c.vppClient.WaitReady(); err != nil { log.Warnf("wait ready failed: %v", err) } if err := c.connectVPP(); err == nil { @@ -362,7 +363,7 @@ func (c *Connection) healthCheckLoop(connChan chan ConnectionEvent) { // sleep until next health check probe period time.Sleep(HealthCheckProbeInterval) - if atomic.LoadUint32(&c.connected) == 0 { + if atomic.LoadUint32(&c.vppConnected) == 0 { // Disconnect has been called in the meantime, return the healthcheck - reconnect loop log.Debug("Disconnected on request, exiting health check loop.") return -- cgit 1.2.3-korg