From 392a56b700fa6715d091e56c49f79bfe32613fc6 Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Thu, 5 Apr 2018 13:46:54 +0200 Subject: Lookup message name by ID when receiving unexpected message Change-Id: I693e8084b7e3f036dec5e557dc772857bb7d5f3d Signed-off-by: Ondrej Fabry --- core/core.go | 8 ++++---- core/request_handler.go | 27 ++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) (limited to 'core') diff --git a/core/core.go b/core/core.go index b912715..ebe7f68 100644 --- a/core/core.go +++ b/core/core.go @@ -73,14 +73,14 @@ type Connection struct { connected uint32 // non-zero if the adapter is connected to VPP codec *MsgCodec // message codec - msgIDs map[string]uint16 // map of message IDs indexed by message name + CRC msgIDsLock sync.RWMutex // lock for the message IDs map + msgIDs map[string]uint16 // map of message IDs indexed by message name + CRC - channels map[uint32]*api.Channel // map of all API channels indexed by the channel ID channelsLock sync.RWMutex // lock for the channels map + channels map[uint32]*api.Channel // map of all API channels indexed by the channel ID - notifSubscriptions map[uint16][]*api.NotifSubscription // map od all notification subscriptions indexed by message ID notifSubscriptionsLock sync.RWMutex // lock for the subscriptions map + notifSubscriptions map[uint16][]*api.NotifSubscription // map od all notification subscriptions indexed by message ID maxChannelID uint32 // maximum used client ID pingReqID uint16 // ID if the ControlPing message @@ -306,7 +306,7 @@ func (c *Connection) healthCheckLoop(connChan chan ConnectionEvent) { failedChecks = 0 } - if failedChecks >= healthCheckThreshold { + if failedChecks > healthCheckThreshold { // in case of error, break & disconnect log.Errorf("VPP health check failed: %v", err) // signal disconnected event via channel diff --git a/core/request_handler.go b/core/request_handler.go index 4a62754..7c185cd 100644 --- a/core/request_handler.go +++ b/core/request_handler.go @@ -24,6 +24,10 @@ import ( "git.fd.io/govpp.git/api" ) +var ( + ErrNotConnected = errors.New("not connected to VPP, ignoring the request") +) + // watchRequests watches for requests on the request API channel and forwards them as messages to VPP. func (c *Connection) watchRequests(ch *api.Channel, chMeta *channelMetadata) { for { @@ -48,7 +52,7 @@ func (c *Connection) watchRequests(ch *api.Channel, chMeta *channelMetadata) { func (c *Connection) processRequest(ch *api.Channel, chMeta *channelMetadata, req *api.VppRequest) error { // check whether we are connected to VPP if atomic.LoadUint32(&c.connected) == 0 { - err := errors.New("not connected to VPP, ignoring the request") + err := ErrNotConnected log.Error(err) sendReply(ch, &api.VppReply{Error: err}) return err @@ -189,9 +193,11 @@ func (c *Connection) GetMessageID(msg api.Message) (uint16, error) { // messageNameToID returns message ID of a message identified by its name and CRC. func (c *Connection) messageNameToID(msgName string, msgCrc string) (uint16, error) { + msgKey := msgName + "_" + msgCrc + // try to get the ID from the map c.msgIDsLock.RLock() - id, ok := c.msgIDs[msgName+msgCrc] + id, ok := c.msgIDs[msgKey] c.msgIDsLock.RUnlock() if ok { return id, nil @@ -209,8 +215,23 @@ func (c *Connection) messageNameToID(msgName string, msgCrc string) (uint16, err } c.msgIDsLock.Lock() - c.msgIDs[msgName+msgCrc] = id + c.msgIDs[msgKey] = id c.msgIDsLock.Unlock() return id, nil } + +// LookupByID looks up message name and crc by ID. +func (c *Connection) LookupByID(ID uint16) (string, error) { + if c == nil { + return "", errors.New("nil connection passed in") + } + + for key, id := range c.msgIDs { + if id == ID { + return key, nil + } + } + + return "", fmt.Errorf("unknown message ID: %d", ID) +} -- cgit 1.2.3-korg