Lookup message name by ID when receiving unexpected message
[govpp.git] / core / request_handler.go
index 4a62754..7c185cd 100644 (file)
@@ -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)
+}