test: Fix test dependancy
[govpp.git] / core / channel.go
index 1b5e77e..112c14e 100644 (file)
@@ -37,16 +37,18 @@ type MessageCodec interface {
        EncodeMsg(msg api.Message, msgID uint16) ([]byte, error)
        // DecodeMsg decodes binary-encoded data of a message into provided Message structure.
        DecodeMsg(data []byte, msg api.Message) error
-       // DecodeMsgContext decodes context from message data.
-       DecodeMsgContext(data []byte, msg api.Message) (context uint32, err error)
+       // DecodeMsgContext decodes context from message data and type.
+       DecodeMsgContext(data []byte, msgType api.MessageType) (context uint32, err error)
 }
 
 // MessageIdentifier provides identification of generated API messages.
 type MessageIdentifier interface {
        // GetMessageID returns message identifier of given API message.
        GetMessageID(msg api.Message) (uint16, error)
+       // GetMessagePath returns path for the given message
+       GetMessagePath(msg api.Message) string
        // LookupByID looks up message name and crc by ID
-       LookupByID(msgID uint16) (api.Message, error)
+       LookupByID(path string, msgID uint16) (api.Message, error)
 }
 
 // vppRequest is a request that will be sent to VPP.
@@ -107,17 +109,36 @@ type Channel struct {
        receiveReplyTimeout time.Duration // maximum time that we wait for receiver to consume reply
 }
 
-func newChannel(id uint16, conn *Connection, codec MessageCodec, identifier MessageIdentifier, reqSize, replySize int) *Channel {
-       return &Channel{
-               id:                  id,
-               conn:                conn,
-               msgCodec:            codec,
-               msgIdentifier:       identifier,
-               reqChan:             make(chan *vppRequest, reqSize),
-               replyChan:           make(chan *vppReply, replySize),
+func (c *Connection) newChannel(reqChanBufSize, replyChanBufSize int) (*Channel, error) {
+       // create new channel
+       channel := &Channel{
+               conn:                c,
+               msgCodec:            c.codec,
+               msgIdentifier:       c,
+               reqChan:             make(chan *vppRequest, reqChanBufSize),
+               replyChan:           make(chan *vppReply, replyChanBufSize),
                replyTimeout:        DefaultReplyTimeout,
                receiveReplyTimeout: ReplyChannelTimeout,
        }
+
+       // store API channel within the client
+       c.channelsLock.Lock()
+       if len(c.channels) >= 0x7fff {
+               return nil, errors.New("all channel IDs are used")
+       }
+       for {
+               c.nextChannelID++
+               chID := c.nextChannelID & 0x7fff
+               _, ok := c.channels[chID]
+               if !ok {
+                       channel.id = chID
+                       c.channels[chID] = channel
+                       break
+               }
+       }
+       c.channelsLock.Unlock()
+
+       return channel, nil
 }
 
 func (ch *Channel) GetID() uint16 {
@@ -329,7 +350,8 @@ func (ch *Channel) processReply(reply *vppReply, expSeqNum uint16, msg api.Messa
 
        if reply.msgID != expMsgID {
                var msgNameCrc string
-               if replyMsg, err := ch.msgIdentifier.LookupByID(reply.msgID); err != nil {
+               pkgPath := ch.msgIdentifier.GetMessagePath(msg)
+               if replyMsg, err := ch.msgIdentifier.LookupByID(pkgPath, reply.msgID); err != nil {
                        msgNameCrc = err.Error()
                } else {
                        msgNameCrc = getMsgNameWithCrc(replyMsg)
@@ -350,7 +372,15 @@ func (ch *Channel) processReply(reply *vppReply, expSeqNum uint16, msg api.Messa
        if strings.HasSuffix(msg.GetMessageName(), "_reply") {
                // TODO: use categories for messages to avoid checking message name
                if f := reflect.Indirect(reflect.ValueOf(msg)).FieldByName("Retval"); f.IsValid() {
-                       retval := int32(f.Int())
+                       var retval int32
+                       switch f.Kind() {
+                       case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+                               retval = int32(f.Int())
+                       case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
+                               retval = int32(f.Uint())
+                       default:
+                               logrus.Warnf("invalid kind (%v) for Retval field of message %v", f.Kind(), msg.GetMessageName())
+                       }
                        err = api.RetvalToVPPApiError(retval)
                }
        }