Optimize socketclient adapter and add various code improvements
[govpp.git] / core / request_handler.go
index 4004d15..e272c6f 100644 (file)
@@ -17,12 +17,17 @@ package core
 import (
        "errors"
        "fmt"
+       "reflect"
        "sync/atomic"
        "time"
 
        logger "github.com/sirupsen/logrus"
+
+       "git.fd.io/govpp.git/api"
 )
 
+var ReplyChannelTimeout = time.Millisecond * 100
+
 var (
        ErrNotConnected = errors.New("not connected to VPP, ignoring the request")
        ErrProbeTimeout = errors.New("probe reply not received within timeout period")
@@ -88,10 +93,10 @@ func (c *Connection) processRequest(ch *Channel, req *vppRequest) error {
                        "context":  context,
                        "is_multi": req.multi,
                        "msg_id":   msgID,
-                       "msg_name": req.msg.GetMessageName(),
                        "msg_size": len(data),
                        "seq_num":  req.seqNum,
-               }).Debug(" -> Sending a message to VPP.")
+                       "msg_crc":  req.msg.GetCrcString(),
+               }).Debugf("--> govpp SEND: %s %+v", req.msg.GetMessageName(), req.msg)
        }
 
        // send the request to VPP
@@ -108,7 +113,7 @@ func (c *Connection) processRequest(ch *Channel, req *vppRequest) error {
 
        if req.multi {
                // send a control ping to determine end of the multipart response
-               pingData, _ := c.codec.EncodeMsg(msgControlPing, c.pingReqID)
+               pingData, _ := c.codec.EncodeMsg(c.msgControlPing, c.pingReqID)
 
                log.WithFields(logger.Fields{
                        "channel":  ch.id,
@@ -116,7 +121,7 @@ func (c *Connection) processRequest(ch *Channel, req *vppRequest) error {
                        "msg_id":   c.pingReqID,
                        "msg_size": len(pingData),
                        "seq_num":  req.seqNum,
-               }).Debug(" -> Sending a control ping to VPP.")
+               }).Debug(" -> sending control ping")
 
                if err := c.vppClient.SendMsg(context, pingData); err != nil {
                        log.WithFields(logger.Fields{
@@ -154,16 +159,25 @@ func (c *Connection) msgCallback(msgID uint16, data []byte) {
        }
 
        chanID, isMulti, seqNum := unpackRequestContext(context)
+
        if log.Level == logger.DebugLevel { // for performance reasons - logrus does some processing even if debugs are disabled
+               msg = reflect.New(reflect.TypeOf(msg).Elem()).Interface().(api.Message)
+
+               // decode the message
+               if err = c.codec.DecodeMsg(data, msg); err != nil {
+                       err = fmt.Errorf("decoding message failed: %w", err)
+                       return
+               }
+
                log.WithFields(logger.Fields{
                        "context":  context,
                        "msg_id":   msgID,
-                       "msg_name": msg.GetMessageName(),
                        "msg_size": len(data),
                        "channel":  chanID,
                        "is_multi": isMulti,
                        "seq_num":  seqNum,
-               }).Debug(" <- Received a message from VPP.")
+                       "msg_crc":  msg.GetCrcString(),
+               }).Debugf("<-- govpp RECEIVE: %s %+v", msg.GetMessageName(), msg)
        }
 
        if context == 0 || c.isNotificationMessage(msgID) {
@@ -208,8 +222,8 @@ func (c *Connection) msgCallback(msgID uint16, data []byte) {
 func sendReply(ch *Channel, reply *vppReply) {
        select {
        case ch.replyChan <- reply:
-               // reply sent successfully
-       case <-time.After(time.Millisecond * 100):
+       // reply sent successfully
+       case <-time.After(ReplyChannelTimeout):
                // receiver still not ready
                log.WithFields(logger.Fields{
                        "channel": ch,