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/request_handler.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'core/request_handler.go') 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